Skip to content
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

Add fee method on transaction results #738

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.4
replace github.com/onflow/flow-go-sdk => ../

require (
github.com/onflow/cadence v1.0.0-preview.38
github.com/onflow/cadence v1.0.0-preview.52
github.com/onflow/flow-cli/flowkit v1.11.0
github.com/onflow/flow-go-sdk v0.41.17
github.com/spf13/afero v1.11.0
Expand Down Expand Up @@ -41,7 +41,7 @@ require (
github.com/logrusorgru/aurora/v4 v4.0.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/onflow/atree v0.7.0-rc.2 // indirect
github.com/onflow/atree v0.8.0-rc.6 // indirect
github.com/onflow/crypto v0.25.1 // indirect
github.com/onflow/flow/protobuf/go/flow v0.4.3 // indirect
github.com/onflow/sdks v0.6.0-preview.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ github.com/onflow/atree v0.6.0/go.mod h1:gBHU0M05qCbv9NN0kijLWMgC47gHVNBIp4KmsVF
github.com/onflow/atree v0.6.1-0.20230711151834-86040b30171f/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ0tHf4DRuM=
github.com/onflow/atree v0.6.1-0.20240429171449-cb486ceb1f9c/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ0tHf4DRuM=
github.com/onflow/atree v0.7.0-rc.2/go.mod h1:xvP61FoOs95K7IYdIYRnNcYQGf4nbF/uuJ0tHf4DRuM=
github.com/onflow/atree v0.8.0-rc.6/go.mod h1:yccR+LR7xc1Jdic0mrjocbHvUD7lnVvg8/Ct1AA5zBo=
github.com/onflow/cadence v0.42.7 h1:Qp9VYX901saO7wPwF/rwV4cMS+0mfWxnm9EqbYElYy4=
github.com/onflow/cadence v0.42.7/go.mod h1:raU8va8QRyTa/eUbhej4mbyW2ETePfSaywoo36MddgE=
github.com/onflow/cadence v1.0.0-M3/go.mod h1:odXGZZ/wGNA5mwT8bC9v8u8EXACHllB2ABSZK65TGL8=
Expand All @@ -115,6 +116,7 @@ github.com/onflow/cadence v1.0.0-preview.31/go.mod h1:3LM1VgE9HkJ815whY/F0LYWULw
github.com/onflow/cadence v1.0.0-preview.35/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0=
github.com/onflow/cadence v1.0.0-preview.36/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0=
github.com/onflow/cadence v1.0.0-preview.38/go.mod h1:jOwvPSSLTr9TvaKMs7KKiBYMmpdpNNAFxBsjMlrqVD0=
github.com/onflow/cadence v1.0.0-preview.52/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/crypto v0.25.0 h1:BeWbLsh3ZD13Ej+Uky6kg1PL1ZIVBDVX+2MVBNwqddg=
github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
github.com/onflow/crypto v0.25.1/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
Expand Down
26 changes: 26 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"sort"
"strings"

"github.com/onflow/go-ethereum/rlp"

Expand Down Expand Up @@ -627,6 +628,31 @@ type TransactionResult struct {
CollectionID Identifier
}

// Fee returns the transaction fee that was paid by the payer account
// in the smallest unit of Flow (10^-8).
//
// Error is returned if the fee events could not be parsed correctly from
// the transaction result.
func (t *TransactionResult) Fee() (uint64, error) {
var feeEvent cadence.Event
for _, e := range t.Events {
if strings.Contains(e.Type, "FlowFees.FeesDeducted") {
if feeEvent.Type() != nil {
return 0, fmt.Errorf("could not extract transaction fee")
}
feeEvent = e.Value
}
Comment on lines +643 to +644
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
feeEvent = e.Value
}
feeEvent = e.Value
break
}

}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fees could be disabled

Suggested change
if feeEvent == nil {
return 0, nil
}

feesValue := cadence.SearchFieldByName(feeEvent, "amount")
fee, ok := feesValue.(cadence.UFix64)
if !ok {
return 0, fmt.Errorf("failed to convert fee value")
}

return uint64(fee), nil
}

// TransactionStatus represents the status of a transaction.
type TransactionStatus int

Expand Down
54 changes: 54 additions & 0 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -692,3 +693,56 @@ func TestTransaction_RLPMessages(t *testing.T) {
})
}
}

func Test_TransactionResult(t *testing.T) {

t.Run("Fees successfully extracted", func(t *testing.T) {
feeAmount, _ := cadence.NewUFix64("0.005")
res := flow.TransactionResult{
Events: []flow.Event{createFeeEvent(feeAmount)},
}

extractedFee, err := res.Fee()
require.NoError(t, err)
require.Equal(t, uint64(feeAmount), extractedFee)
})

t.Run("Fees unsuccessfully extracted", func(t *testing.T) {
fee1, _ := cadence.NewUFix64("0.1")
fee2, _ := cadence.NewUFix64("0.2")

// duplicate fees
res := flow.TransactionResult{
Events: []flow.Event{createFeeEvent(fee1), createFeeEvent(fee2)},
}

fee, err := res.Fee()
require.EqualError(t, err, "could not extract transaction fee")
require.Zero(t, fee)

// no fees
res = flow.TransactionResult{}
fee, err = res.Fee()
require.EqualError(t, err, "failed to convert fee value")
})
}

func createFeeEvent(feeValue cadence.UFix64) flow.Event {
address, _ := common.HexToAddress("f919ee77447b7497")
location := common.NewAddressLocation(nil, address, "FlowFees")

eventType := cadence.NewEventType(
location,
"FlowFees.FeesDeducted",
[]cadence.Field{{
Identifier: "amount",
Type: cadence.UFix64Type,
}},
nil,
)

return flow.Event{
Type: eventType.ID(),
Value: cadence.NewEvent([]cadence.Value{feeValue}).WithType(eventType),
}
}
Loading