Skip to content

Commit

Permalink
Merge pull request #560 from onflow/improvment/update-events
Browse files Browse the repository at this point in the history
update events for interactive accounts creation
  • Loading branch information
psiemens authored Jul 8, 2022
2 parents 3d9998c + 969196c commit 3b28661
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
40 changes: 18 additions & 22 deletions pkg/flowkit/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package flowkit

import (
"strings"
"github.com/onflow/cadence"

"github.com/onflow/flow-go-sdk"
)
Expand All @@ -28,30 +28,37 @@ const addressLength = 16

type Event struct {
Type string
Values map[string]string
Values map[string]cadence.Value
}

type Events []Event

func EventsFromTransaction(tx *flow.TransactionResult) Events {
var events Events
for _, event := range tx.Events {
events = append(events, newEvent(event))
events = append(events, NewEvent(event))
}

return events
}

func newEvent(event flow.Event) Event {
func NewEvents(events []flow.Event) Events {
flowkitEvents := make(Events, len(events))
for i, e := range events {
flowkitEvents[i] = NewEvent(e)
}
return flowkitEvents
}

func NewEvent(event flow.Event) Event {
var names []string

for _, eventType := range event.Value.EventType.Fields {
names = append(names, eventType.Identifier)
}
values := map[string]string{}
values := make(map[string]cadence.Value)
for id, field := range event.Value.Fields {
name := names[id]
values[name] = field.String()
values[names[id]] = field
}

return Event{
Expand All @@ -63,23 +70,12 @@ func newEvent(event flow.Event) Event {
// TODO(sideninja): Refactor this to flow.Address and err as return value instead of returning nil.

func (e *Events) GetAddress() *flow.Address {
addr := ""
for _, event := range *e {
if strings.Contains(event.Type, flow.EventAccountCreated) {
addr = event.Values["address"]
if a, ok := event.Values["address"].(cadence.Address); ok {
address := flow.HexToAddress(a.String())
return &address
}
}

if addr == "" {
return nil
}

// add 0 to beginning of address due to them being stripped
if len(addr) < addressLength {
addr = strings.Repeat("0", addressLength-len(addr)) + addr
}

address := flow.HexToAddress(strings.ReplaceAll(addr, `"`, ""))

return &address
return nil
}
2 changes: 1 addition & 1 deletion pkg/flowkit/tests/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewAccountCreateResult(address flow.Address) *flow.TransactionResult {
Type: cadence.AddressType{},
}},
[]cadence.Value{
cadence.String(address.String()),
cadence.NewAddress(address),
},
),
}
Expand Down

0 comments on commit 3b28661

Please sign in to comment.