WDK logoWDK documentation
VeloraGuides

Get Swap Quotes

Estimate fees and amounts with quoteSwap before executing a swap.

This guide shows how to quote before swapping and use quotes for fee estimation. Quotes use quoteSwap(), which works with read-only accounts as well as signing accounts.

Quote before swapping

You can preview fee and token amounts for the same parameters you would pass to swap() using quoteSwap():

Quote exact input swap
const quote = await swapProtocol.quoteSwap({
  tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
  tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
  tokenInAmount: 1000000n
})

console.log('Estimated fee (account units):', quote.fee)
console.log('Tokens in (base units):', quote.tokenInAmount)
console.log('Tokens out (base units):', quote.tokenOutAmount)

You can quote an exact-output style trade the same way by passing tokenOutAmount instead of tokenInAmount to quoteSwap():

Quote exact output swap
const quote = await swapProtocol.quoteSwap({
  tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
  tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
  tokenOutAmount: 500000000000000000n
})

console.log('Estimated fee (account units):', quote.fee)
console.log('Required token in (base units):', quote.tokenInAmount)

With an ERC‑4337 account, pass the optional second argument to preview the fee for a specific paymaster, sponsorship, or native-fee configuration:

Quote with ERC-4337 fee config
const quote = await swapAA.quoteSwap({
  tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
  tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
  tokenInAmount: 1000000n
}, {
  paymasterToken: {
    address: '0xdAC17F958D2ee523a2206206994597C13D831ec7'
  }
})

Fee estimation

Read quote.fee in the account's fee units: native wei for a standard EVM account, token base units for token-paymaster fees, or zero for sponsorship. The quote excludes any separate input-token approval. quoteSwap() does not enforce swapMaxFee.

The following examples use a standard EVM account, so the cap is in wei:

Quote fee before deciding
const quote = await swapProtocol.quoteSwap({
  tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
  tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
  tokenInAmount: 1000000n
})

const maxFee = 200000000000000n
console.log('Quoted fee (wei):', quote.fee, 'cap:', maxFee)

Compare the estimate with an exclusive cap and pass the same cap to swap(). Execution obtains a fresh fee quote and rejects a fee equal to or above swapMaxFee:

Swap when fee is under cap
const maxFee = 200000000000000n
const quote = await swapProtocol.quoteSwap({
  tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
  tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
  tokenInAmount: 1000000n
})

if (quote.fee < maxFee) {
  const result = await swapProtocol.swap({
    tokenIn: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
    tokenOut: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
    tokenInAmount: 1000000n
  }, { swapMaxFee: maxFee })
  console.log('Swap hash:', result.hash)
}

On-chain conditions can change between quote and execution. The executed swap() may still differ slightly from the last quoteSwap() result.

Next Steps

On this page