From 3769118916e772774810263217cb0ad794b61316 Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 27 Mar 2024 10:10:45 -0700 Subject: [PATCH 1/5] add isValidated functionality --- internal/migrate/is_validated.go | 82 ++++++++++++++++++++++ internal/migrate/is_validated_test.go | 97 +++++++++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 internal/migrate/is_validated.go create mode 100644 internal/migrate/is_validated_test.go diff --git a/internal/migrate/is_validated.go b/internal/migrate/is_validated.go new file mode 100644 index 000000000..3062633aa --- /dev/null +++ b/internal/migrate/is_validated.go @@ -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 isValidatedflags struct{} + +var IsValidatedCommand = &command.Command{ + Cmd: &cobra.Command{ + Use: "is-validated ", + Short: "checks to see if the contract is validated for migration", + Example: `flow migrate is-validated HelloWorld`, + Args: cobra.MinimumNArgs(1), + }, + Flags: &isStagedflags, + RunS: isStaged, +} + +func isValidated( + 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.GenerateIsValidatedScript(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 +} diff --git a/internal/migrate/is_validated_test.go b/internal/migrate/is_validated_test.go new file mode 100644 index 000000000..b83cdf481 --- /dev/null +++ b/internal/migrate/is_validated_test.go @@ -0,0 +1,97 @@ +/* + * 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_IsValidated(t *testing.T) { + srv, state, _ := util.TestMocks(t) + + testContract := tests.ContractSimple + + t.Run("Success", func(t *testing.T) { + + 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, + }, + }, + }, + ) + + srv.Network.Return(config.Network{ + Name: "testnet", + }, nil) + + account, err := state.EmulatorServiceAccount() + assert.NoError(t, err) + + srv.ExecuteScript.Run(func(args mock.Arguments) { + script := args.Get(1).(flowkit.Script) + + assert.Equal(t, templates.GenerateIsValidatedScript(MigrationContractStagingAddress("testnet")), script.Code) + + assert.Equal(t, 2, len(script.Args)) + actualContractAddressArg, actualContractNameArg := script.Args[0], script.Args[1] + + contractName, _ := cadence.NewString(testContract.Name) + contractAddr := cadence.NewAddress(account.Address) + assert.Equal(t, contractName, actualContractNameArg) + assert.Equal(t, contractAddr, actualContractAddressArg) + }).Return(cadence.NewBool(true), nil) + + result, err := isValidated( + []string{testContract.Name}, + command.GlobalFlags{ + Network: "testnet", + }, + util.NoLogger, + srv.Mock, + state, + ) + assert.NoError(t, err) + // TODO: fix this + assert.NotNil(t, result) + }) +} From 57f0447f8f192513f5c05ca810802aabd4e33639 Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 27 Mar 2024 10:11:26 -0700 Subject: [PATCH 2/5] add isvalidated to root --- internal/accounts/create-interactive.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/accounts/create-interactive.go b/internal/accounts/create-interactive.go index b596ec451..ee58e144e 100644 --- a/internal/accounts/create-interactive.go +++ b/internal/accounts/create-interactive.go @@ -223,7 +223,7 @@ type lilicoResponse struct { } `json:"data"` } -var accountToken = "" +var accountToken = "lilico:sF60s3wughJBmNh2" const defaultHashAlgo = crypto.SHA3_256 From f71d05f287bdc8c06d4ba54ff40c519bdbb39689 Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 27 Mar 2024 10:26:31 -0700 Subject: [PATCH 3/5] undo --- internal/accounts/create-interactive.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/accounts/create-interactive.go b/internal/accounts/create-interactive.go index ee58e144e..b596ec451 100644 --- a/internal/accounts/create-interactive.go +++ b/internal/accounts/create-interactive.go @@ -223,7 +223,7 @@ type lilicoResponse struct { } `json:"data"` } -var accountToken = "lilico:sF60s3wughJBmNh2" +var accountToken = "" const defaultHashAlgo = crypto.SHA3_256 From 0f6d4992c2995d6ba8eb2a9ec17d1fdc80e2a719 Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 27 Mar 2024 10:33:43 -0700 Subject: [PATCH 4/5] typo --- internal/migrate/is_validated.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/migrate/is_validated.go b/internal/migrate/is_validated.go index 3062633aa..4ad7e1bdb 100644 --- a/internal/migrate/is_validated.go +++ b/internal/migrate/is_validated.go @@ -41,8 +41,8 @@ var IsValidatedCommand = &command.Command{ Example: `flow migrate is-validated HelloWorld`, Args: cobra.MinimumNArgs(1), }, - Flags: &isStagedflags, - RunS: isStaged, + Flags: &isValidatedflags, + RunS: isValidated, } func isValidated( From 4b650d2a8b11a8b0253d4904662fe0e7aacaaa19 Mon Sep 17 00:00:00 2001 From: Ian Pun Date: Wed, 27 Mar 2024 16:09:11 -0700 Subject: [PATCH 5/5] update short --- internal/migrate/is_validated.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/migrate/is_validated.go b/internal/migrate/is_validated.go index 4ad7e1bdb..0c303c597 100644 --- a/internal/migrate/is_validated.go +++ b/internal/migrate/is_validated.go @@ -37,7 +37,7 @@ var isValidatedflags struct{} var IsValidatedCommand = &command.Command{ Cmd: &cobra.Command{ Use: "is-validated ", - Short: "checks to see if the contract is validated for migration", + Short: "checks to see if the contract has passed the last emulated migration", Example: `flow migrate is-validated HelloWorld`, Args: cobra.MinimumNArgs(1), },