Skip to content

Commit

Permalink
Merge pull request #1188 from onflow/filter-fee-events
Browse files Browse the repository at this point in the history
Filter fee events
  • Loading branch information
chasefleming authored Sep 20, 2023
2 parents 602a2c7 + aecdb6c commit 7c53925
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 10 deletions.
6 changes: 3 additions & 3 deletions flowkit/config/json/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,9 @@ func (a account) JSONSchema() *jsonschema.Schema {
},
},
Definitions: map[string]*jsonschema.Schema{
"simpleAccount": jsonschema.Reflect(simpleAccount{}),
"advancedAccount": jsonschema.Reflect(advancedAccount{}),
"simpleAccountPre022": jsonschema.Reflect(simpleAccountPre022{}),
"simpleAccount": jsonschema.Reflect(simpleAccount{}),
"advancedAccount": jsonschema.Reflect(advancedAccount{}),
"simpleAccountPre022": jsonschema.Reflect(simpleAccountPre022{}),
"advanceAccountPre022": jsonschema.Reflect(advanceAccountPre022{}),
},
}
Expand Down
2 changes: 1 addition & 1 deletion flowkit/config/json/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,4 @@ func (j jsonNetwork) JSONSchema() *jsonschema.Schema {
"advancedNetwork": jsonschema.Reflect(advancedNetwork{}),
},
}
}
}
4 changes: 2 additions & 2 deletions flowkit/config/json/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func GenerateSchema() *jsonschema.Schema {
// This is necessary because the jsonschema library does not support
// definitions in nested schemas and is a workaround
var moveDefinitions func(*jsonschema.Schema)
moveDefinitions = func (s *jsonschema.Schema) {
moveDefinitions = func(s *jsonschema.Schema) {
for k, v := range s.Definitions {
schema.Definitions[k] = v
moveDefinitions(v)
}
if (s != schema) {
if s != schema {
s.Definitions = nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion flowkit/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (p *State) AliasesForNetwork(network config.Network) project.LocationAliase
if contract.IsAliased() && contract.Aliases.ByNetwork(network.Name) != nil {
alias := contract.Aliases.ByNetwork(network.Name).Address.String()
aliases[filepath.Clean(contract.Location)] = alias // alias for import by file location
aliases[contract.Name] = alias // alias for import by name
aliases[contract.Name] = alias // alias for import by name
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/transactions/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

type flagsGet struct {
Sealed bool `default:"true" flag:"sealed" info:"Wait for a sealed result"`
Include []string `default:"" flag:"include" info:"Fields to include in the output. Valid values: signatures, code, payload."`
Include []string `default:"" flag:"include" info:"Fields to include in the output. Valid values: signatures, code, payload, fee-events."`
Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output. Valid values: events."`
}

Expand Down
17 changes: 17 additions & 0 deletions internal/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/onflow/flow-go-sdk"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -90,6 +91,8 @@ func (r *transactionResult) JSON() any {
func (r *transactionResult) String() string {
var b bytes.Buffer
writer := util.CreateTabWriter(&b)
const feeEventsCountAppended = 3
const feeDeductedEvent = "FeesDeducted"

if r.result != nil {
_, _ = fmt.Fprintf(writer, "Block ID\t%s\n", r.result.BlockID)
Expand Down Expand Up @@ -153,6 +156,16 @@ func (r *transactionResult) String() string {
Events: r.result.Events,
}

if r.result != nil && e.Events != nil && !command.ContainsFlag(r.include, "fee-events") {
for _, event := range e.Events {
if strings.Contains(event.Type, feeDeductedEvent) {
// if fee event are present remove them
e.Events = e.Events[:len(e.Events)-feeEventsCountAppended]
break
}
}
}

eventsOutput := e.String()
if eventsOutput == "" {
eventsOutput = "None"
Expand Down Expand Up @@ -184,6 +197,10 @@ func (r *transactionResult) String() string {
_, _ = fmt.Fprint(writer, "\n\nPayload (hidden, use --include payload)")
}

if !command.ContainsFlag(r.include, "fee-events") && !command.ContainsFlag(r.exclude, "events") {
_, _ = fmt.Fprint(writer, "\n\nFee Events (hidden, use --include fee-events)")
}

_ = writer.Flush()
return b.String()
}
Expand Down
120 changes: 118 additions & 2 deletions internal/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,24 @@ func Test_Result(t *testing.T) {
)
event.Payload = []byte("mock_payload")

withdrawEvent := tests.NewEvent(
1,
"A.1654653399040a61.FlowToken.TokensWithdrawn",
[]cadence.Field{{Type: cadence.StringType{}, Identifier: "bar"}},
[]cadence.Value{cadence.NewInt(1)},
)
depositEvent := tests.NewEvent(
2,
"A.1654653399040a61.FlowToken.TokensDeposited",
[]cadence.Field{{Type: cadence.StringType{}, Identifier: "bar"}},
[]cadence.Value{cadence.NewInt(1)},
)
feeEvent := tests.NewEvent(
3,
"A.f919ee77447b7497.FlowFees.FeesDeducted",
[]cadence.Field{{Type: cadence.StringType{}, Identifier: "bar"}},
[]cadence.Value{cadence.NewInt(1)},
)
txResult := &flow.TransactionResult{
Status: flow.TransactionStatusSealed,
Error: nil,
Expand All @@ -314,6 +332,13 @@ func Test_Result(t *testing.T) {
BlockHeight: 1,
}

txResultFeeEvents := &flow.TransactionResult{
Status: flow.TransactionStatusSealed,
Error: nil,
Events: []flow.Event{*event, *withdrawEvent, *depositEvent, *feeEvent},
BlockID: flow.HexToID("7aa74143741c1c3b837d389fcffa7a5e251b67b4ffef6d6887b40cd9c803f537"),
BlockHeight: 1,
}
t.Run("Success with no result", func(t *testing.T) {
result := transactionResult{tx: tx}

Expand All @@ -333,7 +358,9 @@ Signatures (minimized, use --include signatures)
Code (hidden, use --include code)
Payload (hidden, use --include payload)`, "\n"), result.String())
Payload (hidden, use --include payload)
Fee Events (hidden, use --include fee-events)`, "\n"), result.String())

assert.Equal(t, map[string]any{
"authorizers": "[]",
Expand Down Expand Up @@ -374,7 +401,9 @@ Events:
Code (hidden, use --include code)
Payload (hidden, use --include payload)`, output.OkEmoji()), "\n")
Payload (hidden, use --include payload)
Fee Events (hidden, use --include fee-events)`, output.OkEmoji()), "\n")

assert.Equal(t, expectedString, result.String())

Expand All @@ -395,4 +424,91 @@ Payload (hidden, use --include payload)`, output.OkEmoji()), "\n")
"status": "SEALED",
}, result.JSON())
})

t.Run("Result without fee events", func(t *testing.T) {
result := transactionResult{tx: tx, result: txResultFeeEvents}

assert.Equal(t, strings.TrimPrefix(`
Block ID 7aa74143741c1c3b837d389fcffa7a5e251b67b4ffef6d6887b40cd9c803f537
Block Height 1
Status ✅ SEALED
ID e913d1f3e431c7df49c99845bea9ebff9db11bbf25d507b9ad0fad45652d515f
Payer 0000000000000002
Authorizers []
Proposal Key:
Address 0000000000000001
Index 0
Sequence 1
Payload Signature 0: 0000000000000001
Envelope Signature 0: 0000000000000001
Signatures (minimized, use --include signatures)
Events:
Index 0
Type A.foo
Tx ID 0000000000000000000000000000000000000000000000000000000000000000
Values
- bar (String): 1
Code (hidden, use --include code)
Payload (hidden, use --include payload)
Fee Events (hidden, use --include fee-events)`, "\n"), result.String())
})
t.Run("Result with fee events", func(t *testing.T) {
result := transactionResult{tx: tx, result: txResultFeeEvents, include: []string{"fee-events"}}

assert.Equal(t, strings.TrimPrefix(`
Block ID 7aa74143741c1c3b837d389fcffa7a5e251b67b4ffef6d6887b40cd9c803f537
Block Height 1
Status ✅ SEALED
ID e913d1f3e431c7df49c99845bea9ebff9db11bbf25d507b9ad0fad45652d515f
Payer 0000000000000002
Authorizers []
Proposal Key:
Address 0000000000000001
Index 0
Sequence 1
Payload Signature 0: 0000000000000001
Envelope Signature 0: 0000000000000001
Signatures (minimized, use --include signatures)
Events:
Index 0
Type A.foo
Tx ID 0000000000000000000000000000000000000000000000000000000000000000
Values
- bar (String): 1
Index 1
Type A.1654653399040a61.FlowToken.TokensWithdrawn
Tx ID 0000000000000000000000000000000000000000000000000000000000000000
Values
- bar (String): 1
Index 2
Type A.1654653399040a61.FlowToken.TokensDeposited
Tx ID 0000000000000000000000000000000000000000000000000000000000000000
Values
- bar (String): 1
Index 3
Type A.f919ee77447b7497.FlowFees.FeesDeducted
Tx ID 0000000000000000000000000000000000000000000000000000000000000000
Values
- bar (String): 1
Code (hidden, use --include code)
Payload (hidden, use --include payload)`, "\n"), result.String())
})
}

0 comments on commit 7c53925

Please sign in to comment.