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

@@ -5,69 +5,10 @@ import (
"fmt"
"strconv"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
// datetime provides an internal conversion helper
type datetime time.Time
func (d *datetime) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
t, err := convert.UnixTimestampStrToTime(s)
if err != nil {
return err
}
*d = datetime(t)
return nil
}
// Time returns datetime cast directly as time.Time
func (d datetime) Time() time.Time {
return time.Time(d)
}
// microTimestamp provides an internal conversion helper
type microTimestamp time.Time
func (t *microTimestamp) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
if strconv.IntSize == 32 && len(s) >= 10 {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*t = microTimestamp(time.UnixMicro(i))
return nil
}
// Has Fast path optimisation when int == 64
i, err := strconv.Atoi(s)
if err != nil {
return err
}
*t = microTimestamp(time.UnixMicro(int64(i)))
return nil
}
// Time returns datetime cast directly as time.Time
func (t microTimestamp) Time() time.Time {
return time.Time(t)
}
// UnmarshalJSON deserializes JSON, and timestamp information.
func (p *TradingPair) UnmarshalJSON(data []byte) error {
type Alias TradingPair

View File

@@ -4,6 +4,7 @@ import (
"errors"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/types"
)
// Transaction types
@@ -283,14 +284,14 @@ type websocketOrderResponse struct {
}
type websocketOrderData struct {
ID int64 `json:"id"`
IDStr string `json:"id_str"`
ClientOrderID string `json:"client_order_id"`
RemainingAmount float64 `json:"amount"`
ExecutedAmount float64 `json:"amount_traded,string"` // Not Cumulative; Partial fill amount
Amount float64 `json:"amount_at_create,string"`
Price float64 `json:"price"`
Side orderSide `json:"order_type"`
Datetime datetime `json:"datetime"`
Microtimestamp microTimestamp `json:"microtimestamp"`
ID int64 `json:"id"`
IDStr string `json:"id_str"`
ClientOrderID string `json:"client_order_id"`
RemainingAmount float64 `json:"amount"`
ExecutedAmount float64 `json:"amount_traded,string"` // Not Cumulative; Partial fill amount
Amount float64 `json:"amount_at_create,string"`
Price float64 `json:"price"`
Side orderSide `json:"order_type"`
Datetime types.Time `json:"datetime"`
Microtimestamp types.Time `json:"microtimestamp"`
}