Conversation
For wallet-to-wallet transfers (like sweeps), we need balance updates for BOTH sender and receiver. This requires custody to create transaction records for both wallets, which triggers finalization for both. Changes: - Emit incoming event when TO address is monitored (existing) - Also emit outgoing event when FROM address is monitored (NEW) - Both events processed independently by custody deposit_worker - Results in 2 transaction records + 2 finalization tasks - Finalization_worker updates both balances from on-chain state This fixes sweep balance updates: - Receiver balance increases (from incoming event) - Sender balance decreases (from outgoing event) Note: Withdrawals also emit outgoing events, but custody handles them differently (withdrawal_worker vs deposit_worker).
Problem: When emitting both incoming and outgoing events for the same transaction, NATS was deduplicating them because they had the same idempotent key (tx hash). This caused custody to only receive one event and create one transaction record, preventing proper balance updates for both sender and receiver. Solution: - Added EmitTransactionWithKey() method to allow custom idempotent keys - Use txHash+'-in' for incoming events - Use txHash+'-out' for outgoing events - This ensures both events are processed by custody, creating two transaction records and triggering finalization for both wallets Fixes balance update issue where deposit wallet balance wasn't being updated after sweeps.
Problem: When emitting both incoming and outgoing events for sweep
transactions, both events had identical transaction data. Custody
couldn't differentiate between them, so both events tried to create
a transaction record for the same wallet (receiver).
Solution:
- Add Direction field to Transaction struct ('in' or 'out')
- Set Direction='in' for incoming events (toMonitored)
- Set Direction='out' for outgoing events (fromMonitored)
- Create copy of transaction before setting direction to avoid
modifying shared data
Now custody can:
- Check Direction field to know which wallet owns this event
- For 'in': lookup TO address wallet
- For 'out': lookup FROM address wallet
- Create transaction record with correct wallet_id
This enables proper dual-wallet balance updates for sweeps.
* 'main' of https://github.com/fystack/multichain-indexer: Update sample code Add notice for checking destination address Fix bloomfitler prefix Fix bug infinite catch up when confirmation blocks > 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add Direction Field for Dual Event Processing
Problem
When sweeping funds between two monitored wallets (e.g., deposit wallet → hot wallet), the indexer emits events for both the sender and receiver. However, both events contained identical transaction data without any way to distinguish which wallet should own each event.
This caused custody service to:
Solution
This PR implements a comprehensive solution for dual event processing through 3 commits:
1. Emit Both Incoming and Outgoing Events
2. Use Unique Idempotent Keys
{txHash}-{event}-{in|out}3. Add Direction Field to Transaction
Direction(values:"in"or"out")Direction="in"for incoming events (toMonitored)Direction="out"for outgoing events (fromMonitored)Benefits
For Custody Service:
Directionfield to identify wallet ownership"in": Look up TO address wallet"out": Look up FROM address walletwallet_idFor Balance Updates:
For System Integrity:
Technical Details
Changes to Transaction Model
Event Emission Logic
Idempotent Key Format
{txHash}-transfer-in{txHash}-transfer-outTesting Considerations
Scenarios to Test:
Expected Behavior:
Database Impact
Before: Transaction table had duplicate entries or errors
After: Clean transaction records with proper wallet ownership
Example:
Migration Notes
Backward Compatibility: ✅
Directionfield is additive"in"(current behavior)Deployment Order:
Related Issues
Commits
2bec634- feat: emit both incoming and outgoing events for monitored addressesdcd6dcb- fix: use unique idempotent keys for incoming vs outgoing events14b0843- feat: add direction field to Transaction for dual eventsReady for Review ✅
All commits include clear messages with problem/solution descriptions.