Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

staging contract CLI #1393

Merged
merged 80 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
d30101d
add migration folder of scripts
ianthpun Feb 7, 2024
5d35470
update
ianthpun Feb 7, 2024
b41cbbd
update
ianthpun Feb 7, 2024
6b40b1a
update
ianthpun Feb 7, 2024
9e9a15e
update
ianthpun Feb 7, 2024
bdca41f
update
ianthpun Feb 7, 2024
ef2fae1
update
ianthpun Feb 7, 2024
69f0a2d
use global flags
ianthpun Feb 7, 2024
06e077e
commit with file changes
ianthpun Feb 7, 2024
3f6c080
update
ianthpun Feb 7, 2024
dea29bf
update
ianthpun Feb 8, 2024
b0f3f18
move name
ianthpun Feb 8, 2024
59ae218
create isStaged logic
ianthpun Feb 8, 2024
4cf15f3
update
ianthpun Feb 8, 2024
16b6844
update
ianthpun Feb 8, 2024
ec02498
move files to cadence folder
ianthpun Feb 8, 2024
4232bc1
move to contants
ianthpun Feb 8, 2024
7377573
fix linter and also fix unstage
ianthpun Feb 8, 2024
9661458
add new files
ianthpun Feb 8, 2024
2dbf2df
add addtional functionality
ianthpun Feb 8, 2024
a556f83
update
ianthpun Feb 8, 2024
e4f04e1
Merge branch 'master' into ianthpun/stage-contract
ianthpun Feb 9, 2024
4c522c1
upgrade
ianthpun Feb 9, 2024
d1a267f
Merge branch 'ianthpun/stage-contract' of github.com:onflow/flow-cli …
ianthpun Feb 9, 2024
af20ba5
add licence header
ianthpun Feb 9, 2024
b114036
fix linter
ianthpun Feb 9, 2024
29d2819
formatter
ianthpun Feb 9, 2024
c4a56d6
remove gg
ianthpun Feb 9, 2024
16fdf04
fix cache issue on linter
ianthpun Feb 9, 2024
f3c1b04
remove query
ianthpun Feb 9, 2024
d3f5264
clean code up
ianthpun Feb 9, 2024
d93da34
fix interface
ianthpun Feb 9, 2024
621902a
clean things up
ianthpun Feb 9, 2024
5a68d4c
remove gg
ianthpun Feb 9, 2024
7804a10
fix state
ianthpun Feb 10, 2024
f7e9c65
update
ianthpun Feb 13, 2024
5ae42db
use templates library
ianthpun Feb 13, 2024
7f5ad60
update lbis
ianthpun Feb 13, 2024
0dd1a66
remove cadnece and retry
ianthpun Feb 13, 2024
1a02b84
remove changes from gomod
ianthpun Feb 13, 2024
6caf0ca
retry
ianthpun Feb 13, 2024
5e67da5
retry
ianthpun Feb 13, 2024
2c03d73
Merge branch 'master' into ianthpun/stage-contract
ianthpun Feb 13, 2024
6be444a
attempt
ianthpun Feb 13, 2024
e08e2bd
attempt
ianthpun Feb 13, 2024
328102d
update
ianthpun Feb 14, 2024
c9081eb
remove migration files
ianthpun Feb 14, 2024
e6e0f79
remove old files
ianthpun Feb 14, 2024
d8ef040
refactor
ianthpun Feb 14, 2024
92b5f7d
update and refactor
ianthpun Feb 14, 2024
a463509
update gomod
ianthpun Feb 14, 2024
f7e719a
undo
ianthpun Feb 14, 2024
85812d0
update
ianthpun Feb 14, 2024
92c6e7e
rename
ianthpun Feb 14, 2024
270e22e
update
ianthpun Feb 14, 2024
9e9cd23
rename
ianthpun Feb 14, 2024
ebfded8
Merge branch 'master' into ianthpun/stage-contract
ianthpun Feb 14, 2024
ed10292
update
ianthpun Feb 14, 2024
203ea23
Merge branch 'ianthpun/stage-contract' of github.com:onflow/flow-cli …
ianthpun Feb 14, 2024
c7ea6cf
update
ianthpun Feb 14, 2024
79be8c2
linter
ianthpun Feb 14, 2024
def8939
clean up tests
ianthpun Feb 15, 2024
38e5645
update get staged to use cname
ianthpun Feb 15, 2024
2888fda
use getStagedContractNamesForAddress in listStagedContracts
ianthpun Feb 15, 2024
7b29dc4
change min to 1
ianthpun Feb 15, 2024
deeb9dc
revert commits
ianthpun Feb 16, 2024
20253f2
revert changes to update
ianthpun Feb 16, 2024
61e321a
Merge branch 'feature/stable-cadence' into ianthpun/stage-contract
ianthpun Feb 16, 2024
c59384d
downgrade
ianthpun Feb 16, 2024
8f789a8
fix
ianthpun Feb 16, 2024
dc1c27a
fix linter issues
ianthpun Feb 16, 2024
7896ab3
undo changes to flow.Nework for now
ianthpun Feb 16, 2024
b738748
update
ianthpun Feb 16, 2024
756b531
udpate
ianthpun Feb 16, 2024
d70257a
dont use globalflags
ianthpun Feb 16, 2024
06395f8
add back network
ianthpun Feb 16, 2024
ea0e6cb
use bool instead of metered
ianthpun Feb 17, 2024
693beb7
fix issues related to tests
ianthpun Feb 17, 2024
7d04d57
have a random array of string
ianthpun Feb 17, 2024
becb0d2
fix tests
ianthpun Feb 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes an issue related to the linter spamming missing files if there is a linter problem on CI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

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, globalFlags.Network)
ianthpun marked this conversation as resolved.
Show resolved Hide resolved
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(globalFlags.Network)),
ianthpun marked this conversation as resolved.
Show resolved Hide resolved
Args: []cadence.Value{caddr, cName},
},
flowkit.LatestScriptQuery,
)
if err != nil {
return nil, fmt.Errorf("error executing script: %w", err)
}

