Skip to content

Commit

Permalink
Merge pull request #1393 from onflow/ianthpun/stage-contract
Browse files Browse the repository at this point in the history
staging contract CLI
  • Loading branch information
ianthpun authored Feb 17, 2024
2 parents 9ecc47d + becb0d2 commit d09e68f
Show file tree
Hide file tree
Showing 17 changed files with 1,080 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions cmd/flow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/onflow/flow-cli/internal/emulator"
"github.com/onflow/flow-cli/internal/events"
"github.com/onflow/flow-cli/internal/keys"
"github.com/onflow/flow-cli/internal/migrate"
"github.com/onflow/flow-cli/internal/project"
"github.com/onflow/flow-cli/internal/quick"
"github.com/onflow/flow-cli/internal/scripts"
Expand Down Expand Up @@ -120,6 +121,7 @@ func main() {
cmd.AddCommand(super.FlixCmd)
cmd.AddCommand(super.GenerateCommand)
cmd.AddCommand(dependencymanager.Cmd)
cmd.AddCommand(migrate.Cmd)

command.InitFlags(cmd)
cmd.AddGroup(&cobra.Group{
Expand Down Expand Up @@ -150,6 +152,10 @@ func main() {
ID: "manager",
Title: "🔗 Dependency Manager",
})
cmd.AddGroup(&cobra.Group{
ID: "migrate",
Title: "📦 Migration to 1.0",
})

cmd.SetUsageTemplate(command.UsageTemplate)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/onflow/cadence v1.0.0-M7
github.com/onflow/cadence-tools/languageserver v1.0.0-M2
github.com/onflow/cadence-tools/test v1.0.0-M3
github.com/onflow/contract-updater/lib/go/templates v1.0.1
github.com/onflow/fcl-dev-wallet v0.8.0-stable-cadence.1
github.com/onflow/flixkit-go v1.1.1-0.20240214222351-03b90f7d32ef
github.com/onflow/flow-core-contracts/lib/go/templates v0.15.2-0.20240206003101-928bf99024d7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,8 @@ github.com/onflow/cadence-tools/lint v1.0.0-M4 h1:XeBX+/2TKbe04nyMjUbSD9aHcF5+H6
github.com/onflow/cadence-tools/lint v1.0.0-M4/go.mod h1:vgKOF14/NwWktjsRKHlWEz8z/3uQu5nuqDzQOm+u2qY=
github.com/onflow/cadence-tools/test v1.0.0-M3 h1:ZbKoMhXsvafJIOO/MVE7lV5INXfq2IL0Z+W2MsJnKwY=
github.com/onflow/cadence-tools/test v1.0.0-M3/go.mod h1:Yk6OyiRMNseM6C13FpNUxTJ7GekYZMzdX5D2B10y4N8=
github.com/onflow/contract-updater/lib/go/templates v1.0.1 h1:xPj898Y8OgLLbXH8+JeKVBV6J+nqPZjiLgGM3Abucto=
github.com/onflow/contract-updater/lib/go/templates v1.0.1/go.mod h1:OXO6s0X7OW4Q6QTfAfnjoOmibEPgs0psOfMi+tPyzQE=
github.com/onflow/crypto v0.24.9 h1:jYP1qdwid0qCineFzBFlxBchg710A7RuSWpTqxaOdog=
github.com/onflow/crypto v0.24.9/go.mod h1:J/V7IEVaqjDajvF8K0B/SJPJDgAOP2G+LVLeb0hgmbg=
github.com/onflow/fcl-dev-wallet v0.8.0-stable-cadence.1 h1:IqdUzdqFCSW0klWmA3J9c17ZyQTab9SWcWSLouX6o0Q=
Expand Down
83 changes: 83 additions & 0 deletions internal/migrate/get_staged_code.go
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
}
96 changes: 96 additions & 0 deletions internal/migrate/get_staged_code_test.go
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)
})
}
82 changes: 82 additions & 0 deletions internal/migrate/is_staged.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 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
}
Loading

0 comments on commit d09e68f

Please sign in to comment.