-
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 #1477 from onflow/ianthpun/validate-contract
validate contract
- Loading branch information
Showing
2 changed files
with
179 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 |
---|---|---|
@@ -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 <CONTRACT_NAME>", | ||
Short: "checks to see if the contract has passed the last emulated migration", | ||
Example: `flow migrate is-validated HelloWorld`, | ||
Args: cobra.MinimumNArgs(1), | ||
}, | ||
Flags: &isValidatedflags, | ||
RunS: isValidated, | ||
} | ||
|
||
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 | ||
} |
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,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) | ||
}) | ||
} |