forked from adshao/go-binance
/
subaccount_service.go
338 lines (298 loc) · 8.03 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
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"`
}