Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remix #163

Merged
merged 25 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
301 changes: 297 additions & 4 deletions contracts/TopShot.cdc

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions lib/go/events/moment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type MomentMintedEvent interface {
PlayId() uint32
SetId() uint32
SerialNumber() uint32
SubeditionId() uint32
}

type momentMintedEvent cadence.Event
Expand All @@ -37,6 +38,13 @@ func (evt momentMintedEvent) SerialNumber() uint32 {
return uint32(evt.Fields[3].(cadence.UInt32))
}

func (evt momentMintedEvent) SubeditionId() uint32 {
if len(evt.Fields) < 5 {
return 0
}
return uint32(evt.Fields[4].(cadence.UInt32))
}

func (evt momentMintedEvent) validate() error {
if evt.EventType.QualifiedIdentifier != EventMomentMinted {
return fmt.Errorf("error validating event: event is not a valid moment minted event, expected type %s, got %s",
Expand Down
8 changes: 8 additions & 0 deletions lib/go/events/moment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestCadenceEvents_MomentMinted(t *testing.T) {
playID := uint32(1234)
setID := uint32(1234)
serialNumber := uint32(1234)
subeditionID := uint32(1234)

momentMintedEventType := cadence.EventType{
Location: utils.TestLocation,
Expand All @@ -36,6 +37,10 @@ func TestCadenceEvents_MomentMinted(t *testing.T) {
Identifier: "serialNumber",
Type: cadence.UInt32Type{},
},
{
Identifier: "subeditionId",
Type: cadence.UInt32Type{},
},
},
Initializer: []cadence.Parameter{},
}
Expand All @@ -45,6 +50,7 @@ func TestCadenceEvents_MomentMinted(t *testing.T) {
cadence.NewUInt32(playID),
cadence.NewUInt32(setID),
cadence.NewUInt32(serialNumber),
cadence.NewUInt32(subeditionID),
}).WithType(&momentMintedEventType)

payload, err := jsoncdc.Encode(momentMintedEvent)
Expand All @@ -57,4 +63,6 @@ func TestCadenceEvents_MomentMinted(t *testing.T) {
assert.Equal(t, playID, decodedMomentMintedEventType.PlayId())
assert.Equal(t, setID, decodedMomentMintedEventType.SetId())
assert.Equal(t, serialNumber, decodedMomentMintedEventType.SerialNumber())
assert.Equal(t, subeditionID, decodedMomentMintedEventType.SubeditionId())

}
52 changes: 52 additions & 0 deletions lib/go/events/subedition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package events

import (
"fmt"

"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
)

var (
EventSubeditionCreated = "TopShot.SubeditionCreated"
)

type SubeditionCreatedEvent interface {
SubeditionId() uint32
Name() string
MetaData() map[interface{}]interface{}
}

type subeditionCreatedEvent cadence.Event

func (evt subeditionCreatedEvent) SubeditionId() uint32 {
return evt.Fields[0].(cadence.UInt32).ToGoValue().(uint32)
}

func (evt subeditionCreatedEvent) Name() string {
return evt.Fields[1].(cadence.String).ToGoValue().(string)
}

func (evt subeditionCreatedEvent) MetaData() map[interface{}]interface{} {
return evt.Fields[2].(cadence.Dictionary).ToGoValue().(map[interface{}]interface{})
}

func (evt subeditionCreatedEvent) validate() error {
if evt.EventType.QualifiedIdentifier != EventSubeditionCreated {
return fmt.Errorf("error validating event: event is not a valid subedition created event, expected type %s, got %s",
EventSubeditionCreated, evt.EventType.QualifiedIdentifier)
}
return nil
}

func DecodeSubeditionCreatedEvent(b []byte) (SubeditionCreatedEvent, error) {
value, err := jsoncdc.Decode(nil, b)
if err != nil {
return nil, err
}
event := subeditionCreatedEvent(value.(cadence.Event))
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
}
return event, nil
}
47 changes: 47 additions & 0 deletions lib/go/events/subedition_added_to_moment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package events

import (
"fmt"

"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
)

var (
EventSubeditionAddedToMoment = "TopShot.SubeditionAddedToMoment"
)

type SubeditionAddedToMomentEvent interface {
MomentID() uint64
SubeditionID() uint32
}

type subeditionAddedToMomentEvent cadence.Event

func (evt subeditionAddedToMomentEvent) MomentID() uint64 {
return evt.Fields[0].(cadence.UInt64).ToGoValue().(uint64)
}

func (evt subeditionAddedToMomentEvent) SubeditionID() uint32 {
return evt.Fields[1].(cadence.UInt32).ToGoValue().(uint32)
}

func (evt subeditionAddedToMomentEvent) validate() error {
if evt.EventType.QualifiedIdentifier != 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.QualifiedIdentifier)
}
return nil
}