return scripts.NewScriptResult(value), nil
}
83 changes: 83 additions & 0 deletions internal/migrate/get_staged_code_test.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 (
"testing"

"github.com/onflow/cadence"
"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,
},
},
},
)

srv.ExecuteScript.Run(func(args mock.Arguments) {
script := args.Get(1).(flowkit.Script)
ianthpun marked this conversation as resolved.
Show resolved Hide resolved

actualContractAddressArg := script.Args[0]

contractAddr := cadence.NewAddress(util.EmulatorAccountAddress)
assert.Equal(t, contractAddr.String(), actualContractAddressArg.String())
ianthpun marked this conversation as resolved.
Show resolved Hide resolved
ianthpun marked this conversation as resolved.
Show resolved Hide resolved
}).Return(cadence.NewMeteredBool(nil, true), nil)
ianthpun marked this conversation as resolved.
Show resolved Hide resolved

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, globalFlags.Network)
Copy link
Contributor

@jribbink jribbink Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
addr, err := getAddressByContractName(state, contractName, globalFlags.Network)
addr, err := getAddressByContractName(state, contractName, flow.Network().Name)

(or maybe declare network := flow.Network().Name etc. since used in multiple places)

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(globalFlags.Network)),
ianthpun marked this conversation as resolved.
Show resolved Hide resolved
Args: []cadence.Value{caddr, cname},
},
flowkit.LatestScriptQuery,
)
if err != nil {
return nil, fmt.Errorf("error executing script: %w", err)
}

return scripts.NewScriptResult(value), nil
}
86 changes: 86 additions & 0 deletions internal/migrate/is_staged_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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/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_IsStaged(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.ExecuteScript.Run(func(args mock.Arguments) {
script := args.Get(1).(flowkit.Script)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo we should be asserting that ExecuteScript was called with the correct CDC script


actualContractAddressArg, actualContractNameArg := script.Args[0], script.Args[1]

contractName, _ := cadence.NewString(testContract.Name)
contractAddr := cadence.NewAddress(util.EmulatorAccountAddress)
assert.Equal(t, contractName, actualContractNameArg)
assert.Equal(t, contractAddr, actualContractAddressArg)
ianthpun marked this conversation as resolved.
Show resolved Hide resolved
ianthpun marked this conversation as resolved.
Show resolved Hide resolved
}).Return(cadence.NewMeteredBool(nil, true), nil)
ianthpun marked this conversation as resolved.
Show resolved Hide resolved

result, err := isStaged(
[]string{testContract.Name},
command.GlobalFlags{
Network: "testnet",
},
util.NoLogger,
srv.Mock,
state,
)
assert.NoError(t, err)
// TODO: fix this
assert.NotNil(t, result)
})
}
Loading
Loading