Skip to content

Commit

Permalink
fix: implement custom data types decoder (#8)
Browse files Browse the repository at this point in the history
* fix(data): add custom decoder logic for returned objects

Signed-off-by: Luca Georges Francois <[email protected]>

* refactor(data): update struct names

Signed-off-by: Luca Georges Francois <[email protected]>

---------

Signed-off-by: Luca Georges Francois <[email protected]>
  • Loading branch information
0xpanoramix committed Dec 11, 2023
1 parent c271633 commit ff34019
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 13 deletions.
8 changes: 4 additions & 4 deletions sdk/data/v1/data_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewSDK(client *relay.Client) *SDK {
}
}

func (dataV1 *SDK) GetBidsDelivered(ctx context.Context, params *GetBidsDeliveredRequest) ([]*BidsDelivered, error) {
func (dataV1 *SDK) GetBidsDelivered(ctx context.Context, params *GetBidsDeliveredRequest) ([]*BidDelivered, error) {
if params == nil {
params = new(GetBidsDeliveredRequest)
}
Expand All @@ -35,7 +35,7 @@ func (dataV1 *SDK) GetBidsDelivered(ctx context.Context, params *GetBidsDelivere
Query: params.queryParams,
}

resp := make([]*BidsDelivered, 0)
resp := make([]*BidDelivered, 0)

if err := dataV1.client.Do(ctx, relayReq, &resp); err != nil {
return nil, NewDataSDKError("GetBidsDelivered", err)
Expand All @@ -44,7 +44,7 @@ func (dataV1 *SDK) GetBidsDelivered(ctx context.Context, params *GetBidsDelivere
return resp, nil
}

func (dataV1 *SDK) GetBidsReceived(ctx context.Context, params *GetBidsReceivedRequest) ([]*BidsReceived, error) {
func (dataV1 *SDK) GetBidsReceived(ctx context.Context, params *GetBidsReceivedRequest) ([]*BidReceived, error) {
if params == nil {
return nil, NewDataSDKError("GetBidsReceived", ErrMissingMandatoryParam)
}
Expand All @@ -60,7 +60,7 @@ func (dataV1 *SDK) GetBidsReceived(ctx context.Context, params *GetBidsReceivedR
Query: params.queryParams,
}

resp := make([]*BidsReceived, 0)
resp := make([]*BidReceived, 0)

if err := dataV1.client.Do(ctx, relayReq, &resp); err != nil {
return nil, NewDataSDKError("GetBidsReceived", err)
Expand Down
100 changes: 91 additions & 9 deletions sdk/data/v1/res_objects.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,106 @@
package v1

import (
"encoding/json"
"math/big"
"time"

v1 "github.com/attestantio/go-builder-client/api/v1"
)

type BidsDelivered struct {
v1.BidTrace

type BidDeliveredMeta struct {
BlockNumber *big.Int `json:"block_number"`
NumTx uint `json:"num_tx"`
}

type BidsReceived struct {
type bidDeliveredMetaJSON struct {
BlockNumber string `json:"block_number"`
NumTx uint `json:"num_tx,string"`
}

type BidDelivered struct {
v1.BidTrace
BidDeliveredMeta
}

func (b *BidDelivered) UnmarshalJSON(input []byte) error {
bt := new(v1.BidTrace)

if err := json.Unmarshal(input, bt); err != nil {
return err
}

br := new(bidDeliveredMetaJSON)

if err := json.Unmarshal(input, br); err != nil {
return err
}

b.Slot = bt.Slot
b.ParentHash = bt.ParentHash
b.BlockHash = bt.BlockHash
b.BuilderPubkey = bt.BuilderPubkey
b.ProposerPubkey = bt.ProposerPubkey
b.ProposerFeeRecipient = bt.ProposerFeeRecipient
b.GasLimit = bt.GasLimit
b.GasUsed = bt.GasUsed
b.Value = bt.Value

b.BlockNumber = big.NewInt(0).SetBytes([]byte(br.BlockNumber))
b.NumTx = br.NumTx

return nil
}

type BidReceivedMeta struct {
BlockNumber *big.Int
NumTx uint
Timestamp time.Time
TimestampMs time.Time
OptimisticSubmission bool
}

type bidReceivedMetaJSON struct {
BlockNumber string `json:"block_number"`
NumTx uint `json:"num_tx,string"`
Timestamp int64 `json:"timestamp,string"`
TimestampMs int64 `json:"timestamp_ms,string"`
OptimisticSubmission bool `json:"optimistic_submission"`
}

type BidReceived struct {
v1.BidTrace
BidReceivedMeta
}

func (b *BidReceived) UnmarshalJSON(input []byte) error {
bt := new(v1.BidTrace)

if err := json.Unmarshal(input, bt); err != nil {
return err
}

br := new(bidReceivedMetaJSON)

if err := json.Unmarshal(input, br); err != nil {
return err
}

b.Slot = bt.Slot
b.ParentHash = bt.ParentHash
b.BlockHash = bt.BlockHash
b.BuilderPubkey = bt.BuilderPubkey
b.ProposerPubkey = bt.ProposerPubkey
b.ProposerFeeRecipient = bt.ProposerFeeRecipient
b.GasLimit = bt.GasLimit
b.GasUsed = bt.GasUsed
b.Value = bt.Value

b.BlockNumber = big.NewInt(0).SetBytes([]byte(br.BlockNumber))
b.NumTx = br.NumTx
b.Timestamp = time.Unix(br.Timestamp, 0)
b.TimestampMs = time.Unix(br.Timestamp, (br.Timestamp%1_000)*1_000_000)
b.OptimisticSubmission = br.OptimisticSubmission

BlockNumber *big.Int `json:"block_number"`
NumTx uint `json:"num_tx"`
Timestamp uint64 `json:"timestamp"`
TimestampMs uint64 `json:"timestamp_ms"`
OptimisticSubmission bool `json:"optimistic_submission"`
return nil
}

0 comments on commit ff34019

Please sign in to comment.