Skip to content

Commit

Permalink
Merge pull request #1477 from onflow/ianthpun/validate-contract
Browse files Browse the repository at this point in the history
validate contract
  • Loading branch information
ianthpun authored Mar 28, 2024
2 parents f76de3b + 4b650d2 commit 20252b7
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
82 changes: 82 additions & 0 deletions internal/migrate/is_validated.go
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
}
97 changes: 97 additions & 0 deletions internal/migrate/is_validated_test.go
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)
})
}

0 comments on commit 20252b7

Please sign in to comment.