UNPKG

@agentek/tools

Version:

Blockchain tools for AI agents

293 lines 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTransactionReceipt = exports.getTransaction = exports.getFeeHistory = exports.estimateGas = exports.getGasPrice = exports.getBlockNumber = exports.getBlock = exports.getTransactionCount = exports.getCode = exports.getBalance = void 0; const zod_1 = require("zod"); const client_js_1 = require("../client.js"); const viem_1 = require("viem"); const chains_1 = require("viem/chains"); const utils_js_1 = require("../utils.js"); const supportedChains = [ chains_1.mainnet, chains_1.base, chains_1.arbitrum, chains_1.polygon, chains_1.optimism, chains_1.mode, chains_1.sepolia, ]; const getBalance = (0, client_js_1.createTool)({ name: "getBalance", description: "Get the ETH balance for an address", supportedChains, parameters: zod_1.z.object({ address: zod_1.z.string(), chainId: zod_1.z.number().optional(), formatEth: zod_1.z.boolean().optional(), }), execute: async (client, args) => { if (args.chainId) { const publicClient = client.getPublicClient(args.chainId); const balance = await publicClient.getBalance({ address: args.address, }); return (0, utils_js_1.clean)(args.formatEth ? (0, viem_1.formatEther)(balance) : balance.toString()); } const results = await Promise.all(client.filterSupportedChains(supportedChains).map(async (chain) => { const publicClient = client.getPublicClient(chain.id); const balance = await publicClient.getBalance({ address: args.address, }); return (0, utils_js_1.clean)({ chainId: chain.id, balance: args.formatEth ? (0, viem_1.formatEther)(balance) : balance.toString(), }); })); return (0, utils_js_1.clean)(results); }, }); exports.getBalance = getBalance; const getCode = (0, client_js_1.createTool)({ name: "getCode", description: "Get the bytecode of an address", supportedChains, parameters: zod_1.z.object({ address: zod_1.z.string(), chainId: zod_1.z.number().optional(), }), execute: async (client, args) => { if (args.chainId) { const publicClient = client.getPublicClient(args.chainId); const code = await publicClient.getCode({ address: args.address }); return (0, utils_js_1.clean)(code); } const results = await Promise.all(client.filterSupportedChains(supportedChains).map(async (chain) => { const publicClient = client.getPublicClient(chain.id); const code = await publicClient.getCode({ address: args.address, }); return (0, utils_js_1.clean)({ chainId: chain.id, code, }); })); return (0, utils_js_1.clean)(results); }, }); exports.getCode = getCode; const getTransactionCount = (0, client_js_1.createTool)({ name: "getTransactionCount", description: "Get the number of transactions sent from an address", supportedChains, parameters: zod_1.z.object({ address: zod_1.z.string(), chainId: zod_1.z.number().optional(), }), execute: async (client, args) => { if (args.chainId) { const publicClient = client.getPublicClient(args.chainId); const count = await publicClient.getTransactionCount({ address: args.address, }); return (0, utils_js_1.clean)(count); } const results = await Promise.all(client.filterSupportedChains(supportedChains).map(async (chain) => { const publicClient = client.getPublicClient(chain.id); const count = await publicClient.getTransactionCount({ address: args.address, }); return (0, utils_js_1.clean)({ chainId: chain.id, count, }); })); return (0, utils_js_1.clean)(results); }, }); exports.getTransactionCount = getTransactionCount; // Block Tools const getBlock = (0, client_js_1.createTool)({ name: "getBlock", description: "Get information about a block", supportedChains, parameters: zod_1.z.object({ blockNumber: zod_1.z.number(), chainId: zod_1.z.number(), }), execute: async (client, args) => { const publicClient = client.getPublicClient(args.chainId); if (args.blockNumber) { const block = await publicClient.getBlock({ blockNumber: BigInt(args.blockNumber), }); return (0, utils_js_1.clean)(block); } const block = await publicClient.getBlock(); return (0, utils_js_1.clean)(block); }, }); exports.getBlock = getBlock; const getBlockNumber = (0, client_js_1.createTool)({ name: "getBlockNumber", description: "Get the current block number", supportedChains, parameters: zod_1.z.object({ chainId: zod_1.z.number().optional(), }), execute: async (client, args) => { if (args.chainId) { const publicClient = client.getPublicClient(args.chainId); const blockNumber = await publicClient.getBlockNumber(); return (0, utils_js_1.clean)({ chainId: args.chainId, blockNumber: blockNumber.toString(), }); } const results = await Promise.all(client.filterSupportedChains(supportedChains).map(async (chain) => { const publicClient = client.getPublicClient(chain.id); const blockNumber = await publicClient.getBlockNumber(); return (0, utils_js_1.clean)({ chainId: chain.id, blockNumber: blockNumber.toString(), }); })); return (0, utils_js_1.clean)(results); }, }); exports.getBlockNumber = getBlockNumber; // Gas Tools const getGasPrice = (0, client_js_1.createTool)({ name: "getGasPrice", description: "Get the current gas price. If chainId is not specified, will return gas price for all supported chains.", supportedChains, parameters: zod_1.z.object({ chainId: zod_1.z.number().optional(), formatGwei: zod_1.z.boolean().optional(), }), execute: async (client, args) => { const chains = client.filterSupportedChains(supportedChains, args.chainId); if (args.chainId) { const publicClient = client.getPublicClient(args.chainId); const gasPrice = await publicClient.getGasPrice(); return (0, utils_js_1.clean)({ chainId: args.chainId, gasPrice: args.formatGwei ? (0, viem_1.formatUnits)(gasPrice, 9) : gasPrice.toString(), }); } const results = await Promise.all(chains.map(async (chain) => { const publicClient = client.getPublicClient(chain.id); const gasPrice = await publicClient.getGasPrice(); return (0, utils_js_1.clean)({ chainId: chain.id, gasPrice: args.formatGwei ? (0, viem_1.formatUnits)(gasPrice, 9) : gasPrice.toString(), }); })); return (0, utils_js_1.clean)(results); }, }); exports.getGasPrice = getGasPrice; const estimateGas = (0, client_js_1.createTool)({ name: "estimateGas", description: "Estimate gas for a transaction", supportedChains, parameters: zod_1.z.object({ to: utils_js_1.addressSchema, value: zod_1.z.string().optional(), data: zod_1.z.string().optional(), chainId: zod_1.z.number().optional(), }), execute: async (client, args) => { if (args.chainId) { const publicClient = client.getPublicClient(args.chainId); const from = await client.getAddress(); const gas = await publicClient.estimateGas({ account: from, to: args.to, value: args.value ? (0, viem_1.parseEther)(args.value) : undefined, data: args.data, }); return (0, utils_js_1.clean)({ chainId: args.chainId, gas: gas.toString(), }); } const from = await client.getAddress(); const results = await Promise.all(client.filterSupportedChains(supportedChains).map(async (chain) => { const publicClient = client.getPublicClient(chain.id); const gas = await publicClient.estimateGas({ account: from, to: args.to, value: args.value ? (0, viem_1.parseEther)(args.value) : undefined, data: args.data, }); return (0, utils_js_1.clean)({ chainId: chain.id, gas: gas.toString(), }); })); return (0, utils_js_1.clean)(results); }, }); exports.estimateGas = estimateGas; const getFeeHistory = (0, client_js_1.createTool)({ name: "getFeeHistory", description: "Get historical gas fee info", supportedChains, parameters: zod_1.z.object({ blockCount: zod_1.z .number() .describe("Number of blocks in the requested range. Between 1 and 1024 blocks can be requested in a single query. Less than requested may be returned if not all blocks are available."), rewardPercentiles: zod_1.z .array(zod_1.z.number()) .optional() .describe("A monotonically increasing list of percentile values to sample from each block's effective priority fees per gas in ascending order, weighted by gas used."), chainId: zod_1.z.number(), }), execute: async (client, args) => { const publicClient = client.getPublicClient(args.chainId); const history = await publicClient.getFeeHistory({ blockCount: args.blockCount, rewardPercentiles: args.rewardPercentiles || [], }); return (0, utils_js_1.clean)(history); }, }); exports.getFeeHistory = getFeeHistory; const getTransaction = (0, client_js_1.createTool)({ name: "getTransaction", description: "Get details about a transaction", supportedChains, parameters: zod_1.z.object({ hash: zod_1.z.string(), chainId: zod_1.z.number(), }), execute: async (client, args) => { const publicClient = client.getPublicClient(args.chainId); const tx = await publicClient.getTransaction({ hash: args.hash, }); return (0, utils_js_1.clean)(tx); }, }); exports.getTransaction = getTransaction; const getTransactionReceipt = (0, client_js_1.createTool)({ name: "getTransactionReceipt", description: "Get the receipt of a transaction", supportedChains, parameters: zod_1.z.object({ hash: zod_1.z.string(), chainId: zod_1.z.number(), }), execute: async (client, args) => { const publicClient = client.getPublicClient(args.chainId); let receipt = await publicClient.getTransactionReceipt({ hash: args.hash, }); return (0, utils_js_1.clean)(receipt); }, }); exports.getTransactionReceipt = getTransactionReceipt; //# sourceMappingURL=tools.js.map