Skip to content

Commit

Permalink
Order Rate Limit (#320)
Browse files Browse the repository at this point in the history
* added user universal transfer method to Binance Spot

* bug fixed: changed  types into type body field inside user universal transfer method

* fixed test on User Universal Transfer

* added Order Rate Limit fields into Order Response structure

* Revert "added Order Rate Limit fields into Order Response structure"

This reverts commit b5dfc7b.

* managed with headers
  • Loading branch information
amaioli committed Nov 10, 2021
1 parent 7ef5265 commit 15a4341
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 71 deletions.
4 changes: 2 additions & 2 deletions v2/futures/account_service.go
Expand Up @@ -18,7 +18,7 @@ func (s *GetBalanceService) Do(ctx context.Context, opts ...RequestOption) (res
endpoint: "/fapi/v2/balance",
secType: secTypeSigned,
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*Balance{}, err
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func (s *GetAccountService) Do(ctx context.Context, opts ...RequestOption) (res
endpoint: "/fapi/v1/account",
secType: secTypeSigned,
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions v2/futures/client.go
Expand Up @@ -289,14 +289,14 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
return nil
}

func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption) (data []byte, err error) {
func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption) (data []byte, header *http.Header, err error) {
err = c.parseRequest(r, opts...)
if err != nil {
return []byte{}, err
return []byte{}, &http.Header{}, err
}
req, err := http.NewRequest(r.method, r.fullURL, r.body)
if err != nil {
return []byte{}, err
return []byte{}, &http.Header{}, err
}
req = req.WithContext(ctx)
req.Header = r.header
Expand All @@ -307,11 +307,11 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
}
res, err := f(req)
if err != nil {
return []byte{}, err
return []byte{}, &http.Header{}, err
}
data, err = ioutil.ReadAll(res.Body)
if err != nil {
return []byte{}, err
return []byte{}, &http.Header{}, err
}
defer func() {
cerr := res.Body.Close()
Expand All @@ -331,9 +331,9 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
if e != nil {
c.debug("failed to unmarshal json: %s", e)
}
return nil, apiErr
return nil, &http.Header{}, apiErr
}
return data, nil
return data, &res.Header, nil
}

