From c6d8b86e68f9c89a9073c3dc6c9ac6d456c4b181 Mon Sep 17 00:00:00 2001 From: Rostislav <43965646+lyro41@users.noreply.github.com> Date: Tue, 8 Oct 2024 14:35:04 +0300 Subject: [PATCH] feature: POST /v5/account/set-collateral-switch-batch (#185) * POST /v5/account/set-collateral-switch-batch * Update v5_account_service.go * Update README.md --------- Co-authored-by: hirokisan --- README.md | 1 + enum.go | 4 ++ integrationtest/v5/account/account_test.go | 21 ++++++- .../v5-account-set-collateral-coin-batch.json | 20 +++++++ v5_account_service.go | 33 +++++++++++ v5_account_service_test.go | 57 +++++++++++++++++++ 6 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 integrationtest/v5/account/testdata/v5-account-set-collateral-coin-batch.json diff --git a/README.md b/README.md index 8fbe06f..f94ebb3 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,7 @@ The following API endpoints have been implemented - [`/v5/account/transaction-log` Get Transaction Log](https://bybit-exchange.github.io/docs/v5/account/transaction-log) - [`/v5/account/collateral-info` Get Collateral Info](https://bybit-exchange.github.io/docs/v5/account/collateral-info) - [`/v5/account/set-collateral-switch` Set Collateral Coin](https://bybit-exchange.github.io/docs/v5/account/set-collateral) +- [`/v5/account/batch-set-collateral` Batch Set Collateral Coin](https://bybit-exchange.github.io/docs/v5/account/batch-set-collateral) - [`/v5/account/fee-rate` Get Fee Rate](https://bybit-exchange.github.io/docs/v5/account/fee-rate) #### Asset diff --git a/enum.go b/enum.go index 4166aa0..7752957 100644 --- a/enum.go +++ b/enum.go @@ -14,6 +14,10 @@ const ( CoinXRP = Coin("XRP") // CoinUSDT : CoinUSDT = Coin("USDT") + // CoinMATIC : + CoinMATIC = Coin("MATIC") + // CoinSOL : + CoinSOL = Coin("SOL") ) // Side : diff --git a/integrationtest/v5/account/account_test.go b/integrationtest/v5/account/account_test.go index a15bdf2..4710a91 100644 --- a/integrationtest/v5/account/account_test.go +++ b/integrationtest/v5/account/account_test.go @@ -12,7 +12,7 @@ import ( func TestGetWalletBalance(t *testing.T) { client := bybit.NewTestClient().WithAuthFromEnv() - res, err := client.V5().Account().GetWalletBalance(bybit.AccountTypeUnified, nil) + res, err := client.V5().Account().GetWalletBalance(bybit.AccountTypeV5UNIFIED, nil) require.NoError(t, err) { goldenFilename := "./testdata/v5-account-get-wallet-balance.json" @@ -62,7 +62,6 @@ func TestGetCollateralInfo(t *testing.T) { func TestSetCollateralCoin(t *testing.T) { client := bybit.NewTestClient().WithAuthFromEnv() - coins := []bybit.Coin{bybit.CoinBTC} res, err := client.V5().Account().SetCollateralCoin(bybit.V5SetCollateralCoinParam{ Coin: bybit.CoinBTC, CollateralSwitch: bybit.CollateralSwitchV5On, @@ -74,3 +73,21 @@ func TestSetCollateralCoin(t *testing.T) { testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) } } + +func TestBatchSetCollateralCoin(t *testing.T) { + client := bybit.NewTestClient().WithAuthFromEnv() + res, err := client.V5().Account().BatchSetCollateralCoin(bybit.V5BatchSetCollateralCoinParam{ + Request: []bybit.V5BatchSetCollateralCoinListItem{ + {Coin: bybit.CoinMATIC, CollateralSwitch: bybit.CollateralSwitchV5Off}, + {Coin: bybit.CoinBTC, CollateralSwitch: bybit.CollateralSwitchV5Off}, + {Coin: bybit.CoinETH, CollateralSwitch: bybit.CollateralSwitchV5Off}, + {Coin: bybit.CoinSOL, CollateralSwitch: bybit.CollateralSwitchV5Off}, + }, + }) + require.NoError(t, err) + { + goldenFilename := "./testdata/v5-account-set-collateral-coin-batch.json" + testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) + testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result)) + } +} diff --git a/integrationtest/v5/account/testdata/v5-account-set-collateral-coin-batch.json b/integrationtest/v5/account/testdata/v5-account-set-collateral-coin-batch.json new file mode 100644 index 0000000..1d427fe --- /dev/null +++ b/integrationtest/v5/account/testdata/v5-account-set-collateral-coin-batch.json @@ -0,0 +1,20 @@ +{ + "list": [ + { + "coin": "MATIC", + "collateralSwitch": "OFF" + }, + { + "coin": "BTC", + "collateralSwitch": "OFF" + }, + { + "coin": "ETH", + "collateralSwitch": "OFF" + }, + { + "coin": "SOL", + "collateralSwitch": "OFF" + } + ] +} \ No newline at end of file diff --git a/v5_account_service.go b/v5_account_service.go index ea2f156..5d940aa 100644 --- a/v5_account_service.go +++ b/v5_account_service.go @@ -13,6 +13,7 @@ import ( type V5AccountServiceI interface { GetWalletBalance(AccountTypeV5, []Coin) (*V5GetWalletBalanceResponse, error) SetCollateralCoin(V5SetCollateralCoinParam) (*V5SetCollateralCoinResponse, error) + BatchSetCollateralCoin(V5BatchSetCollateralCoinParam) (*V5BatchSetCollateralCoinResponse, error) GetCollateralInfo(V5GetCollateralInfoParam) (*V5GetCollateralInfoResponse, error) GetAccountInfo() (*V5GetAccountInfoResponse, error) GetTransactionLog(V5GetTransactionLogParam) (*V5GetTransactionLogResponse, error) @@ -136,6 +137,38 @@ func (s *V5AccountService) SetCollateralCoin(param V5SetCollateralCoinParam) (*V return &res, nil } +type V5BatchSetCollateralCoinParam struct { + Request []V5BatchSetCollateralCoinListItem `json:"request"` +} + +type V5BatchSetCollateralCoinListItem struct { + Coin Coin `json:"coin"` + CollateralSwitch CollateralSwitchV5 `json:"collateralSwitch"` +} + +type V5BatchSetCollateralCoinResponse struct { + CommonV5Response `json:",inline"` + Result struct { + List []V5BatchSetCollateralCoinListItem `json:"list"` + } `json:"result"` +} + +// BatchSetCollateralCoin : +func (s *V5AccountService) BatchSetCollateralCoin(param V5BatchSetCollateralCoinParam) (*V5BatchSetCollateralCoinResponse, error) { + var res V5BatchSetCollateralCoinResponse + + body, err := json.Marshal(param) + if err != nil { + return nil, err + } + + if err = s.client.postV5JSON("/v5/account/set-collateral-switch-batch", body, &res); err != nil { + return nil, err + } + + return &res, nil +} + // V5GetCollateralInfoParam : type V5GetCollateralInfoParam struct { Currency *string `url:"currency,omitempty"` diff --git a/v5_account_service_test.go b/v5_account_service_test.go index 9116aa7..6dfccb8 100644 --- a/v5_account_service_test.go +++ b/v5_account_service_test.go @@ -107,6 +107,63 @@ func TestV5Account_SetCollateralCoin(t *testing.T) { }) } +func TestV5Account_BatchSetCollateralCoin(t *testing.T) { + t.Run("success", func(t *testing.T) { + param := V5BatchSetCollateralCoinParam{ + Request: []V5BatchSetCollateralCoinListItem{ + {Coin: CoinMATIC, CollateralSwitch: CollateralSwitchV5Off}, + {Coin: CoinBTC, CollateralSwitch: CollateralSwitchV5Off}, + {Coin: CoinETH, CollateralSwitch: CollateralSwitchV5Off}, + {Coin: CoinSOL, CollateralSwitch: CollateralSwitchV5Off}, + }, + } + + path := "/v5/account/set-collateral-switch-batch" + method := http.MethodPost + status := http.StatusOK + respBody := map[string]interface{}{ + "result": map[string]interface{}{ + "list": []map[string]interface{}{ + { + "coin": "MATIC", + "collateralSwitch": "OFF", + }, + { + "coin": "BTC", + "collateralSwitch": "OFF", + }, + { + "coin": "ETH", + "collateralSwitch": "OFF", + }, + { + "coin": "SOL", + "collateralSwitch": "OFF", + }, + }, + }, + } + + bytesBody, err := json.Marshal(respBody) + require.NoError(t, err) + + server, teardown := testhelper.NewServer( + testhelper.WithHandlerOption(path, method, status, bytesBody), + ) + defer teardown() + + client := NewTestClient(). + WithBaseURL(server.URL). + WithAuth("test", "test") + + resp, err := client.V5().Account().BatchSetCollateralCoin(param) + require.NoError(t, err) + + require.NotNil(t, resp) + testhelper.Compare(t, respBody["result"], resp.Result) + }) +} + func TestV5Account_GetCollateralInfo(t *testing.T) { t.Run("success", func(t *testing.T) { currency := "BTC"