Skip to content

Commit

Permalink
Merge pull request #444 from onflow/bugfix/funcs
Browse files Browse the repository at this point in the history
Bugfix transaction multiple function declaration
  • Loading branch information
devbugging authored Jan 25, 2022
2 parents a47607e + 80436ee commit 3d9966a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
24 changes: 23 additions & 1 deletion pkg/flowkit/services/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestTransactions_Integration(t *testing.T) {
ftx := tx.FlowTransaction()
assert.Equal(t, ftx.Script, i.code)
assert.Equal(t, ftx.Payer, i.payer)
assert.Equal(t, ftx.Authorizers, i.auth)
assert.Equal(t, len(ftx.Authorizers), 0) // make sure authorizers weren't added as tx input doesn't require them
assert.Equal(t, ftx.ProposalKey.Address, i.prop)
assert.Equal(t, ftx.ProposalKey.KeyIndex, i.index)
}
Expand Down Expand Up @@ -429,4 +429,26 @@ func TestTransactions_Integration(t *testing.T) {
assert.Nil(t, txr.Error)
assert.Equal(t, txr.Status, flow.TransactionStatusSealed)
})

t.Run("Send transaction with multiple func declaration", func(t *testing.T) {
t.Parallel()
state, s := setupIntegration()
setupAccounts(state, s)

a, _ := state.Accounts().ByName("Alice")

tx, txr, err := s.Transactions.Send(
a,
tests.TransactionMultipleDeclarations.Source,
tests.TransactionMultipleDeclarations.Filename,
1000,
nil,
"",
)
assert.NoError(t, err)
assert.Equal(t, tx.Payer.String(), a.Address().String())
assert.Equal(t, tx.ProposalKey.KeyIndex, a.Key().Index())
assert.Nil(t, txr.Error)
assert.Equal(t, txr.Status, flow.TransactionStatusSealed)
})
}
32 changes: 20 additions & 12 deletions pkg/flowkit/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import (
"encoding/hex"
"fmt"

"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/cmd"
"github.com/onflow/cadence/runtime/common"

jsoncdc "github.com/onflow/cadence/encoding/json"

"github.com/onflow/flow-go-sdk/templates"

"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/templates"
)

const maxGasLimit uint64 = 9999
Expand Down Expand Up @@ -291,12 +289,20 @@ func (t *Transaction) AddAuthorizers(authorizers []flow.Address) (*Transaction,
)

// get authorizers param list if exists
if program.SoleTransactionDeclaration().Prepare != nil {
requiredAuths := program.SoleTransactionDeclaration().
Prepare.
FunctionDeclaration.
ParameterList.
Parameters
if len(program.TransactionDeclarations()) == 1 {
declaration := program.TransactionDeclarations()[0]
requiredAuths := make([]*ast.Parameter, 0)

// if prepare block is missing set default authorizers to empty
if declaration.Prepare == nil {
authorizers = nil
} else { // if prepare block is present get authorizers
requiredAuths = declaration.
Prepare.
FunctionDeclaration.
ParameterList.
Parameters
}

if len(requiredAuths) != len(authorizers) {
return nil, fmt.Errorf(
Expand All @@ -305,6 +311,8 @@ func (t *Transaction) AddAuthorizers(authorizers []flow.Address) (*Transaction,
len(authorizers),
)
}
} else {
return nil, fmt.Errorf("can only support one transaction declaration per file")
}

for _, authorizer := range authorizers {
Expand Down
11 changes: 11 additions & 0 deletions tests/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ var TransactionTwoAuth = resource{
`),
}

var TransactionMultipleDeclarations = resource{
Filename: "transactionMultipleDec.cdc",
Source: []byte(`
pub fun dummy(_ address: Address): Void {}
transaction() {
prepare(authorizer: AuthAccount) {}
}
`),
}

var ScriptWithError = resource{
Filename: "scriptError.cdc",
Source: []byte(`
Expand Down

0 comments on commit 3d9966a

Please sign in to comment.