-
-
Notifications
You must be signed in to change notification settings - Fork 359
FEATURE: [binance] listen token support for margin trading #2289
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements support for Binance's new listenToken subscription method for margin trading user data streams, replacing the deprecated listenKey method. According to Binance's changelog, the old method via wss://stream.binance.com:9443 will be removed in the future.
Key changes:
- Adds new
listenTokenAPI integration with automatic token refresh - Maintains backward compatibility via
UseListenKeyflag for deprecated method - Implements WebSocket subscription using
userDataStream.subscribe.listenToken
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/exchange/binance/stream.go | Implements listenToken lifecycle management, adds token refresh worker, and updates WebSocket endpoint selection logic |
| pkg/exchange/binance/exchange.go | Adds UseListenKey flag and EnableListenKey() method for backward compatibility |
| pkg/exchange/binance/binanceapi/create_margin_account_listentoken_request.go | Defines API request structure for creating margin account listen tokens |
| pkg/exchange/binance/binanceapi/create_margin_account_listen_token_request_requestgen.go | Auto-generated request builder code for listen token API |
| pkg/exchange/binance/listentoken_test.go | Unit tests verifying listenToken/listenKey configuration behavior |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| log.Info("listenToken is about to expire, refreshing...") | ||
|
|
||
| // Refresh the listenToken |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The log message on line 771 logs every 15 minutes regardless of whether action is needed. Consider moving this log statement inside the condition block (after line 774) or changing it to debug level to reduce noise in production logs.
| log.Info("listenToken is about to expire, refreshing...") | |
| // Refresh the listenToken | |
| // Refresh the listenToken | |
| log.Info("listenToken is about to expire, refreshing...") |
| return | ||
| } | ||
|
|
||
| timeout := listenTokenKeepAliveInterval * 3 |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number '3' used to calculate timeout lacks explanation. Consider adding a comment explaining why the timeout is 3x the keep-alive interval, or extract it as a named constant (e.g., listenTokenRefreshTimeoutMultiplier).
| s.listenTokenExpiration = newExpiration | ||
|
|
||
| if err := s.sendListenTokenSubscribeCommand(); err != nil { | ||
| log.WithError(err).Error("listenToken subscribe error") |
Copilot
AI
Oct 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After successfully refreshing the token but failing to subscribe (lines 785-788), the worker continues without resetting the ticker. This could lead to rapid retry attempts if the subscription consistently fails. Consider adding ticker.Reset(time.Minute) after the log statement, similar to the error handling in line 778.
| log.WithError(err).Error("listenToken subscribe error") | |
| log.WithError(err).Error("listenToken subscribe error") | |
| ticker.Reset(time.Minute) |
f13be91 to
64c4320
Compare
1c4ed15 to
5698af8
Compare
…nToken to replace listenKey
5698af8 to
53c6cf8
Compare
…nToken to replace listenKey