Skip to content

fix: parallel swap approval race condition#215

Merged
premiumjibles merged 1 commit intomainfrom
fix/parallel-swap-approval-race
Mar 15, 2026
Merged

fix: parallel swap approval race condition#215
premiumjibles merged 1 commit intomainfrom
fix/parallel-swap-approval-race

Conversation

@premiumjibles
Copy link
Contributor

@premiumjibles premiumjibles commented Mar 14, 2026

Summary

  • Re-checks on-chain ERC20 allowance inside withWalletLock instead of trusting the server's stale needsApproval flag
  • Adds ensureAllowance utility that reads allowance via publicClient.readContract and conditionally approves
  • Fixes "insufficient allowance" / "execution reverted" errors when multiple swaps of the same token run in parallel

Test plan

  • Trigger 2-3 parallel swaps of the same token pair → all should succeed
  • Single swap works identically (on-chain read adds ~100ms)
  • First swap with no prior allowance → approval executes correctly
  • Subsequent swaps with existing allowance → approval correctly skipped

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • Refactor
    • Optimized swap approval workflow with improved on-chain allowance verification. The system now intelligently checks existing token allowances and processes approval transactions only when necessary, reducing unnecessary steps and improving swap efficiency.

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]>
@vercel
Copy link

vercel bot commented Mar 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
shapeshift-agentic Ready Ready Preview, Comment Mar 14, 2026 1:44am

Request Review

@coderabbitai
Copy link

coderabbitai bot commented Mar 14, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c86d3901-e0ab-44c7-8bd3-6447cf145098

📥 Commits

Reviewing files that changed from the base of the PR and between ee8f396 and 5495cd5.

📒 Files selected for processing (2)
  • apps/agentic-chat/src/components/tools/useSwapExecution.tsx
  • apps/agentic-chat/src/utils/ensureAllowance.ts

Disabled knowledge base sources:

  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.


📝 Walkthrough

Walkthrough

This change refactors the swap approval flow by introducing a new ensureAllowance utility that dynamically checks on-chain ERC20 allowances during execution, replacing the previous approval transaction extraction logic with a runtime allowance verification approach.

Changes

Cohort / File(s) Summary
Swap Execution Refactoring
apps/agentic-chat/src/components/tools/useSwapExecution.tsx
Replaced executeApproval import with ensureAllowance; removed needsApproval and approvalTx data extraction; refactored approval flow to call ensureAllowance with substatus "Checking allowance..." and conditionally proceed based on returned approvalTxHash.
Allowance Check Utility
apps/agentic-chat/src/utils/ensureAllowance.ts
New utility module introducing EnsureAllowanceParams interface and ensureAllowance function that performs on-chain ERC20 allowance verification and dispatches approval transactions if needed, returning the approval transaction hash or undefined.

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 With a hop and a check, on-chain we now peek,
Allowances verified, no approvals too sleek,
Dynamic and clever, this flow's rather keen,
The cleanest swap dance I ever have seen! ✨

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/parallel-swap-approval-race
📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@premiumjibles premiumjibles marked this pull request as ready for review March 15, 2026 22:51
@premiumjibles premiumjibles merged commit 12b8580 into main Mar 15, 2026
4 checks passed
@premiumjibles premiumjibles deleted the fix/parallel-swap-approval-race branch March 15, 2026 22:51
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