Skip to content

Commit 7b5430b

Browse files
authored
libxc/binance: Handle invalid listen key error (#3139)
1 parent 86a200d commit 7b5430b

1 file changed

Lines changed: 29 additions & 11 deletions

File tree

client/mm/libxc/binance.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ const (
5050
// /sapi/v1/capital/config/getall endpoint.
5151
fakeBinanceURL = "http://localhost:37346"
5252
fakeBinanceWsURL = "ws://localhost:37346"
53+
54+
bnErrCodeInvalidListenKey = -1125
5355
)
5456

5557
// binanceOrderBook manages an orderbook for a single market. It keeps
@@ -452,6 +454,23 @@ type withdrawInfo struct {
452454
lotSize uint64
453455
}
454456

457+
type BinanceCodedErr struct {
458+
Code int `json:"code"`
459+
Msg string `json:"msg"`
460+
}
461+
462+
func (e *BinanceCodedErr) Error() string {
463+
return fmt.Sprintf("code = %d, msg = %q", e.Code, e.Msg)
464+
}
465+
466+
func errHasBnCode(err error, code int) bool {
467+
var bnErr *BinanceCodedErr
468+
if errors.As(err, &bnErr) && bnErr.Code == code {
469+
return true
470+
}
471+
return false
472+
}
473+
455474
type binance struct {
456475
log dex.Logger
457476
marketsURL string
@@ -1324,17 +1343,11 @@ func (bnc *binance) request(ctx context.Context, method, endpoint string, query,
13241343

13251344
req.Header = header
13261345

1327-
var errPayload struct {
1328-
Code int `json:"code"`
1329-
Msg string `json:"msg"`
1330-
}
1331-
if err := dexnet.Do(req, thing, dexnet.WithSizeLimit(1<<24), dexnet.WithErrorParsing(&errPayload)); err != nil {
1332-
bnc.log.Errorf("request error from endpoint %s %q with query = %q, body = %q, err = %v, bn code = %d, msg = %q",
1333-
method, endpoint, queryString, bodyString, err, errPayload.Code, errPayload.Msg)
1334-
if errPayload.Msg != "" {
1335-
return fmt.Errorf("Binance error: %v (%d)", errPayload.Msg, errPayload.Code)
1336-
}
1337-
return err
1346+
var bnErr BinanceCodedErr
1347+
if err := dexnet.Do(req, thing, dexnet.WithSizeLimit(1<<24), dexnet.WithErrorParsing(&bnErr)); err != nil {
1348+
bnc.log.Errorf("request error from endpoint %s %q with query = %q, body = %q, bn coded error: %v, msg = %q",
1349+
method, endpoint, queryString, bodyString, &bnErr, bnErr.Msg)
1350+
return errors.Join(err, &bnErr)
13381351
}
13391352

13401353
return nil
@@ -1564,6 +1577,11 @@ func (bnc *binance) getUserDataStream(ctx context.Context) (err error) {
15641577
q.Add("listenKey", bnc.listenKey.Load().(string))
15651578
// Doing a PUT on a listenKey will extend its validity for 60 minutes.
15661579
if err := bnc.request(ctx, http.MethodPut, "/api/v3/userDataStream", q, nil, true, false, nil); err != nil {
1580+
if errHasBnCode(err, bnErrCodeInvalidListenKey) {
1581+
bnc.log.Warnf("Invalid listen key. Reconnecting...")
1582+
doReconnect()
1583+
return
1584+
}
15671585
bnc.log.Errorf("Error sending keep-alive request: %v. Trying again in 10 seconds", err)
15681586
retryKeepAlive = time.After(time.Second * 10)
15691587
return

0 commit comments

Comments
 (0)