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.

// TODO: ensure that we receive an authorized event to trigger this auth event
go s.EmitAuth()
} else if !s.useListenKey && s.exchange.IsMargin {
// there is still no 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 comment on line 279 is misleading - the condition returns when there IS no token or when it's expired, so the subsequent code runs only when a valid token EXISTS. Consider revising to: 'Skip subscription if listenToken is missing or expired'.

Suggested change
// there is still no listenToken
// Skip subscription if listenToken is missing or expired

Copilot uses AI. Check for mistakes.
Comment on lines +772 to +776
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
// UseListenKey enables the deprecated listenKey method via wss://stream.binance.com:9443
// This method is deprecated as of 2025-10-06 and will be removed by Binance in the future
// Default behavior (false) uses the new recommended listenToken method
UseListenKey bool
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we have a setter for UseListenKey, I think it's better to make it a private field?

Copy link
Owner

Choose a reason for hiding this comment

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

Agree

mu sync.RWMutex

// Deprecated listenKey support (will be removed by Binance in the future)
useListenKey bool
Copy link
Owner

Choose a reason for hiding this comment

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

since we store the exchange instance at line 73, you don't need this field?

s.listenTokenExpiration = expiration

debug("listenToken created, expires at: %s, starting token refresh worker", expiration)
go s.listenTokenRefreshWorker(ctx)
Copy link
Owner

Choose a reason for hiding this comment

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

createUserDataStreamEndpoint will be triggered every time the connection is reconnected,
you will start multiple workers here

@kbearXD kbearXD force-pushed the kbearXD/binance/margin-trading-listenToken branch from b2b0137 to f13be91 Compare October 29, 2025 08:30
@kbearXD kbearXD force-pushed the kbearXD/binance/margin-trading-listenToken branch from f13be91 to 64c4320 Compare November 4, 2025 09:33
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