-
Couldn't load subscription status.
- Fork 0
fix: Add Improved chain switching detection [P-1563] #127
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.
Greptile Overview
Greptile Summary
This PR fixes chain switching detection when using excludeChains configuration. The implementation adds context-aware tracking that distinguishes between chain transition events (always tracked) and other events like transactions/signatures (excluded on specified chains).
Key changes:
- Enhanced
shouldTrack()method witheventTypeandchainIdparameters for context-aware decisions - Chain transition events (
EventType.CHAIN) are now always tracked, even for excluded chains, to capture valuable analytics about network switching behavior onChainChanged()now capturespreviousChainIdbefore updating state and includes it in event propertiestrackEvent()extracts chainId from event payload to ensure correct chain is checked
Behavior improvement:
- Before: Switching TO an excluded chain wouldn't track the transition
- After: All chain transitions are tracked with
previousChainIdincluded, while other events (transactions, signatures) correctly respectexcludeChainsexclusion
Documentation:
- Added 4 comprehensive markdown files documenting the fix, improvements, and usage examples
- Created example file with 5 usage scenarios demonstrating proper configuration
Testing:
- Test file created with comprehensive test cases but marked as
describe.skip- tests are not implemented yet
Confidence Score: 3/5
- This PR is mostly safe to merge but has minor implementation concerns and lacks test coverage
- The core logic is sound and addresses the reported issue effectively. However, confidence is reduced due to: (1) redundant
currentChainIdassignment in two places, and (2) comprehensive test suite defined but completely skipped/unimplemented, leaving the changes untested - test/lib/chain-switching.spec.ts requires test implementation before merging to production
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| src/FormoAnalytics.ts | 4/5 | Enhanced chain switching detection with context-aware tracking; properly handles excludeChains for transitions vs other events |
| test/lib/chain-switching.spec.ts | 2/5 | Test file created with comprehensive test cases but all tests are skipped (not implemented) |
Sequence Diagram
sequenceDiagram
participant User
participant Wallet
participant SDK as FormoAnalytics SDK
participant shouldTrack
participant Analytics as Analytics Backend
Note over User,Analytics: Chain Switching Flow
User->>Wallet: Switch from Chain 1 to Chain 41455 (excluded)
Wallet->>SDK: chainChanged event (0xa1ef)
SDK->>SDK: onChainChanged(provider, "0xa1ef")
Note over SDK: Capture previousChainId = 1
SDK->>SDK: Parse chainId: 41455
SDK->>SDK: Update currentChainId = 41455
SDK->>SDK: chain({ chainId: 41455, address }, { previousChainId: 1 })
SDK->>SDK: trackEvent(CHAIN, { chainId: 41455 })
SDK->>shouldTrack: shouldTrack(CHAIN, 41455)
Note over shouldTrack: Check if 41455 in excludeChains<br/>EventType is CHAIN, so return TRUE
shouldTrack-->>SDK: true (always track chain transitions)
SDK->>Analytics: Track chain event { chainId: 41455, previousChainId: 1 }
Note over User,Analytics: Transaction on Excluded Chain
User->>SDK: transaction({ chainId: 41455, ... })
SDK->>SDK: trackEvent(TRANSACTION, { chainId: 41455 })
SDK->>shouldTrack: shouldTrack(TRANSACTION, 41455)
Note over shouldTrack: Check if 41455 in excludeChains<br/>EventType is TRANSACTION, so return FALSE
shouldTrack-->>SDK: false (exclude transaction)
SDK->>SDK: Skip tracking (log: "Skipping transaction on excluded chain")
Note over User,Analytics: Switch Back to Normal Chain
User->>Wallet: Switch from Chain 41455 to Chain 1
Wallet->>SDK: chainChanged event (0x1)
SDK->>SDK: onChainChanged(provider, "0x1")
Note over SDK: Capture previousChainId = 41455
SDK->>SDK: Parse chainId: 1
SDK->>SDK: Update currentChainId = 1
SDK->>SDK: chain({ chainId: 1, address }, { previousChainId: 41455 })
SDK->>SDK: trackEvent(CHAIN, { chainId: 1 })
SDK->>shouldTrack: shouldTrack(CHAIN, 1)
Note over shouldTrack: Chain 1 not in excludeChains<br/>Return TRUE
shouldTrack-->>SDK: true
SDK->>Analytics: Track chain event { chainId: 1, previousChainId: 41455 }
Note over User,Analytics: Transaction on Normal Chain
User->>SDK: transaction({ chainId: 1, ... })
SDK->>SDK: trackEvent(TRANSACTION, { chainId: 1 })
SDK->>shouldTrack: shouldTrack(TRANSACTION, 1)
Note over shouldTrack: Chain 1 not in excludeChains<br/>Return TRUE
shouldTrack-->>SDK: true
SDK->>Analytics: Track transaction event
Additional Comments (1)
-
src/FormoAnalytics.ts, line 390 (link)style:
currentChainIdupdated twice for chain events: once here inchain()method (line 390) and once inonChainChanged()(line 1041). WhenonChainChangedcallschain(), the chain ID gets set twice - first at line 1041 before callingchain(), then again here at line 390 whenchain()executes. This redundancy could cause confusion.
7 files reviewed, 1 comment
|
bugbot run |
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.
✅ Bugbot reviewed your changes and found no bugs!
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.
Greptile Overview
Greptile Summary
Enhances chain switching detection to always track CHAIN transition events while respecting excludeChains for other event types, and adds previousChainId to chain event properties for better analytics context.
Key improvements:
- Captures
previousChainIdbefore chain transitions for analytics - Context-aware
shouldTrack()method acceptseventTypeandchainIdparameters - CHAIN events always tracked (even on excluded chains) to capture network switching behavior
- Other events (transactions, signatures) properly filtered on excluded chains
- Better logging for chain transitions and tracking decisions
- Fixed chain ID 0 handling with
!== undefinedcheck
Minor concerns:
- Type coercion on line 1460 could potentially mask bugs if unexpected types are passed
- Tests marked as skipped - implementation should be validated with actual test coverage
Confidence Score: 4/5
- This PR is safe to merge with low risk - logic improvements are well-reasoned and backward compatible
- Score reflects solid implementation of chain switching improvements with clear documentation. The logic for always tracking CHAIN events while filtering other events on excluded chains is sound. Minor deduction for type coercion that could be more defensive, and skipped tests that should be implemented before production use. No breaking changes or critical issues identified.
- Pay attention to
src/FormoAnalytics.tsline 1460 for potential type safety improvements
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| src/FormoAnalytics.ts | 4/5 | Improved chain switching detection with previousChainId tracking, context-aware shouldTrack logic, and better handling of excludeChains - minor edge case concerns around type coercion |
Sequence Diagram
sequenceDiagram
participant Provider as EIP1193 Provider
participant Analytics as FormoAnalytics
participant ShouldTrack as shouldTrack()
participant Backend as Analytics Backend
Note over Provider,Backend: Chain Switching Flow with excludeChains
Provider->>Analytics: chainChanged(newChainIdHex)
activate Analytics
Analytics->>Analytics: previousChainId = currentChainId
Analytics->>Analytics: nextChainId = parseChainId(hex)
Analytics->>Analytics: Log transition (from → to)
Analytics->>Analytics: chain({chainId: nextChainId, ...}, {previousChainId})
activate Analytics
Note over Analytics: Line 390: currentChainId updated here
Analytics->>Analytics: currentChainId = nextChainId
Analytics->>Analytics: trackEvent(CHAIN, payload, properties)
activate Analytics
Analytics->>Analytics: chainIdForCheck = payload.chainId
Analytics->>ShouldTrack: shouldTrack(EventType.CHAIN, chainIdForCheck)
activate ShouldTrack
alt Chain ID in excludeChains
ShouldTrack->>ShouldTrack: eventType === EventType.CHAIN?
Note over ShouldTrack: CHAIN events always tracked<br/>to capture network transitions
ShouldTrack-->>Analytics: return true
else Chain ID not excluded
ShouldTrack-->>Analytics: return true
end
deactivate ShouldTrack
Analytics->>Backend: Send CHAIN event with previousChainId
Backend-->>Analytics: Event tracked
deactivate Analytics
deactivate Analytics
deactivate Analytics
Note over Provider,Backend: Subsequent Event on Excluded Chain
Provider->>Analytics: Transaction event on excluded chain
activate Analytics
Analytics->>Analytics: trackEvent(TRANSACTION, payload)
activate Analytics
Analytics->>ShouldTrack: shouldTrack(TRANSACTION, excludedChainId)
activate ShouldTrack
ShouldTrack->>ShouldTrack: Chain in excludeChains?
ShouldTrack->>ShouldTrack: eventType === CHAIN? No
Note over ShouldTrack: Non-CHAIN events filtered<br/>on excluded chains
ShouldTrack-->>Analytics: return false
deactivate ShouldTrack
Analytics->>Analytics: Skip event
Note over Analytics: Event not sent to backend
deactivate Analytics
deactivate Analytics
2 files reviewed, 1 comment
|
This is redundant |
Note
Ensure CHAIN transitions are always tracked while other events on excluded chains are skipped; add previousChainId, refine shouldTrack, and include docs, example, and test stubs.
shouldTrack(eventType?, chainId?)to be context-aware; always trackEventType.CHAIN, skip other events ontracking.excludeChains.trackEvent()now passestypeandpayload.chainIdtoshouldTrackand improves skip logging;trackPageHit()callsshouldTrack(EventType.PAGE).onChainChanged()capturespreviousChainId, logs transitions, and emitschain({ chainId: nextChainId }, { previousChainId })without redundantcurrentChainIdassignment.CHAIN_SWITCHING_IMPROVEMENTS.md,CHAIN_SWITCHING_FIX_SUMMARY.md,IMPLEMENTATION_COMPLETE.md, andQUICK_REFERENCE.mddescribing behavior and guidance.examples/chain-switching-example.tsdemonstratingexcludeChainsand transition tracking.test/lib/chain-switching.spec.ts(skipped/TODO) for chain switching and exclusions.Written by Cursor Bugbot for commit 1187153. This will update automatically on new commits. Configure here.