Skip to content

Commit

Permalink
fix minted event
Browse files Browse the repository at this point in the history
  • Loading branch information
judezhu committed Aug 7, 2024
1 parent dbf8487 commit a21834d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
28 changes: 13 additions & 15 deletions lib/go/events/moment_minted.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package events

import (
"fmt"

"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder"
)

var (
Expand All @@ -20,47 +18,47 @@ type MomentMintedEvent interface {
SubeditionId() uint32
}

type momentMintedEvent cadence.Event
type momentMintedEvent map[string]any

func (evt momentMintedEvent) MomentId() uint64 {
return uint64(evt.Fields[0].(cadence.UInt64))
return evt["momentId"].(uint64)
}

func (evt momentMintedEvent) PlayId() uint32 {
return uint32(evt.Fields[1].(cadence.UInt32))
return evt["playId"].(uint32)
}

func (evt momentMintedEvent) SetId() uint32 {
return uint32(evt.Fields[2].(cadence.UInt32))
return evt["setId"].(uint32)
}

func (evt momentMintedEvent) SerialNumber() uint32 {
return uint32(evt.Fields[3].(cadence.UInt32))
return evt["serialNumber"].(uint32)
}

func (evt momentMintedEvent) SubeditionId() uint32 {
if len(evt.Fields) < 5 {
return 0
if val, ok := evt["subeditionId"]; ok {
return val.(uint32)
}
return uint32(evt.Fields[4].(cadence.UInt32))
return 0
}

func (evt momentMintedEvent) validate() error {
if evt.EventType.QualifiedIdentifier != EventMomentMinted {
if evt["eventType"].(string) != EventMomentMinted {
return fmt.Errorf("error validating event: event is not a valid moment minted event, expected type %s, got %s",
EventMomentMinted, evt.EventType.QualifiedIdentifier)
EventMomentMinted, evt["eventType"].(string))
}
return nil
}

var _ MomentMintedEvent = (*momentMintedEvent)(nil)

func DecodeMomentMintedEvent(b []byte) (MomentMintedEvent, error) {
value, err := jsoncdc.Decode(nil, b)
eventMap, err := decoder.DecodeToEventMap(b)
if err != nil {
return nil, err
}
event := momentMintedEvent(value.(cadence.Event))
event := momentMintedEvent(eventMap)
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
}
Expand Down
24 changes: 12 additions & 12 deletions lib/go/events/moment_minted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,41 @@ func TestCadenceEvents_MomentMinted(t *testing.T) {
serialNumber := uint32(1234)
subeditionID := uint32(1234)

momentMintedEventType := cadence.EventType{
Location: utils.TestLocation,
QualifiedIdentifier: "TopShot.MomentMinted",
Fields: []cadence.Field{
momentMintedEventType := cadence.NewEventType(
utils.TestLocation,
"TopShot.MomentMinted",
[]cadence.Field{
{
Identifier: "momentId",
Type: cadence.UInt64Type{},
Type: cadence.UInt64Type,
},
{
Identifier: "playId",
Type: cadence.UInt32Type{},
Type: cadence.UInt32Type,
},
{
Identifier: "setId",
Type: cadence.UInt32Type{},
Type: cadence.UInt32Type,
},
{
Identifier: "serialNumber",
Type: cadence.UInt32Type{},
Type: cadence.UInt32Type,
},
{
Identifier: "subeditionId",
Type: cadence.UInt32Type{},
Type: cadence.UInt32Type,
},
},
Initializer: []cadence.Parameter{},
}
nil,
)

momentMintedEvent := cadence.NewEvent([]cadence.Value{
cadence.NewUInt64(momentID),
cadence.NewUInt32(playID),
cadence.NewUInt32(setID),
cadence.NewUInt32(serialNumber),
cadence.NewUInt32(subeditionID),
}).WithType(&momentMintedEventType)
}).WithType(momentMintedEventType)

payload, err := jsoncdc.Encode(momentMintedEvent)
require.NoError(t, err, "failed to encode moment minted cadence event")
Expand Down

0 comments on commit a21834d

Please sign in to comment.