Skip to content

Conversation

@mgrabina
Copy link
Contributor

@mgrabina mgrabina commented Oct 29, 2025

Summary

  • Refactors all swaps modules for consistency, composability, and multi‑provider support.
  • Adds CoW Protocol adapters (including flash‑loan adapters) alongside existing ParaSwap flows.
  • Documents architecture, types, hooks, provider selection, and critical execution paths.

Why

  • Unify UX, robustness, analytics and error handling across all swap‑related modals.
  • Remove duplicated and dispatch‑based code; centralize logic.
  • Enable easy provider extension.
  • Make complex flows (Debt Swap, Repay With Collateral) explicit and maintainable.

Architecture

image (4)

Key changes

  • New module structure under src/components/transactions/Swap/ with a clear separation:
    • modals/ (entry + content), inputs/, warnings/, details/, actions/, hooks/, helpers/, types/, constants/, analytics/.
  • Added CoW Protocol provider support and adapters:
    • CoW market/limit orders for simple swaps.
    • CoW flash‑loan adapters for protocol flows (Debt Swap, Repay With Collateral).
  • Provider selection logic:
    • helpers/shared/provider.helpers.ts#getSwitchProvider chooses CoW when supported, else falls back to ParaSwap.
  • Centralized quoting and flow selection:
    • hooks/useSwapQuote orchestrates quotes and normalizes results across providers.
    • hooks/useFlowSelector derives HF impact and selects flash‑loan vs simple flow.
  • Centralized amount processing:
    • hooks/useSwapOrderAmounts computes normalized sell/buy amounts, applies partner fee, slippage, and handles inversion.
  • Centralized gas estimation:
    • hooks/useSwapGasEstimation provides consistent gas hints and toggles Gas Station.
  • Unified approvals:
    • actions/approval/useSwapTokenApproval.ts handles ERC‑20 approvals, EIP‑2612 permits, and Aave credit delegation, including USDT reset logic.

Architecture and docs

  • Readme: src/components/transactions/Swap/README.md
    • Diagram: src/components/transactions/Swap/docs/swap-modal-architecture.png
    • Directory overview, data model, and extension steps.
  • Deep docblocks in:
    • modals/request/BaseSwapModalContent.tsx (composition root, hook order, state ownership)
    • hooks/* (quotes, flow selection, amounts, slippage, gas, reserves, native max)
    • actions/* (CoW/ParaSwap, protocol flows, responsibilities, flash‑loan notes)
    • helpers/shared/provider.helpers.ts (provider selection policy)
    • Types: types/state.types.ts, types/params.types.ts, types/tokens.types.ts, types/quote.types.ts, types/shared.types.ts (field‑level TSDoc)

Important logic clarifications

  • Inversion (Debt Swap, Repay With Collateral):
    • UI inputs are expressed as “from” and “to”, but the economic swap behind protocol actions is reversed.
    • We compute and store isInvertedSwap and processedSide so orders/quotes are correctly built:
      • Debt Swap: acquire new debt asset to repay old debt → build route/order newDebt → oldDebt.
      • Repay With Collateral: sell collateral to acquire debt asset to repay → collateral → debt asset.
  • Flash‑loans (CoW Adapters):
    • Used when HF would drop below thresholds or flow semantics require atomicity.
    • We compute flash‑loan fee, sign limit order, and post via adapter’s swap settings.

UX behavior

  • Quotes refreshed every 30s when idle; paused during actions, manual limit‑price edits, or explicit approval flows.
  • Slippage:
    • Validated with safeSlippage derived for guards; CoW suggested slippage surfaces warnings if user input is too low.
  • Token selection/persistence:
    • Session‑scoped persistence of selections; safe defaults; filters prevent wrapping pitfalls and native‑token issues for SCWs.

Testing notes

  • Verify simple swaps across supported networks for both providers.
  • Verify protocol flows:
    • Debt Swap: delegation approval/permit, inverted route, adapter success path.
    • Repay With Collateral: aToken approval/permit, inverted route, repayAll detection, adapter success path.
  • Verify edge cases:
    • USDT approval reset path.
    • Smart contract wallets (Safe vs non‑Safe) in CoW flows.
    • Quote pause/resume and limit‑order edits.

@vercel
Copy link

vercel bot commented Oct 29, 2025

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

Project Deployment Preview Comments Updated (UTC)
interface Ready Ready Preview Comment Nov 3, 2025 6:15pm

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.

2 participants