// NewPingService init ping service
Expand Down
2 changes: 1 addition & 1 deletion v2/futures/depth_service.go
Expand Up @@ -36,7 +36,7 @@ func (s *DepthService) Do(ctx context.Context, opts ...RequestOption) (res *Dept
if s.limit != nil {
r.setParam("limit", *s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion v2/futures/exchange_info_service.go
Expand Up @@ -18,7 +18,7 @@ func (s *ExchangeInfoService) Do(ctx context.Context, opts ...RequestOption) (re
endpoint: "/fapi/v1/exchangeInfo",
secType: secTypeNone,
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion v2/futures/income_history.go
Expand Up @@ -67,7 +67,7 @@ func (s *GetIncomeHistoryService) Do(ctx context.Context, opts ...RequestOption)
r.setParam("limit", *s.limit)
}

data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion v2/futures/kline_service.go
Expand Up @@ -63,7 +63,7 @@ func (s *KlinesService) Do(ctx context.Context, opts ...RequestOption) (res []*K
if s.endTime != nil {
r.setParam("endTime", *s.endTime)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*Kline{}, err
}
Expand Down
6 changes: 3 additions & 3 deletions v2/futures/mark_price.go
Expand Up @@ -30,7 +30,7 @@ func (s *PremiumIndexService) Do(ctx context.Context, opts ...RequestOption) (re
if s.symbol != nil {
r.setParam("symbol", *s.symbol)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
data = common.ToJSONList(data)
if err != nil {
return []*PremiumIndex{}, err
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *FundingRateService) Do(ctx context.Context, opts ...RequestOption) (res
if s.limit != nil {
r.setParam("limit", *s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*FundingRate{}, err
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (s *GetLeverageBracketService) Do(ctx context.Context, opts ...RequestOptio
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*LeverageBracket{}, err
}
Expand Down
76 changes: 41 additions & 35 deletions v2/futures/order_service.go
Expand Up @@ -125,7 +125,8 @@ func (s *CreateOrderService) ClosePosition(closePosition bool) *CreateOrderServi
return s
}

func (s *CreateOrderService) createOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, err error) {
func (s *CreateOrderService) createOrder(ctx context.Context, endpoint string, opts ...RequestOption) (data []byte, header *http.Header, err error) {

r := &request{
method: http.MethodPost,
endpoint: endpoint,
Expand Down Expand Up @@ -172,21 +173,24 @@ func (s *CreateOrderService) createOrder(ctx context.Context, endpoint string, o
m["closePosition"] = *s.closePosition
}
r.setFormParams(m)
data, err = s.c.callAPI(ctx, r, opts...)
data, header, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return []byte{}, err
return []byte{}, &http.Header{}, err
}
return data, nil
return data, header, nil
}

// Do send request
func (s *CreateOrderService) Do(ctx context.Context, opts ...RequestOption) (res *CreateOrderResponse, err error) {
data, err := s.createOrder(ctx, "/fapi/v1/order", opts...)
data, header, err := s.createOrder(ctx, "/fapi/v1/order", opts...)
if err != nil {
return nil, err
}
res = new(CreateOrderResponse)
err = json.Unmarshal(data, res)
res.RateLimitOrder10s = header.Get("X-Mbx-Order-Count-10s")
res.RateLimitOrder1m = header.Get("X-Mbx-Order-Count-1m")

if err != nil {
return nil, err
}
Expand All @@ -195,27 +199,29 @@ func (s *CreateOrderService) Do(ctx context.Context, opts ...RequestOption) (res

// CreateOrderResponse define create order response
type CreateOrderResponse struct {
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CumQuote string `json:"cumQuote"`
ReduceOnly bool `json:"reduceOnly"`
Status OrderStatusType `json:"status"`
StopPrice string `json:"stopPrice"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
UpdateTime int64 `json:"updateTime"`
WorkingType WorkingType `json:"workingType"`
ActivatePrice string `json:"activatePrice"`
PriceRate string `json:"priceRate"`
AvgPrice string `json:"avgPrice"`
PositionSide PositionSideType `json:"positionSide"`
ClosePosition bool `json:"closePosition"`
PriceProtect bool `json:"priceProtect"`
Symbol string `json:"symbol"`
OrderID int64 `json:"orderId"`
ClientOrderID string `json:"clientOrderId"`
Price string `json:"price"`
OrigQuantity string `json:"origQty"`
ExecutedQuantity string `json:"executedQty"`
CumQuote string `json:"cumQuote"`
ReduceOnly bool `json:"reduceOnly"`
Status OrderStatusType `json:"status"`
StopPrice string `json:"stopPrice"`
TimeInForce TimeInForceType `json:"timeInForce"`
Type OrderType `json:"type"`
Side SideType `json:"side"`
UpdateTime int64 `json:"updateTime"`
WorkingType WorkingType `json:"workingType"`
ActivatePrice string `json:"activatePrice"`
PriceRate string `json:"priceRate"`
AvgPrice string `json:"avgPrice"`
PositionSide PositionSideType `json:"positionSide"`
ClosePosition bool `json:"closePosition"`
PriceProtect bool `json:"priceProtect"`
RateLimitOrder10s string `json:"rateLimitOrder10s,omitempty"`
RateLimitOrder1m string `json:"rateLimitOrder1m,omitempty"`
}

// ListOpenOrdersService list opened orders
Expand All @@ -240,7 +246,7 @@ func (s *ListOpenOrdersService) Do(ctx context.Context, opts ...RequestOption) (
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*Order{}, err
}
Expand Down Expand Up @@ -292,7 +298,7 @@ func (s *GetOrderService) Do(ctx context.Context, opts ...RequestOption) (res *O
if s.origClientOrderID != nil {
r.setParam("origClientOrderId", *s.origClientOrderID)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -392,7 +398,7 @@ func (s *ListOrdersService) Do(ctx context.Context, opts ...RequestOption) (res
if s.limit != nil {
r.setParam("limit", *s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*Order{}, err
}
Expand Down Expand Up @@ -444,7 +450,7 @@ func (s *CancelOrderService) Do(ctx context.Context, opts ...RequestOption) (res
if s.origClientOrderID != nil {
r.setFormParam("origClientOrderId", *s.origClientOrderID)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -501,7 +507,7 @@ func (s *CancelAllOpenOrdersService) Do(ctx context.Context, opts ...RequestOpti
secType: secTypeSigned,
}
r.setFormParam("symbol", s.symbol)
_, err = s.c.callAPI(ctx, r, opts...)
_, _, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return err
}
Expand Down Expand Up @@ -550,7 +556,7 @@ func (s *CancelMultiplesOrdersService) Do(ctx context.Context, opts ...RequestOp
if s.origClientOrderIDList != nil {
r.setFormParam("origClientOrderIdList", s.origClientOrderIDList)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -614,7 +620,7 @@ func (s *ListLiquidationOrdersService) Do(ctx context.Context, opts ...RequestOp
if s.limit != nil {
r.setParam("limit", *s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*LiquidationOrder{}, err
}
Expand Down Expand Up @@ -701,7 +707,7 @@ func (s *ListUserLiquidationOrdersService) Do(ctx context.Context, opts ...Reque
if s.limit != nil {
r.setParam("limit", *s.limit)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*UserLiquidationOrder{}, err
}
Expand Down Expand Up @@ -813,7 +819,7 @@ func (s *CreateBatchOrdersService) Do(ctx context.Context, opts ...RequestOption

r.setFormParams(m)

data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)

if err != nil {
return &CreateBatchOrdersResponse{}, err
Expand Down
2 changes: 1 addition & 1 deletion v2/futures/position_margin_history.go
Expand Up @@ -67,7 +67,7 @@ func (s *GetPositionMarginHistoryService) Do(ctx context.Context, opts ...Reques
r.setParam("limit", *s.limit)
}

data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion v2/futures/position_risk.go
Expand Up @@ -28,7 +28,7 @@ func (s *GetPositionRiskService) Do(ctx context.Context, opts ...RequestOption)
if s.symbol != "" {
r.setParam("symbol", s.symbol)
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return []*PositionRisk{}, err
}
Expand Down
10 changes: 5 additions & 5 deletions v2/futures/position_service.go
Expand Up @@ -36,7 +36,7 @@ func (s *ChangeLeverageService) Do(ctx context.Context, opts ...RequestOption) (
"symbol": s.symbol,
"leverage": s.leverage,
})
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func (s *ChangeMarginTypeService) Do(ctx context.Context, opts ...RequestOption)
"symbol": s.symbol,
"marginType": s.marginType,
})
_, err = s.c.callAPI(ctx, r, opts...)
_, _, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return err
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func (s *UpdatePositionMarginService) Do(ctx context.Context, opts ...RequestOpt
}
r.setFormParams(m)

_, err = s.c.callAPI(ctx, r, opts...)
_, _, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return err
}
Expand Down Expand Up @@ -175,7 +175,7 @@ func (s *ChangePositionModeService) Do(ctx context.Context, opts ...RequestOptio
r.setFormParams(params{
"dualSidePosition": s.dualSide,
})
_, err = s.c.callAPI(ctx, r, opts...)
_, _, err = s.c.callAPI(ctx, r, opts...)
if err != nil {
return err
}
Expand All @@ -200,7 +200,7 @@ func (s *GetPositionModeService) Do(ctx context.Context, opts ...RequestOption)
secType: secTypeSigned,
}
r.setFormParams(params{})
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion v2/futures/rebate_newuser.go
Expand Up @@ -40,7 +40,7 @@ func (s *GetRebateNewUserService) Do(ctx context.Context, opts ...RequestOption)
r.setParam("type", s.type_future)
}

data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return &RebateNewUser{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions v2/futures/server_service.go
Expand Up @@ -16,7 +16,7 @@ func (s *PingService) Do(ctx context.Context, opts ...RequestOption) (err error)
method: http.MethodGet,
endpoint: "/fapi/v1/ping",
}
_, err = s.c.callAPI(ctx, r, opts...)
_, _, err = s.c.callAPI(ctx, r, opts...)
return err
}

Expand All @@ -31,7 +31,7 @@ func (s *ServerTimeService) Do(ctx context.Context, opts ...RequestOption) (serv
method: http.MethodGet,
endpoint: "/fapi/v1/time",
}
data, err := s.c.callAPI(ctx, r, opts...)
data, _, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit 15a4341

Please sign in to comment.