Skip to content

Commit e626695

Browse files
authored
Parse contract deployment arg type from source file (#104)
1 parent 4f3fd61 commit e626695

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

flowkit_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ func TestAccountsAddContractWithArgs(t *testing.T) {
630630
UpdateExistingContract(false),
631631
)
632632
assert.Error(t, err)
633-
assert.True(t, strings.Contains(err.Error(), "invalid argument count, too few arguments: expected 1, got 0"))
633+
assert.ErrorContains(t, err, "provided arguments length mismatch, required arguments 1, but provided 0")
634634

635635
c := resourceToContract(tests.ContractSimpleWithArgs)
636636
c.Args = []cadence.Value{cadence.UInt64(4)}
@@ -1082,7 +1082,7 @@ func TestProject(t *testing.T) {
10821082
Network: config.EmulatorNetwork.Name,
10831083
Account: a.Name,
10841084
Contracts: []config.ContractDeployment{
1085-
{Name: c1.Name}, {Name: c3.Name},
1085+
{Name: c1.Name}, {Name: c3.Name, Args: []cadence.Value{cadence.String("foo")}},
10861086
},
10871087
}
10881088
state.Deployments().AddOrUpdate(d)
@@ -1159,7 +1159,7 @@ func TestProject(t *testing.T) {
11591159
Network: config.EmulatorNetwork.Name,
11601160
Account: a.Name,
11611161
Contracts: []config.ContractDeployment{
1162-
{Name: c1.Name}, {Name: c3.Name},
1162+
{Name: c1.Name}, {Name: c3.Name, Args: []cadence.Value{cadence.String("foo")}},
11631163
},
11641164
}
11651165
state.Deployments().AddOrUpdate(d)

transactions/transaction.go

+40-4
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,51 @@ func addAccountContractWithArgs(
118118
AddAuthorizer(signer.Address)
119119

120120
for _, arg := range args {
121-
arg.Type().ID()
122121
tx.AddRawArgument(jsoncdc.MustEncode(arg))
123122
}
124123

124+
// Determine the argument types for the contract init function
125+
program, err := parser.ParseProgram(nil, []byte(contract.Source), parser.Config{})
126+
if err != nil {
127+
return nil, err
128+
}
129+
130+
// get contract init function
131+
contractAst := program.SoleContractDeclaration()
132+
if contractAst == nil {
133+
return nil, fmt.Errorf("failed to find contract declaration")
134+
}
135+
136+
// get contract init function
137+
specialFunctions := contractAst.Members.SpecialFunctions()
138+
var initFunction *ast.SpecialFunctionDeclaration
139+
for _, specialFunction := range specialFunctions {
140+
if specialFunction.FunctionDeclaration.Identifier.Identifier == "init" {
141+
initFunction = specialFunction
142+
break
143+
}
144+
}
145+
146+
// if init function is not found, return error
147+
contractInitArgs := make([]*ast.Parameter, 0)
148+
if initFunction != nil {
149+
contractInitArgs = initFunction.FunctionDeclaration.ParameterList.Parameters
150+
}
151+
152+
// get contract init function arguments
153+
if len(contractInitArgs) != len(args) {
154+
return nil, fmt.Errorf(
155+
"provided arguments length mismatch, required arguments %d, but provided %d",
156+
len(contractInitArgs),
157+
len(args),
158+
)
159+
}
160+
125161
// here we itterate over all arguments and possibly extend the transaction input argument
126162
// in the above template to include them
127163
txArgs, addArgs := "", ""
128-
for i, arg := range args {
129-
txArgs += fmt.Sprintf(",arg%d:%s", i, arg.Type().ID())
164+
for i, arg := range contractInitArgs {
165+
txArgs += fmt.Sprintf(",arg%d:%s", i, arg.TypeAnnotation.Type.String())
130166
addArgs += fmt.Sprintf(",arg%d", i)
131167
}
132168

@@ -135,7 +171,7 @@ func addAccountContractWithArgs(
135171
tx.SetComputeLimit(flow.DefaultTransactionGasLimit)
136172

137173
t := &Transaction{tx: tx}
138-
err := t.SetSigner(signer)
174+
err = t.SetSigner(signer)
139175
if err != nil {
140176
return nil, err
141177
}

0 commit comments

Comments
 (0)