From 20b6046ee72124a4de563ce7fd52ae26a18f9a27 Mon Sep 17 00:00:00 2001 From: Taylor Petrychyn Date: Wed, 22 Feb 2023 14:34:15 -0800 Subject: [PATCH] add event models for moment locking (#208) Co-authored-by: Taylor Petrychyn --- lib/go/events/moment_locked.go | 55 ++++++++++++++++++++++++++++++++ lib/go/events/moment_unlocked.go | 45 ++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 lib/go/events/moment_locked.go create mode 100644 lib/go/events/moment_unlocked.go diff --git a/lib/go/events/moment_locked.go b/lib/go/events/moment_locked.go new file mode 100644 index 00000000..c0f30660 --- /dev/null +++ b/lib/go/events/moment_locked.go @@ -0,0 +1,55 @@ +package events + +import ( + "fmt" + "github.com/onflow/cadence" + jsoncdc "github.com/onflow/cadence/encoding/json" +) + +var ( + MomentLocked = "TopShotLocking.MomentLocked" +) + +type MomentLockedEvent interface { + FlowID() uint64 + Duration() uint64 + ExpiryTimestamp() uint64 +} + +type momentLockedEvent cadence.Event + +func (evt momentLockedEvent) FlowID() uint64 { + return uint64(evt.Fields[0].(cadence.UInt64)) +} + +func (evt momentLockedEvent) Duration() uint64 { + return uint64(evt.Fields[1].(cadence.UInt64)) +} + +func (evt momentLockedEvent) ExpiryTimestamp() uint64 { + return uint64(evt.Fields[2].(cadence.UInt64)) +} + +func (evt momentLockedEvent) validate() error { + if evt.EventType.QualifiedIdentifier != MomentLocked { + return fmt.Errorf("error validating event: event is not a valid moment locked event, expected type %s, got %s", + MomentLocked, evt.EventType.QualifiedIdentifier) + } + return nil +} + +var _ MomentLockedEvent = (*momentLockedEvent)(nil) + +func DecodeMomentLockedEvent(b []byte) (MomentLockedEvent, error) { + value, err := jsoncdc.Decode(nil, b) + if err != nil { + return nil, err + } + + event := momentLockedEvent(value.(cadence.Event)) + 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 new file mode 100644 index 00000000..4c82a457 --- /dev/null +++ b/lib/go/events/moment_unlocked.go @@ -0,0 +1,45 @@ +package events + +import ( + "fmt" + "github.com/onflow/cadence" + jsoncdc "github.com/onflow/cadence/encoding/json" +) + +var ( + MomentUnlocked = "TopShotLocking.MomentUnlocked" +) + +type MomentUnlockedEvent interface { + FlowID() uint64 +} + +type momentUnlockedEvent cadence.Event + +func (evt momentUnlockedEvent) FlowID() uint64 { + return uint64(evt.Fields[0].(cadence.UInt64)) +} + +func (evt momentUnlockedEvent) validate() error { + if evt.EventType.QualifiedIdentifier != MomentUnlocked { + return fmt.Errorf("error validating event: event is not a valid moment unlocked event, expected type %s, got %s", + MomentUnlocked, evt.EventType.QualifiedIdentifier) + } + return nil +} + +var _ MomentUnlockedEvent = (*momentUnlockedEvent)(nil) + +func DecodeMomentUnlockedEvent(b []byte) (MomentUnlockedEvent, error) { + value, err := jsoncdc.Decode(nil, b) + if err != nil { + return nil, err + } + + event := momentUnlockedEvent(value.(cadence.Event)) + if err := event.validate(); err != nil { + return nil, fmt.Errorf("error decoding event: %w", err) + } + + return event, nil +}