From 55645478594858a6830e4ab095034068ef9753e9 Mon Sep 17 00:00:00 2001 From: Quinn Hou Date: Mon, 18 Oct 2021 11:58:27 -0700 Subject: [PATCH] Add revealed event (#129) --- lib/go/events/revealed.go | 59 +++++++++++++++++++++++++++++++++ lib/go/events/revealed_test.go | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 lib/go/events/revealed.go create mode 100644 lib/go/events/revealed_test.go diff --git a/lib/go/events/revealed.go b/lib/go/events/revealed.go new file mode 100644 index 00000000..f27dacf7 --- /dev/null +++ b/lib/go/events/revealed.go @@ -0,0 +1,59 @@ +package events + +import ( + "fmt" + "github.com/onflow/cadence" + jsoncdc "github.com/onflow/cadence/encoding/json" + "strings" +) + +const ( + // EventRevealed specifies that there is a Revealed Event on a PackNFT Contract located at the address + EventRevealed = "PackNFT.Revealed" +) + +type RevealedEvent interface { + Id() uint64 + Salt() string + NFTs() string +} + +type revealedEvent cadence.Event + +var _ RevealedEvent = (*revealedEvent)(nil) + +func (evt revealedEvent) Id() uint64 { + return uint64(evt.Fields[0].(cadence.UInt64)) +} + +func (evt revealedEvent) Salt() string { + return string(evt.Fields[1].(cadence.String)) +} + +func (evt revealedEvent) NFTs() string { + return string(evt.Fields[2].(cadence.String)) +} + +func (evt revealedEvent) validate() error { + if evt.EventType.QualifiedIdentifier != EventRevealed { + return fmt.Errorf("error validating event: event is not a valid revealed event, expected type %s, got %s", + EventRevealed, evt.EventType.QualifiedIdentifier) + } + return nil +} + +func parseNFTs(nft string) []string { + return strings.Split(nft, ",") +} + +func DecodeRevealedEvent(b []byte) (RevealedEvent, error) { + value, err := jsoncdc.Decode(b) + if err != nil { + return nil, err + } + event := revealedEvent(value.(cadence.Event)) + if err := event.validate(); err != nil { + return nil, fmt.Errorf("error decoding event: %w", err) + } + return event, nil +} \ No newline at end of file diff --git a/lib/go/events/revealed_test.go b/lib/go/events/revealed_test.go new file mode 100644 index 00000000..9e87dba8 --- /dev/null +++ b/lib/go/events/revealed_test.go @@ -0,0 +1,60 @@ +package events + +import ( + "fmt" + "github.com/stretchr/testify/require" + "testing" + + "github.com/onflow/cadence" + jsoncdc "github.com/onflow/cadence/encoding/json" + "github.com/onflow/cadence/runtime/tests/utils" + "github.com/stretchr/testify/assert" +) + +func TestCadenceEvents_Reveal(t *testing.T) { + var ( + packID = uint64(10) + salt = "salt" + + momentID1 = uint64(1) + momentID2 = uint64(2) + momentID3 = uint64(3) + momentIDs = fmt.Sprintf(`%d,%d,%d`, momentID1, momentID2, momentID3) + ) + + revealedEventType := cadence.EventType{ + Location: utils.TestLocation, + QualifiedIdentifier: "PackNFT.Revealed", + Fields: []cadence.Field{ + { + Identifier: "id", + Type: cadence.UInt32Type{}, + }, + { + Identifier: "salt", + Type: cadence.StringType{}, + }, + { + Identifier: "nfts", + Type: cadence.StringType{}, + }, + }, + } + + revealedEvent := cadence.NewEvent([]cadence.Value{ + cadence.NewUInt64(packID), + cadence.NewString(salt), + cadence.NewString(momentIDs), + }).WithType(&revealedEventType) + + revealedPayload, err := jsoncdc.Encode(revealedEvent) + require.NoError(t, err, "failed to encode revealed cadence event") + + decodedRevealedEventType, err := DecodeRevealedEvent(revealedPayload) + require.NoError(t, err, "failed to decode revealed cadence event") + + assert.Equal(t, packID, decodedRevealedEventType.Id()) + assert.Equal(t, salt, decodedRevealedEventType.Salt()) + assert.Equal(t, momentIDs, decodedRevealedEventType.NFTs()) + +}