iswap integrators

A production AMM integration needs to choose among exact-input swaps on a V2 path, exact-input swaps through V3 pools, and exact-output swaps. The deciding condition is whether the user fixes what they spend or what they receive, then whether the selected pools and token behavior match the router call.

Key points

  • Quote from the exact pools and fee tiers you intend to execute against; a displayed rate alone is not an execution bound.
  • For exact input, derive amountOutMin from the quote and an explicit tolerance; for exact output, cap spend with amountInMax.
  • Set a short transaction deadline, but treat it as an expiry check, not price protection or a defense against sandwiching.

V2 exact input fits reserve-based routes

A V2 exact-input swap spends a fixed token amount and accepts any output at or above amountOutMin. The router computes amounts across the address path, transfers the input, then invokes each pair in sequence; the pair enforces its invariant as the swaps settle.

For a constant-product pool, let reserves be \(R_{in}\) and \(R_{out}\), input be \(a\), and the fee be \(f\) basis points. The integer quote is approximately \(a(10{,}000-f)R_{out} / (10{,}000R_{in}+a(10{,}000-f))\), with flooring at each hop. On Biswap V2, the documented standard fee is 20 bps, with 10 bps for certain stablecoin pairs; fetch the applicable pool fee instead of assuming one global rate.

For example, if the router quotes 0.50 WETH for 1,000 USDC and the integrator permits 0.5% execution slippage, set amountOutMin to 0.4975 WETH, or 497,500,000,000,000,000 wei. Compute in raw token units using each token’s decimals; format values only at the UI boundary. Biswap’s contract documentation lists getAmountsOut and swapExactTokensForTokens for the V2-style route, while Ethereum.org’s annotated V2 walkthrough shows how the router applies the minimum-output check.

This path is best when a single pool or a short, known reserve route offers sufficient depth and the tokens follow ordinary transfer semantics. It does not fit V3-only liquidity or tokens that tax transfers unless you use the router’s fee-on-transfer supporting method; rebasing tokens and unusual callbacks still need explicit testing because a successful quote can diverge from the amount the pair receives.

V3 exact input fits fee-tiered concentrated pools

A V3 exact-input call fixes the amount in and bounds the output, while each hop identifies a pool by token pair and fee tier. Biswap documents tiers of 0.015%, 0.08%, 0.28%, and 1%; those are pool parameters, not a single exchange-wide rate. Discover the actual pool and tier for each hop, then quote against its current liquidity and tick state.

For integration, encode the route in the router’s expected path format and use the matching deployed ABI for the selected chain. Biswap’s smart-router documentation identifies a router address, but an integrator should verify the current deployment, code, and supported methods against the intended network before signing calldata. Quote with a read-only call or supported quoter at a known block, then submit the execution transaction with amountOutMinimum and a deadline derived from the application’s freshness policy.

The V3 route is best when concentrated liquidity improves the price or when fee-tier choice matters; it does not fit when the path encoder, pool fee, or router ABI is inferred from a V2 integration. V3 price movement can cross initialized ticks during execution, so quoting only the spot tick or reading a pool’s current price is insufficient for a size-sensitive output estimate. A quoter simulates the route; it is not a promise that the same state will remain available at inclusion.

Route selection also belongs to the product decision: how Biswap divides swaps, liquidity, and staking clarifies which feature path an integrator is exposing. That distinction matters because a swap router call cannot stand in for creating a liquidity position, and a route that spans multiple pools may have different gas and fee costs from the best single-pool quote.

Exact output fits a fixed recipient amount

An exact-output swap targets a fixed amount received and caps the input with amountInMax. The router works backward through the route to calculate the required input; if reserves or ticks move enough that execution needs more than the cap, the transaction reverts. This is the better fit for purchases where the recipient amount is contractual, and a poor fit when the user only cares about disposing of a fixed balance.

Derive the cap from a fresh input quote: for a 100-token target quoted at 250 USDC, a 0.4% input tolerance gives amountInMax = 251 USDC after rounding up in raw units. Ensure the allowance covers the cap, not merely the quoted amount. Avoid unlimited approvals where the integration’s threat model or user expectations call for exact or bounded approvals; a separate approval transaction can also make the quote stale before swap submission.

Across both exact-input and exact-output calls, use a Unix timestamp deadline, commonly 60–180 seconds after the latest quote for an interactive flow, adjusted for chain inclusion times and wallet delays. Recheck chain ID, token addresses, recipient, route, approval, gas estimate, and quote age before submission. On INSUFFICIENT_OUTPUT_AMOUNT, EXCESSIVE_INPUT_AMOUNT, expired deadline, or a changed pool state, refresh and present a new quote; do not silently widen tolerance or retry against the same stale calldata.

The final question before acting is: does the user want to bound the amount spent, the amount received, or both—and does the calldata enforce that choice against the exact route being quoted?

Comments

Popular posts from this blog

How Do Sequential Cross-Chain Swaps Work From Your Wallet?

4 Checks to Reconcile a Pending Across Transfer

Pending Base Swaps: Diagnosis and Recovery