Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 35 additions & 0 deletions flowkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,30 @@ func simpleDeploy(state *State, flowkit Flowkit, update bool) ([]*project.Contra
return flowkit.DeployProject(ctx, UpdateExistingContract(update))
}

// used for integration tests
func simpleDeployInterface(state *State, flowkit Flowkit, update bool) ([]*project.Contract, error) {
srvAcc, _ := state.EmulatorServiceAccount()

c := config.Contract{
Name: tests.ContractInterfaceSimple.Name,
Location: tests.ContractInterfaceSimple.Filename,
}
state.Contracts().AddOrUpdate(c)
state.Networks().AddOrUpdate(config.EmulatorNetwork)

d := config.Deployment{
Network: config.EmulatorNetwork.Name,
Account: srvAcc.Name,
Contracts: []config.ContractDeployment{{
Name: c.Name,
Args: nil,
}},
}
state.Deployments().AddOrUpdate(d)

return flowkit.DeployProject(ctx, UpdateExistingContract(update))
}

func TestProject_Integration(t *testing.T) {
t.Run("Deploy Project", func(t *testing.T) {
t.Parallel()
Expand All @@ -1280,6 +1304,17 @@ func TestProject_Integration(t *testing.T) {
assert.Equal(t, string(contracts[0].Code()), string(tests.ContractHelloString.Source))
})

t.Run("Deploy Interface", func(t *testing.T) {
t.Parallel()

state, flowkit := setupIntegration()
contracts, err := simpleDeployInterface(state, flowkit, false)
assert.NoError(t, err)
assert.Len(t, contracts, 1)
assert.Equal(t, contracts[0].Name, tests.ContractInterfaceSimple.Name)
assert.Equal(t, string(contracts[0].Code()), string(tests.ContractInterfaceSimple.Source))
})

t.Run("Deploy Complex Project", func(t *testing.T) {
t.Parallel()

Expand Down
13 changes: 12 additions & 1 deletion tests/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ var ContractHelloString = Resource{
`),
}

var ContractInterfaceSimple = Resource{
Name: "ISimple",
Filename: "contractInterfaceSimple.cdc",
Source: []byte(`
access(all) contract interface ISimple {
access(all) fun aFunc()
}
`),
}

var ContractSimple = Resource{
Name: "Simple",
Filename: "contractSimple.cdc",
Expand Down Expand Up @@ -521,10 +531,11 @@ var resources = []Resource{
ContractBB,
ContractCC,
ContractFooCoverage,
ContractInterfaceSimple,
}

func ReaderWriter() (afero.Afero, afero.Fs) {
var mockFS = afero.NewMemMapFs()
mockFS := afero.NewMemMapFs()

for _, c := range resources {
_ = afero.WriteFile(mockFS, c.Filename, c.Source, 0644)
Expand Down
68 changes: 35 additions & 33 deletions transactions/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,43 +127,45 @@ func addAccountContractWithArgs(
return nil, err
}

// get contract init function
contractAst := program.SoleContractDeclaration()
if contractAst == nil {
return nil, fmt.Errorf("failed to find contract declaration")
}

// get contract init function
specialFunctions := contractAst.Members.SpecialFunctions()
var initFunction *ast.SpecialFunctionDeclaration
for _, specialFunction := range specialFunctions {
if specialFunction.FunctionDeclaration.Identifier.Identifier == "init" {
initFunction = specialFunction
break
txArgs, addArgs := "", ""
interfaceAst := program.SoleContractInterfaceDeclaration()
if interfaceAst == nil {
// get contract init function
contractAst := program.SoleContractDeclaration()
if contractAst == nil {
return nil, fmt.Errorf("failed to find contract declaration")
}
}

// if init function is not found, return error
contractInitArgs := make([]*ast.Parameter, 0)
if initFunction != nil {
contractInitArgs = initFunction.FunctionDeclaration.ParameterList.Parameters
}
// get contract init function
specialFunctions := contractAst.Members.SpecialFunctions()
var initFunction *ast.SpecialFunctionDeclaration
for _, specialFunction := range specialFunctions {
if specialFunction.FunctionDeclaration.Identifier.Identifier == "init" {
initFunction = specialFunction
break
}
}

// get contract init function arguments
if len(contractInitArgs) != len(args) {
return nil, fmt.Errorf(
"provided arguments length mismatch, required arguments %d, but provided %d",
len(contractInitArgs),
len(args),
)
}
// if init function is not found, return error
contractInitArgs := make([]*ast.Parameter, 0)
if initFunction != nil {
contractInitArgs = initFunction.FunctionDeclaration.ParameterList.Parameters
}

// here we itterate over all arguments and possibly extend the transaction input argument
// in the above template to include them
txArgs, addArgs := "", ""
for i, arg := range contractInitArgs {
txArgs += fmt.Sprintf(",arg%d:%s", i, arg.TypeAnnotation.Type.String())
addArgs += fmt.Sprintf(",arg%d", i)
// get contract init function arguments
if len(contractInitArgs) != len(args) {
return nil, fmt.Errorf(
"provided arguments length mismatch, required arguments %d, but provided %d",
len(contractInitArgs),
len(args),
)
}
// here we itterate over all arguments and possibly extend the transaction input argument
// in the above template to include them
for i, arg := range contractInitArgs {
txArgs += fmt.Sprintf(",arg%d:%s", i, arg.TypeAnnotation.Type.String())
addArgs += fmt.Sprintf(",arg%d", i)
}
}

script := fmt.Sprintf(addAccountContractTemplate, txArgs, addArgs)
Expand Down
Loading