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

feature: POST /v5/account/set-collateral-switch-batch #185

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const (
CoinXRP = Coin("XRP")
// CoinUSDT :
CoinUSDT = Coin("USDT")
// CoinMATIC :
CoinMATIC = Coin("MATIC")
// CoinSOL :
CoinSOL = Coin("SOL")
)

// Side :
Expand Down
21 changes: 19 additions & 2 deletions integrationtest/v5/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

require.NoError(t, err)
{
goldenFilename := "./testdata/v5-account-get-wallet-balance.json"
Expand Down Expand Up @@ -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,
Expand All @@ -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))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"list": [
{
"coin": "MATIC",
"collateralSwitch": "OFF"
},
{
"coin": "BTC",
"collateralSwitch": "OFF"
},
{
"coin": "ETH",
"collateralSwitch": "OFF"
},
{
"coin": "SOL",
"collateralSwitch": "OFF"
}
]
}
33 changes: 33 additions & 0 deletions v5_account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"`
Expand Down
57 changes: 57 additions & 0 deletions v5_account_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down