/
account.go
316 lines (265 loc) · 7.55 KB
/
account.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
package spot
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/NadiaSama/ccexgo/exchange"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type (
Account struct {
ID int64 `json:"id"`
Type string `json:"type"`
State string `json:"state"`
SubType string `json:"subtype"`
}
BalanceReq struct {
AccountID int
}
Balance struct {
Currency string `json:"currency"`
Type string `json:"type"`
Balance string `json:"balance"`
SeqNum string `json:"seq-num"`
}
BalanceResp struct {
ID int `json:"id"`
Type string `json:"type"`
State string `json:"state"`
List []Balance `json:"list"`
}
AccountHistoryReq struct {
*exchange.RestReq
}
AccountHistoryData struct {
AccountID int `json:"account-id"`
Currency string `json:"currency"`
RecordID int `json:"record-id"`
TransactAmt string `json:"transact-amt"`
TransactType string `json:"transact-type"`
AvailBalance string `json:"avail-balance"`
AcctBalance string `json:"acct-balance"`
TransactTime int `json:"transact-time"`
}
AccountHistoryResp struct {
Status string `json:"status"`
NextID int `json:"next-id"`
ErrMsg string `json:"err-msg"`
ErrCode string `json:"err-code"`
Data []AccountHistoryData `json:"data"`
}
AccountLedgerReq struct {
*exchange.RestReq
}
AccountLedgerData struct {
AccountID int `json:"accountId"`
Currency string `json:"currency"`
TransactAmt float64 `json:"transactAmt"`
TransactType string `json:"transactType"`
TransferType string `json:"transferType"`
TransactID int `json:"transactId"`
TransactTime int64 `json:"transactTime"`
Transferer int `json:"transferer"`
Transferee int `json:"transferee"`
}
AccountLedgerResp struct {
Code int `json:"code"`
Message string `json:"message"`
NextID int `json:"nextId"`
OK bool `json:"ok"`
Data []AccountLedgerData `json:"data"`
}
)
const (
AccountsEndPoint = "/v1/account/accounts"
AccountHistoryEndPoint = "/v1/account/history"
AccountLedgerEndPoint = "/v2/account/ledger"
TypeFrozen = "frozen"
TypeTrade = "trade"
)
//Init spot account id for Balance request
func (rc *RestClient) Init(ctx context.Context) error {
accounts, err := rc.Accounts(ctx)
if err != nil {
return err
}
for _, ac := range accounts {
if ac.Type == "spot" {
rc.spotAccountID = int(ac.ID)
return nil
}
}
return errors.Errorf("no spot account")
}
func (rc *RestClient) Accounts(ctx context.Context) ([]Account, error) {
var ret []Account
if err := rc.Request(ctx, http.MethodGet, AccountsEndPoint, nil, nil, true, &ret); err != nil {
return nil, err
}
return ret, nil
}
func (rc *RestClient) Balance(ctx context.Context, req *BalanceReq) (*BalanceResp, error) {
endPoint := fmt.Sprintf("%s/%d/balance", AccountsEndPoint, req.AccountID)
var ret BalanceResp
if err := rc.RestClient.Request(ctx, http.MethodGet, endPoint, nil, nil, true, &ret); err != nil {
return nil, errors.WithMessage(err, "fetch balance fail")
}
return &ret, nil
}
func (rc *RestClient) FetchBalance(ctx context.Context, currencies ...string) (*exchange.Balances, error) {
if rc.spotAccountID == 0 {
return nil, errors.Errorf("client not init yet")
}
resp, err := rc.Balance(ctx, &BalanceReq{
AccountID: rc.spotAccountID,
})
if err != nil {
return nil, err
}
m := map[string]*exchange.Balance{}
for _, b := range resp.List {
currency := strings.ToUpper(b.Currency)
amount, err := decimal.NewFromString(b.Balance)
if err != nil {
return nil, errors.WithMessagef(err, "invalid balance for currency '%s'", b.Currency)
}
bal, ok := m[currency]
if !ok {
bal = &exchange.Balance{
Currency: currency,
}
m[currency] = bal
}
if b.Type == TypeFrozen {
bal.Frozen = bal.Frozen.Add(amount)
bal.Total = bal.Total.Add(amount)
} else if b.Type == TypeTrade {
bal.Free = bal.Free.Add(amount)
bal.Total = bal.Total.Add(amount)
} else {
return nil, errors.Errorf("unsupport balance type currency '%s' type '%s'", b.Currency, b.Type)
}
}
ret := exchange.NewBalances()
ret.Raw = resp
if len(currencies) != 0 {
for _, c := range currencies {
c = strings.ToUpper(c)
bal, ok := m[c]
if !ok {
ret.Balances[c] = &exchange.Balance{
Currency: c,
}
} else {
ret.Balances[c] = bal
}
}
} else {
for k, v := range m {
ret.Balances[k] = v
}
}
return ret, nil
}
func NewAccountHistoryReq(uid int) *AccountHistoryReq {
req := exchange.NewRestReq()
req.AddFields("account-id", uid)
return &AccountHistoryReq{
RestReq: req,
}
}
func (ar *AccountHistoryReq) Currency(cy string) *AccountHistoryReq {
ar.AddFields("currency", cy)
return ar
}
func (ar *AccountHistoryReq) TransactTypes(types ...string) *AccountHistoryReq {
typ := strings.Join(types, ",")
ar.AddFields("transact-types", typ)
return ar
}
func (ar *AccountHistoryReq) AddTime(ts time.Time) *AccountHistoryReq {
ar.AddFields("start-time", ts.Unix()*1000)
return ar
}
func (ar *AccountHistoryReq) EndTime(ts time.Time) *AccountHistoryReq {
ar.AddFields("end-time", ts.Unix()*1000)
return ar
}
func (ar *AccountHistoryReq) Sort(sort string) *AccountHistoryReq {
ar.AddFields("sort", sort)
return ar
}
func (ar *AccountHistoryReq) Size(size int) *AccountHistoryReq {
ar.AddFields("size", size)
return ar
}
func (ar *AccountHistoryReq) FromID(id int) *AccountHistoryReq {
ar.AddFields("from-id", id)
return ar
}
func (rc *RestClient) AccountHistory(ctx context.Context, req *AccountHistoryReq) (*AccountHistoryResp, error) {
values, err := req.Values()
if err != nil {
return nil, errors.WithMessage(err, "build request param fail")
}
var ret AccountHistoryResp
if err := rc.RequestWithRawResp(ctx, http.MethodGet, AccountHistoryEndPoint, values, nil, true, &ret); err != nil {
return nil, errors.WithMessage(err, "request failed")
}
if ret.Status != "ok" {
return nil, errors.Errorf("reqeust response failed %+v", ret)
}
return &ret, nil
}
func NewAccountLedgerReq(uid int) *AccountLedgerReq {
req := exchange.NewRestReq()
req.AddFields("accountId", uid)
return &AccountLedgerReq{
RestReq: req,
}
}
func (ar *AccountLedgerReq) Currency(cy string) *AccountLedgerReq {
ar.AddFields("currency", cy)
return ar
}
func (ar *AccountLedgerReq) TransactTypes(typ string) *AccountLedgerReq {
ar.AddFields("transactTypes", typ)
return ar
}
func (ar *AccountLedgerReq) StartTime(st time.Time) *AccountLedgerReq {
ar.AddFields("startTime", st.Unix()*1000)
return ar
}
func (ar *AccountLedgerReq) EndTime(et time.Time) *AccountLedgerReq {
ar.AddFields("endTime", et.Unix()*1000)
return ar
}
func (ar *AccountLedgerReq) Sort(s string) *AccountLedgerReq {
ar.AddFields("sort", s)
return ar
}
func (ar *AccountLedgerReq) Limit(limit int) *AccountLedgerReq {
ar.AddFields("limit", limit)
return ar
}
func (ar *AccountLedgerReq) FromID(id int) *AccountLedgerReq {
ar.AddFields("fromId", id)
return ar
}
func (rc *RestClient) AccountLedger(ctx context.Context, req *AccountLedgerReq) (*AccountLedgerResp, error) {
values, err := req.Values()
if err != nil {
return nil, errors.WithMessage(err, "build request param fail")
}
var ret AccountLedgerResp
if err := rc.RequestWithRawResp(ctx, http.MethodGet, AccountLedgerEndPoint, values, nil, true, &ret); err != nil {
return nil, errors.WithMessage(err, "build request fail")
}
if !ret.OK {
return nil, errors.Errorf("response error %+v", ret)
}
return &ret, nil
}