Skip to content

Commit

Permalink
complete options module. imple interfaces that are not impled before.
Browse files Browse the repository at this point in the history
add the websocket implement. add unittest inlcude lost and new
  • Loading branch information
xyq-c-cpp committed Jun 9, 2024
1 parent 6d8559f commit 66d76db
Show file tree
Hide file tree
Showing 27 changed files with 5,300 additions and 472 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Simply call API in chain style. Call Do() in the end to send HTTP request.

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

If you have any questions, please refer to the specific version of the code for specific reference definitions or usage methods

#### Create Order

```golang
Expand Down
59 changes: 59 additions & 0 deletions v2/options/account_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package options

import (
"context"
"encoding/json"
"net/http"
)

// AccountService create order
type AccountService struct {
c *Client
}

// Do send request
func (s *AccountService) Do(ctx context.Context, opts ...RequestOption) (res *Account, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/eapi/v1/account",
secType: secTypeSigned,
}

data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}

res = new(Account)
err = json.Unmarshal(data, res)

if err != nil {
return nil, err
}
return res, nil
}

type Asset struct {
Asset string `json:"asset"`
MarginBalance string `json:"marginBalance"`
Equity string `json:"equity"`
Available string `json:"available"`
Locked string `json:"locked"`
UnrealizedPNL string `json:"unrealizedPNL"`
}

type Greek struct {
Underlying string `json:"underlying"`
Delta string `json:"delta"`
Gamma string `json:"gamma"`
Theta string `json:"theta"`
Vega string `json:"vega"`
}

// Account define create order response
type Account struct {
Asset []*Asset `json:"asset"`
Greek []*Greek `json:"greek"`
RiskLevel string `json:"riskLevel"`
Time uint64 `json:"time"`
}
88 changes: 88 additions & 0 deletions v2/options/account_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package options

import (
"testing"

"github.com/stretchr/testify/suite"
)

type AccountServiceTestSuite struct {
baseTestSuite
}

func TestAccountService(t *testing.T) {
suite.Run(t, new(AccountServiceTestSuite))
}

func (s *AccountServiceTestSuite) TestAccount() {
data := []byte(`{
"asset": [
{
"asset": "USDT",
"marginBalance": "1.73304384",
"equity": "1.73304384",
"available": "1.73304384",
"locked": "0",
"unrealizedPNL": "0"
}
],
"greek":[{
"underlying": "BTC",
"delta": "1",
"gamma": "0.01",
"theta": "0.001",
"vega": "0.0001"
}],
"time": 1717037493533,
"riskLevel": "NORMAL"
}`)
s.mockDo(data, nil)
defer s.assertDo()

account, err := s.client.NewAccountService().Do(newContext())
targetAccount := &Account{
Asset: []*Asset{
{
Asset: "USDT",
MarginBalance: "1.73304384",
Equity: "1.73304384",
Available: "1.73304384",
Locked: "0",
UnrealizedPNL: "0",
},
},
Greek: []*Greek{
{
Underlying: "BTC",
Delta: "1",
Gamma: "0.01",
Theta: "0.001",
Vega: "0.0001",
},
},
Time: 1717037493533,
RiskLevel: "NORMAL",
}

s.r().Equal(err, nil, "err != nil")

r := s.r()
for i := range account.Asset {
r.Equal(account.Asset[i].Asset, targetAccount.Asset[i].Asset, "Asset.Asset")
r.Equal(account.Asset[i].MarginBalance, targetAccount.Asset[i].MarginBalance, "Asset.MarginBalance")
r.Equal(account.Asset[i].Equity, targetAccount.Asset[i].Equity, "Asset.Equity")
r.Equal(account.Asset[i].Available, targetAccount.Asset[i].Available, "Asset.Available")
r.Equal(account.Asset[i].Locked, targetAccount.Asset[i].Locked, "Asset.Locked")
r.Equal(account.Asset[i].UnrealizedPNL, targetAccount.Asset[i].UnrealizedPNL, "Asset.UnrealizedPNL")
}
for i := range account.Greek {
r.Equal(account.Greek[i].Underlying, targetAccount.Greek[i].Underlying, "Asset.Underlying")
r.Equal(account.Greek[i].Delta, targetAccount.Greek[i].Delta, "Asset.Delta")
r.Equal(account.Greek[i].Gamma, targetAccount.Greek[i].Gamma, "Asset.Gamma")
r.Equal(account.Greek[i].Theta, targetAccount.Greek[i].Theta, "Asset.Theta")
r.Equal(account.Greek[i].Vega, targetAccount.Greek[i].Vega, "Asset.Vega")
}
r.Equal(account.Time, targetAccount.Time, "Time")
r.Equal(account.RiskLevel, targetAccount.RiskLevel, "RiskLevel")

}
146 changes: 126 additions & 20 deletions v2/options/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
}
req = req.WithContext(ctx)
req.Header = r.header
c.debug("request: %#v", req)
c.debug("request: %#v\n", req)
f := c.do
if f == nil {
f = c.HTTPClient.Do
Expand All @@ -348,15 +348,17 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
err = cerr
}
}()
c.debug("response: %#v", res)
c.debug("response body: %s", string(data))
c.debug("response status code: %d", res.StatusCode)
c.debug("response: %#v\n", res)
c.debug("response body: %s\n", string(data))
c.debug("response status code: %d\n", res.StatusCode)

if res.StatusCode >= http.StatusBadRequest {
apiErr := new(common.APIError)
e := json.Unmarshal(data, apiErr)
if e != nil {
c.debug("failed to unmarshal json: %s", e)
c.debug("failed to unmarshal json: %s\n", e)
apiErr.Code = int64(res.StatusCode)
apiErr.Message = string(data)
}
return nil, &http.Header{}, apiErr
}
Expand All @@ -369,52 +371,156 @@ func (c *Client) SetApiEndpoint(url string) *Client {
return c
}

// NewKlinesService init klines service
func (c *Client) NewKlinesService() *KlinesService {
return &KlinesService{c: c}
// ping server
func (c *Client) NewPingService() *PingService {
return &PingService{c: c}
}

// NewDepthService init depth service
func (c *Client) NewDepthService() *DepthService {
return &DepthService{c: c}
// get timestamp(ms) of server
func (c *Client) NewServerTimeService() *ServerTimeService {
return &ServerTimeService{c: c}
}

// NewExchangeInfoService init exchange info service
func (c *Client) NewExchangeInfoService() *ExchangeInfoService {
return &ExchangeInfoService{c: c}
}

// NewDepthService init depth service
func (c *Client) NewDepthService() *DepthService {
return &DepthService{c: c}
}

func (c *Client) NewTradesService() *TradesService {
return &TradesService{c: c}
}

func (c *Client) NewHistoricalTradesService() *HistoricalTradesService {
return &HistoricalTradesService{c: c}
}

// NewKlinesService init klines service
func (c *Client) NewKlinesService() *KlinesService {
return &KlinesService{c: c}
}

func (c *Client) NewMarkService() *MarkService {
return &MarkService{c: c}
}

func (c *Client) NewTickerService() *TickerService {
return &TickerService{c: c}
}

func (c *Client) NewIndexService() *IndexService {
return &IndexService{c: c}
}

func (c *Client) NewExerciseHistoryService() *ExerciseHistoryService {
return &ExerciseHistoryService{c: c}
}

func (c *Client) NewOpenInterestService() *OpenInterestService {
return &OpenInterestService{c: c}
}

func (c *Client) NewAccountService() *AccountService {
return &AccountService{c: c}
}

// NewCreateOrderService init creating order service
// POST /eapi/v1/order
func (c *Client) NewCreateOrderService() *CreateOrderService {
return &CreateOrderService{c: c}
}

// NewListOpenOrdersService init list open orders service
func (c *Client) NewListOpenOrdersService() *ListOpenOrdersService {
return &ListOpenOrdersService{c: c}
// NewCreateBatchOrdersService init creating batch order service
// POST /eapi/v1/batchOrders
func (c *Client) NewCreateBatchOrdersService() *CreateBatchOrdersService {
return &CreateBatchOrdersService{c: c}
}

// NewGetOrderService init get order service
// GET /eapi/v1/order
func (c *Client) NewGetOrderService() *GetOrderService {
return &GetOrderService{c: c}
}

// NewCancelOrderService init cancel order service
// DELETE /eapi/v1/order
func (c *Client) NewCancelOrderService() *CancelOrderService {
return &CancelOrderService{c: c}
}

// NewCancelBatchOrdersService init cancel multiple orders service
// DELETE /eapi/v1/batchOrders
func (c *Client) NewCancelBatchOrdersService() *CancelBatchOrdersService {
return &CancelBatchOrdersService{c: c}
}

// NewCancelAllOpenOrdersService init cancel all open orders service
// DELETE /eapi/v1/allOpenOrders
func (c *Client) NewCancelAllOpenOrdersService() *CancelAllOpenOrdersService {
return &CancelAllOpenOrdersService{c: c}
}

// NewCancelMultipleOrdersService init cancel multiple orders service
func (c *Client) NewCancelMultipleOrdersService() *CancelMultiplesOrdersService {
return &CancelMultiplesOrdersService{c: c}
// DELETE /eapi/v1/allOpenOrdersByUnderlying
func (c *Client) NewCancelAllOpenOrdersByUnderlyingService() *CancelAllOpenOrdersByUnderlyingService {
return &CancelAllOpenOrdersByUnderlyingService{c: c}
}

// NewCreateBatchOrdersService init creating batch order service
func (c *Client) NewCreateBatchOrdersService() *CreateBatchOrdersService {
return &CreateBatchOrdersService{c: c}
// NewListOpenOrdersService init list open orders service
// GET /eapi/v1/openOrders
func (c *Client) NewListOpenOrdersService() *ListOpenOrdersService {
return &ListOpenOrdersService{c: c}
}

// GET /eapi/v1/historyOrders
func (c *Client) NewHistoryOrdersService() *HistoryOrdersService {
return &HistoryOrdersService{c: c}
}

// GET /eapi/v1/position
func (c *Client) NewPositionService() *PositionService {
return &PositionService{c: c}
}

// GET /eapi/v1/userTrades
func (c *Client) NewUserTradesService() *UserTradesService {
return &UserTradesService{c: c}
}

// GET /eapi/v1/exerciseRecord
func (c *Client) NewExercistRecordService() *ExerciseRecordService {
return &ExerciseRecordService{c: c}
}

// GET /eapi/v1/bill
// Obtain account fund flow
func (c *Client) NewBillService() *BillService {
return &BillService{c: c}
}

// GET /eapi/v1/income/asyn
// Get option fund flow download ID
func (c *Client) NewIncomeDownloadIdService() *IncomeDownloadIdService {
return &IncomeDownloadIdService{c: c}
}

// GET /eapi/v1/income/asyn/id
// Obtain the contract fund flow download link through the download ID
func (c *Client) NewIncomeDownloadLinkService() *IncomeDownloadLinkService {
return &IncomeDownloadLinkService{c: c}
}

func (c *Client) NewStartUserStreamService() *StartUserStreamService {
return &StartUserStreamService{c: c}
}

func (c *Client) NewKeepaliveUserStreamService() *KeepaliveUserStreamService {
return &KeepaliveUserStreamService{c: c}
}

func (c *Client) NewCloseUserStreamService() *CloseUserStreamService {
return &CloseUserStreamService{c: c}
}
2 changes: 1 addition & 1 deletion v2/options/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func (s *baseTestSuite) assertURLValuesEqual(e, a url.Values) {
case timestampKey, signatureKey:
r.NotEmpty(a.Get(k))
continue
r.Equal(e.Get(k), a.Get(k), k)
}
r.Equal(e.Get(k), a.Get(k), k)
}
}

Expand Down
Loading

0 comments on commit 66d76db

Please sign in to comment.