Skip to content

Commit 5824a29

Browse files
authored
complete options module (#568)
* complete options module. imple interfaces that are not impled before. add the websocket implement. add unittest inlcude lost and new * remove unreachable code * fix some unittest * correct some name * change OrderID to OrderId * set RateLimitOrder10s RateLimitOrder1m in CreateBatchOrders
1 parent 5c4ac82 commit 5824a29

26 files changed

+5305
-472
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Simply call API in chain style. Call Do() in the end to send HTTP request.
7070

7171
Following are some simple examples, please refer to [godoc](https://godoc.org/github.com/adshao/go-binance) for full references.
7272

73+
If you have any questions, please refer to the specific version of the code for specific reference definitions or usage methods
74+
7375
#### Create Order
7476

7577
```golang

v2/options/account_service.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package options
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
)
8+
9+
// AccountService create order
10+
type AccountService struct {
11+
c *Client
12+
}
13+
14+
// Do send request
15+
func (s *AccountService) Do(ctx context.Context, opts ...RequestOption) (res *Account, err error) {
16+
r := &request{
17+
method: http.MethodGet,
18+
endpoint: "/eapi/v1/account",
19+
secType: secTypeSigned,
20+
}
21+
22+
data, _, err := s.c.callAPI(ctx, r, opts...)
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
res = new(Account)
28+
err = json.Unmarshal(data, res)
29+
30+
if err != nil {
31+
return nil, err
32+
}
33+
return res, nil
34+
}
35+
36+
type Asset struct {
37+
Asset string `json:"asset"`
38+
MarginBalance string `json:"marginBalance"`
39+
Equity string `json:"equity"`
40+
Available string `json:"available"`
41+
Locked string `json:"locked"`
42+
UnrealizedPNL string `json:"unrealizedPNL"`
43+
}
44+
45+
type Greek struct {
46+
Underlying string `json:"underlying"`
47+
Delta string `json:"delta"`
48+
Gamma string `json:"gamma"`
49+
Theta string `json:"theta"`
50+
Vega string `json:"vega"`
51+
}
52+
53+
// Account define create order response
54+
type Account struct {
55+
Asset []*Asset `json:"asset"`
56+
Greek []*Greek `json:"greek"`
57+
RiskLevel string `json:"riskLevel"`
58+
Time uint64 `json:"time"`
59+
}

v2/options/account_service_test.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package options
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/suite"
7+
)
8+
9+
type AccountServiceTestSuite struct {
10+
baseTestSuite
11+
}
12+
13+
func TestAccountService(t *testing.T) {
14+
suite.Run(t, new(AccountServiceTestSuite))
15+
}
16+
17+
func (s *AccountServiceTestSuite) TestAccount() {
18+
data := []byte(`{
19+
"asset": [
20+
{
21+
"asset": "USDT",
22+
"marginBalance": "1.73304384",
23+
"equity": "1.73304384",
24+
"available": "1.73304384",
25+
"locked": "0",
26+
"unrealizedPNL": "0"
27+
}
28+
],
29+
"greek":[{
30+
"underlying": "BTC",
31+
"delta": "1",
32+
"gamma": "0.01",
33+
"theta": "0.001",
34+
"vega": "0.0001"
35+
}],
36+
"time": 1717037493533,
37+
"riskLevel": "NORMAL"
38+
}`)
39+
s.mockDo(data, nil)
40+
defer s.assertDo()
41+
42+
account, err := s.client.NewAccountService().Do(newContext())
43+
targetAccount := &Account{
44+
Asset: []*Asset{
45+
{
46+
Asset: "USDT",
47+
MarginBalance: "1.73304384",
48+
Equity: "1.73304384",
49+
Available: "1.73304384",
50+
Locked: "0",
51+
UnrealizedPNL: "0",
52+
},
53+
},
54+
Greek: []*Greek{
55+
{
56+
Underlying: "BTC",
57+
Delta: "1",
58+
Gamma: "0.01",
59+
Theta: "0.001",
60+
Vega: "0.0001",
61+
},
62+
},
63+
Time: 1717037493533,
64+
RiskLevel: "NORMAL",
65+
}
66+
67+
s.r().Equal(err, nil, "err != nil")
68+
69+
r := s.r()
70+
for i := range account.Asset {
71+
r.Equal(account.Asset[i].Asset, targetAccount.Asset[i].Asset, "Asset.Asset")
72+
r.Equal(account.Asset[i].MarginBalance, targetAccount.Asset[i].MarginBalance, "Asset.MarginBalance")
73+
r.Equal(account.Asset[i].Equity, targetAccount.Asset[i].Equity, "Asset.Equity")
74+
r.Equal(account.Asset[i].Available, targetAccount.Asset[i].Available, "Asset.Available")
75+
r.Equal(account.Asset[i].Locked, targetAccount.Asset[i].Locked, "Asset.Locked")
76+
r.Equal(account.Asset[i].UnrealizedPNL, targetAccount.Asset[i].UnrealizedPNL, "Asset.UnrealizedPNL")
77+
}
78+
for i := range account.Greek {
79+
r.Equal(account.Greek[i].Underlying, targetAccount.Greek[i].Underlying, "Asset.Underlying")
80+
r.Equal(account.Greek[i].Delta, targetAccount.Greek[i].Delta, "Asset.Delta")
81+
r.Equal(account.Greek[i].Gamma, targetAccount.Greek[i].Gamma, "Asset.Gamma")
82+
r.Equal(account.Greek[i].Theta, targetAccount.Greek[i].Theta, "Asset.Theta")
83+
r.Equal(account.Greek[i].Vega, targetAccount.Greek[i].Vega, "Asset.Vega")
84+
}
85+
r.Equal(account.Time, targetAccount.Time, "Time")
86+
r.Equal(account.RiskLevel, targetAccount.RiskLevel, "RiskLevel")
87+
88+
}

v2/options/client.go

+126-20
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
327327
}
328328
req = req.WithContext(ctx)
329329
req.Header = r.header
330-
c.debug("request: %#v", req)
330+
c.debug("request: %#v\n", req)
331331
f := c.do
332332
if f == nil {
333333
f = c.HTTPClient.Do
@@ -348,15 +348,17 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
348348
err = cerr
349349
}
350350
}()
351-
c.debug("response: %#v", res)
352-
c.debug("response body: %s", string(data))
353-
c.debug("response status code: %d", res.StatusCode)
351+
c.debug("response: %#v\n", res)
352+
c.debug("response body: %s\n", string(data))
353+
c.debug("response status code: %d\n", res.StatusCode)
354354

355355
if res.StatusCode >= http.StatusBadRequest {
356356
apiErr := new(common.APIError)
357357
e := json.Unmarshal(data, apiErr)
358358
if e != nil {
359-
c.debug("failed to unmarshal json: %s", e)
359+
c.debug("failed to unmarshal json: %s\n", e)
360+
apiErr.Code = int64(res.StatusCode)
361+
apiErr.Message = string(data)
360362
}
361363
return nil, &http.Header{}, apiErr
362364
}
@@ -369,52 +371,156 @@ func (c *Client) SetApiEndpoint(url string) *Client {
369371
return c
370372
}
371373

372-
// NewKlinesService init klines service
373-
func (c *Client) NewKlinesService() *KlinesService {
374-
return &KlinesService{c: c}
374+
// ping server
375+
func (c *Client) NewPingService() *PingService {
376+
return &PingService{c: c}
375377
}
376378

377-
// NewDepthService init depth service
378-
func (c *Client) NewDepthService() *DepthService {
379-
return &DepthService{c: c}
379+
// get timestamp(ms) of server
380+
func (c *Client) NewServerTimeService() *ServerTimeService {
381+
return &ServerTimeService{c: c}
380382
}
381383

382384
// NewExchangeInfoService init exchange info service
383385
func (c *Client) NewExchangeInfoService() *ExchangeInfoService {
384386
return &ExchangeInfoService{c: c}
385387
}
386388

389+
// NewDepthService init depth service
390+
func (c *Client) NewDepthService() *DepthService {
391+
return &DepthService{c: c}
392+
}
393+
394+
func (c *Client) NewTradesService() *TradesService {
395+
return &TradesService{c: c}
396+
}
397+
398+
func (c *Client) NewHistoricalTradesService() *HistoricalTradesService {
399+
return &HistoricalTradesService{c: c}
400+
}
401+
402+
// NewKlinesService init klines service
403+
func (c *Client) NewKlinesService() *KlinesService {
404+
return &KlinesService{c: c}
405+
}
406+
407+
func (c *Client) NewMarkService() *MarkService {
408+
return &MarkService{c: c}
409+
}
410+
411+
func (c *Client) NewTickerService() *TickerService {
412+
return &TickerService{c: c}
413+
}
414+
415+
func (c *Client) NewIndexService() *IndexService {
416+
return &IndexService{c: c}
417+
}
418+
419+
func (c *Client) NewExerciseHistoryService() *ExerciseHistoryService {
420+
return &ExerciseHistoryService{c: c}
421+
}
422+
423+
func (c *Client) NewOpenInterestService() *OpenInterestService {
424+
return &OpenInterestService{c: c}
425+
}
426+
427+
func (c *Client) NewAccountService() *AccountService {
428+
return &AccountService{c: c}
429+
}
430+
387431
// NewCreateOrderService init creating order service
432+
// POST /eapi/v1/order
388433
func (c *Client) NewCreateOrderService() *CreateOrderService {
389434
return &CreateOrderService{c: c}
390435
}
391436

392-
// NewListOpenOrdersService init list open orders service
393-
func (c *Client) NewListOpenOrdersService() *ListOpenOrdersService {
394-
return &ListOpenOrdersService{c: c}
437+
// NewCreateBatchOrdersService init creating batch order service
438+
// POST /eapi/v1/batchOrders
439+
func (c *Client) NewCreateBatchOrdersService() *CreateBatchOrdersService {
440+
return &CreateBatchOrdersService{c: c}
395441
}
396442

397443
// NewGetOrderService init get order service
444+
// GET /eapi/v1/order
398445
func (c *Client) NewGetOrderService() *GetOrderService {
399446
return &GetOrderService{c: c}
400447
}
401448

402449
// NewCancelOrderService init cancel order service
450+
// DELETE /eapi/v1/order
403451
func (c *Client) NewCancelOrderService() *CancelOrderService {
404452
return &CancelOrderService{c: c}
405453
}
406454

455+
// NewCancelBatchOrdersService init cancel multiple orders service
456+
// DELETE /eapi/v1/batchOrders
457+
func (c *Client) NewCancelBatchOrdersService() *CancelBatchOrdersService {
458+
return &CancelBatchOrdersService{c: c}
459+
}
460+
407461
// NewCancelAllOpenOrdersService init cancel all open orders service
462+
// DELETE /eapi/v1/allOpenOrders
408463
func (c *Client) NewCancelAllOpenOrdersService() *CancelAllOpenOrdersService {
409464
return &CancelAllOpenOrdersService{c: c}
410465
}
411466

412-
// NewCancelMultipleOrdersService init cancel multiple orders service
413-
func (c *Client) NewCancelMultipleOrdersService() *CancelMultiplesOrdersService {
414-
return &CancelMultiplesOrdersService{c: c}
467+
// DELETE /eapi/v1/allOpenOrdersByUnderlying
468+
func (c *Client) NewCancelAllOpenOrdersByUnderlyingService() *CancelAllOpenOrdersByUnderlyingService {
469+
return &CancelAllOpenOrdersByUnderlyingService{c: c}
415470
}
416471

417-
// NewCreateBatchOrdersService init creating batch order service
418-
func (c *Client) NewCreateBatchOrdersService() *CreateBatchOrdersService {
419-
return &CreateBatchOrdersService{c: c}
472+
// NewListOpenOrdersService init list open orders service
473+
// GET /eapi/v1/openOrders
474+
func (c *Client) NewListOpenOrdersService() *ListOpenOrdersService {
475+
return &ListOpenOrdersService{c: c}
476+
}
477+
478+
// GET /eapi/v1/historyOrders
479+
func (c *Client) NewHistoryOrdersService() *HistoryOrdersService {
480+
return &HistoryOrdersService{c: c}
481+
}
482+
483+
// GET /eapi/v1/position
484+
func (c *Client) NewPositionService() *PositionService {
485+
return &PositionService{c: c}
486+
}
487+
488+
// GET /eapi/v1/userTrades
489+
func (c *Client) NewUserTradesService() *UserTradesService {
490+
return &UserTradesService{c: c}
491+
}
492+
493+
// GET /eapi/v1/exerciseRecord
494+
func (c *Client) NewExercistRecordService() *ExerciseRecordService {
495+
return &ExerciseRecordService{c: c}
496+
}
497+
498+
// GET /eapi/v1/bill
499+
// Obtain account fund flow
500+
func (c *Client) NewBillService() *BillService {
501+
return &BillService{c: c}
502+
}
503+
504+
// GET /eapi/v1/income/asyn
505+
// Get option fund flow download ID
506+
func (c *Client) NewIncomeDownloadIdService() *IncomeDownloadIdService {
507+
return &IncomeDownloadIdService{c: c}
508+
}
509+
510+
// GET /eapi/v1/income/asyn/id
511+
// Obtain the contract fund flow download link through the download ID
512+
func (c *Client) NewIncomeDownloadLinkService() *IncomeDownloadLinkService {
513+
return &IncomeDownloadLinkService{c: c}
514+
}
515+
516+
func (c *Client) NewStartUserStreamService() *StartUserStreamService {
517+
return &StartUserStreamService{c: c}
518+
}
519+
520+
func (c *Client) NewKeepaliveUserStreamService() *KeepaliveUserStreamService {
521+
return &KeepaliveUserStreamService{c: c}
522+
}
523+
524+
func (c *Client) NewCloseUserStreamService() *CloseUserStreamService {
525+
return &CloseUserStreamService{c: c}
420526
}

0 commit comments

Comments
 (0)