-
Notifications
You must be signed in to change notification settings - Fork 66
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
staging contract CLI #1393
Changes from 74 commits
d30101d
5d35470
b41cbbd
6b40b1a
9e9a15e
bdca41f
ef2fae1
69f0a2d
06e077e
3f6c080
dea29bf
b0f3f18
59ae218
4cf15f3
16b6844
ec02498
4232bc1
7377573
9661458
2dbf2df
a556f83
e4f04e1
4c522c1
d1a267f
af20ba5
b114036
29d2819
c4a56d6
16fdf04
f3c1b04
d3f5264
d93da34
621902a
5a68d4c
7804a10
f7e9c65
5ae42db
7f5ad60
0dd1a66
1a02b84
6caf0ca
5e67da5
2c03d73
6be444a
e08e2bd
328102d
c9081eb
e6e0f79
d8ef040
92b5f7d
a463509
f7e719a
85812d0
92c6e7e
270e22e
9e9cd23
ebfded8
ed10292
203ea23
c7ea6cf
79be8c2
def8939
38e5645
2888fda
7b29dc4
deeb9dc
20253f2
61e321a
c59384d
8f789a8
dc1c27a
7896ab3
b738748
756b531
d70257a
06395f8
ea0e6cb
693beb7
7d04d57
becb0d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
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 | ||
} |
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) | ||
}) | ||
} |
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(or maybe declare |
||||||
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 | ||||||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
}) | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.