QUOTE
Get real-time price quotes for token swaps on decentralized exchanges. Specify token addresses, amount, and slippage to calculate exchange rates across blockchain networks.
Instructions
Quote the price of a specific trading pair
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chain | No | The blockchain network to execute the transaction on. uses fraxtal as default | fraxtal |
| inTokenAddress | Yes | The token to swap from (address). | |
| outTokenAddress | Yes | The token to swap to (address). | |
| amount | Yes | Token amount with decimals. For example, if 1 USDT is input, use 1000000 (1 USDT * 10^6). | |
| slippage | No | Define the acceptable slippage level by inputting a percentage value within the range of 0.05 to 50. 1% slippage set as 1. | 1 |
Implementation Reference
- src/tools/swap.ts:7-41 (handler)The main handler function for the 'QUOTE' tool. It processes input arguments, retrieves chain and gas price information, calls the SwapService to fetch the quote, and returns the result as JSON.export const quote = async (args: z.infer<typeof getQuoteParamsSchema>) => { try { const inputChain = args.chain.toLowerCase(); const chainObject = getChainFromName(inputChain); console.error(`[QUOTE] Using chain: ${chainObject.name}`, args); const chainService = new ChainService(); const gasRes: any = await chainService.gasPrice(chainObject.id); const gasPrice = gasRes.data.fast; const swapService = new SwapService(); const quote = await swapService.quote( args.inTokenAddress, args.outTokenAddress, chainObject.id, args.amount, args.slippage ? Number(args.slippage) * 100 : 100, gasPrice, ); if (quote instanceof Error) { return `Error fetching quote: ${quote.message}`; } // return JSON.stringify(quote); return JSON.stringify(quote, null, 2); } catch (error: unknown) { const message = error instanceof Error ? error.message : "An unknown error occurred while fetching quote."; console.error(`[QUOTE] Error: ${message}`); throw new Error(`Failed to fetch quote: ${message}`); } };
- src/tools/index.ts:34-39 (registration)Registration of the 'QUOTE' tool in the tools export object, specifying name, description, parameters schema, and execute function.quote: { name: "QUOTE", description: "Quote the price of a specific trading pair", parameters: getQuoteParamsSchema, execute: swapExecute.quote },
- src/types.ts:28-53 (schema)Zod schema defining the input parameters for the 'QUOTE' tool: chain, inTokenAddress, outTokenAddress, amount, slippage.export const getQuoteParamsSchema = z.object({ chain: z .string() .optional() .describe( "The blockchain network to execute the transaction on. uses fraxtal as default", ) .default("fraxtal"), inTokenAddress: z .string() .refine(isAddress, { message: "Invalid inToken address" }) .describe("The token to swap from (address)."), outTokenAddress: z .string() .refine(isAddress, { message: "Invalid outToken address" }) .describe("The token to swap to (address)."), amount: z .string() .regex(/^\d+$/, { message: "Amount must be a string in wei (no decimals)" }) .describe("Token amount with decimals. For example, if 1 USDT is input, use 1000000 (1 USDT * 10^6). "), slippage: z .string() .optional() .describe("Define the acceptable slippage level by inputting a percentage value within the range of 0.05 to 50. 1% slippage set as 1.") .default('1'), });
- src/services/swap.ts:9-44 (helper)The SwapService.quote method, which makes the actual API call to the DEX to retrieve the quote data used by the QUOTE tool handler.async quote( inTokenAddress: string, outTokenAddress: string, chainId: number, amount: string, slippage: number, gasPrice: string ) { try { const response = await fetch(`${DEX_API_URL}/v2/${chainId}/quote?inTokenAddress=${inTokenAddress}&outTokenAddress=${outTokenAddress}&amount=${amount}&gasPrice=${gasPrice}&slippage=${slippage}&referrer=0xC5d4de874CfE6aac6BC9CAD5Cb6b2B35bd7b8392&flags=4`, { method: "GET", headers: { "Content-Type": "application/json", } }); const data: any = await response.json(); if (!response.ok) { const errorData = data as ErrorResponse; throw new Error( `Failed to fetch quote: ${errorData.detail} (Trace ID: ${errorData.traceId}, Error Code: ${errorData.errorCode})`, ); } const { inToken, outToken, inAmount, outAmount, estimatedGas } = data; return { inToken, outToken, inAmount, outAmount, estimatedGas } as QuoteResponse; } catch (error) { console.error("Error fetching quote:", error); throw new Error( `Fatally Failed to fetch quote: ${(error as Error).message} with code ${ (error as { code?: string }).code || "unknown" }`, ); } }