mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
ftx: unmarshal all fields at the same time
This commit is contained in:
parent
009dafd176
commit
e34f68ab90
|
@ -2,7 +2,6 @@ package ftx
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -56,7 +55,7 @@ type rawResponse struct {
|
|||
// Example 1: {"type": "error", "code": 404, "msg": "No such market: BTCUSDT"}
|
||||
Code int64 `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data map[string]json.RawMessage `json:"data"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
func (r rawResponse) toSubscribedResp() subscribedResponse {
|
||||
|
@ -70,28 +69,12 @@ func (r rawResponse) toSnapshotResp() (snapshotResponse, error) {
|
|||
mandatoryFields: r.mandatoryFields,
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(r.Data["action"], &o.Action); err != nil {
|
||||
return snapshotResponse{}, fmt.Errorf("failed to unmarshal data.action field: %w", err)
|
||||
if err := json.Unmarshal(r.Data, &o); err != nil {
|
||||
return snapshotResponse{}, err
|
||||
}
|
||||
|
||||
var t float64
|
||||
if err := json.Unmarshal(r.Data["time"], &t); err != nil {
|
||||
return snapshotResponse{}, fmt.Errorf("failed to unmarshal data.time field: %w", err)
|
||||
}
|
||||
sec, dec := math.Modf(t)
|
||||
o.Time = time.Unix(int64(sec), int64(dec*1e9))
|
||||
|
||||
if err := json.Unmarshal(r.Data["checksum"], &o.Checksum); err != nil {
|
||||
return snapshotResponse{}, fmt.Errorf("failed to unmarshal data.checksum field: %w", err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(r.Data["bids"], &o.Bids); err != nil {
|
||||
return snapshotResponse{}, fmt.Errorf("failed to unmarshal data.bids field: %w", err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(r.Data["asks"], &o.Asks); err != nil {
|
||||
return snapshotResponse{}, fmt.Errorf("failed to unmarshal data.asks field: %w", err)
|
||||
}
|
||||
sec, dec := math.Modf(o.Time)
|
||||
o.Timestamp = time.Unix(int64(sec), int64(dec*1e9))
|
||||
|
||||
return o, nil
|
||||
}
|
||||
|
@ -104,17 +87,19 @@ type subscribedResponse struct {
|
|||
type snapshotResponse struct {
|
||||
mandatoryFields
|
||||
|
||||
Action string
|
||||
Action string `json:"action"`
|
||||
|
||||
Time time.Time
|
||||
Time float64 `json:"time"`
|
||||
|
||||
Checksum int64
|
||||
Timestamp time.Time
|
||||
|
||||
Checksum int64 `json:"checksum"`
|
||||
|
||||
// Best 100 orders
|
||||
Bids [][]float64
|
||||
Bids [][]float64 `json:"bids"`
|
||||
|
||||
// Best 100 orders
|
||||
Asks [][]float64
|
||||
Asks [][]float64 `json:"asks"`
|
||||
}
|
||||
|
||||
func (r snapshotResponse) toGlobalOrderBook() types.OrderBook {
|
||||
|
|
|
@ -31,7 +31,7 @@ func Test_rawResponse_toSnapshotResp(t *testing.T) {
|
|||
assert.Equal(t, partialRespType, r.Type)
|
||||
assert.Equal(t, orderbook, r.Channel)
|
||||
assert.Equal(t, "BTC/USDT", r.Market)
|
||||
assert.Equal(t, int64(1614520368), r.Time.Unix())
|
||||
assert.Equal(t, int64(1614520368), r.Timestamp.Unix())
|
||||
assert.Equal(t, int64(2150525410), r.Checksum)
|
||||
assert.Len(t, r.Bids, 100)
|
||||
assert.Equal(t, []float64{44555.0, 3.3968}, r.Bids[0])
|
||||
|
|
Loading…
Reference in New Issue
Block a user