@agentek/tools
Version:
Blockchain tools for AI agents
98 lines • 3.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.intentSendTransaction = 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 intentSendTransactionParameters = zod_1.z.object({
to: utils_js_1.addressSchema.describe("The target contract or recipient address"),
value: zod_1.z
.string()
.optional()
.default("0")
.describe("ETH value to send in ether (e.g. '0.1' for 0.1 ETH)"),
abi: zod_1.z
.array(zod_1.z.string())
.optional()
.describe("Human-readable ABI signatures for the function to call, e.g. ['function transfer(address to, uint256 amount) returns (bool)']"),
functionName: zod_1.z
.string()
.optional()
.describe("The function name to call, e.g. 'transfer'"),
args: zod_1.z
.array(zod_1.z.any())
.optional()
.describe("The arguments to pass to the function, e.g. ['0x1234...', '1000000000000000000']"),
data: zod_1.z
.string()
.optional()
.describe("Raw hex calldata. Use this only if abi/functionName/args are not provided."),
chainId: zod_1.z.number().describe("Chain ID to send the transaction on"),
});
exports.intentSendTransaction = (0, client_js_1.createTool)({
name: "intentSendTransaction",
description: "Send an arbitrary transaction to any address. Specify a human-readable ABI signature with function name and arguments to encode calldata automatically, or provide raw hex data. Use this for any contract interaction not covered by other tools.",
supportedChains,
parameters: intentSendTransactionParameters,
execute: async (client, args) => {
const { to, value, abi, functionName, args: fnArgs, data, chainId } = args;
let calldata = "0x";
if (abi && functionName) {
const parsedAbi = (0, viem_1.parseAbi)(abi);
calldata = (0, viem_1.encodeFunctionData)({
abi: parsedAbi,
functionName,
args: fnArgs || [],
});
}
else if (data) {
if (!data.startsWith("0x")) {
throw new Error("Raw data must be a hex string starting with 0x");
}
calldata = data;
}
const weiValue = (0, viem_1.parseEther)(value || "0").toString();
const ops = [
{
target: to,
value: weiValue,
data: calldata,
},
];
const fnDesc = functionName
? `${functionName}(${(fnArgs || []).join(", ")})`
: calldata === "0x"
? "transfer ETH"
: "raw call";
const intent = `Send tx to ${to}: ${fnDesc}`;
const walletClient = client.getWalletClient(chainId);
if (!walletClient) {
return {
intent,
ops,
chain: chainId,
};
}
else {
const hash = await client.executeOps(ops, chainId);
return {
intent,
ops,
chain: chainId,
hash,
};
}
},
});
//# sourceMappingURL=intents.js.map