-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1393 from onflow/ianthpun/stage-contract
staging contract CLI
- Loading branch information
Showing
17 changed files
with
1,080 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,4 +48,6 @@ jobs: | |
- uses: golangci/[email protected] | ||
with: | ||
version: v1.52.2 | ||
only-new-issues: true | ||
skip-pkg-cache: true | ||
args: --timeout=3m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Flow CLI | ||
* | ||
* Copyright 2019 Dapper Labs, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package migrate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/onflow/cadence" | ||
"github.com/onflow/flowkit/v2" | ||
"github.com/onflow/flowkit/v2/output" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/onflow/contract-updater/lib/go/templates" | ||
|
||
"github.com/onflow/flow-cli/internal/command" | ||
"github.com/onflow/flow-cli/internal/scripts" | ||
) | ||
|
||
var getStagedCodeflags struct{} | ||
|
||
var getStagedCodeCommand = &command.Command{ | ||
Cmd: &cobra.Command{ | ||
Use: "staged-code <CONTRACT_NAME>", | ||
Short: "returns back the staged code for a contract", | ||
Example: `flow migrate staged-code HelloWorld`, | ||
Args: cobra.MinimumNArgs(1), | ||
}, | ||
Flags: &getStagedCodeflags, | ||
RunS: getStagedCode, | ||
} | ||
|
||
func getStagedCode( | ||
args []string, | ||
globalFlags command.GlobalFlags, | ||
_ output.Logger, | ||
flow flowkit.Services, | ||
state *flowkit.State, | ||
) (command.Result, error) { | ||
contractName := args[0] | ||
|
||
addr, err := getAddressByContractName(state, contractName, flow.Network()) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting address by contract name: %w", err) | ||
} | ||
|
||
cName, err := cadence.NewString(contractName) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating cadence string: %w", err) | ||
} | ||
|
||
caddr := cadence.NewAddress(addr) | ||
|
||
value, err := flow.ExecuteScript( | ||
context.Background(), | ||
flowkit.Script{ | ||
Code: templates.GenerateGetStagedContractCodeScript(MigrationContractStagingAddress(flow.Network().Name)), | ||
Args: []cadence.Value{caddr, cName}, | ||
}, | ||
flowkit.LatestScriptQuery, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("error executing script: %w", err) | ||
} | ||
|
||
return scripts.NewScriptResult(value), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Flow CLI | ||
* | ||
* Copyright 2019 Dapper Labs, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package migrate | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onflow/cadence" | ||
"github.com/onflow/contract-updater/lib/go/templates" | ||
"github.com/onflow/flowkit/v2" | ||
"github.com/onflow/flowkit/v2/config" | ||
"github.com/onflow/flowkit/v2/tests" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/onflow/flow-cli/internal/command" | ||
"github.com/onflow/flow-cli/internal/util" | ||
) | ||
|
||
func Test_GetStagedCode(t *testing.T) { | ||
srv, state, _ := util.TestMocks(t) | ||
|
||
t.Run("Success", func(t *testing.T) { | ||
testContract := tests.ContractSimple | ||
|
||
// Add contract to state | ||
state.Contracts().AddOrUpdate( | ||
config.Contract{ | ||
Name: testContract.Name, | ||
Location: testContract.Filename, | ||
}, | ||
) | ||
|
||
// Add deployment to state | ||
state.Deployments().AddOrUpdate( | ||
config.Deployment{ | ||
Network: "testnet", | ||
Account: "emulator-account", | ||
Contracts: []config.ContractDeployment{ | ||
{ | ||
Name: testContract.Name, | ||
}, | ||
}, | ||
}, | ||
) | ||
account, err := state.EmulatorServiceAccount() | ||
assert.NoError(t, err) | ||
|
||
srv.Network.Return(config.Network{ | ||
Name: "testnet", | ||
}, nil) | ||
|
||
srv.ExecuteScript.Run(func(args mock.Arguments) { | ||
script := args.Get(1).(flowkit.Script) | ||
|
||
actualContractAddressArg, actualContractNameArg := script.Args[0], script.Args[1] | ||
|
||
assert.Equal(t, templates.GenerateGetStagedContractCodeScript(MigrationContractStagingAddress("testnet")), script.Code) | ||
|
||
assert.Equal(t, 2, len(script.Args)) | ||
|
||
assert.EqualValues(t, testContract.Name, actualContractNameArg) | ||
|
||
contractAddr := cadence.NewAddress(account.Address) | ||
assert.Equal(t, contractAddr.String(), actualContractAddressArg.String()) | ||
}).Return(cadence.NewString(string(testContract.Source))) | ||
|
||
result, err := getStagedCode( | ||
[]string{testContract.Name}, | ||
command.GlobalFlags{ | ||
Network: "testnet", | ||
}, | ||
util.NoLogger, | ||
srv.Mock, | ||
state, | ||
) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, result) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Flow CLI | ||
* | ||
* Copyright 2019 Dapper Labs, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package migrate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/onflow/cadence" | ||
"github.com/onflow/contract-updater/lib/go/templates" | ||
"github.com/onflow/flowkit/v2" | ||
"github.com/onflow/flowkit/v2/output" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/onflow/flow-cli/internal/command" | ||
"github.com/onflow/flow-cli/internal/scripts" | ||
) | ||
|
||
var isStagedflags struct{} | ||
|
||
var IsStagedCommand = &command.Command{ | ||
Cmd: &cobra.Command{ | ||
Use: "is-staged <CONTRACT_NAME>", | ||
Short: "checks to see if the contract is staged for migration", | ||
Example: `flow migrate is-staged HelloWorld`, | ||
Args: cobra.MinimumNArgs(1), | ||
}, | ||
Flags: &isStagedflags, | ||
RunS: isStaged, | ||
} | ||
|
||
func isStaged( | ||
args []string, | ||
globalFlags command.GlobalFlags, | ||
_ output.Logger, | ||
flow flowkit.Services, | ||
state *flowkit.State, | ||
) (command.Result, error) { | ||
contractName := args[0] | ||
|
||
addr, err := getAddressByContractName(state, contractName, flow.Network()) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting address by contract name: %w", err) | ||
} | ||
|
||
caddr := cadence.NewAddress(addr) | ||
|
||
cname, err := cadence.NewString(contractName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get cadence string from contract name: %w", err) | ||
} | ||
|
||
value, err := flow.ExecuteScript( | ||
context.Background(), | ||
flowkit.Script{ | ||
Code: templates.GenerateIsStagedScript(MigrationContractStagingAddress(flow.Network().Name)), | ||
Args: []cadence.Value{caddr, cname}, | ||
}, | ||
flowkit.LatestScriptQuery, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("error executing script: %w", err) | ||
} | ||
|
||
return scripts.NewScriptResult(value), nil | ||
} |
Oops, something went wrong.