From 84c1b69ecb05b646434e079d4bd4b8430c99f0c6 Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Tue, 3 Sep 2024 18:32:13 +0200 Subject: [PATCH 1/4] add fees from transaction result --- transaction.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/transaction.go b/transaction.go index 2b094003a..848d51746 100644 --- a/transaction.go +++ b/transaction.go @@ -23,6 +23,7 @@ import ( "errors" "fmt" "sort" + "strings" "github.com/onflow/go-ethereum/rlp" @@ -627,6 +628,31 @@ type TransactionResult struct { CollectionID Identifier } +// Fee returns the transaction fee that was paid by the payer account +// in Flow. +// +// 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 + } + } + + 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 From 935d9cab84386a672b72b878134e8eee3fbf0a08 Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Tue, 3 Sep 2024 18:32:18 +0200 Subject: [PATCH 2/4] add fee test --- transaction_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/transaction_test.go b/transaction_test.go index 124097a3e..7c67927ef 100644 --- a/transaction_test.go +++ b/transaction_test.go @@ -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" @@ -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), + } +} From d134403489c608065b4c584803f514f9dcb78479 Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Tue, 3 Sep 2024 18:33:05 +0200 Subject: [PATCH 3/4] update cadence --- examples/go.mod | 4 ++-- examples/go.sum | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/go.mod b/examples/go.mod index b1c8aae8f..03a20899a 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -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 @@ -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 diff --git a/examples/go.sum b/examples/go.sum index cbdeadaee..262eb6949 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -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= @@ -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= From e3cd773f151b65c18fea01390246c053452af3af Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Tue, 3 Sep 2024 18:38:17 +0200 Subject: [PATCH 4/4] update comment --- transaction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transaction.go b/transaction.go index 848d51746..6c578fef3 100644 --- a/transaction.go +++ b/transaction.go @@ -629,7 +629,7 @@ type TransactionResult struct { } // Fee returns the transaction fee that was paid by the payer account -// in Flow. +// in the smallest unit of Flow (10^-8). // // Error is returned if the fee events could not be parsed correctly from // the transaction result.