Skip to content

Conversation

yosriady
Copy link
Contributor

  1. Enhanced Provider Detection:

    • Extended WalletProviderFlags interface with mobile wallet flags
    • Added comprehensive isWalletConnectProvider() method that checks multiple WalletConnect indicators
    • Enhanced detectWalletConnectProvider() to identify specific mobile wallets
  2. Mobile Wallet Support:

    • Trust Wallet
    • Coinbase Wallet
    • Binance Wallet
    • Rainbow
    • ImToken
    • MathWallet
    • TokenPocket
    • OKX Wallet
    • BitKeep
    • MetaMask Mobile
  3. Improved Event Handling:

    • Added registerWalletConnectSpecificListeners() method
    • Listens for WalletConnect-specific events: session_update, connect, disconnect
    • Enhanced logging for better debugging
  4. Debug Utilities:

    • Created /examples/walletconnect-debug.js with browser console debugging tools
    • Added checkWalletConnectConnections() method for manual connection testing
    • Enhanced getProviderState() to include WalletConnect provider count
  5. Comprehensive Documentation:

    • Created WALLETCONNECT_INTEGRATION.md with full usage guide
    • Included troubleshooting section and best practices
    • Added API reference for new methods

🔧 Key Improvements:

The main issue you were experiencing - WalletConnect providers not emitting connect events - should now be resolved because:

  1. Better Detection: The SDK now detects WalletConnect providers using multiple methods, not just the isWalletConnect flag
  2. Additional Event Listeners: WalletConnect-specific events are now monitored (session_update, etc.)
  3. Enhanced Logging: You can now see exactly what's happening during connection attempts

🧪 How to Test:

  1. Enable Debug Mode:

    const analytics = await FormoAnalytics.init('your-write-key', {
      logger: { enabled: true, levels: ['info', 'warn', 'error', 'debug'] }
    });
  2. Use Debug Utilities:

    • Include examples/walletconnect-debug.js in your test app
    • Open browser console and run:
      FormoWalletConnectDebug.checkProviderState();
      FormoWalletConnectDebug.testConnection();
      FormoWalletConnectDebug.monitorProviderEvents();
  3. Test Mobile Wallets:

    • Connect with Trust Wallet, Coinbase Wallet, or Binance Wallet via WalletConnect
    • Check console logs for connection detection
    • Verify connect events are properly emitted

🎯 Expected Results:

When you connect a mobile wallet via WalletConnect, you should now see:

  • Proper wallet detection in logs
  • Connect events being emitted
  • Correct provider information (name, rdns)
  • Successful signature and transaction tracking

The enhanced detection should resolve the issues you mentioned with:

  • ✅ Coinbase Wallet not recognizing WalletConnect
  • ✅ Trust Wallet not emitting connect events
  • ✅ Binance Wallet connection detection

Try testing with your example app at https://formo-analytics-example.vercel.app/ and let me know if you're still experiencing any issues!

@yosriady yosriady requested a review from Copilot September 12, 2025 08:56
Copy link
Contributor

@Copilot 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 comprehensive WalletConnect support for the Formo Analytics SDK to address mobile wallet connection detection issues. The main focus is resolving problems where WalletConnect providers weren't emitting connect events and improving mobile wallet recognition.

  • Enhanced provider detection using multiple WalletConnect indicators beyond just the isWalletConnect flag
  • Added support for 10 popular mobile wallets with proper identification and event handling
  • Implemented WalletConnect-specific event listeners for session updates, connect/disconnect events

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
src/FormoAnalytics.ts Core implementation with enhanced WalletConnect detection, mobile wallet support, and WalletConnect-specific event handling
examples/walletconnect-debug.js Debug utilities for testing and monitoring WalletConnect connections in browser console
WALLETCONNECT_INTEGRATION.md Comprehensive documentation covering usage, troubleshooting, and API reference

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +1088 to +1106
const sessionUpdateListener = (error: any, payload: any) => {
if (error) {
logger.error("WalletConnect session update error:", error);
return;
}

logger.info("WalletConnect session update:", payload);

// Handle session connection
if (payload?.params?.[0]?.accounts?.length > 0) {
const accounts = payload.params[0].accounts;
const chainId = payload.params[0].chainId;

// Simulate accountsChanged event for WalletConnect
this.onAccountsChanged(provider, accounts).catch(error => {
logger.error("Failed to handle WalletConnect session update:", error);
});
}
};
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

The extracted variables accounts and chainId on lines 1098-1099 are not used consistently. The chainId variable is extracted but never used in the subsequent onAccountsChanged call, which could be confusing to future maintainers.

Copilot uses AI. Check for mistakes.

if (session.accounts?.length > 0) {
// Simulate connect event
this.onConnected(provider, {
chainId: `0x${session.chainId?.toString(16) || '1'}`
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

Potential issue with chainId conversion. If session.chainId is already a hex string, calling toString(16) will result in an invalid chainId format. Should check if chainId is already in hex format before conversion.

Suggested change
chainId: `0x${session.chainId?.toString(16) || '1'}`
chainId: typeof session.chainId === "string" && session.chainId.startsWith("0x")
? session.chainId
: `0x${Number(session.chainId ?? 1).toString(16)}`

Copilot uses AI. Check for mistakes.

}

// Additional event listeners for different WalletConnect implementations
const additionalEvents = ['session_request', 'call_request', 'wc_sessionUpdate'];
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

The hardcoded event names array should be defined as a constant at the class or module level to improve maintainability and avoid potential typos when these events need to be referenced elsewhere.

Copilot uses AI. Check for mistakes.

console.log('All Providers:', providers.map(p => ({
name: p.info.name,
rdns: p.info.rdns,
isWalletConnect: window.formoAnalytics.isWalletConnectProvider?.(p.provider) || 'unknown'
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

The isWalletConnectProvider method is private in the FormoAnalytics class but is being accessed externally. This will result in undefined behavior since private methods are not accessible outside the class.

Suggested change
isWalletConnect: window.formoAnalytics.isWalletConnectProvider?.(p.provider) || 'unknown'
isWalletConnect: window.formoAnalytics.isWalletConnectProviderPublic?.(p.provider) || 'unknown'

Copilot uses AI. Check for mistakes.

console.log('Provider State:', state);

// List all providers
const providers = window.formoAnalytics.providers;
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

The providers property appears to be accessing a private field _providers from the FormoAnalytics class. This should use the public method getProviders() instead to access provider information.

Suggested change
const providers = window.formoAnalytics.providers;
const providers = window.formoAnalytics.getProviders();

Copilot uses AI. Check for mistakes.

};

provider.on(eventName, listener);
this.addProviderListener(provider, `wc_${eventName}`, listener);
Copy link

Choose a reason for hiding this comment

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

Bug: WalletConnect Event Listener Cleanup Fails

WalletConnect-specific event listeners, added to provider.connector, aren't correctly removed. The cleanup logic targets the main provider with different event names, causing memory leaks. Separately, the chainId conversion in onConnected can generate an invalid 0x0x format if session.chainId is already a hex string.

Fix in Cursor Fix in Web

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.

1 participant