Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GateIO: Split Futures into USDT and CoinM #1786

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
11 changes: 6 additions & 5 deletions config/versions/v5.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ func (v *Version5) UpgradeExchange(_ context.Context, e []byte) ([]byte, error)
if err := json.Unmarshal(fsJSON, &fs); err != nil {
return e, err
}
if _, ok := fs["futures"]; !ok {
futures, ok := fs["futures"]
if !ok {
// Not our job to add CoinM, USDT. Only to split them
return e, nil
}
for _, p := range strings.Split(fs["futures"].Available, ",") {
for _, p := range strings.Split(futures.Available, ",") {
where := "usdtmarginedfutures"
if strings.HasSuffix(p, "USD") {
where = "coinmarginedfutures"
Expand All @@ -43,7 +44,7 @@ func (v *Version5) UpgradeExchange(_ context.Context, e []byte) ([]byte, error)
}
fs[where].Available += p
}
for _, p := range strings.Split(fs["futures"].Enabled, ",") {
for _, p := range strings.Split(futures.Enabled, ",") {
where := "usdtmarginedfutures"
if strings.HasSuffix(p, "USD") {
where = "coinmarginedfutures"
Expand All @@ -53,8 +54,8 @@ func (v *Version5) UpgradeExchange(_ context.Context, e []byte) ([]byte, error)
}
fs[where].Enabled += p
}
fs["usdtmarginedfutures"].AssetEnabled = fs["futures"].AssetEnabled
fs["coinmarginedfutures"].AssetEnabled = fs["futures"].AssetEnabled
fs["usdtmarginedfutures"].AssetEnabled = futures.AssetEnabled
fs["coinmarginedfutures"].AssetEnabled = futures.AssetEnabled
delete(fs, "futures")
val, err := json.Marshal(fs)
if err == nil {
Expand Down
8 changes: 4 additions & 4 deletions exchanges/gateio/gateio.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var (
errInvalidOrderSize = errors.New("invalid order size")
errInvalidOrderID = errors.New("invalid order id")
errInvalidAmount = errors.New("invalid amount")
errInvalidOrEmptySubaccount = errors.New("invalid or empty subaccount")
errInvalidSubAccount = errors.New("invalid or empty subaccount")
errInvalidTransferDirection = errors.New("invalid transfer direction")
errInvalidOrderSide = errors.New("invalid order side")
errDifferentAccount = errors.New("account type must be identical for all orders")
Expand Down Expand Up @@ -1184,7 +1184,7 @@ func (g *Gateio) SubAccountTransfer(ctx context.Context, arg SubAccountTransferP
return currency.ErrCurrencyCodeEmpty
}
if arg.SubAccount == "" {
return errInvalidOrEmptySubaccount
return errInvalidSubAccount
}
arg.Direction = strings.ToLower(arg.Direction)
if arg.Direction != "to" && arg.Direction != "from" {
Expand All @@ -1194,9 +1194,9 @@ func (g *Gateio) SubAccountTransfer(ctx context.Context, arg SubAccountTransferP
return errInvalidAmount
}
switch arg.SubAccountType {
case "", "spot", "futures", "delivery":
case asset.Empty, asset.Spot, asset.Futures, asset.DeliveryFutures:
default:
return fmt.Errorf("%v; only spot, futures and delivery are allowed", asset.ErrNotSupported)
return fmt.Errorf("%w: `%s`", asset.ErrNotSupported, arg.SubAccountType)
}
return g.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, walletSubAccountTransferEPL, http.MethodPost, walletSubAccountTransfer, nil, &arg, nil)
}
Expand Down
31 changes: 18 additions & 13 deletions exchanges/gateio/gateio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,15 +744,19 @@ func TestTransferCurrency(t *testing.T) {

func TestSubAccountTransfer(t *testing.T) {
t.Parallel()
ctx := context.Background()
req := SubAccountTransferParam{SubAccountType: asset.Index}
require.ErrorIs(t, g.SubAccountTransfer(ctx, req), currency.ErrCurrencyCodeEmpty)
req.Currency = currency.BTC
require.ErrorIs(t, g.SubAccountTransfer(ctx, req), errInvalidSubAccount)
req.SubAccount = "1337"
require.ErrorIs(t, g.SubAccountTransfer(ctx, req), errInvalidTransferDirection)
req.Direction = "to"
require.ErrorIs(t, g.SubAccountTransfer(ctx, req), errInvalidAmount)
req.Amount = 1.337
require.ErrorIs(t, g.SubAccountTransfer(ctx, req), asset.ErrNotSupported)
sharedtestvalues.SkipTestIfCredentialsUnset(t, g, canManipulateRealOrders)
if err := g.SubAccountTransfer(context.Background(), SubAccountTransferParam{
Currency: currency.BTC,
SubAccount: "12222",
Direction: "to",
Amount: 1,
}); err != nil {
t.Errorf("%s SubAccountTransfer() error %v", g.Name, err)
}
require.NoError(t, g.SubAccountTransfer(ctx, req))
}

func TestGetSubAccountTransferHistory(t *testing.T) {
Expand Down Expand Up @@ -865,9 +869,8 @@ func TestGetAllFutureContracts(t *testing.T) {
t.Parallel()

for _, c := range []currency.Code{currency.BTC, currency.USDT} {
if _, err := g.GetAllFutureContracts(context.Background(), c); err != nil {
assert.Errorf(t, err, "GetAllFutureContracts %s should not error", c)
}
_, err := g.GetAllFutureContracts(context.Background(), c)
assert.NoErrorf(t, err, "GetAllFutureContracts %s should not error", c)
}
}

Expand Down Expand Up @@ -2053,11 +2056,13 @@ func TestFuturesDataHandler(t *testing.T) {
g := new(Gateio) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
require.NoError(t, testexch.Setup(g), "Test instance Setup must not error")
testexch.FixtureToDataHandler(t, "testdata/wsFutures.json", func(m []byte) error { return g.WsHandleFuturesData(context.Background(), m, asset.CoinMarginedFutures) })
g.Websocket.DataHandler <- errors.New("hamster")
close(g.Websocket.DataHandler)
assert.Len(t, g.Websocket.DataHandler, 14, "Should see the correct number of messages")
for resp := range g.Websocket.DataHandler {
_, isErr := resp.(error)
assert.False(t, isErr, "Should not get any errors down the data handler")
if err, isErr := resp.(error); isErr {
assert.NoError(t, err, "Should not get any errors down the data handler")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion exchanges/gateio/gateio_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ type SubAccountTransferParam struct {
SubAccount string `json:"sub_account"`
Direction string `json:"direction"`
Amount types.Number `json:"amount"`
SubAccountType string `json:"sub_account_type"`
SubAccountType asset.Item `json:"sub_account_type"`
}

// SubAccountTransferResponse represents transfer records between main and sub accounts
Expand Down
2 changes: 1 addition & 1 deletion exchanges/gateio/gateio_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ func (g *Gateio) UpdateAccountInfo(ctx context.Context, a asset.Item) (account.H
for i := range balances {
currencies[i] = account.Balance{
Currency: currency.NewCode(balances[i].Currency),
Total: balances[i].Available.Float64() - balances[i].Locked.Float64(),
Total: balances[i].Available.Float64() + balances[i].Locked.Float64(),
Hold: balances[i].Locked.Float64(),
Free: balances[i].Available.Float64(),
}
Expand Down
Loading