Skip to content

Routing ​

A route swaps one voucher for another through a chain of pools. Each hop is one swap in one pool, and its output token is the next hop's input token. Routing reaches vouchers that no single pool connects to what you hold.

A ──pool 1──▶ B ──pool 2──▶ C

Quote a route ​

SwapRouter chains each pool's getAmountOut, or getAmountIn backwards from the last hop. It does not move tokens.

solidity
struct Hop { address pool; address tokenIn; address tokenOut; }

function quoteExactInput(Hop[] calldata path, uint256 amountIn)
    external returns (uint256 amountOut);
function quoteExactOutput(Hop[] calldata path, uint256 amountOut)
    external returns (uint256 amountIn);
bash
cast call "$SWAP_ROUTER" \
  'quoteExactInput((address,address,address)[],uint256)(uint256)' \
  "[($POOL_1,$TOKEN_A,$TOKEN_B),($POOL_2,$TOKEN_B,$TOKEN_C)]" "$AMOUNT_IN" \
  --rpc-url "$RPC_URL"

Each hop takes its pool fee and the protocol fee from its own output, so fees compound along the route. The SwapRouter address is in the deployments table.

Execute a route ​

No contract executes a route. Run every hop as a pool swap inside one atomic batch, so a failed hop reverts the whole route. For each hop:

  1. approve(pool, 0), then approve(pool, amountIn) on the hop's input token. The first call clears any leftover allowance.
  2. withdraw(tokenOut, tokenIn, amountIn, recipient, minAmountOut, deadline) on the pool.

Send each intermediate output to the wallet, and set that hop's minAmountOut to the next hop's amountIn. Set the last hop's minAmountOut to the smallest output you accept.

CLC wallets send the batch as a Calibur EIP-7702 batched call. The user signs it, and the CLC relayer submits it and pays the gas.

The CLC router ​

clc-core keeps a graph of pools and vouchers from indexed chain state, and searches it for routes.

  • A route has at most 3 hops by default, and never uses a pool or voucher twice. Set router.max_hops to allow up to 5.
  • Only pools with a RelativeQuoter or no quoter are routed.
  • Only plain GiftableToken vouchers with no expiry are routed.
  • A hop needs room under the pool's limit for the input voucher, and a balance of the output voucher.
  • Routes from the same input voucher are ranked by output (exact input) or by input (exact output). Results within 0.5% are a tie, and the route with fewer hops wins.
  • Quotes are calculated off-chain, then checked against each pool's getAmountOut at a single block before the user signs.

If a pool's rate, fee or balance changes before the batch lands, the batch reverts and the user quotes again.

These endpoints need a signed-in user:

EndpointReturns
GET /api/v2/users/me/creditVouchers and pools the wallet can reach, with the hops needed for each
GET /api/v2/users/me/credit/routes?to=Routes to a voucher, grouped by input voucher, with fees and capacity
POST /api/v2/users/me/credit/quoteA firm quote and the batch to sign
POST /api/v2/relayer/relaySubmits the signed batch