Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order Rate Limit #320

Merged
merged 8 commits into from Nov 10, 2021
18 changes: 18 additions & 0 deletions v2/futures/client.go
Expand Up @@ -290,6 +290,9 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
}

func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption) (data []byte, err error) {

var t map[string]interface{}

err = c.parseRequest(r, opts...)
if err != nil {
return []byte{}, err
Expand All @@ -313,6 +316,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
if err != nil {
return []byte{}, err
}

defer func() {
cerr := res.Body.Close()
// Only overwrite the retured error if the original error was nil and an
Expand All @@ -333,6 +337,20 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
}
return nil, apiErr
}

err = json.Unmarshal(data, &t)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all the API response are map, for example, maybe array, so it will fail here.
It's not a good idea to mix response headers with response body, maybe we need a dedicated return value for headers:

func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption) (data []byte, headers Headers, err error) 

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And there will be performance issue to Unmarshal & Marshal to add headers for every API call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok let me change the codes adding a return. In that case we've to modify all the others functions but it's more clean

if err != nil {
return data, nil
// c.debug("failed to unmarshal json: %s", err)
// return nil, err
}

// add rate limit by order

t["rateLimitOrder10s"] = res.Header.Get("X-Mbx-Order-Count-10s")
t["rateLimitOrder1m"] = res.Header.Get("X-Mbx-Order-Count-1m")

data, err = json.Marshal(t)
return data, nil
}

Expand Down
44 changes: 23 additions & 21 deletions v2/futures/order_service.go
Expand Up @@ -195,27 +195,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 Down
1 change: 1 addition & 0 deletions v2/futures/order_service_test.go
Expand Up @@ -80,6 +80,7 @@ func (s *orderServiceTestSuite) TestCreateOrder() {
})
s.assertRequestEqual(e, r)
})

res, err := s.client.NewCreateOrderService().Symbol(symbol).Side(side).
Type(orderType).TimeInForce(timeInForce).Quantity(quantity).ClosePosition(closePosition).
ReduceOnly(reduceOnly).Price(price).NewClientOrderID(newClientOrderID).
Expand Down
3 changes: 2 additions & 1 deletion v2/user_universal_transfer.go
Expand Up @@ -63,7 +63,8 @@ func (s *CreateUserUniversalTransferService) Do(ctx context.Context) (*CreateUse
endpoint: "/sapi/v1/asset/transfer",
secType: secTypeSigned,
}
r.setParam("types", s.types)

r.setParam("type", s.types)
r.setParam("asset", s.asset)
r.setParam("amount", s.amount)
if v := s.fromSymbol; v != nil {
Expand Down
2 changes: 1 addition & 1 deletion v2/user_universal_transfer_test.go
Expand Up @@ -31,7 +31,7 @@ func (s *userUniversalTransferTestSuite) TestUserUniversalTransfer() {

s.assertReq(func(r *request) {
e := newSignedRequest().setParams(params{
"types": types,
"type": types,
"asset": asset,
"amount": amount,
"fromSymbol": fromSymbol,
Expand Down