forked from hirokisan/bybit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv5_account_service.go
366 lines (308 loc) · 11.4 KB
/
v5_account_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package bybit
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/google/go-querystring/query"
)
// V5AccountServiceI :
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)
GetFeeRate(V5GetFeeRateParam) (*V5GetFeeRateResponse, error)
}
// V5AccountService :
type V5AccountService struct {
client *Client
}
// V5GetWalletBalanceResponse :
type V5GetWalletBalanceResponse struct {
CommonV5Response `json:",inline"`
Result V5WalletBalanceResult `json:"result"`
}
// V5WalletBalanceResult :
type V5WalletBalanceResult struct {
List []V5WalletBalanceList `json:"list"`
}
// V5WalletBalanceCoin :
type V5WalletBalanceCoin struct {
Coin Coin `json:"coin"`
Equity string `json:"equity"`
UsdValue string `json:"usdValue"`
WalletBalance string `json:"walletBalance"`
Free string `json:"free"`
Locked string `json:"locked"`
SpotHedgingQuantity string `json:"spotHedgingQty"`
BorrowAmount string `json:"borrowAmount"`
AvailableToWithdraw string `json:"availableToWithdraw"`
AccruedInterest string `json:"accruedInterest"`
TotalOrderIM string `json:"totalOrderIM"`
TotalPositionIM string `json:"totalPositionIM"`
TotalPositionMM string `json:"totalPositionMM"`
UnrealisedPnl string `json:"unrealisedPnl"`
CumRealisedPnl string `json:"cumRealisedPnl"`
Bonus string `json:"bonus"`
MarginCollateral bool `json:"marginCollateral"`
CollateralSwitch bool `json:"collateralSwitch"`
AvailableToBorrow string `json:"availableToBorrow"`
}
// V5WalletBalanceList :
type V5WalletBalanceList struct {
AccountType string `json:"accountType"`
// Deprecated:
AccountLTV string `json:"accountLTV"`
AccountIMRate string `json:"accountIMRate"`
AccountMMRate string `json:"accountMMRate"`
TotalEquity string `json:"totalEquity"`
TotalWalletBalance string `json:"totalWalletBalance"`
TotalMarginBalance string `json:"totalMarginBalance"`
TotalAvailableBalance string `json:"totalAvailableBalance"`
TotalPerpUPL string `json:"totalPerpUPL"`
TotalInitialMargin string `json:"totalInitialMargin"`
TotalMaintenanceMargin string `json:"totalMaintenanceMargin"`
Coin []V5WalletBalanceCoin `json:"coin"`
}
// GetWalletBalance :
//
// at: UNIFIED, CONTRACT, SPOT
//
// coin:
// If not passed, it returns non-zero asset info
// You can pass multiple coins to query, separated by comma. "USDT,USDC".
func (s *V5AccountService) GetWalletBalance(at AccountTypeV5, coins []Coin) (*V5GetWalletBalanceResponse, error) {
switch at {
case AccountTypeV5UNIFIED, AccountTypeV5CONTRACT, AccountTypeV5SPOT:
default:
return nil, fmt.Errorf("wrong account type")
}
var (
res V5GetWalletBalanceResponse
query = make(url.Values)
)
query.Add("accountType", string(at))
if len(coins) > 0 {
var coinsStr []string
for _, c := range coins {
coinsStr = append(coinsStr, string(c))
}
query.Add("coin", strings.Join(coinsStr, ","))
}
if err := s.client.getV5Privately("/v5/account/wallet-balance", query, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5SetCollateralCoinParam :
type V5SetCollateralCoinParam struct {
// Coin:
// You cannot pass multiple coins to query
// USDT,USDC cannot be switched off
Coin Coin `json:"coin"`
// CollateralSwitch: CollateralSwitchV5On or CollateralSwitchV5Off
CollateralSwitch CollateralSwitchV5 `json:"collateralSwitch"`
}
// V5SetCollateralCoinResponse :
type V5SetCollateralCoinResponse struct {
CommonV5Response `json:",inline"`
Result interface{} `json:"result"`
}
// SetCollateralCoin :
func (s *V5AccountService) SetCollateralCoin(param V5SetCollateralCoinParam) (*V5SetCollateralCoinResponse, error) {
var res V5SetCollateralCoinResponse
body, err := json.Marshal(param)
if err != nil {
return nil, err
}
if err := s.client.postV5JSON("/v5/account/set-collateral-switch", body, &res); err != nil {
return nil, err
}
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"`
}
// V5GetCollateralInfoResponse :
type V5GetCollateralInfoResponse struct {
CommonV5Response `json:",inline"`
Result V5GetCollateralInfoResult
}
// V5GetCollateralInfoResult :
type V5GetCollateralInfoResult struct {
List []V5GetCollateralInfoList `json:"list"`
}
// V5GetCollateralInfoList :
type V5GetCollateralInfoList struct {
Currency string `json:"currency"`
HourlyBorrowRate string `json:"hourlyBorrowRate"`
MaxBorrowingAmount string `json:"maxBorrowingAmount"`
FreeBorrowingLimit string `json:"freeBorrowingLimit"`
FreeBorrowAmount string `json:"freeBorrowAmount"`
BorrowAmount string `json:"borrowAmount"`
FreeBorrowingAmount string `json:"freeBorrowingAmount"`
AvailableToBorrow string `json:"availableToBorrow"`
Borrowable bool `json:"borrowable"`
BorrowUsageRate string `json:"borrowUsageRate"`
MarginCollateral bool `json:"marginCollateral"`
CollateralSwitch bool `json:"collateralSwitch"`
CollateralRatio string `json:"collateralRatio"`
}
// GetCollateralInfo :
func (s *V5AccountService) GetCollateralInfo(param V5GetCollateralInfoParam) (*V5GetCollateralInfoResponse, error) {
var res V5GetCollateralInfoResponse
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err = s.client.getV5Privately("/v5/account/collateral-info", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetAccountInfoResponse :
type V5GetAccountInfoResponse struct {
CommonV5Response `json:",inline"`
Result V5AccountInfoResult `json:"result"`
}
// V5AccountInfoResult :
type V5AccountInfoResult struct {
MarginMode MarginMode `json:"marginMode"`
UpdatedTime string `json:"updatedTime"`
UnifiedMarginStatus UnifiedMarginStatus `json:"unifiedMarginStatus"`
}
// GetAccountInfo :
func (s *V5AccountService) GetAccountInfo() (*V5GetAccountInfoResponse, error) {
var (
res V5GetAccountInfoResponse
query = make(url.Values)
)
if err := s.client.getV5Privately("/v5/account/info", query, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetTransactionLogParam :
type V5GetTransactionLogParam struct {
AccountType *AccountTypeV5 `url:"accountType,omitempty"`
Category *CategoryV5 `url:"category,omitempty"`
Currency *string `url:"currency,omitempty"`
BaseCoin *Coin `url:"baseCoin,omitempty"`
Type *TransactionLogTypeV5 `url:"type,omitempty"`
StartTime *int64 `url:"startTime,omitempty"` // The start timestamp (ms)
EndTime *int64 `url:"endTime,omitempty"` // The start timestamp (ms)
Limit *int `url:"limit,omitempty"` // Limit for data size per page. [1, 50]. Default: 20
Cursor *string `url:"cursor,omitempty"`
}
// V5GetTransactionLogResponse :
type V5GetTransactionLogResponse struct {
CommonV5Response `json:",inline"`
Result V5GetTransactionLogResult `json:"result"`
}
// V5GetTransactionLogResult :
type V5GetTransactionLogResult struct {
NextPageCursor string `json:"nextPageCursor"`
List V5GetTransactionLogList `json:"list"`
}
// V5GetTransactionLogList :
type V5GetTransactionLogList []V5GetTransactionLogItem
// V5GetTransactionLogItem :
type V5GetTransactionLogItem struct {
Symbol SymbolV5 `json:"symbol"`
Category CategoryV5 `json:"category"`
Side Side `json:"side"`
TransactionTime string `json:"transactionTime"`
Type TransactionLogTypeV5 `json:"type"`
Qty string `json:"qty"`
Size string `json:"size"`
Currency string `json:"currency"`
TradePrice string `json:"tradePrice"`
Funding string `json:"funding"`
Fee string `json:"fee"`
CashFlow string `json:"cashFlow"`
Change string `json:"change"`
CashBalance string `json:"cashBalance"`
FeeRate string `json:"feeRate"`
BonusChange string `json:"bonusChange"`
TradeID string `json:"tradeId"`
OrderID string `json:"orderId"`
OrderLinkID string `json:"orderLinkId"`
}
// GetTransactionLog :
func (s *V5AccountService) GetTransactionLog(param V5GetTransactionLogParam) (*V5GetTransactionLogResponse, error) {
var res V5GetTransactionLogResponse
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getV5Privately("/v5/account/transaction-log", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}
// V5GetFeeRateParam :
type V5GetFeeRateParam struct {
Category CategoryV5 `url:"category"`
Symbol *SymbolV5 `url:"symbol,omitempty"`
BaseCoin *Coin `url:"baseCoin,omitempty"`
}
// V5GetFeeRateResponse :
type V5GetFeeRateResponse struct {
CommonV5Response `json:",inline"`
Result V5GetFeeRateResult `json:"result"`
}
// V5GetFeeRateResult :
type V5GetFeeRateResult struct {
Category CategoryV5 `json:"category,omitempty"`
List V5GetFeeRateList `json:"list"`
}
// V5GetFeeRateList :
type V5GetFeeRateList []V5GetFeeRateItem
// V5GetFeeRateItem :
type V5GetFeeRateItem struct {
Symbol SymbolV5 `json:"symbol"`
BaseCoin Coin `json:"baseCoin"`
TakerFeeRate string `json:"takerFeeRate"`
MakerFeeRate string `json:"makerFeeRate"`
}
// GetFeeRate :
func (s *V5AccountService) GetFeeRate(param V5GetFeeRateParam) (*V5GetFeeRateResponse, error) {
var res V5GetFeeRateResponse
queryString, err := query.Values(param)
if err != nil {
return nil, err
}
if err := s.client.getV5Privately("/v5/account/fee-rate", queryString, &res); err != nil {
return nil, err
}
return &res, nil
}