From 0c3db898d2fcae02c372a0c209fd301f46038c76 Mon Sep 17 00:00:00 2001 From: Jude Zhu Date: Thu, 8 Aug 2024 13:56:32 -0700 Subject: [PATCH 1/5] fix tests --- lib/go/events/decoder/decoder.go | 1 + lib/go/events/moment_destroy.go | 3 --- lib/go/events/moment_destroy_test.go | 22 +++++++++++----------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/go/events/decoder/decoder.go b/lib/go/events/decoder/decoder.go index de6fa249..8bbd40b8 100644 --- a/lib/go/events/decoder/decoder.go +++ b/lib/go/events/decoder/decoder.go @@ -24,6 +24,7 @@ func DecodeCadenceValue(payload []byte) (cadence.Value, error) { ) if err != nil { // json decode failed, try ccf decode + fmt.Printf("json decode failed, try ccf decode, reason: %s\n", err.Error()) ccfValue, ccfErr := ccf.Decode(nil, payload) if ccfErr != nil { return cadence.Event{}, pkgerrors.Wrapf( diff --git a/lib/go/events/moment_destroy.go b/lib/go/events/moment_destroy.go index e14b83f4..90641c88 100644 --- a/lib/go/events/moment_destroy.go +++ b/lib/go/events/moment_destroy.go @@ -33,8 +33,5 @@ func DecodeMomentDestroyedEvent(b []byte) (MomentDestroyedEvent, error) { return nil, err } event := momentDestroyedEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/moment_destroy_test.go b/lib/go/events/moment_destroy_test.go index 43a68058..b28afb1b 100644 --- a/lib/go/events/moment_destroy_test.go +++ b/lib/go/events/moment_destroy_test.go @@ -12,27 +12,27 @@ import ( func TestCadenceEvents_MomentDestroyed(t *testing.T) { id := uint64(1234) - - momentDestroyedEventType := cadence.EventType{ - Location: utils.TestLocation, - QualifiedIdentifier: "TopShot.MomentDestroyed", - Fields: []cadence.Field{ + momentDestroyedEventType := cadence.NewEventType( + utils.TestLocation, + "TopShot.MomentDestroyed", + []cadence.Field{ { - Identifier: "setID", - Type: cadence.UInt64Type{}, + Identifier: "id", + Type: cadence.UInt64Type, }, }, - } + nil, + ) momentDestroyedEvent := cadence.NewEvent([]cadence.Value{ cadence.NewUInt64(id), - }).WithType(&momentDestroyedEventType) + }).WithType(momentDestroyedEventType) payload, err := jsoncdc.Encode(momentDestroyedEvent) require.NoError(t, err, "failed to encode moment destroyed cadence event") - decodeSetLockPurchasedEvent, err := DecodeMomentDestroyedEvent(payload) + decodeSetLockDestroyedEvent, err := DecodeMomentDestroyedEvent(payload) require.NoError(t, err, "failed to decode moment destroyed cadence event") - assert.Equal(t, id, decodeSetLockPurchasedEvent.Id()) + assert.Equal(t, id, decodeSetLockDestroyedEvent.Id()) } From 5455f72eb30288fe8e55cb5ccd2fd9987acf05d4 Mon Sep 17 00:00:00 2001 From: Jude Zhu Date: Mon, 12 Aug 2024 03:56:10 -0700 Subject: [PATCH 2/5] fix more tests --- lib/go/events/deposit.go | 12 ------------ lib/go/events/moment_destroy.go | 9 --------- lib/go/events/moment_locked.go | 12 ------------ lib/go/events/moment_minted.go | 12 ------------ lib/go/events/moment_unlocked.go | 13 ------------- lib/go/events/play.go | 14 +------------- lib/go/events/revealed.go | 12 ------------ lib/go/events/set.go | 12 ------------ lib/go/events/set_locked.go | 12 ------------ lib/go/events/set_play.go | 12 ------------ lib/go/events/set_play_retired.go | 12 ------------ lib/go/events/set_play_test.go | 4 ++-- lib/go/events/set_test.go | 2 +- lib/go/events/subedition.go | 14 +------------- lib/go/events/subedition_added_to_moment.go | 12 ------------ lib/go/events/subedition_test.go | 4 ++-- lib/go/events/withdraw.go | 12 ------------ 17 files changed, 7 insertions(+), 173 deletions(-) diff --git a/lib/go/events/deposit.go b/lib/go/events/deposit.go index 5004c3a7..77ad43e3 100644 --- a/lib/go/events/deposit.go +++ b/lib/go/events/deposit.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -37,22 +36,11 @@ func (evt depositEvent) Owner() string { return evt.To() } -func (evt depositEvent) validate() error { - if evt["eventType"].(string) != EventDeposit { - return fmt.Errorf("error validating event: event is not a valid moment destroyed event, expected type %s, got %s", - EventDeposit, evt["eventType"].(string)) - } - return nil -} - func DecodeDepositEvent(b []byte) (DepositEvent, error) { eventMap, err := decoder.DecodeToEventMap(b) if err != nil { return nil, err } event := depositEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/moment_destroy.go b/lib/go/events/moment_destroy.go index 90641c88..0583effc 100644 --- a/lib/go/events/moment_destroy.go +++ b/lib/go/events/moment_destroy.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -19,14 +18,6 @@ func (evt momentDestroyedEvent) Id() uint64 { return evt["id"].(uint64) } -func (evt momentDestroyedEvent) validate() error { - if evt["eventType"].(string) != EventMomentDestroyed { - return fmt.Errorf("error validating event: event is not a valid moment destroyed event, expected type %s, got %s", - EventMomentDestroyed, evt["eventType"].(string)) - } - return nil -} - func DecodeMomentDestroyedEvent(b []byte) (MomentDestroyedEvent, error) { eventMap, err := decoder.DecodeToEventMap(b) if err != nil { diff --git a/lib/go/events/moment_locked.go b/lib/go/events/moment_locked.go index 0538dfa7..bf15ce71 100644 --- a/lib/go/events/moment_locked.go +++ b/lib/go/events/moment_locked.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -29,14 +28,6 @@ func (evt momentLockedEvent) ExpiryTimestamp() float64 { return evt["expiryTimestamp"].(float64) } -func (evt momentLockedEvent) validate() error { - if evt["eventType"].(string) != MomentLocked { - return fmt.Errorf("error validating event: event is not a valid moment locked event, expected type %s, got %s", - MomentLocked, evt["eventType"].(string)) - } - return nil -} - var _ MomentLockedEvent = (*momentLockedEvent)(nil) func DecodeMomentLockedEvent(b []byte) (MomentLockedEvent, error) { @@ -46,9 +37,6 @@ func DecodeMomentLockedEvent(b []byte) (MomentLockedEvent, error) { } event := momentLockedEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/moment_minted.go b/lib/go/events/moment_minted.go index 1769abde..615bb074 100644 --- a/lib/go/events/moment_minted.go +++ b/lib/go/events/moment_minted.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -43,14 +42,6 @@ func (evt momentMintedEvent) SubeditionId() uint32 { return 0 } -func (evt momentMintedEvent) validate() error { - 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"].(string)) - } - return nil -} - var _ MomentMintedEvent = (*momentMintedEvent)(nil) func DecodeMomentMintedEvent(b []byte) (MomentMintedEvent, error) { @@ -59,8 +50,5 @@ func DecodeMomentMintedEvent(b []byte) (MomentMintedEvent, error) { return nil, err } event := momentMintedEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/moment_unlocked.go b/lib/go/events/moment_unlocked.go index ef0bb5d6..6f18e328 100644 --- a/lib/go/events/moment_unlocked.go +++ b/lib/go/events/moment_unlocked.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -19,14 +18,6 @@ func (evt momentUnlockedEvent) FlowID() uint64 { return evt["flowID"].(uint64) } -func (evt momentUnlockedEvent) validate() error { - if evt["eventType"].(string) != MomentUnlocked { - return fmt.Errorf("error validating event: event is not a valid moment unlocked event, expected type %s, got %s", - MomentUnlocked, evt["eventType"].(string)) - } - return nil -} - var _ MomentUnlockedEvent = (*momentUnlockedEvent)(nil) func DecodeMomentUnlockedEvent(b []byte) (MomentUnlockedEvent, error) { @@ -36,9 +27,5 @@ func DecodeMomentUnlockedEvent(b []byte) (MomentUnlockedEvent, error) { } event := momentUnlockedEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } - return event, nil } diff --git a/lib/go/events/play.go b/lib/go/events/play.go index 3ef33dbe..c332d99c 100644 --- a/lib/go/events/play.go +++ b/lib/go/events/play.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -21,15 +20,7 @@ func (evt playCreatedEvent) Id() uint32 { } func (evt playCreatedEvent) MetaData() map[interface{}]interface{} { - return evt["metaData"].(map[interface{}]interface{}) -} - -func (evt playCreatedEvent) validate() error { - if evt["eventType"].(string) != EventPlayCreated { - return fmt.Errorf("error validating event: event is not a valid play created event, expected type %s, got %s", - EventPlayCreated, evt["eventType"].(string)) - } - return nil + return evt["metadata"].(map[interface{}]interface{}) } func DecodePlayCreatedEvent(b []byte) (PlayCreatedEvent, error) { @@ -38,8 +29,5 @@ func DecodePlayCreatedEvent(b []byte) (PlayCreatedEvent, error) { return nil, err } event := playCreatedEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/revealed.go b/lib/go/events/revealed.go index 4ead0db0..2d26abf8 100644 --- a/lib/go/events/revealed.go +++ b/lib/go/events/revealed.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" "strings" ) @@ -33,14 +32,6 @@ func (evt revealedEvent) NFTs() string { return evt["nfts"].(string) } -func (evt revealedEvent) validate() error { - if evt["eventType"].(string) != EventRevealed { - return fmt.Errorf("error validating event: event is not a valid revealed event, expected type %s, got %s", - EventRevealed, evt["eventType"].(string)) - } - return nil -} - func parseNFTs(nft string) []string { return strings.Split(nft, ",") } @@ -51,8 +42,5 @@ func DecodeRevealedEvent(b []byte) (RevealedEvent, error) { return nil, err } event := revealedEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/set.go b/lib/go/events/set.go index 34c99bb7..fe7dd80b 100644 --- a/lib/go/events/set.go +++ b/lib/go/events/set.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -24,14 +23,6 @@ func (evt setCreatedEvent) Series() uint32 { return evt["series"].(uint32) } -func (evt setCreatedEvent) validate() error { - if evt["eventType"].(string) != EventSetCreated { - return fmt.Errorf("error validating event: event is not a valid set created event, expected type %s, got %s", - EventSetCreated, evt["eventType"].(string)) - } - return nil -} - var _ SetCreatedEvent = (*setCreatedEvent)(nil) func DecodeSetCreatedEvent(b []byte) (SetCreatedEvent, error) { @@ -40,8 +31,5 @@ func DecodeSetCreatedEvent(b []byte) (SetCreatedEvent, error) { return nil, err } event := setCreatedEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/set_locked.go b/lib/go/events/set_locked.go index 4541101b..ca5e31ac 100644 --- a/lib/go/events/set_locked.go +++ b/lib/go/events/set_locked.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -21,22 +20,11 @@ func (evt setLockedEvent) SetID() uint32 { return evt["setID"].(uint32) } -func (evt setLockedEvent) validate() error { - if evt["eventType"].(string) != EventSetLocked { - return fmt.Errorf("error validating event: event is not a valid set locked event, expected type %s, got %s", - EventSetLocked, evt["eventType"].(string)) - } - return nil -} - func DecodeSetLockedEvent(b []byte) (SetLockedEvent, error) { eventMap, err := decoder.DecodeToEventMap(b) if err != nil { return nil, err } event := setLockedEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/set_play.go b/lib/go/events/set_play.go index 5dea6ba2..58cbefa6 100644 --- a/lib/go/events/set_play.go +++ b/lib/go/events/set_play.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -24,14 +23,6 @@ func (evt playAddedToSetEvent) PlayID() uint32 { return evt["playID"].(uint32) } -func (evt playAddedToSetEvent) validate() error { - if evt["eventType"].(string) != EventPlayAddedToSet { - return fmt.Errorf("error validating event: event is not a valid play added to set event, expected type %s, got %s", - EventPlayAddedToSet, evt["eventType"].(string)) - } - return nil -} - var _ PlayAddedToSetEvent = (*playAddedToSetEvent)(nil) func DecodePlayAddedToSetEvent(b []byte) (PlayAddedToSetEvent, error) { @@ -40,8 +31,5 @@ func DecodePlayAddedToSetEvent(b []byte) (PlayAddedToSetEvent, error) { return nil, err } event := playAddedToSetEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/set_play_retired.go b/lib/go/events/set_play_retired.go index a9b55aa2..097f6eea 100644 --- a/lib/go/events/set_play_retired.go +++ b/lib/go/events/set_play_retired.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -29,14 +28,6 @@ func (evt setPlayRetiredEvent) NumMoments() uint32 { return evt["numMoments"].(uint32) } -func (evt setPlayRetiredEvent) validate() error { - if evt["eventType"].(string) != EventPlayRetiredFromSet { - return fmt.Errorf("error validating event: event is not a valid play retired from set event, expected type %s, got %s", - EventPlayRetiredFromSet, evt["eventType"].(string)) - } - return nil -} - var _ SetPlayRetiredEvent = (*setPlayRetiredEvent)(nil) func DecodeSetPlayRetiredEvent(b []byte) (SetPlayRetiredEvent, error) { @@ -45,8 +36,5 @@ func DecodeSetPlayRetiredEvent(b []byte) (SetPlayRetiredEvent, error) { return nil, err } event := setPlayRetiredEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/set_play_test.go b/lib/go/events/set_play_test.go index cb3f3833..2a0edbbb 100644 --- a/lib/go/events/set_play_test.go +++ b/lib/go/events/set_play_test.go @@ -19,11 +19,11 @@ func TestCadenceEvents_PlayAddedToSet(t *testing.T) { "TopShot.PlayAddedToSet", []cadence.Field{ { - Identifier: "setId", + Identifier: "setID", Type: cadence.UInt32Type, }, { - Identifier: "playId", + Identifier: "playID", Type: cadence.UInt32Type, }, }, diff --git a/lib/go/events/set_test.go b/lib/go/events/set_test.go index ac08f76f..27e7ebdf 100644 --- a/lib/go/events/set_test.go +++ b/lib/go/events/set_test.go @@ -19,7 +19,7 @@ func TestCadenceEvents_SetCreated(t *testing.T) { "TopShot.SetCreated", []cadence.Field{ { - Identifier: "setId", + Identifier: "setID", Type: cadence.UInt32Type, }, { diff --git a/lib/go/events/subedition.go b/lib/go/events/subedition.go index 2f577d85..ff64677f 100644 --- a/lib/go/events/subedition.go +++ b/lib/go/events/subedition.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -18,7 +17,7 @@ type SubeditionCreatedEvent interface { type subeditionCreatedEvent map[string]any func (evt subeditionCreatedEvent) SubeditionId() uint32 { - return evt["subeditionId"].(uint32) + return evt["subeditionID"].(uint32) } func (evt subeditionCreatedEvent) Name() string { @@ -34,22 +33,11 @@ func (evt subeditionCreatedEvent) MetaData() map[string]interface{} { return result } -func (evt subeditionCreatedEvent) validate() error { - if evt["eventType"].(string) != EventSubeditionCreated { - return fmt.Errorf("error validating event: event is not a valid subedition created event, expected type %s, got %s", - EventSubeditionCreated, evt["eventType"].(string)) - } - return nil -} - func DecodeSubeditionCreatedEvent(b []byte) (SubeditionCreatedEvent, error) { eventMap, err := decoder.DecodeToEventMap(b) if err != nil { return nil, err } event := subeditionCreatedEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/subedition_added_to_moment.go b/lib/go/events/subedition_added_to_moment.go index 0421d079..a46723d2 100644 --- a/lib/go/events/subedition_added_to_moment.go +++ b/lib/go/events/subedition_added_to_moment.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -24,22 +23,11 @@ func (evt subeditionAddedToMomentEvent) SubeditionID() uint32 { return evt["subeditionID"].(uint32) } -func (evt subeditionAddedToMomentEvent) validate() error { - if evt["eventType"].(string) != EventSubeditionAddedToMoment { - return fmt.Errorf("error validating event: event is not a valid subedition added to moment event, expected type %s, got %s", - EventSubeditionAddedToMoment, evt["eventType"].(string)) - } - return nil -} - func DecodeSubeditionAddedToMomentEvent(b []byte) (SubeditionAddedToMomentEvent, error) { eventMap, err := decoder.DecodeToEventMap(b) if err != nil { return nil, err } event := subeditionAddedToMomentEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } diff --git a/lib/go/events/subedition_test.go b/lib/go/events/subedition_test.go index bae9d33f..5be55470 100644 --- a/lib/go/events/subedition_test.go +++ b/lib/go/events/subedition_test.go @@ -26,7 +26,7 @@ func TestCadenceEvents_SubeditionCreated(t *testing.T) { "TopShot.SubeditionCreated", []cadence.Field{ { - Identifier: "subeditionId", + Identifier: "subeditionID", Type: cadence.UInt32Type, }, { @@ -58,7 +58,7 @@ func TestCadenceEvents_SubeditionCreated(t *testing.T) { assert.Equal(t, id, decodedSubeditionCreatedEventType.SubeditionId()) assert.Equal(t, name, decodedSubeditionCreatedEventType.Name()) - assert.Equal(t, map[interface{}]interface{}{ + assert.Equal(t, map[string]interface{}{ setKey: setValue, playKey: playValue, }, decodedSubeditionCreatedEventType.MetaData()) diff --git a/lib/go/events/withdraw.go b/lib/go/events/withdraw.go index e55460d3..c39e78ea 100644 --- a/lib/go/events/withdraw.go +++ b/lib/go/events/withdraw.go @@ -1,7 +1,6 @@ package events import ( - "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -35,22 +34,11 @@ func (evt withdrawEvent) Owner() string { return evt.From() } -func (evt withdrawEvent) validate() error { - if evt["eventType"].(string) != EventWithdraw { - return fmt.Errorf("error validating event: event is not a valid withdraw event, expected type %s, got %s", - EventWithdraw, evt["eventType"].(string)) - } - return nil -} - func DecodeWithdrawEvent(b []byte) (WithdrawEvent, error) { eventMap, err := decoder.DecodeToEventMap(b) if err != nil { return nil, err } event := withdrawEvent(eventMap) - if err := event.validate(); err != nil { - return nil, fmt.Errorf("error decoding event: %w", err) - } return event, nil } From 08687ddd5bae29df5478d893c855aef302a2f253 Mon Sep 17 00:00:00 2001 From: Jude Zhu Date: Mon, 12 Aug 2024 03:56:47 -0700 Subject: [PATCH 3/5] remove unused code --- lib/go/events/decoder/decoder.go | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/go/events/decoder/decoder.go b/lib/go/events/decoder/decoder.go index 8bbd40b8..de6fa249 100644 --- a/lib/go/events/decoder/decoder.go +++ b/lib/go/events/decoder/decoder.go @@ -24,7 +24,6 @@ func DecodeCadenceValue(payload []byte) (cadence.Value, error) { ) if err != nil { // json decode failed, try ccf decode - fmt.Printf("json decode failed, try ccf decode, reason: %s\n", err.Error()) ccfValue, ccfErr := ccf.Decode(nil, payload) if ccfErr != nil { return cadence.Event{}, pkgerrors.Wrapf( From 209b9f6de3056858b6a5a91bd57275f49d450b00 Mon Sep 17 00:00:00 2001 From: Jude Zhu Date: Mon, 12 Aug 2024 12:05:50 -0700 Subject: [PATCH 4/5] added events validateion --- lib/go/events/moment_destroy.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/go/events/moment_destroy.go b/lib/go/events/moment_destroy.go index 0583effc..4d8d7a7c 100644 --- a/lib/go/events/moment_destroy.go +++ b/lib/go/events/moment_destroy.go @@ -1,6 +1,7 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -19,10 +20,14 @@ func (evt momentDestroyedEvent) Id() uint64 { } func DecodeMomentDestroyedEvent(b []byte) (MomentDestroyedEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventMomentDestroyed { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := momentDestroyedEvent(eventMap) return event, nil } From 9920f4921ce1fa095831ae95ac10f97a317d5c32 Mon Sep 17 00:00:00 2001 From: Jude Zhu Date: Mon, 12 Aug 2024 12:20:41 -0700 Subject: [PATCH 5/5] updated all event type check --- lib/go/events/deposit.go | 9 +++++++-- lib/go/events/moment_destroy.go | 2 +- lib/go/events/moment_locked.go | 7 ++++++- lib/go/events/moment_minted.go | 9 +++++++-- lib/go/events/moment_unlocked.go | 9 +++++++-- lib/go/events/play.go | 9 +++++++-- lib/go/events/revealed.go | 7 ++++++- lib/go/events/set.go | 9 +++++++-- lib/go/events/set_locked.go | 9 +++++++-- lib/go/events/set_play.go | 9 +++++++-- lib/go/events/set_play_retired.go | 9 +++++++-- lib/go/events/subedition.go | 9 +++++++-- lib/go/events/subedition_added_to_moment.go | 7 ++++++- lib/go/events/withdraw.go | 9 +++++++-- 14 files changed, 89 insertions(+), 24 deletions(-) diff --git a/lib/go/events/deposit.go b/lib/go/events/deposit.go index 77ad43e3..234f9615 100644 --- a/lib/go/events/deposit.go +++ b/lib/go/events/deposit.go @@ -1,10 +1,11 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( // This variable specifies that there is a Deposit Event on a TopShot Contract located at address 0x04 EventDeposit = "TopShot.Deposit" ) @@ -37,10 +38,14 @@ func (evt depositEvent) Owner() string { } func DecodeDepositEvent(b []byte) (DepositEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventDeposit { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := depositEvent(eventMap) return event, nil } diff --git a/lib/go/events/moment_destroy.go b/lib/go/events/moment_destroy.go index 4d8d7a7c..b3ebe0f4 100644 --- a/lib/go/events/moment_destroy.go +++ b/lib/go/events/moment_destroy.go @@ -5,7 +5,7 @@ import ( "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( EventMomentDestroyed = "TopShot.MomentDestroyed" ) diff --git a/lib/go/events/moment_locked.go b/lib/go/events/moment_locked.go index bf15ce71..a2a65ef1 100644 --- a/lib/go/events/moment_locked.go +++ b/lib/go/events/moment_locked.go @@ -1,6 +1,7 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -31,10 +32,14 @@ func (evt momentLockedEvent) ExpiryTimestamp() float64 { var _ MomentLockedEvent = (*momentLockedEvent)(nil) func DecodeMomentLockedEvent(b []byte) (MomentLockedEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != MomentLocked { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := momentLockedEvent(eventMap) diff --git a/lib/go/events/moment_minted.go b/lib/go/events/moment_minted.go index 615bb074..f52e84ca 100644 --- a/lib/go/events/moment_minted.go +++ b/lib/go/events/moment_minted.go @@ -1,10 +1,11 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( // This variable specifies that there is a MomentMinted Event on a TopShot Contract located at address 0x04 EventMomentMinted = "TopShot.MomentMinted" ) @@ -45,10 +46,14 @@ func (evt momentMintedEvent) SubeditionId() uint32 { var _ MomentMintedEvent = (*momentMintedEvent)(nil) func DecodeMomentMintedEvent(b []byte) (MomentMintedEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventMomentMinted { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := momentMintedEvent(eventMap) return event, nil } diff --git a/lib/go/events/moment_unlocked.go b/lib/go/events/moment_unlocked.go index 6f18e328..7e61ec91 100644 --- a/lib/go/events/moment_unlocked.go +++ b/lib/go/events/moment_unlocked.go @@ -1,10 +1,11 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( MomentUnlocked = "TopShotLocking.MomentUnlocked" ) @@ -21,10 +22,14 @@ func (evt momentUnlockedEvent) FlowID() uint64 { var _ MomentUnlockedEvent = (*momentUnlockedEvent)(nil) func DecodeMomentUnlockedEvent(b []byte) (MomentUnlockedEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != MomentUnlocked { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := momentUnlockedEvent(eventMap) return event, nil diff --git a/lib/go/events/play.go b/lib/go/events/play.go index c332d99c..8f779d61 100644 --- a/lib/go/events/play.go +++ b/lib/go/events/play.go @@ -1,10 +1,11 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( EventPlayCreated = "TopShot.PlayCreated" ) @@ -24,10 +25,14 @@ func (evt playCreatedEvent) MetaData() map[interface{}]interface{} { } func DecodePlayCreatedEvent(b []byte) (PlayCreatedEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventPlayCreated { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := playCreatedEvent(eventMap) return event, nil } diff --git a/lib/go/events/revealed.go b/lib/go/events/revealed.go index 2d26abf8..d826ebba 100644 --- a/lib/go/events/revealed.go +++ b/lib/go/events/revealed.go @@ -1,6 +1,7 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" "strings" ) @@ -37,10 +38,14 @@ func parseNFTs(nft string) []string { } func DecodeRevealedEvent(b []byte) (RevealedEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventRevealed { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := revealedEvent(eventMap) return event, nil } diff --git a/lib/go/events/set.go b/lib/go/events/set.go index fe7dd80b..7b083e30 100644 --- a/lib/go/events/set.go +++ b/lib/go/events/set.go @@ -1,10 +1,11 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( EventSetCreated = "TopShot.SetCreated" ) @@ -26,10 +27,14 @@ func (evt setCreatedEvent) Series() uint32 { var _ SetCreatedEvent = (*setCreatedEvent)(nil) func DecodeSetCreatedEvent(b []byte) (SetCreatedEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventSetCreated { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := setCreatedEvent(eventMap) return event, nil } diff --git a/lib/go/events/set_locked.go b/lib/go/events/set_locked.go index ca5e31ac..29c263cc 100644 --- a/lib/go/events/set_locked.go +++ b/lib/go/events/set_locked.go @@ -1,10 +1,11 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( EventSetLocked = "TopShot.SetLocked" ) @@ -21,10 +22,14 @@ func (evt setLockedEvent) SetID() uint32 { } func DecodeSetLockedEvent(b []byte) (SetLockedEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventSetLocked { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := setLockedEvent(eventMap) return event, nil } diff --git a/lib/go/events/set_play.go b/lib/go/events/set_play.go index 58cbefa6..26ccd90e 100644 --- a/lib/go/events/set_play.go +++ b/lib/go/events/set_play.go @@ -1,10 +1,11 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( EventPlayAddedToSet = "TopShot.PlayAddedToSet" ) @@ -26,10 +27,14 @@ func (evt playAddedToSetEvent) PlayID() uint32 { var _ PlayAddedToSetEvent = (*playAddedToSetEvent)(nil) func DecodePlayAddedToSetEvent(b []byte) (PlayAddedToSetEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventPlayAddedToSet { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := playAddedToSetEvent(eventMap) return event, nil } diff --git a/lib/go/events/set_play_retired.go b/lib/go/events/set_play_retired.go index 097f6eea..6061615e 100644 --- a/lib/go/events/set_play_retired.go +++ b/lib/go/events/set_play_retired.go @@ -1,10 +1,11 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( EventPlayRetiredFromSet = "TopShot.PlayRetiredFromSet" ) @@ -31,10 +32,14 @@ func (evt setPlayRetiredEvent) NumMoments() uint32 { var _ SetPlayRetiredEvent = (*setPlayRetiredEvent)(nil) func DecodeSetPlayRetiredEvent(b []byte) (SetPlayRetiredEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventPlayRetiredFromSet { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := setPlayRetiredEvent(eventMap) return event, nil } diff --git a/lib/go/events/subedition.go b/lib/go/events/subedition.go index ff64677f..93d97699 100644 --- a/lib/go/events/subedition.go +++ b/lib/go/events/subedition.go @@ -1,10 +1,11 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( EventSubeditionCreated = "TopShot.SubeditionCreated" ) @@ -34,10 +35,14 @@ func (evt subeditionCreatedEvent) MetaData() map[string]interface{} { } func DecodeSubeditionCreatedEvent(b []byte) (SubeditionCreatedEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventSubeditionCreated { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := subeditionCreatedEvent(eventMap) return event, nil } diff --git a/lib/go/events/subedition_added_to_moment.go b/lib/go/events/subedition_added_to_moment.go index a46723d2..4037ca3a 100644 --- a/lib/go/events/subedition_added_to_moment.go +++ b/lib/go/events/subedition_added_to_moment.go @@ -1,6 +1,7 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) @@ -24,10 +25,14 @@ func (evt subeditionAddedToMomentEvent) SubeditionID() uint32 { } func DecodeSubeditionAddedToMomentEvent(b []byte) (SubeditionAddedToMomentEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventSubeditionAddedToMoment { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := subeditionAddedToMomentEvent(eventMap) return event, nil } diff --git a/lib/go/events/withdraw.go b/lib/go/events/withdraw.go index c39e78ea..8232affd 100644 --- a/lib/go/events/withdraw.go +++ b/lib/go/events/withdraw.go @@ -1,10 +1,11 @@ package events import ( + "fmt" "github.com/dapperlabs/nba-smart-contracts/lib/go/events/decoder" ) -var ( +const ( EventWithdraw = "TopShot.Withdraw" ) @@ -35,10 +36,14 @@ func (evt withdrawEvent) Owner() string { } func DecodeWithdrawEvent(b []byte) (WithdrawEvent, error) { - eventMap, err := decoder.DecodeToEventMap(b) + cadenceValue, err := decoder.GetCadenceEvent(b) if err != nil { return nil, err } + if cadenceValue.EventType.QualifiedIdentifier != EventWithdraw { + return nil, fmt.Errorf("unexpected event type: %s", cadenceValue.EventType.QualifiedIdentifier) + } + eventMap, err := decoder.ConvertEvent(cadenceValue) event := withdrawEvent(eventMap) return event, nil }