Skip to content

Commit 7f2bec8

Browse files
authored
Merge pull request #371 from bluesign/master
Arguments fix ( checker rollback)
2 parents 3f2cabb + 54595c8 commit 7f2bec8

5 files changed

Lines changed: 42 additions & 16 deletions

File tree

internal/scripts/execute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func execute(
6868
if scriptFlags.ArgsJSON != "" || len(scriptFlags.Arg) != 0 {
6969
scriptArgs, err = flowkit.ParseArguments(scriptFlags.Arg, scriptFlags.ArgsJSON)
7070
} else {
71-
scriptArgs, err = flowkit.ParseArgumentsWithoutType(code, args[1:])
71+
scriptArgs, err = flowkit.ParseArgumentsWithoutType(filename, code, args[1:])
7272
}
7373

7474
if err != nil {

internal/transactions/build.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package transactions
2121
import (
2222
"fmt"
2323

24+
"github.com/onflow/cadence"
2425
"github.com/onflow/flow-go-sdk"
2526
"github.com/spf13/cobra"
2627

@@ -32,7 +33,7 @@ import (
3233

3334
type flagsBuild struct {
3435
ArgsJSON string `default:"" flag:"args-json" info:"arguments in JSON-Cadence format"`
35-
Args []string `default:"" flag:"arg" info:"argument in Type:Value format"`
36+
Args []string `default:"" flag:"arg" info:"⚠️ Deprecated: use command arguments"`
3637
Proposer string `default:"emulator-account" flag:"proposer" info:"transaction proposer"`
3738
ProposerKeyIndex int `default:"0" flag:"proposer-key-index" info:"proposer key index"`
3839
Payer string `default:"emulator-account" flag:"payer" info:"transaction payer"`
@@ -44,10 +45,10 @@ var buildFlags = flagsBuild{}
4445

4546
var BuildCommand = &command.Command{
4647
Cmd: &cobra.Command{
47-
Use: "build <code filename>",
48+
Use: "build <code filename> [<argument> <argument> ...]",
4849
Short: "Build an unsigned transaction",
49-
Example: "flow transactions build ./transaction.cdc --proposer alice --authorizer alice --payer bob",
50-
Args: cobra.ExactArgs(1),
50+
Example: `flow transactions build ./transaction.cdc "Hello" --proposer alice --authorizer alice --payer bob`,
51+
Args: cobra.MinimumNArgs(1),
5152
},
5253
Flags: &buildFlags,
5354
RunS: build,
@@ -86,9 +87,19 @@ func build(
8687
return nil, fmt.Errorf("error loading transaction file: %w", err)
8788
}
8889

89-
txArgs, err := flowkit.ParseArguments(buildFlags.Args, buildFlags.ArgsJSON)
90+
if len(buildFlags.Args) != 0 {
91+
fmt.Println("⚠️ DEPRECATION WARNING: use transaction arguments as command arguments: send <code filename> [<argument> <argument> ...]")
92+
}
93+
94+
var transactionArgs []cadence.Value
95+
if buildFlags.ArgsJSON != "" || len(buildFlags.Args) != 0 {
96+
transactionArgs, err = flowkit.ParseArguments(buildFlags.Args, buildFlags.ArgsJSON)
97+
} else {
98+
transactionArgs, err = flowkit.ParseArgumentsWithoutType(filename, code, args[1:])
99+
}
100+
90101
if err != nil {
91-
return nil, err
102+
return nil, fmt.Errorf("error parsing transaction arguments: %w", err)
92103
}
93104

94105
build, err := services.Transactions.Build(
@@ -99,7 +110,7 @@ func build(
99110
code,
100111
filename,
101112
buildFlags.GasLimit,
102-
txArgs,
113+
transactionArgs,
103114
globalFlags.Network,
104115
)
105116
if err != nil {

internal/transactions/send.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func send(
7979
if sendFlags.ArgsJSON != "" || len(sendFlags.Arg) != 0 {
8080
transactionArgs, err = flowkit.ParseArguments(sendFlags.Arg, sendFlags.ArgsJSON)
8181
} else {
82-
transactionArgs, err = flowkit.ParseArgumentsWithoutType(code, args[1:])
82+
transactionArgs, err = flowkit.ParseArgumentsWithoutType(codeFilename, code, args[1:])
8383
}
8484

8585
if err != nil {

pkg/flowkit/arguments.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/onflow/cadence"
2828
jsoncdc "github.com/onflow/cadence/encoding/json"
2929
"github.com/onflow/cadence/runtime"
30+
"github.com/onflow/cadence/runtime/ast"
3031
"github.com/onflow/cadence/runtime/cmd"
3132
"github.com/onflow/cadence/runtime/common"
3233
"github.com/onflow/cadence/runtime/sema"
@@ -120,20 +121,32 @@ func ParseArguments(args []string, argsJSON string) (scriptArgs []cadence.Value,
120121
return
121122
}
122123

123-
func ParseArgumentsWithoutType(code []byte, args []string) (scriptArgs []cadence.Value, err error) {
124+
func ParseArgumentsWithoutType(fileName string, code []byte, args []string) (scriptArgs []cadence.Value, err error) {
124125

125126
var resultArgs []cadence.Value = make([]cadence.Value, 0)
126127

127128
codes := map[common.LocationID]string{}
128-
location := common.StringLocation("")
129+
location := common.StringLocation(fileName)
129130
program, must := cmd.PrepareProgram(string(code), location, codes)
130131
checker, _ := cmd.PrepareChecker(program, location, codes, nil, must)
131132

132-
err = checker.Check()
133-
var parameterList []*sema.Parameter = checker.EntryPointParameters()
133+
var parameterList []*ast.Parameter
134134

135-
//return on checker error or no entry
136-
if err != nil || parameterList == nil {
135+
transactionDeclaration := program.SoleTransactionDeclaration()
136+
if transactionDeclaration != nil {
137+
if transactionDeclaration.ParameterList != nil {
138+
parameterList = transactionDeclaration.ParameterList.Parameters
139+
}
140+
}
141+
142+
functionDeclaration := sema.FunctionEntryPointDeclaration(program)
143+
if functionDeclaration != nil {
144+
if functionDeclaration.ParameterList != nil {
145+
parameterList = functionDeclaration.ParameterList.Parameters
146+
}
147+
}
148+
149+
if parameterList == nil {
137150
return resultArgs, nil
138151
}
139152

@@ -142,7 +155,8 @@ func ParseArgumentsWithoutType(code []byte, args []string) (scriptArgs []cadence
142155
}
143156

144157
for index, argumentString := range args {
145-
semaType := parameterList[index].TypeAnnotation.Type
158+
astType := parameterList[index].TypeAnnotation.Type
159+
semaType := checker.ConvertType(astType)
146160

147161
switch semaType {
148162
case sema.StringType:

pkg/flowkit/arguments_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func TestArguments(t *testing.T) {
5050
var sampleType string = sample.Type().ID()
5151

5252
args, err := flowkit.ParseArgumentsWithoutType(
53+
"",
5354
[]byte(fmt.Sprintf(`
5455
pub fun main(test: %s): Void {
5556
}`, sampleType)),

0 commit comments

Comments
 (0)