gateio: update FuturesAccount and Position structs; rename gateioTime to Time (#1532)

* gateio: update FuturesAccount struct

* gateio: update Position struct

* gateio: rm redundant checks and add in actual required checks

* export GateioTime

* linter: fix

* linter: again fix

* issue: fix

* gateio: update fee struct and such

* Update exchanges/gateio/gateio_types.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/gateio/gateio_convert.go

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

* use Time type acrost file

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2024-06-14 11:54:27 +10:00
committed by GitHub
parent 06b9980f77
commit 4c4b6935be
8 changed files with 410 additions and 388 deletions

View File

@@ -1152,30 +1152,28 @@ func (g *Gateio) TransferCurrency(ctx context.Context, arg *TransferCurrencyPara
if arg.Currency.IsEmpty() {
return nil, currency.ErrCurrencyCodeEmpty
}
if !strings.EqualFold(arg.From, asset.Spot.String()) {
return nil, fmt.Errorf("%w, only %s accounts can be used to transfer from", asset.ErrNotSupported, asset.Spot)
if arg.From == "" {
return nil, errors.New("from account is required")
}
if !g.isAccountAccepted(arg.To) {
return nil, fmt.Errorf("%w, only %v,%v,%v,%v,%v,and %v are supported", asset.ErrNotSupported, asset.Spot, asset.Margin, asset.Futures, asset.DeliveryFutures, asset.CrossMargin, asset.Options)
if arg.To == "" {
return nil, errors.New("to account is required")
}
if arg.Amount < 0 {
if arg.To == arg.From {
return nil, errors.New("from and to account cannot be the same")
}
if (arg.To == "margin" || arg.From == "margin") && arg.CurrencyPair.IsEmpty() {
return nil, errors.New("currency pair is required for margin account transfer")
}
if (arg.To == "futures" || arg.From == "futures") && arg.Settle == "" {
return nil, errors.New("settle is required for futures account transfer")
}
if arg.Amount <= 0 {
return nil, errInvalidAmount
}
var response *TransactionIDResponse
return response, g.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, walletEPL, http.MethodPost, walletTransfer, nil, &arg, &response)
}
func (g *Gateio) isAccountAccepted(account string) bool {
if account == "" {
return false
}
acc, err := asset.New(account)
if err != nil {
return false
}
return acc == asset.Spot || acc == asset.Margin || acc == asset.CrossMargin || acc == asset.Futures || acc == asset.DeliveryFutures || acc == asset.Options
}
func (g *Gateio) assetTypeToString(acc asset.Item) string {
if acc == asset.Options {
return "options"

View File

@@ -8,10 +8,11 @@ import (
"time"
)
type gateioTime time.Time
// Time represents a time.Time object that can be unmarshalled from a float64 or string.
type Time time.Time
// UnmarshalJSON deserializes json, and timestamp information.
func (a *gateioTime) UnmarshalJSON(data []byte) error {
func (a *Time) UnmarshalJSON(data []byte) error {
var value interface{}
err := json.Unmarshal(data, &value)
if err != nil {
@@ -34,20 +35,20 @@ func (a *gateioTime) UnmarshalJSON(data []byte) error {
return err
}
if math.Trunc(parsedValue) != parsedValue {
*a = gateioTime(time.UnixMicro(int64(parsedValue * 1e3))) // Account for "1691122380942.173000" microseconds
*a = Time(time.UnixMicro(int64(parsedValue * 1e3))) // Account for "1691122380942.173000" microseconds
return nil
}
standard = int64(parsedValue)
default:
return fmt.Errorf("cannot unmarshal %T into gateioTime", val)
return fmt.Errorf("cannot unmarshal %T into Time", val)
}
if standard > 9999999999 {
*a = gateioTime(time.UnixMilli(standard))
*a = Time(time.UnixMilli(standard))
} else {
*a = gateioTime(time.Unix(standard, 0))
*a = Time(time.Unix(standard, 0))
}
return nil
}
// Time represents a time instance.
func (a gateioTime) Time() time.Time { return time.Time(a) }
func (a Time) Time() time.Time { return time.Time(a) }

View File

@@ -3147,7 +3147,7 @@ func TestParseGateioMilliSecTimeUnmarshal(t *testing.T) {
float64JSON := `{"number": 1684981731.098}`
time := time.UnixMilli(timeWhenTesting)
var in gateioTime
var in Time
err := json.Unmarshal([]byte(timeWhenTestingString), &in)
if err != nil {
t.Fatal(err)
@@ -3156,7 +3156,7 @@ func TestParseGateioMilliSecTimeUnmarshal(t *testing.T) {
t.Fatalf("found %v, but expected %v", in.Time(), time)
}
inInteger := struct {
Number gateioTime `json:"number"`
Number Time `json:"number"`
}{}
err = json.Unmarshal([]byte(integerJSON), &inInteger)
if err != nil {
@@ -3167,7 +3167,7 @@ func TestParseGateioMilliSecTimeUnmarshal(t *testing.T) {
}
inFloat64 := struct {
Number gateioTime `json:"number"`
Number Time `json:"number"`
}{}
err = json.Unmarshal([]byte(float64JSON), &inFloat64)
if err != nil {
@@ -3178,7 +3178,7 @@ func TestParseGateioMilliSecTimeUnmarshal(t *testing.T) {
}
}
func TestParseGateioTimeUnmarshal(t *testing.T) {
func TestParseTimeUnmarshal(t *testing.T) {
t.Parallel()
var timeWhenTesting int64 = 1684981731
timeWhenTestingString := `"1684981731"`
@@ -3187,7 +3187,7 @@ func TestParseGateioTimeUnmarshal(t *testing.T) {
timeWhenTestingStringMicroSecond := `"1691122380942.173000"`
whenTime := time.Unix(timeWhenTesting, 0)
var in gateioTime
var in Time
err := json.Unmarshal([]byte(timeWhenTestingString), &in)
if err != nil {
t.Fatal(err)
@@ -3196,7 +3196,7 @@ func TestParseGateioTimeUnmarshal(t *testing.T) {
t.Fatalf("found %v, but expected %v", in.Time(), whenTime)
}
inInteger := struct {
Number gateioTime `json:"number"`
Number Time `json:"number"`
}{}
err = json.Unmarshal([]byte(integerJSON), &inInteger)
if err != nil {
@@ -3207,7 +3207,7 @@ func TestParseGateioTimeUnmarshal(t *testing.T) {
}
inFloat64 := struct {
Number gateioTime `json:"number"`
Number Time `json:"number"`
}{}
err = json.Unmarshal([]byte(float64JSON), &inFloat64)
if err != nil {
@@ -3218,7 +3218,7 @@ func TestParseGateioTimeUnmarshal(t *testing.T) {
t.Fatalf("found %v, but expected %v", inFloat64.Number.Time(), msTime)
}
var microSeconds gateioTime
var microSeconds Time
err = json.Unmarshal([]byte(timeWhenTestingStringMicroSecond), &microSeconds)
if err != nil {
t.Fatal(err)

File diff suppressed because it is too large Load Diff

View File

@@ -125,7 +125,7 @@ func (g *Gateio) wsHandleData(respRaw []byte) error {
switch push.Channel { // TODO: Convert function params below to only use push.Result
case spotTickerChannel:
return g.processTicker(push.Result, push.Time)
return g.processTicker(push.Result, push.Time.Time())
case spotTradesChannel:
return g.processTrades(push.Result)
case spotCandlesticksChannel:
@@ -160,7 +160,7 @@ func (g *Gateio) wsHandleData(respRaw []byte) error {
return nil
}
func (g *Gateio) processTicker(incoming []byte, pushTime int64) error {
func (g *Gateio) processTicker(incoming []byte, pushTime time.Time) error {
var data WsTicker
err := json.Unmarshal(incoming, &data)
if err != nil {
@@ -177,7 +177,7 @@ func (g *Gateio) processTicker(incoming []byte, pushTime int64) error {
Ask: data.LowestAsk.Float64(),
AssetType: asset.Spot,
Pair: data.CurrencyPair,
LastUpdated: time.Unix(pushTime, 0),
LastUpdated: pushTime,
}
assetPairEnabled := g.listOfAssetsCurrencyPairEnabledFor(data.CurrencyPair)
if assetPairEnabled[asset.Spot] {
@@ -253,7 +253,7 @@ func (g *Gateio) processCandlestick(incoming []byte) error {
Pair: currencyPair,
AssetType: asset.Spot,
Exchange: g.Name,
StartTime: time.Unix(data.Timestamp, 0),
StartTime: data.Timestamp.Time(),
Interval: icp[0],
OpenPrice: data.OpenPrice.Float64(),
ClosePrice: data.ClosePrice.Float64(),
@@ -289,7 +289,7 @@ func (g *Gateio) processOrderbookTicker(incoming []byte) error {
Exchange: g.Name,
Pair: data.CurrencyPair,
Asset: asset.Spot,
LastUpdated: time.UnixMilli(data.UpdateTimeMS),
LastUpdated: data.UpdateTimeMS.Time(),
Bids: []orderbook.Tranche{{Price: data.BestBidPrice.Float64(), Amount: data.BestBidAmount.Float64()}},
Asks: []orderbook.Tranche{{Price: data.BestAskPrice.Float64(), Amount: data.BestAskAmount.Float64()}},
})

View File

@@ -1606,7 +1606,7 @@ func (g *Gateio) GetActiveOrders(ctx context.Context, req *order.MultiOrderReque
continue
}
var side order.Side
side, err = order.StringToOrderSide(spotOrders[x].Orders[x].Side)
side, err = order.StringToOrderSide(spotOrders[x].Orders[y].Side)
if err != nil {
log.Errorf(log.ExchangeSys, "%s %v", g.Name, err)
}

View File

@@ -236,7 +236,7 @@ func (g *Gateio) wsHandleFuturesData(respRaw []byte, assetType asset.Item) error
case futuresTradesChannel:
return g.processFuturesTrades(respRaw, assetType)
case futuresOrderbookChannel:
return g.processFuturesOrderbookSnapshot(push.Event, push.Result, assetType, push.Time)
return g.processFuturesOrderbookSnapshot(push.Event, push.Result, assetType, push.Time.Time())
case futuresOrderbookTickerChannel:
return g.processFuturesOrderbookTicker(push.Result)
case futuresOrderbookUpdateChannel:
@@ -557,7 +557,7 @@ func (g *Gateio) processFuturesAndOptionsOrderbookUpdate(incoming []byte, assetT
}
}
updates := orderbook.Update{
UpdateTime: time.UnixMilli(data.TimestampInMs),
UpdateTime: data.TimestampInMs.Time(),
Pair: data.ContractName,
Asset: assetType,
}
@@ -577,7 +577,7 @@ func (g *Gateio) processFuturesAndOptionsOrderbookUpdate(incoming []byte, assetT
return g.Websocket.Orderbook.Update(&updates)
}
func (g *Gateio) processFuturesOrderbookSnapshot(event string, incoming []byte, assetType asset.Item, pushTime int64) error {
func (g *Gateio) processFuturesOrderbookSnapshot(event string, incoming []byte, assetType asset.Item, pushTime time.Time) error {
if event == "all" {
var data WsFuturesOrderbookSnapshot
err := json.Unmarshal(incoming, &data)
@@ -643,7 +643,7 @@ func (g *Gateio) processFuturesOrderbookSnapshot(event string, incoming []byte,
Asset: assetType,
Exchange: g.Name,
Pair: currencyPair,
LastUpdated: time.Unix(pushTime, 0),
LastUpdated: pushTime,
VerifyOrderbook: g.CanVerifyOrderbook,
})
if err != nil {

View File

@@ -379,7 +379,7 @@ func (g *Gateio) wsHandleOptionsData(respRaw []byte) error {
optionsUnderlyingCandlesticksChannel:
return g.processOptionsCandlestickPushData(respRaw)
case optionsOrderbookChannel:
return g.processOptionsOrderbookSnapshotPushData(push.Event, push.Result, push.Time)
return g.processOptionsOrderbookSnapshotPushData(push.Event, push.Result, push.Time.Time())
case optionsOrderbookTickerChannel:
return g.processOrderbookTickerPushData(respRaw)
case optionsOrderbookUpdateChannel:
@@ -531,7 +531,7 @@ func (g *Gateio) processOptionsCandlestickPushData(data []byte) error {
Pair: currencyPair,
AssetType: asset.Options,
Exchange: g.Name,
StartTime: time.Unix(resp.Result[x].Timestamp, 0),
StartTime: resp.Result[x].Timestamp.Time(),
Interval: icp[0],
OpenPrice: resp.Result[x].OpenPrice.Float64(),
ClosePrice: resp.Result[x].ClosePrice.Float64(),
@@ -554,7 +554,7 @@ func (g *Gateio) processOrderbookTickerPushData(incoming []byte) error {
return nil
}
func (g *Gateio) processOptionsOrderbookSnapshotPushData(event string, incoming []byte, pushTime int64) error {
func (g *Gateio) processOptionsOrderbookSnapshotPushData(event string, incoming []byte, pushTime time.Time) error {
if event == "all" {
var data WsOptionsOrderbookSnapshot
err := json.Unmarshal(incoming, &data)
@@ -618,7 +618,7 @@ func (g *Gateio) processOptionsOrderbookSnapshotPushData(event string, incoming
Asset: asset.Options,
Exchange: g.Name,
Pair: currencyPair,
LastUpdated: time.Unix(pushTime, 0),
LastUpdated: pushTime,
VerifyOrderbook: g.CanVerifyOrderbook,
})
if err != nil {