forked from adshao/go-binance
/
subaccount_service.go
499 lines (430 loc) · 12.4 KB
/
subaccount_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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package binance
import (
"context"
)
// TransferToSubAccountService transfer to subaccount
type TransferToSubAccountService struct {
c *Client
toEmail string
asset string
amount string
}
// ToEmail set toEmail
func (s *TransferToSubAccountService) ToEmail(toEmail string) *TransferToSubAccountService {
s.toEmail = toEmail
return s
}
// Asset set asset
func (s *TransferToSubAccountService) Asset(asset string) *TransferToSubAccountService {
s.asset = asset
return s
}
// Amount set amount
func (s *TransferToSubAccountService) Amount(amount string) *TransferToSubAccountService {
s.amount = amount
return s
}
func (s *TransferToSubAccountService) transferToSubaccount(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, err error) {
r := &request{
method: "POST",
endpoint: endpoint,
secType: secTypeSigned,
}
m := params{
"toEmail": s.toEmail,
"asset": s.asset,
"amount": s.amount,
}
r.setParams(m)
data, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return []byte{}, err
}
return data, nil
}
// Do send request
func (s *TransferToSubAccountService) Do(ctx context.Context, opts ...RequestOption) (res *TransferToSubAccountResponse, err error) {
data, err := s.transferToSubaccount(ctx, "/sapi/v1/sub-account/transfer/subToSub", opts...)
if err != nil {
return nil, err
}
res = &TransferToSubAccountResponse{}
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
// TransferToSubAccountResponse define transfer to subaccount response
type TransferToSubAccountResponse struct {
TxnID int64 `json:"txnId"`
}
type SubaccountDepositAddressService struct {
c *Client
email string
coin string
network string
}
// Email set email
func (s *SubaccountDepositAddressService) Email(email string) *SubaccountDepositAddressService {
s.email = email
return s
}
// Coin set coin
func (s *SubaccountDepositAddressService) Coin(coin string) *SubaccountDepositAddressService {
s.coin = coin
return s
}
// Network set network
func (s *SubaccountDepositAddressService) Network(network string) *SubaccountDepositAddressService {
s.network = network
return s
}
func (s *SubaccountDepositAddressService) subaccountDepositAddress(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, err error) {
r := &request{
method: "GET",
endpoint: endpoint,
secType: secTypeSigned,
}
m := params{
"email": s.email,
"coin": s.coin,
"network": s.network,
}
r.setParams(m)
data, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return []byte{}, err
}
return data, nil
}
// Do send request
func (s *SubaccountDepositAddressService) Do(ctx context.Context, opts ...RequestOption) (res *SubaccountDepositAddressResponse, err error) {
data, err := s.subaccountDepositAddress(ctx, "/sapi/v1/capital/deposit/subAddress", opts...)
if err != nil {
return nil, err
}
res = &SubaccountDepositAddressResponse{}
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
type SubaccountDepositAddressResponse struct {
Address string `json:"address"`
Coin string `json:"coin"`
Tag string `json:"tag"`
URL string `json:"url"`
}
type SubaccountAssetsService struct {
c *Client
email string
}
// Email set email
func (s *SubaccountAssetsService) Email(email string) *SubaccountAssetsService {
s.email = email
return s
}
func (s *SubaccountAssetsService) subaccountAssets(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, err error) {
r := &request{
method: "GET",
endpoint: endpoint,
secType: secTypeSigned,
}
m := params{
"email": s.email,
}
r.setParams(m)
data, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return []byte{}, err
}
return data, nil
}
// Do send request
func (s *SubaccountAssetsService) Do(ctx context.Context, opts ...RequestOption) (res *SubaccountAssetsResponse, err error) {
data, err := s.subaccountAssets(ctx, "/sapi/v3/sub-account/assets", opts...)
if err != nil {
return nil, err
}
res = &SubaccountAssetsResponse{}
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
// SubaccountAssetsResponse Query Sub-account Assets response
type SubaccountAssetsResponse struct {
Balances []AssetBalance `json:"balances"`
}
type AssetBalance struct {
Asset string `json:"asset"`
Free float64 `json:"free"`
Locked float64 `json:"locked"`
}
type SubaccountSpotSummaryService struct {
c *Client
email *string
page *int32
size *int32
}
// Email set email
func (s *SubaccountSpotSummaryService) Email(email string) *SubaccountSpotSummaryService {
s.email = &email
return s
}
func (s *SubaccountSpotSummaryService) Page(page int32) *SubaccountSpotSummaryService {
s.page = &page
return s
}
func (s *SubaccountSpotSummaryService) Size(size int32) *SubaccountSpotSummaryService {
s.size = &size
return s
}
func (s *SubaccountSpotSummaryService) subaccountSpotSummary(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, err error) {
r := &request{
method: "GET",
endpoint: endpoint,
secType: secTypeSigned,
}
if s.size != nil {
r.setParam("size", *s.size)
}
if s.page != nil {
r.setParam("page", *s.page)
}
if s.email != nil {
r.setParam("email", *s.email)
}
data, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return []byte{}, err
}
return data, nil
}
// Do send request
func (s *SubaccountSpotSummaryService) Do(ctx context.Context, opts ...RequestOption) (res *SubaccountSpotSummaryResponse, err error) {
data, err := s.subaccountSpotSummary(ctx, "/sapi/v1/sub-account/spotSummary", opts...)
if err != nil {
return nil, err
}
res = &SubaccountSpotSummaryResponse{}
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
// SubaccountSpotSummaryResponse Query Sub-account Spot Assets Summary response
type SubaccountSpotSummaryResponse struct {
TotalCount int64 `json:"totalCount"`
MasterAccountTotalAsset string `json:"masterAccountTotalAsset"`
SpotSubUserAssetBtcVoList []SpotSubUserAssetBtcVoList `json:"spotSubUserAssetBtcVoList"`
}
type SpotSubUserAssetBtcVoList struct {
Email string `json:"email"`
TotalAsset string `json:"totalAsset"`
}
// SubAccountListService Query Sub-account List (For Master Account)
// https://binance-docs.github.io/apidocs/spot/en/#query-sub-account-list-for-master-account
type SubAccountListService struct {
c *Client
email *string
isFreeze bool
page, limit int
}
func (s *SubAccountListService) Email(v string) *SubAccountListService {
s.email = &v
return s
}
func (s *SubAccountListService) IsFreeze(v bool) *SubAccountListService {
s.isFreeze = v
return s
}
func (s *SubAccountListService) Page(v int) *SubAccountListService {
s.page = v
return s
}
func (s *SubAccountListService) Limit(v int) *SubAccountListService {
s.limit = v
return s
}
func (s *SubAccountListService) Do(ctx context.Context, opts ...RequestOption) (res *SubAccountList, err error) {
r := &request{
method: "GET",
endpoint: "/sapi/v1/sub-account/list",
secType: secTypeSigned,
}
if s.email != nil {
r.setParam("email", *s.email)
}
if s.isFreeze {
r.setParam("isFreeze", "true")
} else {
r.setParam("isFreeze", "false")
}
if s.page > 0 {
r.setParam("page", s.page)
}
if s.limit > 200 {
r.setParam("limit", 200)
} else if s.limit <= 0 {
r.setParam("limit", 10)
} else {
r.setParam("limit", s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(SubAccountList)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}
type SubAccountList struct {
SubAccounts []SubAccount `json:"subAccounts"`
}
type SubAccount struct {
Email string `json:"email"`
IsFreeze bool `json:"isFreeze"`
CreateTime uint64 `json:"createTime"`
IsManagedSubAccount bool `json:"isManagedSubAccount"`
IsAssetManagementSubAccount bool `json:"isAssetManagementSubAccount"`
}
// ManagedSubAccountDepositService
// Deposit Assets Into The Managed Sub-account(For Investor Master Account)
// https://binance-docs.github.io/apidocs/spot/en/#deposit-assets-into-the-managed-sub-account-for-investor-master-account
type ManagedSubAccountDepositService struct {
c *Client
toEmail string
asset string
amount float64
}
func (s *ManagedSubAccountDepositService) ToEmail(email string) *ManagedSubAccountDepositService {
s.toEmail = email
return s
}
func (s *ManagedSubAccountDepositService) Asset(asset string) *ManagedSubAccountDepositService {
s.asset = asset
return s
}
func (s *ManagedSubAccountDepositService) Amount(amount float64) *ManagedSubAccountDepositService {
s.amount = amount
return s
}
type ManagedSubAccountDepositResponse struct {
ID int64 `json:"tranId"`
}
// Do send request
func (s *ManagedSubAccountDepositService) Do(ctx context.Context, opts ...RequestOption) (*ManagedSubAccountDepositResponse, error) {
r := &request{
method: "POST",
endpoint: "/sapi/v1/managed-subaccount/deposit",
secType: secTypeSigned,
}
r.setParam("toEmail", s.toEmail)
r.setParam("asset", s.asset)
r.setParam("amount", s.amount)
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res := &ManagedSubAccountDepositResponse{}
if err := json.Unmarshal(data, res); err != nil {
return nil, err
}
return res, nil
}
// ManagedSubAccountWithdrawalService
// Withdrawal Assets From The Managed Sub-account(For Investor Master Account)
// https://binance-docs.github.io/apidocs/spot/en/#withdrawl-assets-from-the-managed-sub-account-for-investor-master-account
type ManagedSubAccountWithdrawalService struct {
c *Client
fromEmail string
asset string
amount float64
transferDate int64 // Withdrawals is automatically occur on the transfer date(UTC0). If a date is not selected, the withdrawal occurs right now
}
func (s *ManagedSubAccountWithdrawalService) FromEmail(email string) *ManagedSubAccountWithdrawalService {
s.fromEmail = email
return s
}
func (s *ManagedSubAccountWithdrawalService) Asset(asset string) *ManagedSubAccountWithdrawalService {
s.asset = asset
return s
}
func (s *ManagedSubAccountWithdrawalService) Amount(amount float64) *ManagedSubAccountWithdrawalService {
s.amount = amount
return s
}
func (s *ManagedSubAccountWithdrawalService) TransferDate(val int64) *ManagedSubAccountWithdrawalService {
s.transferDate = val
return s
}
type ManagedSubAccountWithdrawalResponse struct {
ID int64 `json:"tranId"`
}
// Do send request
func (s *ManagedSubAccountWithdrawalService) Do(ctx context.Context, opts ...RequestOption) (*ManagedSubAccountWithdrawalResponse, error) {
r := &request{
method: "POST",
endpoint: "/sapi/v1/managed-subaccount/withdraw",
secType: secTypeSigned,
}
r.setParam("fromEmail", s.fromEmail)
r.setParam("asset", s.asset)
r.setParam("amount", s.amount)
if s.transferDate > 0 {
r.setParam("transferDate", s.transferDate)
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res := &ManagedSubAccountWithdrawalResponse{}
if err := json.Unmarshal(data, res); err != nil {
return nil, err
}
return res, nil
}
// ManagedSubAccountAssetsService
// Query Managed Sub-account Asset Details(For Investor Master Account)
// https://binance-docs.github.io/apidocs/spot/en/#query-managed-sub-account-asset-details-for-investor-master-account
type ManagedSubAccountAssetsService struct {
c *Client
email string
}
func (s *ManagedSubAccountAssetsService) Email(email string) *ManagedSubAccountAssetsService {
s.email = email
return s
}
type ManagedSubAccountAsset struct {
Coin string `json:"coin"`
Name string `json:"name"`
TotalBalance string `json:"totalBalance"`
AvailableBalance string `json:"availableBalance"`
InOrder string `json:"inOrder"`
BtcValue string `json:"btcValue"`
}
func (s *ManagedSubAccountAssetsService) Do(ctx context.Context, opts ...RequestOption) ([]*ManagedSubAccountAsset, error) {
r := &request{
method: "GET",
endpoint: "/sapi/v1/managed-subaccount/asset",
secType: secTypeSigned,
}
r.setParam("email", s.email)
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res := make([]*ManagedSubAccountAsset, 0)
if err := json.Unmarshal(data, &res); err != nil {
return nil, err
}
return res, nil
}