fix: parallel swap approval race condition#215
Conversation
When multiple swaps execute in parallel, the server-provided needsApproval flag becomes stale after the first swap consumes the allowance. This adds an ensureAllowance utility that reads the actual on-chain ERC20 allowance inside withWalletLock before executing, fixing "insufficient allowance" errors on concurrent swaps of the same token. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
Disabled knowledge base sources:
📝 WalkthroughWalkthroughThis change refactors the swap approval flow by introducing a new Changes
Sequence Diagram(s)sequenceDiagram
participant SwapExecution as Swap Execution
participant AllowanceUtil as ensureAllowance
participant ApprovalExec as executeApproval
participant Blockchain as Blockchain
SwapExecution->>AllowanceUtil: ensureAllowance(params)
Note over SwapExecution: Set substatus to<br/>"Checking allowance..."
AllowanceUtil->>Blockchain: Check ERC20 allowance
Blockchain-->>AllowanceUtil: Current allowance
alt Allowance Insufficient
AllowanceUtil->>ApprovalExec: executeApproval(...)
ApprovalExec->>Blockchain: Send approval tx
Blockchain-->>ApprovalExec: txHash
ApprovalExec-->>AllowanceUtil: txHash
AllowanceUtil-->>SwapExecution: approvalTxHash
SwapExecution->>Blockchain: Wait for receipt
Blockchain-->>SwapExecution: Receipt confirmed
else Allowance Sufficient
AllowanceUtil-->>SwapExecution: undefined
end
SwapExecution->>SwapExecution: Proceed to swap
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
withWalletLockinstead of trusting the server's staleneedsApprovalflagensureAllowanceutility that reads allowance viapublicClient.readContractand conditionally approvesTest plan
🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes