Using the Quoter

The VinuSwapQuoter simulates swaps to provide expected amounts without executing on-chain.

Overview

The quoter helps you:

  • Get expected output amounts before swapping

  • Calculate required input for exact output swaps

  • Estimate gas costs

  • Compare routes

Basic Quoting

Quote Exact Input (Single Hop)

async function quoteExactInputSingle(tokenIn, tokenOut, fee, amountIn) {
    // IMPORTANT: Use callStatic for view-like behavior
    // IQuoterV2 uses struct parameters
    const [amountOut, sqrtPriceX96After, ticksCrossed, gasEstimate] =
        await quoter.callStatic.quoteExactInputSingle({
            tokenIn,
            tokenOut,
            amountIn,
            fee,
            sqrtPriceLimitX96: 0
        });

    return {
        amountOut,
        priceImpact: calculatePriceImpact(amountIn, amountOut, sqrtPriceX96After),
        ticksCrossed,
        gasEstimate
    };
}

// Usage
const quote = await quoteExactInputSingle(
    WVC,
    USDT,
    3000,
    ethers.utils.parseEther('1')
);

console.log('Expected output:', ethers.utils.formatUnits(quote.amountOut, 6), 'USDT');

Quote Exact Output (Single Hop)

Multi-Hop Quoting

Path Encoding

Quote Multi-Hop Exact Input

Quote Multi-Hop Exact Output

Note: Path is reversed for exact output.

Route Comparison

Find Best Route

Compare Gas Costs

Price Impact Calculation

Calculate Price Impact

Warning Thresholds

Building a Quote Interface

Full Quote Function

React Hook Example

Error Handling

Caching Quotes

Best Practices

  1. Always use callStatic - Quoter functions revert with data

  2. Cache quotes - Reduce RPC calls for repeated queries

  3. Handle failures gracefully - Routes may not exist

  4. Consider gas costs - For small trades, multi-hop may not be worth it

  5. Update quotes before execution - Prices change rapidly

Last updated