Skip to content

Commit

Permalink
added events sdk and rest of models
Browse files Browse the repository at this point in the history
  • Loading branch information
0xluk committed Oct 2, 2024
1 parent 3ab389b commit 2b4db1d
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 3 deletions.
57 changes: 57 additions & 0 deletions sdk/events/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package events

import (
"context"
"github.com/pkg/errors"
"github.com/qubic/go-qubic/internal/connector"
)

type Client struct {
connector *connector.Connector
}

func NewClient(connector *connector.Connector) *Client {
return &Client{
connector: connector,
}
}

func (c *Client) GetTickTransactionEventsRange(ctx context.Context, passcode [4]uint64, tickNumber, txIndex uint32) (*TransactionEventsRange, error) {
request := struct {
Passcode [4]uint64
TickNumber uint32
TxIndex uint32
}{
Passcode: passcode,
TickNumber: tickNumber,
TxIndex: txIndex,
}

var result TransactionEventsRange
err := c.connector.PerformCoreRequest(ctx, TransactionEventsRangeTypeRequest, request, &result)
if err != nil {
return nil, errors.Wrap(err, "performing core request")
}

return &result, nil
}

func (c *Client) GetEvents(ctx context.Context, passcode [4]uint64, fromEventID, toEventID uint64) (*Events, error) {
request := struct {
Passcode [4]uint64
FromEventID uint64
ToEventID uint64
}{
Passcode: passcode,
FromEventID: fromEventID,
ToEventID: toEventID,
}

result := Events{Count: int64(toEventID - fromEventID)}
err := c.connector.PerformCoreRequest(ctx, EventTypeRequest, request, &result)
if err != nil {
return nil, errors.Wrap(err, "performing core request")
}

return &result, nil
}
150 changes: 147 additions & 3 deletions sdk/events/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,152 @@ import (
"bytes"
"encoding/binary"
"github.com/pkg/errors"
"github.com/qubic/go-qubic/internal/connector"
"io"
)

const (
EventHeaderSize = 26
)

const (
EventTypeQuTransfer = uint8(iota)
EventTypeAssetIssuance
EventTypeAssetOwnershipChange
EventTypeAssetPossessionChange
EventTypeContractErrorMessage
EventTypeContractWarningMessage
EventTypeContractInformationMessage
EventTypeContractDebugMessage
EventTypeBurning
EventTypeDustBurning
EventTypeSpectrumStats
)

const EventTypeCustomMessage = 255

const (
EventTypeRequest = 44
EventTypeResponse = 45
TransactionEventsRangeTypeRequest = 48
TransactionEventsRangeTypeResponse = 49
)

type TransactionEventsRange struct {
FromEventID int64
NumberOfEvents int64
}

func (ter *TransactionEventsRange) UnmarshallFromReader(r io.Reader) error {
var header connector.RequestResponseHeader

err := binary.Read(r, binary.BigEndian, &header)
if err != nil {
return errors.Wrap(err, "reading tick data from reader")
}

if header.Type == connector.EndResponse {
return nil
}

packetSize := header.GetSize()
_ = packetSize

headerSize := binary.Size(header)
_ = headerSize

if header.Type != TransactionEventsRangeTypeResponse {
return errors.Errorf("Invalid header type, expected %d, found %d", TransactionEventsRangeTypeResponse, header.Type)
}

err = binary.Read(r, binary.LittleEndian, ter)
if err != nil {
return errors.Wrap(err, "reading transaction events range from reader")
}

return nil
}

type Event struct {
Header Header
EventType uint8
EventSize uint32
Data []byte
}

type Header struct {
Epoch uint16
Tick uint32
Tmp uint32
EventID uint64
EventDigest [8]byte
}

func (ev *Event) UnmarshalBinary(data []byte) error {
r := bytes.NewReader(data)
err := binary.Read(r, binary.LittleEndian, &ev.Header)
if err != nil {
return errors.Wrap(err, "reading event header")
}

ev.EventType = uint8(ev.Header.Tmp >> 24)
ev.EventSize = (ev.Header.Tmp << 8) >> 8

eventData := make([]byte, ev.EventSize)
err = binary.Read(r, binary.LittleEndian, eventData)
if err != nil {
return errors.Wrap(err, "reading event data")
}

ev.Data = eventData

return nil
}

type Events struct {
Items []Event
Count int64
}

func (evs *Events) UnmarshallFromReader(r io.Reader) error {
items := make([]Event, 0, evs.Count)
for range evs.Count {
var header connector.RequestResponseHeader
err := binary.Read(r, binary.BigEndian, &header)
if err != nil {
return errors.Wrap(err, "reading header")
}

if header.Type == connector.EndResponse {
break
}

if header.Type != EventTypeResponse {
return errors.Errorf("Invalid header type, expected %d, found %d", EventTypeResponse, header.Type)
}

headerSize := binary.Size(header)
event := make([]byte, header.GetSize()-uint32(headerSize))

err = binary.Read(r, binary.LittleEndian, event)
if err != nil {
return errors.Wrap(err, "reading event data")
}

var ev Event
err = ev.UnmarshalBinary(event)
if err != nil {
return errors.Wrap(err, "unmarshalling event")
}

items = append(items, ev)
}

evs.Items = items

return nil
}

type QuTransferEvent struct {
SourceIdentityPubKey [32]byte
DestinationIdentityPubKey [32]byte
Expand All @@ -24,7 +168,7 @@ func (e *QuTransferEvent) UnmarshalBinary(data []byte) error {

type AssetIssuanceEvent struct {
SourceIdentityPubKey [32]byte
AssetName uint64
AssetName [8]byte
NumberOfDecimals uint8
MeasurementUnit [8]byte
NumberOfShares int64
Expand All @@ -44,7 +188,7 @@ type AssetOwnershipChangeEvent struct {
SourceIdentityPubKey [32]byte
DestinationIdentityPubKey [32]byte
IssuerIdentityPubKey [32]byte
AssetName uint64
AssetName [8]byte
NumberOfDecimals uint8
MeasurementUnit [8]byte
NumberOfShares int64
Expand All @@ -64,7 +208,7 @@ type AssetPossessionChangeEvent struct {
SourceIdentityPubKey [32]byte
DestinationIdentityPubKey [32]byte
IssuerIdentityPubKey [32]byte
AssetName uint64
AssetName [8]byte
NumberOfDecimals uint8
MeasurementUnit [8]byte
NumberOfShares int64
Expand Down

0 comments on commit 2b4db1d

Please sign in to comment.