From ff340197855a2370fd88fefc8ebd22708fa48175 Mon Sep 17 00:00:00 2001 From: "Luca G.F" Date: Mon, 11 Dec 2023 18:11:49 +0100 Subject: [PATCH] fix: implement custom data types decoder (#8) * fix(data): add custom decoder logic for returned objects Signed-off-by: Luca Georges Francois * refactor(data): update struct names Signed-off-by: Luca Georges Francois --------- Signed-off-by: Luca Georges Francois --- sdk/data/v1/data_sdk.go | 8 +-- sdk/data/v1/res_objects.go | 100 +++++++++++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 13 deletions(-) diff --git a/sdk/data/v1/data_sdk.go b/sdk/data/v1/data_sdk.go index 50a522c..005519d 100644 --- a/sdk/data/v1/data_sdk.go +++ b/sdk/data/v1/data_sdk.go @@ -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) } @@ -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) @@ -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) } @@ -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) diff --git a/sdk/data/v1/res_objects.go b/sdk/data/v1/res_objects.go index 2fd9bfc..93c7b98 100644 --- a/sdk/data/v1/res_objects.go +++ b/sdk/data/v1/res_objects.go @@ -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 }