Skip to content

Commit

Permalink
added balances and signatures to OverflowTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
bjartek committed Sep 19, 2024
1 parent d4521ff commit 581b9af
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 60 deletions.
15 changes: 13 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 7 additions & 16 deletions event_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}
Expand All @@ -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{}{
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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{}{
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -189,5 +181,4 @@ func TestFilterOverflowEvents(t *testing.T) {
want := autogold.Want("filter all empty deposit withdraw", OverflowEvents{})
want.Equal(t, filtered)
})

}
11 changes: 6 additions & 5 deletions interaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type OverflowResult struct {
Fee map[string]interface{}
FeeGas int

Balance FeeBalance
// The name of the Transaction
Name string

Expand Down
81 changes: 44 additions & 37 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -115,32 +118,36 @@ 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 {
eventList = append(eventList, evList...)
}

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
}

Expand Down

0 comments on commit 581b9af

Please sign in to comment.