@agentek/tools
Version:
Blockchain tools for AI agents
300 lines • 12.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokenMetadataTool = exports.getSymbolTool = exports.getNameTool = exports.getDecimalsTool = exports.getTotalSupplyTool = exports.getBalanceOfTool = exports.getAllowanceTool = void 0;
const zod_1 = require("zod");
const client_js_1 = require("../client.js");
const viem_1 = require("viem");
const constants_js_1 = require("./constants.js");
const getAllowanceParameters = zod_1.z.object({
token: zod_1.z.string().describe("The token address"),
owner: zod_1.z.string().describe("The token owner's address"),
spender: zod_1.z.string().describe("The spender's address"),
chainId: zod_1.z
.number()
.optional()
.describe("If not specified, returns approval for all supported chains."),
});
const getBalanceOfParameters = zod_1.z.object({
token: zod_1.z.string().describe("The token address"),
owner: zod_1.z.string().describe("The token owner's address"),
chainId: zod_1.z
.number()
.optional()
.describe("If not specified, returns balance for all supported chains."),
});
const getTotalSupplyParameters = zod_1.z.object({
token: zod_1.z.string().describe("The token address"),
chainId: zod_1.z
.number()
.optional()
.describe("If not specified, returns total supply for all supported chains."),
});
const getDecimalsParameters = zod_1.z.object({
token: zod_1.z.string().describe("The token address"),
chainId: zod_1.z
.number()
.optional()
.describe("If not specified, returns decimals for all supported chains."),
});
const getNameParameters = zod_1.z.object({
token: zod_1.z.string().describe("The token address"),
chainId: zod_1.z
.number()
.optional()
.describe("If not specified, returns name for all supported chains."),
});
const getSymbolParameters = zod_1.z.object({
token: zod_1.z.string().describe("The token address"),
chainId: zod_1.z
.number()
.optional()
.describe("If not specified, returns symbol for all supported chains."),
});
const tokenMetadataParameters = zod_1.z.object({
token: zod_1.z.string().describe("The token address"),
chainId: zod_1.z.number().describe("The token chain"),
});
exports.getAllowanceTool = (0, client_js_1.createTool)({
name: "getAllowance",
description: "Gets the ERC20 token allowance between an owner and spender",
supportedChains: constants_js_1.erc20Chains,
parameters: getAllowanceParameters,
execute: async (client, args) => {
const { token, owner, spender, chainId } = args;
const chains = client.filterSupportedChains(constants_js_1.erc20Chains, chainId);
const allowances = await Promise.all(chains.map(async (chain) => {
const publicClient = client.getPublicClient(chain.id);
try {
const decimals = await publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "decimals",
});
const allowance = await publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "allowance",
args: [owner, spender],
});
return {
chain: chain.id,
allowance: (0, viem_1.formatUnits)(allowance, decimals),
};
}
catch (error) {
// @TODO type specific viem short errors
return {
chain: chain.id,
error: `Failed to fetch allowance: ${typeof error === "object" && error ? ("shortMessage" in error ? error.shortMessage : "message" in error ? error.message : "Unknown error") : String(error)}`,
};
}
}));
return allowances;
},
});
exports.getBalanceOfTool = (0, client_js_1.createTool)({
name: "getBalanceOf",
description: "Gets the ERC20 token balance of an address",
supportedChains: constants_js_1.erc20Chains,
parameters: getBalanceOfParameters,
execute: async (client, args) => {
const { token, owner, chainId } = args;
const chains = client.filterSupportedChains(constants_js_1.erc20Chains, chainId);
const balances = await Promise.all(chains.map(async (chain) => {
const publicClient = client.getPublicClient(chain.id);
try {
const decimals = await publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "decimals",
});
const balance = await publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "balanceOf",
args: [owner],
});
return {
chain: chain.id,
balance: (0, viem_1.formatUnits)(balance, decimals),
};
}
catch (error) {
return {
chain: chain.id,
error: `Failed to fetch balance: ${typeof error === "object" && error ? ("shortMessage" in error ? error.shortMessage : "message" in error ? error.message : "Unknown error") : String(error)}`,
};
}
}));
return balances;
},
});
exports.getTotalSupplyTool = (0, client_js_1.createTool)({
name: "getTotalSupply",
description: "Gets the total supply of an ERC20 token",
supportedChains: constants_js_1.erc20Chains,
parameters: getTotalSupplyParameters,
execute: async (client, args) => {
const { token, chainId } = args;
const chains = client.filterSupportedChains(constants_js_1.erc20Chains, chainId);
const supplies = await Promise.all(chains.map(async (chain) => {
const publicClient = client.getPublicClient(chain.id);
try {
const decimals = await publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "decimals",
});
const totalSupply = await publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "totalSupply",
});
return {
chain: chain.id,
totalSupply: (0, viem_1.formatUnits)(totalSupply, decimals),
};
}
catch (error) {
return {
chain: chain.id,
error: `Failed to fetch total supply: ${typeof error === "object" && error ? ("shortMessage" in error ? error.shortMessage : "message" in error ? error.message : "Unknown error") : String(error)}`,
};
}
}));
return supplies;
},
});
exports.getDecimalsTool = (0, client_js_1.createTool)({
name: "getDecimals",
description: "Gets the number of decimals of an ERC20 token",
supportedChains: constants_js_1.erc20Chains,
parameters: getDecimalsParameters,
execute: async (client, args) => {
const { token, chainId } = args;
const chains = client.filterSupportedChains(constants_js_1.erc20Chains, chainId);
const tokenDecimals = await Promise.all(chains.map(async (chain) => {
const publicClient = client.getPublicClient(chain.id);
try {
const decimals = await publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "decimals",
});
return {
chain: chain.id,
decimals,
};
}
catch (error) {
return {
chain: chain.id,
error: `Failed to fetch decimals: ${typeof error === "object" && error ? ("shortMessage" in error ? error.shortMessage : "message" in error ? error.message : "Unknown error") : String(error)}`,
};
}
}));
return tokenDecimals;
},
});
exports.getNameTool = (0, client_js_1.createTool)({
name: "getName",
description: "Gets the name of an ERC20 token",
supportedChains: constants_js_1.erc20Chains,
parameters: getNameParameters,
execute: async (client, args) => {
const { token, chainId } = args;
const chains = client.filterSupportedChains(constants_js_1.erc20Chains, chainId);
const names = await Promise.all(chains.map(async (chain) => {
const publicClient = client.getPublicClient(chain.id);
try {
const name = await publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "name",
});
return {
chain: chain.id,
name,
};
}
catch (error) {
return {
chain: chain.id,
error: `Failed to fetch name: ${typeof error === "object" && error ? ("shortMessage" in error ? error.shortMessage : "message" in error ? error.message : "Unknown error") : String(error)}`,
};
}
}));
return names;
},
});
exports.getSymbolTool = (0, client_js_1.createTool)({
name: "getSymbol",
description: "Gets the symbol of an ERC20 token",
supportedChains: constants_js_1.erc20Chains,
parameters: getSymbolParameters,
execute: async (client, args) => {
const { token, chainId } = args;
const chains = client.filterSupportedChains(constants_js_1.erc20Chains, chainId);
const symbols = await Promise.all(chains.map(async (chain) => {
const publicClient = client.getPublicClient(chain.id);
try {
const symbol = await publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "symbol",
});
return {
chain: chain.id,
symbol,
};
}
catch (error) {
return {
chain: chain.id,
error: `Failed to fetch symbol: ${typeof error === "object" && error ? ("shortMessage" in error ? error.shortMessage : "message" in error ? error.message : "Unknown error") : String(error)}`,
};
}
}));
return symbols;
},
});
exports.getTokenMetadataTool = (0, client_js_1.createTool)({
name: "getTokenMetadata",
description: "Gets all metadata (name, symbol, decimals, totalSupply) of an ERC20 token",
supportedChains: constants_js_1.erc20Chains,
parameters: tokenMetadataParameters,
execute: async (client, args) => {
const { token, chainId } = args;
const publicClient = client.getPublicClient(chainId);
const [name, symbol, decimals, totalSupply] = await Promise.all([
publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "name",
}),
publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "symbol",
}),
publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "decimals",
}),
publicClient.readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "totalSupply",
}),
]);
return {
chain: chainId,
name,
symbol,
decimals,
totalSupply: (0, viem_1.formatUnits)(totalSupply, decimals),
};
},
});
//# sourceMappingURL=tools.js.map