Skip to content

Commit

Permalink
libxc/binance: Handle invalid listen key error
Browse files Browse the repository at this point in the history
  • Loading branch information
martonp committed Jan 6, 2025
1 parent 0030e49 commit 3ce73ac
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions client/mm/libxc/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func (bnc *binance) setBalances(ctx context.Context) error {

func (bnc *binance) refreshBalances(ctx context.Context) error {
var resp bntypes.Account
err := bnc.getAPI(ctx, "/api/v3/account", nil, true, true, &resp)
err, _ := bnc.getAPI(ctx, "/api/v3/account", nil, true, true, &resp)
if err != nil {
return fmt.Errorf("error getting balances: %w", err)
}
Expand Down Expand Up @@ -641,7 +641,7 @@ func (bnc *binance) readCoins(coins []*bntypes.CoinInfo) {
// the tokenIDs.
func (bnc *binance) getCoinInfo(ctx context.Context) error {
coins := make([]*bntypes.CoinInfo, 0)
err := bnc.getAPI(ctx, "/sapi/v1/capital/config/getall", nil, true, true, &coins)
err, _ := bnc.getAPI(ctx, "/sapi/v1/capital/config/getall", nil, true, true, &coins)
if err != nil {
return fmt.Errorf("error getting binance coin info: %w", err)
}
Expand All @@ -652,7 +652,7 @@ func (bnc *binance) getCoinInfo(ctx context.Context) error {

func (bnc *binance) getMarkets(ctx context.Context) (map[string]*bntypes.Market, error) {
var exchangeInfo bntypes.ExchangeInfo
err := bnc.getAPI(ctx, "/api/v3/exchangeInfo", nil, false, false, &exchangeInfo)
err, _ := bnc.getAPI(ctx, "/api/v3/exchangeInfo", nil, false, false, &exchangeInfo)
if err != nil {
return nil, fmt.Errorf("error getting markets from Binance: %w", err)
}
Expand Down Expand Up @@ -897,7 +897,7 @@ func (bnc *binance) Trade(ctx context.Context, baseID, quoteID uint32, sell bool
}()

var orderResponse bntypes.OrderResponse
err = bnc.postAPI(ctx, "/api/v3/order", v, nil, true, true, &orderResponse)
err, _ = bnc.postAPI(ctx, "/api/v3/order", v, nil, true, true, &orderResponse)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -935,7 +935,7 @@ func (bnc *binance) ConfirmWithdrawal(ctx context.Context, withdrawalID string,
withdrawHistoryResponse := []*withdrawalHistoryStatus{}
v := make(url.Values)
v.Add("coin", assetCfg.coin)
err = bnc.getAPI(ctx, "/sapi/v1/capital/withdraw/history", v, true, true, &withdrawHistoryResponse)
err, _ = bnc.getAPI(ctx, "/sapi/v1/capital/withdraw/history", v, true, true, &withdrawHistoryResponse)
if err != nil {
return 0, "", err
}
Expand Down Expand Up @@ -989,7 +989,7 @@ func (bnc *binance) Withdraw(ctx context.Context, assetID uint32, qty uint64, ad
withdrawResp := struct {
ID string `json:"id"`
}{}
err = bnc.postAPI(ctx, "/sapi/v1/capital/withdraw/apply", nil, v, true, true, &withdrawResp)
err, _ = bnc.postAPI(ctx, "/sapi/v1/capital/withdraw/apply", nil, v, true, true, &withdrawResp)
if err != nil {
return "", err
}
Expand All @@ -1011,7 +1011,7 @@ func (bnc *binance) GetDepositAddress(ctx context.Context, assetID uint32) (stri
resp := struct {
Address string `json:"address"`
}{}
err = bnc.getAPI(ctx, "/sapi/v1/capital/deposit/address", v, true, true, &resp)
err, _ = bnc.getAPI(ctx, "/sapi/v1/capital/deposit/address", v, true, true, &resp)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -1041,7 +1041,7 @@ func (bnc *binance) ConfirmDeposit(ctx context.Context, deposit *DepositData) (b
}
// TODO: Use the "startTime" parameter to apply a reasonable limit to
// this request.
err := bnc.getAPI(ctx, "/sapi/v1/capital/deposit/hisrec", query, true, true, &resp)
err, _ := bnc.getAPI(ctx, "/sapi/v1/capital/deposit/hisrec", query, true, true, &resp)
if err != nil {
bnc.log.Errorf("error getting deposit status: %v", err)
return false, 0
Expand Down Expand Up @@ -1121,7 +1121,8 @@ func (bnc *binance) CancelTrade(ctx context.Context, baseID, quoteID uint32, tra
v.Add("symbol", slug)
v.Add("origClientOrderId", tradeID)

return bnc.request(ctx, "DELETE", "/api/v3/order", v, nil, true, true, nil)
err, _ = bnc.request(ctx, "DELETE", "/api/v3/order", v, nil, true, true, nil)
return err
}

func (bnc *binance) Balances(ctx context.Context) (map[uint32]*ExchangeBalance, error) {
Expand Down Expand Up @@ -1202,7 +1203,7 @@ func (bnc *binance) Markets(ctx context.Context) (map[string]*Market, error) {
q.Set("symbols", string(encSymbols))

var ds []*bntypes.MarketTicker24
if err = bnc.getAPI(ctx, "/api/v3/ticker/24hr", q, false, false, &ds); err != nil {
if err, _ = bnc.getAPI(ctx, "/api/v3/ticker/24hr", q, false, false, &ds); err != nil {
return nil, err
}

Expand Down Expand Up @@ -1265,15 +1266,15 @@ func (bnc *binance) MatchedMarkets(ctx context.Context) (_ []*MarketMatch, err e
return markets, nil
}

func (bnc *binance) getAPI(ctx context.Context, endpoint string, query url.Values, key, sign bool, thing interface{}) error {
func (bnc *binance) getAPI(ctx context.Context, endpoint string, query url.Values, key, sign bool, thing interface{}) (error, int) {
return bnc.request(ctx, http.MethodGet, endpoint, query, nil, key, sign, thing)
}

func (bnc *binance) postAPI(ctx context.Context, endpoint string, query, form url.Values, key, sign bool, thing interface{}) error {
func (bnc *binance) postAPI(ctx context.Context, endpoint string, query, form url.Values, key, sign bool, thing interface{}) (error, int) {
return bnc.request(ctx, http.MethodPost, endpoint, query, form, key, sign, thing)
}

func (bnc *binance) request(ctx context.Context, method, endpoint string, query, form url.Values, key, sign bool, thing interface{}) error {
func (bnc *binance) request(ctx context.Context, method, endpoint string, query, form url.Values, key, sign bool, thing interface{}) (error, int) {
var fullURL string
if strings.Contains(endpoint, "sapi") {
fullURL = bnc.accountsURL + endpoint
Expand Down Expand Up @@ -1303,7 +1304,7 @@ func (bnc *binance) request(ctx context.Context, method, endpoint string, query,
raw := queryString + bodyString
mac := hmac.New(sha256.New, []byte(bnc.secretKey))
if _, err := mac.Write([]byte(raw)); err != nil {
return fmt.Errorf("hmax Write error: %w", err)
return fmt.Errorf("hmax Write error: %w", err), 0
}
v := url.Values{}
v.Set("signature", hex.EncodeToString(mac.Sum(nil)))
Expand All @@ -1319,7 +1320,7 @@ func (bnc *binance) request(ctx context.Context, method, endpoint string, query,

req, err := http.NewRequestWithContext(ctx, method, fullURL, body)
if err != nil {
return fmt.Errorf("NewRequestWithContext error: %w", err)
return fmt.Errorf("NewRequestWithContext error: %w", err), 0
}

req.Header = header
Expand All @@ -1330,9 +1331,9 @@ func (bnc *binance) request(ctx context.Context, method, endpoint string, query,
}
if err := dexnet.Do(req, thing, dexnet.WithSizeLimit(1<<24), dexnet.WithErrorParsing(&errPayload)); err != nil {
bnc.log.Errorf("request error from endpoint %s %q with query = %q, body = %q", method, endpoint, queryString, bodyString)
return fmt.Errorf("%w, bn code = %d, msg = %q", err, errPayload.Code, errPayload.Msg)
return fmt.Errorf("%w, bn code = %d, msg = %q", err, errPayload.Code, errPayload.Msg), errPayload.Code
}
return nil
return nil, 0
}

func (bnc *binance) handleOutboundAccountPosition(update *bntypes.StreamUpdate) {
Expand Down Expand Up @@ -1478,7 +1479,7 @@ func (bnc *binance) handleUserDataStreamUpdate(b []byte) {

func (bnc *binance) getListenID(ctx context.Context) (string, error) {
var resp *bntypes.DataStreamKey
if err := bnc.postAPI(ctx, "/api/v3/userDataStream", nil, nil, true, false, &resp); err != nil {
if err, _ := bnc.postAPI(ctx, "/api/v3/userDataStream", nil, nil, true, false, &resp); err != nil {
return "", err
}
bnc.listenKey.Store(resp.ListenKey)
Expand Down Expand Up @@ -1558,7 +1559,12 @@ func (bnc *binance) getUserDataStream(ctx context.Context) (err error) {
q := make(url.Values)
q.Add("listenKey", bnc.listenKey.Load().(string))
// Doing a PUT on a listenKey will extend its validity for 60 minutes.
if err := bnc.request(ctx, http.MethodPut, "/api/v3/userDataStream", q, nil, true, false, nil); err != nil {
if err, errCode := bnc.request(ctx, http.MethodPut, "/api/v3/userDataStream", q, nil, true, false, nil); err != nil {
if errCode == -1125 { // Invalid listen key
bnc.log.Warnf("Invalid listen key. Reconnecting...")
doReconnect()
return
}
bnc.log.Errorf("Error sending keep-alive request: %v. Trying again in 10 seconds", err)
retryKeepAlive = time.After(time.Second * 10)
return
Expand Down Expand Up @@ -1677,7 +1683,8 @@ func (bnc *binance) getOrderbookSnapshot(ctx context.Context, mktSymbol string)
v.Add("symbol", strings.ToUpper(mktSymbol))
v.Add("limit", "1000")
var resp bntypes.OrderbookSnapshot
return &resp, bnc.getAPI(ctx, "/api/v3/depth", v, false, false, &resp)
err, _ := bnc.getAPI(ctx, "/api/v3/depth", v, false, false, &resp)
return &resp, err
}

// subscribeToAdditionalMarketDataStream is called when a new market is
Expand Down Expand Up @@ -2111,7 +2118,7 @@ func (bnc *binance) TradeStatus(ctx context.Context, tradeID string, baseID, quo
v.Add("origClientOrderId", tradeID)

var resp bntypes.BookedOrder
err = bnc.getAPI(ctx, "/api/v3/order", v, true, true, &resp)
err, _ = bnc.getAPI(ctx, "/api/v3/order", v, true, true, &resp)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 3ce73ac

Please sign in to comment.