func DecodeSubeditionAddedToMomentEvent(b []byte) (SubeditionAddedToMomentEvent, error) {
value, err := jsoncdc.Decode(nil, b)
if err != nil {
return nil, err
}
event := subeditionAddedToMomentEvent(value.(cadence.Event))
if err := event.validate(); err != nil {
return nil, fmt.Errorf("error decoding event: %w", err)
}
return event, nil
}
49 changes: 49 additions & 0 deletions lib/go/events/subedition_added_to_moment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package events

import (
"testing"

"github.com/stretchr/testify/require"

"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_SubeditionAddedToMoment(t *testing.T) {
var (
subeditionID = uint32(1234)
momentID = uint64(1234)
)

playCreatedEventType := cadence.EventType{
Location: utils.TestLocation,
QualifiedIdentifier: "TopShot.SubeditionAddedToMoment",
Fields: []cadence.Field{
{
Identifier: "momentID",
Type: cadence.UInt64Type{},
},
{
Identifier: "subeditionID",
Type: cadence.UInt32Type{},
},
},
Initializer: []cadence.Parameter{},
}

subeditionAddedToMomentEvent := cadence.NewEvent([]cadence.Value{
cadence.NewUInt64(momentID),
cadence.NewUInt32(subeditionID),
}).WithType(&playCreatedEventType)

payload, err := jsoncdc.Encode(subeditionAddedToMomentEvent)
require.NoError(t, err, "failed to encode subedition added to moment cadence event")

decodedPlayCreatedEventType, err := DecodeSubeditionAddedToMomentEvent(payload)
require.NoError(t, err, "failed to decode subedition added to moment cadence event")

assert.Equal(t, momentID, decodedPlayCreatedEventType.MomentID())
assert.Equal(t, subeditionID, decodedPlayCreatedEventType.SubeditionID())
}
65 changes: 65 additions & 0 deletions lib/go/events/subedition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package events

import (
"testing"

"github.com/stretchr/testify/require"

"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_SubeditionCreated(t *testing.T) {
var (
id = uint32(1234)
name = "Subedition #1"
setKey = "setID"
setValue = "1234"
playKey = "playID"
playValue = "1234"
)

playCreatedEventType := cadence.EventType{
Location: utils.TestLocation,
QualifiedIdentifier: "TopShot.SubeditionCreated",
Fields: []cadence.Field{
{
Identifier: "subeditionId",
Type: cadence.UInt32Type{},
},
{
Identifier: "name",
Type: cadence.StringType{},
},
{
Identifier: "metadata",
Type: cadence.DictionaryType{},
},
},
Initializer: []cadence.Parameter{},
}

subeditionCreatedEvent := cadence.NewEvent([]cadence.Value{
cadence.NewUInt32(id),
NewCadenceString(name),
cadence.NewDictionary([]cadence.KeyValuePair{
{Key: NewCadenceString(setKey), Value: NewCadenceString(setValue)},
{Key: NewCadenceString(playKey), Value: NewCadenceString(playValue)},
}),
}).WithType(&playCreatedEventType)

payload, err := jsoncdc.Encode(subeditionCreatedEvent)
require.NoError(t, err, "failed to encode play created cadence event")

decodedSubeditionCreatedEventType, err := DecodeSubeditionCreatedEvent(payload)
require.NoError(t, err, "failed to decode play created cadence event")

assert.Equal(t, id, decodedSubeditionCreatedEventType.SubeditionId())
assert.Equal(t, name, decodedSubeditionCreatedEventType.Name())
assert.Equal(t, map[interface{}]interface{}{
setKey: setValue,
playKey: playValue,
}, decodedSubeditionCreatedEventType.MetaData())
}
Loading