types: Add Time type from Gateio and share across codebase (#1648)

* consolidate type to types package and share across code base

* rm convert type and convert codebase

* rm irrelavant test cases

* Fix tests

* glorious nits

* Update exchanges/gateio/gateio_types.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* thrasher: nits

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2024-10-01 10:46:55 +10:00
committed by GitHub
parent bfd499f0c9
commit d31fa3ff3d
29 changed files with 2633 additions and 2894 deletions

View File

@@ -1307,7 +1307,7 @@ var ticker24hourChangeStream = `{
"stream":"btcusdt@ticker",
"data" :{
"e": "24hrTicker",
"E": 123456789,
"E": 1234567891,
"s": "BNBBTC",
"p": "0.0015",
"P": "250.00",
@@ -1325,7 +1325,7 @@ var ticker24hourChangeStream = `{
"v": "10000",
"q": "18",
"O": 0,
"C": 86400000,
"C": 8640000011,
"F": 0,
"L": 18150,
"n": 18151
@@ -1346,11 +1346,11 @@ func TestWebsocketKlineUpdate(t *testing.T) {
"stream":"btcusdt@kline_1m",
"data":{
"e": "kline",
"E": 123456789,
"E": 1234567891,
"s": "BNBBTC",
"k": {
"t": 123400000,
"T": 123460000,
"t": 1234000001,
"T": 1234600001,
"s": "BNBBTC",
"i": "1m",
"f": 100,
@@ -1429,7 +1429,7 @@ func TestWebsocketOrderBookDepthDiffStream(t *testing.T) {
}
update1 := []byte(`{"stream":"btcusdt@depth","data":{
"e": "depthUpdate",
"E": 123456788,
"E": 1234567891,
"s": "BTCUSDT",
"U": 157,
"u": 160,
@@ -1446,7 +1446,7 @@ func TestWebsocketOrderBookDepthDiffStream(t *testing.T) {
t.Fatal(err)
}
if err := bi.wsHandleData(update1); err != nil {
t.Error(err)
t.Fatal(err)
}
bi.obm.state[currency.BTC][currency.USDT][asset.Spot].fetchingBook = false
ob, err := bi.Websocket.Orderbook.GetOrderbook(p, asset.Spot)
@@ -1465,7 +1465,7 @@ func TestWebsocketOrderBookDepthDiffStream(t *testing.T) {
update2 := []byte(`{
"stream":"btcusdt@depth","data":{
"e": "depthUpdate",
"E": 123456789,
"E": 1234567892,
"s": "BTCUSDT",
"U": 161,
"u": 165,
@@ -1686,7 +1686,7 @@ func TestExecutionTypeToOrderStatus(t *testing.T) {
var websocketDepthUpdate = []byte(
`{
"e": "depthUpdate",
"E": 123456789,
"E": 12345678911,
"s": "BNBBTC",
"U": 157,
"u": 160,

View File

@@ -3,6 +3,8 @@ package binanceus
import (
"encoding/json"
"time"
"github.com/thrasher-corp/gocryptotrader/types"
)
// UnmarshalJSON deserialises the JSON info, including the timestamp
@@ -139,7 +141,7 @@ func (a *Account) UnmarshalJSON(data []byte) error {
func (a *NewOrderResponse) UnmarshalJSON(data []byte) error {
type Alias NewOrderResponse
aux := &struct {
TransactionTime binanceusTime `json:"transactTime"`
TransactionTime types.Time `json:"transactTime"`
*Alias
}{
Alias: (*Alias)(a),
@@ -306,8 +308,8 @@ func (a *WsListStatus) UnmarshalJSON(data []byte) error {
type Alias WsListStatus
aux := &struct {
Data struct {
EventTime binanceusTime `json:"E"`
TransactionTime binanceusTime `json:"T"`
EventTime types.Time `json:"E"`
TransactionTime types.Time `json:"T"`
*WsListStatusData
} `json:"data"`
*Alias
@@ -327,9 +329,9 @@ func (a *WsListStatus) UnmarshalJSON(data []byte) error {
func (a *TickerStream) UnmarshalJSON(data []byte) error {
type Alias TickerStream
aux := &struct {
EventTime binanceusTime `json:"E"`
OpenTime binanceusTime `json:"O"`
CloseTime binanceusTime `json:"C"`
EventTime types.Time `json:"E"`
OpenTime types.Time `json:"O"`
CloseTime types.Time `json:"C"`
*Alias
}{
Alias: (*Alias)(a),
@@ -347,10 +349,10 @@ func (a *TickerStream) UnmarshalJSON(data []byte) error {
func (a *KlineStream) UnmarshalJSON(data []byte) error {
type Alias KlineStream
aux := &struct {
EventTime binanceusTime `json:"E"`
EventTime types.Time `json:"E"`
Kline struct {
StartTime binanceusTime `json:"t"`
CloseTime binanceusTime `json:"T"`
StartTime types.Time `json:"t"`
CloseTime types.Time `json:"T"`
*KlineStreamData
} `json:"k"`
*Alias
@@ -371,8 +373,8 @@ func (a *KlineStream) UnmarshalJSON(data []byte) error {
func (a *TradeStream) UnmarshalJSON(data []byte) error {
type Alias TradeStream
aux := &struct {
TimeStamp binanceusTime `json:"T"`
EventTime binanceusTime `json:"E"`
TimeStamp types.Time `json:"T"`
EventTime types.Time `json:"E"`
*Alias
}{
Alias: (*Alias)(a),
@@ -390,9 +392,9 @@ func (a *wsOrderUpdate) UnmarshalJSON(data []byte) error {
type Alias wsOrderUpdate
aux := &struct {
Data struct {
EventTime binanceusTime `json:"E"`
OrderCreationTime binanceusTime `json:"O"`
TransactionTime binanceusTime `json:"T"`
EventTime types.Time `json:"E"`
OrderCreationTime types.Time `json:"O"`
TransactionTime types.Time `json:"T"`
*WsOrderUpdateData
} `json:"data"`
*Alias
@@ -414,8 +416,8 @@ func (a *wsBalanceUpdate) UnmarshalJSON(data []byte) error {
type Alias wsBalanceUpdate
aux := &struct {
Data struct {
EventTime binanceusTime `json:"E"`
ClearTime binanceusTime `json:"T"`
EventTime types.Time `json:"E"`
ClearTime types.Time `json:"T"`
*WsBalanceUpdateData
} `json:"data"`
*Alias
@@ -436,8 +438,8 @@ func (a *wsAccountPosition) UnmarshalJSON(data []byte) error {
type Alias wsAccountPosition
aux := &struct {
Data struct {
EventTime binanceusTime `json:"E"`
LastUpdated binanceusTime `json:"u"`
EventTime types.Time `json:"E"`
LastUpdated types.Time `json:"u"`
*WsAccountPositionData
} `json:"data"`
*Alias
@@ -457,7 +459,7 @@ func (a *wsAccountPosition) UnmarshalJSON(data []byte) error {
func (a *WebsocketDepthStream) UnmarshalJSON(data []byte) error {
type Alias WebsocketDepthStream
aux := &struct {
Timestamp binanceusTime `json:"E"`
Timestamp types.Time `json:"E"`
*Alias
}{
Alias: (*Alias)(a),
@@ -491,23 +493,6 @@ func (a *WebsocketAggregateTradeStream) UnmarshalJSON(data []byte) error {
return nil
}
// binanceTime provides an internal conversion helper
type binanceusTime time.Time
func (t *binanceusTime) UnmarshalJSON(data []byte) error {
var timestamp int64
if err := json.Unmarshal(data, &timestamp); err != nil {
return err
}
*t = binanceusTime(time.UnixMilli(timestamp))
return nil
}
// Time returns a time.Time object
func (t binanceusTime) Time() time.Time {
return time.Time(t)
}
// UnmarshalJSON deserialises createTime timestamp to built in time.
func (a *OCBSOrder) UnmarshalJSON(data []byte) error {
type Alias OCBSOrder