Skip to content

Conversation

@kbearXD
Copy link
Collaborator

@kbearXD kbearXD commented Oct 27, 2025

…nToken to replace listenKey

Copy link
Contributor

Copilot AI left a 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 listenToken API integration with automatic token refresh
  • Maintains backward compatibility via UseListenKey flag 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.

Comment on lines +772 to +780
log.Info("listenToken is about to expire, refreshing...")

// Refresh the listenToken
Copy link

Copilot AI Oct 27, 2025

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.

Suggested change
log.Info("listenToken is about to expire, refreshing...")
// Refresh the listenToken
// Refresh the listenToken
log.Info("listenToken is about to expire, refreshing...")

Copilot uses AI. Check for mistakes.
return
}

timeout := listenTokenKeepAliveInterval * 3
Copy link

Copilot AI Oct 27, 2025

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).

Copilot uses AI. Check for mistakes.
s.listenTokenExpiration = newExpiration

if err := s.sendListenTokenSubscribeCommand(); err != nil {
log.WithError(err).Error("listenToken subscribe error")
Copy link

Copilot AI Oct 27, 2025

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.

Suggested change
log.WithError(err).Error("listenToken subscribe error")
log.WithError(err).Error("listenToken subscribe error")
ticker.Reset(time.Minute)

Copilot uses AI. Check for mistakes.
@c9s c9s changed the title FEATURE: according to margin trading change log, we need to use liste… FEATURE: [binance] listen token support for margin trading Oct 27, 2025
@kbearXD kbearXD force-pushed the kbearXD/binance/margin-trading-listenToken branch 2 times, most recently from f13be91 to 64c4320 Compare November 4, 2025 09:33
@kbearXD kbearXD force-pushed the kbearXD/binance/margin-trading-listenToken branch 3 times, most recently from 1c4ed15 to 5698af8 Compare January 7, 2026 08:31
@kbearXD kbearXD requested a review from c9s January 7, 2026 08:31
@kbearXD kbearXD force-pushed the kbearXD/binance/margin-trading-listenToken branch from 5698af8 to 53c6cf8 Compare January 7, 2026 08:47
@kbearXD kbearXD merged commit ddcbadb into main Jan 7, 2026
3 checks passed
@kbearXD kbearXD deleted the kbearXD/binance/margin-trading-listenToken branch January 7, 2026 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants