get_swap_quote
Retrieve swap quotes from Uniswap protocols to compare estimated outputs, routing paths, gas fees, and price impact before executing token trades.
Instructions
Get a swap quote from Uniswap. Returns estimated output, routing path, gas fees, and price impact. Supports V2, V3, V4, and UniswapX protocols. Requires UNISWAP_API_KEY env var.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tokenIn | Yes | Input token symbol (ETH, USDC, USDT, DAI, WETH) or contract address | |
| tokenOut | Yes | Output token symbol (ETH, USDC, USDT, DAI, WETH) or contract address | |
| amount | Yes | Amount of input token to swap (in human-readable units, e.g. "100" for 100 USDC) | |
| walletAddress | No | Wallet address of the swapper. Uses W3SHIP_PUBLIC_KEY if not provided. | |
| chainId | No | Chain ID (default: 8453 for Base) |
Implementation Reference
- src/index.ts:742-836 (handler)The handler logic for 'get_swap_quote' tool, which fetches a quote from the Uniswap API.
case 'get_swap_quote': { if (!UNISWAP_API_KEY) { return { content: [{ type: 'text', text: 'Error: UNISWAP_API_KEY environment variable is not set. Get your API key at https://developers.uniswap.org' }], isError: true, }; } const { tokenIn: tokenInArg, tokenOut: tokenOutArg, amount: amountArg, walletAddress: swapWallet, chainId: swapChain } = args as any; const chainId = swapChain || 8453; // Default to Base const wallet = swapWallet || CONFIGURED_KEY; if (!wallet) { return { content: [{ type: 'text', text: 'Error: Wallet address required. Set W3SHIP_PUBLIC_KEY or provide walletAddress.' }], isError: true, }; } // Resolve token symbols to addresses const resolveToken = (t: string) => { const upper = t.toUpperCase(); return TOKENS[upper]?.address || t; }; const resolveDecimals = (t: string) => { const upper = t.toUpperCase(); return TOKENS[upper]?.decimals || 18; }; const tokenInAddr = resolveToken(tokenInArg); const tokenOutAddr = resolveToken(tokenOutArg); const decimals = resolveDecimals(tokenInArg); // Convert human-readable amount to wei/smallest unit const amountRaw = BigInt(Math.floor(parseFloat(amountArg) * (10 ** decimals))).toString(); try { const quoteRes = await fetch(`${UNISWAP_API}/quote`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': UNISWAP_API_KEY, }, body: JSON.stringify({ tokenIn: tokenInAddr, tokenOut: tokenOutAddr, amount: amountRaw, type: 'EXACT_INPUT', swapper: wallet, tokenInChainId: chainId, tokenOutChainId: chainId, protocols: ['V2', 'V3', 'V4', 'UNISWAPX'], }), }); const quoteData = await quoteRes.json() as any; if (!quoteRes.ok) { return { content: [{ type: 'text', text: `Uniswap API error: ${quoteData.errorCode || quoteData.detail || JSON.stringify(quoteData)}` }], isError: true, }; } // Format output amount const outDecimals = resolveDecimals(tokenOutArg); const outputRaw = BigInt(quoteData.quote?.amountOut || quoteData.amountOut || '0'); const outputFormatted = (Number(outputRaw) / (10 ** outDecimals)).toFixed(6); return { content: [{ type: 'text', text: JSON.stringify({ quote: { tokenIn: tokenInArg.toUpperCase(), tokenOut: tokenOutArg.toUpperCase(), amountIn: amountArg, amountOut: outputFormatted, chainId, gasEstimate: quoteData.quote?.gasEstimate || quoteData.gasEstimate || 'N/A', priceImpact: quoteData.quote?.priceImpact || quoteData.priceImpact || 'N/A', routingPath: quoteData.quote?.route || quoteData.route || [], }, message: `Swap ${amountArg} ${tokenInArg.toUpperCase()} → ${outputFormatted} ${tokenOutArg.toUpperCase()} on chain ${chainId}`, needsApproval: quoteData.permit2 ? true : false, }, null, 2) }] }; } catch (e: any) { return { content: [{ type: 'text', text: `Error getting swap quote: ${e.message}` }], isError: true, }; } }