UNPKG

@agentek/tools

Version:

Blockchain tools for AI agents

66 lines 3.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getQuote = void 0; const zod_1 = require("zod"); const client_js_1 = require("../client.js"); const zrouter_sdk_1 = require("zrouter-sdk"); const chains_1 = require("viem/chains"); const utils_js_1 = require("./utils.js"); const types_js_1 = require("./types.js"); const constants_js_1 = require("./constants.js"); const api_js_1 = require("./api.js"); exports.getQuote = (0, client_js_1.createTool)({ name: "getQuote", description: "Get a price quote for swapping ERC20 or ERC6909 tokens via the zRouter. Returns expected output amount and routing info. Does not execute the swap.", supportedChains: constants_js_1.supportedChains, parameters: zod_1.z.object({ chainId: zod_1.z.number().default(1).describe("Chain ID (1 for Mainnet, 8453 for Base). Default: 1"), tokenIn: types_js_1.SymbolOrTokenSchema.describe('Input token — either a symbol string (e.g. "USDT", "ETH") or an object { address, id? } for ERC6909 tokens'), tokenOut: types_js_1.SymbolOrTokenSchema.describe('Output token — either a symbol string (e.g. "IZO", "WETH") or an object { address, id? } for ERC6909 tokens'), amount: zod_1.z.union([zod_1.z.number(), zod_1.z.string()]).describe("Amount in human-readable units (e.g. 1.5 or '1.5'). Refers to tokenIn for EXACT_IN, tokenOut for EXACT_OUT."), side: zod_1.z.enum(["EXACT_IN", "EXACT_OUT"]).describe("EXACT_IN: specify the input amount and get the best output. EXACT_OUT: specify the desired output and get the required input."), }), execute: async (client, args) => { const { tokenIn, tokenOut, side } = args; const chainId = (args.chainId || chains_1.mainnet.id); const amountInput = typeof args.amount === "number" ? args.amount.toString() : args.amount; const publicClient = client.getPublicClient(chainId); // --- resolve tokens (symbol -> address[/id]) and decimals/standard --- const [tIn, tOut] = await Promise.all([ (0, utils_js_1.resolveInputToToken)(tokenIn, chainId), (0, utils_js_1.resolveInputToToken)(tokenOut, chainId), ]); // --- parse amount into base units depending on standard --- const parsedAmount = side === "EXACT_IN" ? (0, utils_js_1.toBaseUnits)(amountInput, tIn) : (0, utils_js_1.toBaseUnits)(amountInput, tOut); // Try API first (includes Matcha/0x aggregated quotes alongside on-chain) const apiRoutes = await (0, api_js_1.fetchApiRoutes)({ chainId, tokenIn: { address: tIn.address, ...(tIn.id !== undefined ? { id: tIn.id } : {}) }, tokenOut: { address: tOut.address, ...(tOut.id !== undefined ? { id: tOut.id } : {}) }, side, amount: parsedAmount, owner: "0x0000000000000000000000000000000000010000", }); if (apiRoutes && apiRoutes.length > 0) { const best = apiRoutes[0]; return { expectedAmount: best.expectedAmount, venue: best.venue, sources: best.metadata.sources, routeCount: apiRoutes.length, source: "api", }; } // Fallback to SDK quote (fully on-chain, no API key needed) const q = await (0, zrouter_sdk_1.quote)(publicClient, { tokenIn: (0, utils_js_1.asToken)(tIn), tokenOut: (0, utils_js_1.asToken)(tOut), amount: parsedAmount, side, }); return q; }, }); //# sourceMappingURL=tools.js.map