-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Quinn Hou
authored
Oct 18, 2021
1 parent
26101e5
commit 5564547
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
|
||
} |