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

added balances and signatures to OverflowTransaction #175

Merged
merged 1 commit into from
Sep 22, 2024
Merged
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
15 changes: 13 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"strings"
"testing"

"github.com/bjartek/underflow"

Check failure on line 9 in event.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/bjartek/underflow' is not allowed from list 'Main' (depguard)
"github.com/onflow/cadence"

Check failure on line 10 in event.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/onflow/cadence' is not allowed from list 'Main' (depguard)
"github.com/onflow/flow-go-sdk"

Check failure on line 11 in event.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/onflow/flow-go-sdk' is not allowed from list 'Main' (depguard)
"github.com/sanity-io/litter"

Check failure on line 12 in event.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/sanity-io/litter' is not allowed from list 'Main' (depguard)
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -52,6 +52,11 @@
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 @@
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 @@
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 @@
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 @@

}
}
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 @@ -8,12 +8,12 @@
"strings"
"testing"

"github.com/bjartek/underflow"

Check failure on line 11 in interaction_builder.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/bjartek/underflow' is not allowed from list 'Main' (depguard)
"github.com/enescakir/emoji"

Check failure on line 12 in interaction_builder.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/enescakir/emoji' is not allowed from list 'Main' (depguard)
"github.com/onflow/cadence"

Check failure on line 13 in interaction_builder.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/onflow/cadence' is not allowed from list 'Main' (depguard)
"github.com/onflow/flow-go-sdk"

Check failure on line 14 in interaction_builder.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/onflow/flow-go-sdk' is not allowed from list 'Main' (depguard)
"github.com/onflow/flowkit/v2"

Check failure on line 15 in interaction_builder.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/onflow/flowkit/v2' is not allowed from list 'Main' (depguard)
"github.com/onflow/flowkit/v2/accounts"

Check failure on line 16 in interaction_builder.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/onflow/flowkit/v2/accounts' is not allowed from list 'Main' (depguard)
"github.com/onflow/flowkit/v2/transactions"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -612,12 +612,13 @@
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
Loading