From 71751e47eb7556caf79140ba2c11903d3b76f1cc Mon Sep 17 00:00:00 2001 From: "Bjarte S. Karlsen" Date: Sun, 22 Sep 2024 21:09:14 +0200 Subject: [PATCH] added balances and signatures to OverflowTransaction (#175) --- event.go | 15 ++++++-- event_filter_test.go | 23 ++++-------- interaction_builder.go | 11 +++--- result.go | 1 + transaction.go | 81 +++++++++++++++++++++++------------------- 5 files changed, 71 insertions(+), 60 deletions(-) diff --git a/event.go b/event.go index cfda78b..af9b169 100644 --- a/event.go +++ b/event.go @@ -52,6 +52,11 @@ type OverflowEvent struct { EventIndex uint32 `json:"eventIndex"` } +type FeeBalance struct { + PayerBalance float64 `json:"payerBalance"` + TotalFeeBalance float64 `json:"totalFeeBalance"` +} + // Check if an event exist in the other events func (o OverflowEvent) ExistIn(events []OverflowEvent) bool { for _, ev := range events { @@ -231,8 +236,10 @@ func (overflowEvents OverflowEvents) FilterTempWithdrawDeposit() OverflowEvents var feeReceipients = []string{"0xf919ee77447b7497", "0x912d5440f7e3769e", "0xe5a8b7f23e8b548f", "0xab086ce9cc29fc80"} // Filtter out fee events -func (overflowEvents OverflowEvents) FilterFees(fee float64, payer string) OverflowEvents { +func (overflowEvents OverflowEvents) FilterFees(fee float64, payer string) (OverflowEvents, FeeBalance) { filteredEvents := overflowEvents + + fees := FeeBalance{} for name, events := range overflowEvents { if strings.HasSuffix(name, "FlowFees.FeesDeducted") { delete(filteredEvents, name) @@ -250,6 +257,8 @@ func (overflowEvents OverflowEvents) FilterFees(fee float64, payer string) Overf from, ok := value.Fields["from"].(string) if ok && amount == fee && from == payer { + balance, _ := value.Fields["balanceAfter"].(float64) + fees.PayerBalance = balance continue } @@ -274,6 +283,8 @@ func (overflowEvents OverflowEvents) FilterFees(fee float64, payer string) Overf to, ok := value.Fields["to"].(string) if ok && amount == fee && slices.Contains(feeReceipients, to) { + balance, _ := value.Fields["balanceAfter"].(float64) + fees.TotalFeeBalance = balance continue } depositEvents = append(depositEvents, value) @@ -326,7 +337,7 @@ func (overflowEvents OverflowEvents) FilterFees(fee float64, payer string) Overf } } - return filteredEvents + return filteredEvents, fees } func printOrLog(t *testing.T, s string) { diff --git a/event_filter_test.go b/event_filter_test.go index ae78c13..2861ba2 100644 --- a/event_filter_test.go +++ b/event_filter_test.go @@ -8,26 +8,23 @@ import ( ) func TestFilterOverflowEvents(t *testing.T) { - events := OverflowEvents{ - "A.123.Test.Deposit": []OverflowEvent{{Fields: map[string]interface{}{ - "id": 1, - "string": "string", - }}, + "A.123.Test.Deposit": []OverflowEvent{ + {Fields: map[string]interface{}{ + "id": 1, + "string": "string", + }}, }, } t.Run("Filter out all events should yield empty", func(t *testing.T) { - filter := OverflowEventFilter{ "Deposit": []string{"id", "string"}, } filtered := events.FilterEvents(filter) assert.Empty(t, filtered) - }) t.Run("Filter out single field", func(t *testing.T) { - filter := OverflowEventFilter{ "Deposit": []string{"id"}, } @@ -39,7 +36,6 @@ func TestFilterOverflowEvents(t *testing.T) { }) t.Run("Filter fees", func(t *testing.T) { - eventsWithFees := OverflowEvents{ "A.f919ee77447b7497.FlowFees.FeesDeducted": []OverflowEvent{ {Fields: map[string]interface{}{ @@ -64,7 +60,7 @@ func TestFilterOverflowEvents(t *testing.T) { }}, }, } - filtered := eventsWithFees.FilterFees(0.00000918, "0x55ad22f01ef568a1") + filtered, _ := eventsWithFees.FilterFees(0.00000918, "0x55ad22f01ef568a1") want := autogold.Want("fees filtered", OverflowEvents{"A.1654653399040a61.FlowToken.TokensDeposited": []OverflowEvent{ {Fields: map[string]interface{}{ "amount": 1, @@ -75,7 +71,6 @@ func TestFilterOverflowEvents(t *testing.T) { }) t.Run("Filter fees with other transfer", func(t *testing.T) { - eventsWithFees := OverflowEvents{ "A.f919ee77447b7497.FlowFees.FeesDeducted": []OverflowEvent{{Fields: map[string]interface{}{ "amount": 0.00000918, @@ -97,7 +92,7 @@ func TestFilterOverflowEvents(t *testing.T) { "to": "0xf919ee77447b7497", }}}, } - filtered := eventsWithFees.FilterFees(0.00000918, "0x55ad22f01ef568a1") + filtered, _ := eventsWithFees.FilterFees(0.00000918, "0x55ad22f01ef568a1") want := autogold.Want("fees filtered with transfer", OverflowEvents{ "A.1654653399040a61.FlowToken.TokensDeposited": []OverflowEvent{ {Fields: map[string]interface{}{ @@ -114,7 +109,6 @@ func TestFilterOverflowEvents(t *testing.T) { }) t.Run("Filter empty deposit withdraw", func(t *testing.T) { - eventsWithFees := OverflowEvents{ "A.1654653399040a61.FlowToken.TokensWithdrawn": []OverflowEvent{{Fields: map[string]interface{}{ "amount": 0.00000918, @@ -139,7 +133,6 @@ func TestFilterOverflowEvents(t *testing.T) { }) t.Run("Filter non-empty deposit withdraw", func(t *testing.T) { - eventsWithFees := OverflowEvents{ "A.1654653399040a61.FlowToken.TokensWithdrawn": []OverflowEvent{{Fields: map[string]interface{}{ "amount": 0.00000918, @@ -174,7 +167,6 @@ func TestFilterOverflowEvents(t *testing.T) { }) t.Run("Filter all empty deposit withdraw", func(t *testing.T) { - eventsWithFees := OverflowEvents{ "A.1654653399040a61.FlowToken.TokensWithdrawn": []OverflowEvent{{Fields: map[string]interface{}{ "amount": 0.00000918, @@ -189,5 +181,4 @@ func TestFilterOverflowEvents(t *testing.T) { want := autogold.Want("filter all empty deposit withdraw", OverflowEvents{}) want.Equal(t, filtered) }) - } diff --git a/interaction_builder.go b/interaction_builder.go index d4bcd8c..9594a5c 100644 --- a/interaction_builder.go +++ b/interaction_builder.go @@ -612,12 +612,13 @@ func (oib OverflowInteractionBuilder) Send() *OverflowResult { result.FeeGas = gas } - if !oib.IgnoreGlobalEventFilters { - - fee := result.Fee["amount"] + feeAmount := result.Fee["amount"] + eventsWithoutFees, feeFromEvents := overflowEvents.FilterFees(feeAmount.(float64), fmt.Sprintf("0x%s", result.Transaction.Payer.Hex())) + result.Balance = feeFromEvents - if oib.Overflow.FilterOutFeeEvents && fee != nil { - overflowEvents = overflowEvents.FilterFees(fee.(float64), fmt.Sprintf("0x%s", result.Transaction.Payer.Hex())) + if !oib.IgnoreGlobalEventFilters { + if oib.Overflow.FilterOutFeeEvents && feeAmount != nil { + overflowEvents = eventsWithoutFees } if oib.Overflow.FilterOutEmptyWithDrawDepositEvents { overflowEvents = overflowEvents.FilterTempWithdrawDeposit() diff --git a/result.go b/result.go index 3703555..b911734 100644 --- a/result.go +++ b/result.go @@ -58,6 +58,7 @@ type OverflowResult struct { Fee map[string]interface{} FeeGas int + Balance FeeBalance // The name of the Transaction Name string diff --git a/transaction.go b/transaction.go index eafb218..7d612cc 100644 --- a/transaction.go +++ b/transaction.go @@ -21,24 +21,27 @@ type Argument struct { } type OverflowTransaction struct { - Error error - AuthorizerTypes map[string][]string - Stakeholders map[string][]string - Payer string - Id string - Status string - BlockId string - Authorizers []string - Arguments []Argument - Events []OverflowEvent - Imports []Import - Script []byte - ProposalKey flow.ProposalKey - Fee float64 - TransactionIndex int - GasLimit uint64 - GasUsed uint64 - ExecutionEffort float64 + Error error + AuthorizerTypes map[string][]string + Stakeholders map[string][]string + Payer string + Id string + Status string + BlockId string + Authorizers []string + Arguments []Argument + Events []OverflowEvent + Imports []Import + Script []byte + ProposalKey flow.ProposalKey + Fee float64 + TransactionIndex int + GasLimit uint64 + GasUsed uint64 + ExecutionEffort float64 + Balances FeeBalance + PayloadSignatures []flow.TransactionSignature + EnvelopeSignatures []flow.TransactionSignature } func (o *OverflowState) CreateOverflowTransaction(blockId string, transactionResult flow.TransactionResult, transaction flow.Transaction, txIndex int) (*OverflowTransaction, error) { @@ -115,7 +118,8 @@ func (o *OverflowState) CreateOverflowTransaction(blockId string, transactionRes standardStakeholders[fmt.Sprintf("0x%s", transaction.ProposalKey.Address.Hex())] = proposer } - eventsWithoutFees := events.FilterFees(feeAmount, fmt.Sprintf("0x%s", transaction.Payer.Hex())) + // TODO: here we need to get out the balance of the payer and the fee recipient + eventsWithoutFees, balanceFees := events.FilterFees(feeAmount, fmt.Sprintf("0x%s", transaction.Payer.Hex())) eventList := []OverflowEvent{} for _, evList := range eventsWithoutFees { @@ -123,24 +127,27 @@ func (o *OverflowState) CreateOverflowTransaction(blockId string, transactionRes } return &OverflowTransaction{ - Id: transactionResult.TransactionID.String(), - TransactionIndex: txIndex, - BlockId: blockId, - Status: status, - Events: eventList, - Stakeholders: eventsWithoutFees.GetStakeholders(standardStakeholders), - Imports: imports, - Error: transactionResult.Error, - Arguments: args, - Fee: feeAmount, - Script: transaction.Script, - Payer: fmt.Sprintf("0x%s", transaction.Payer.String()), - ProposalKey: transaction.ProposalKey, - GasLimit: transaction.GasLimit, - GasUsed: uint64(gas), - ExecutionEffort: executionEffort, - Authorizers: authorizers, - AuthorizerTypes: authorizerTypes, + Id: transactionResult.TransactionID.String(), + TransactionIndex: txIndex, + BlockId: blockId, + Status: status, + Events: eventList, + Stakeholders: eventsWithoutFees.GetStakeholders(standardStakeholders), + Imports: imports, + Error: transactionResult.Error, + Arguments: args, + Fee: feeAmount, + Script: transaction.Script, + Payer: fmt.Sprintf("0x%s", transaction.Payer.String()), + ProposalKey: transaction.ProposalKey, + GasLimit: transaction.GasLimit, + GasUsed: uint64(gas), + ExecutionEffort: executionEffort, + Authorizers: authorizers, + AuthorizerTypes: authorizerTypes, + Balances: balanceFees, + PayloadSignatures: transaction.PayloadSignatures, + EnvelopeSignatures: transaction.EnvelopeSignatures, }, nil }