@agentek/tools
Version:
Blockchain tools for AI agents
281 lines • 10.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.intentReverseSlowTransfer = exports.intentUnlockSlow = exports.intentApproveSlowTransfer = exports.intentWithdrawFromSlow = exports.intentSetSlowGuardian = exports.intentDepositToSlow = void 0;
const zod_1 = require("zod");
const client_js_1 = require("../client.js");
const constants_js_1 = require("./constants.js");
const viem_1 = require("viem");
const utils_js_1 = require("../utils.js");
exports.intentDepositToSlow = (0, client_js_1.createTool)({
name: "intentDepositToSlow",
description: "Deposit tokens or ETH into SLOW contract with a timelock",
supportedChains: constants_js_1.slowTransferChains,
parameters: zod_1.z.object({
chainId: zod_1.z.number().describe("The chainId to execute the intent on."),
token: utils_js_1.addressSchema.describe("The token address to deposit (use 0x0000000000000000000000000000000000000000 for ETH)"),
to: utils_js_1.addressSchema.describe("The recipient address"),
amount: zod_1.z
.string()
.describe("The amount to deposit in human readable format (e.g. for 0.01 ETH use 0.01)"),
delay: zod_1.z.number().describe("The timelock delay in seconds"),
}),
execute: async (client, args) => {
const { chainId, token, to, amount, delay } = args;
const isEth = token === "0x0000000000000000000000000000000000000000";
const value = isEth ? (0, viem_1.parseEther)(amount).toString() : "0";
const intent = `Deposit ${amount} ${isEth ? "ETH" : token} to ${to} with a ${delay} seconds delay`;
const ops = [];
if (isEth) {
ops.push({
target: constants_js_1.SLOW_ADDRESS,
data: (0, viem_1.encodeFunctionData)({
abi: constants_js_1.slowAbi,
functionName: "depositTo",
args: [token, to, 0n, BigInt(delay), "0x"],
}),
value,
});
}
else {
const decimals = await client.getPublicClient(chainId).readContract({
address: token,
abi: viem_1.erc20Abi,
functionName: "decimals",
});
const tokenAmount = (0, viem_1.parseUnits)(amount, decimals);
ops.push({
target: token,
data: (0, viem_1.encodeFunctionData)({
abi: viem_1.erc20Abi,
functionName: "approve",
args: [constants_js_1.SLOW_ADDRESS, tokenAmount],
}),
value: "0",
});
ops.push({
target: constants_js_1.SLOW_ADDRESS,
data: (0, viem_1.encodeFunctionData)({
abi: constants_js_1.slowAbi,
functionName: "depositTo",
args: [token, to, tokenAmount, BigInt(delay), "0x"],
}),
value,
});
}
const walletClient = client.getWalletClient(chainId);
if (walletClient) {
const hash = await client.executeOps(ops, chainId);
return {
intent,
chain: chainId,
ops,
hash,
};
}
return {
intent,
chain: chainId,
ops,
};
},
});
exports.intentSetSlowGuardian = (0, client_js_1.createTool)({
name: "intentSetSlowGuardian",
description: "Set a guardian for a user in the SLOW contract",
supportedChains: constants_js_1.slowTransferChains,
parameters: zod_1.z.object({
chainId: zod_1.z.number().describe("The chainId to execute the intent on."),
guardian: utils_js_1.addressSchema.describe("The guardian address to set (use zero address to remove guardian)"),
}),
execute: async (client, args) => {
const { chainId, guardian } = args;
const intent = `Set guardian to ${guardian}`;
const ops = [
{
target: constants_js_1.SLOW_ADDRESS,
data: (0, viem_1.encodeFunctionData)({
abi: constants_js_1.slowAbi,
functionName: "setGuardian",
args: [guardian],
}),
value: "0",
},
];
const walletClient = client.getWalletClient(chainId);
if (walletClient) {
const hash = await client.executeOps(ops, chainId);
return {
intent,
chain: chainId,
ops,
hash,
};
}
return {
intent,
chain: chainId,
ops,
};
},
});
exports.intentWithdrawFromSlow = (0, client_js_1.createTool)({
name: "intentWithdrawFromSlow",
description: "Withdraw unlocked tokens from SLOW contract",
supportedChains: constants_js_1.slowTransferChains,
parameters: zod_1.z.object({
chainId: zod_1.z.number().describe("The chainId to execute the intent on."),
from: utils_js_1.addressSchema.describe("The address to withdraw from"),
to: utils_js_1.addressSchema.describe("The recipient address"),
id: zod_1.z.string().describe("The token ID to withdraw"),
amount: zod_1.z.string().describe("The amount to withdraw"),
}),
execute: async (client, args) => {
const { chainId, from, to, id, amount } = args;
const intent = `Withdraw ${amount} of ${id} tokenId from ${from} to ${to}`;
const ops = [
{
target: constants_js_1.SLOW_ADDRESS,
data: (0, viem_1.encodeFunctionData)({
abi: constants_js_1.slowAbi,
functionName: "withdrawFrom",
args: [from, to, BigInt(id), BigInt(amount)],
}),
value: "0",
},
];
const walletClient = client.getWalletClient(chainId);
if (walletClient) {
const hash = await client.executeOps(ops, chainId);
return {
intent,
chain: chainId,
ops,
hash,
};
}
return {
intent,
chain: chainId,
ops,
};
},
});
exports.intentApproveSlowTransfer = (0, client_js_1.createTool)({
name: "intentApproveSlowTransfer",
description: "Guardian approves a transfer in SLOW contract",
supportedChains: constants_js_1.slowTransferChains,
parameters: zod_1.z.object({
chainId: zod_1.z.number().describe("The chainId to execute the intent on."),
from: utils_js_1.addressSchema.describe("The user address that initiated the transfer"),
transferId: zod_1.z.string().describe("The transfer ID to approve"),
}),
execute: async (client, args) => {
const { chainId, from, transferId } = args;
const intent = `Approve transfer with ID ${transferId}`;
const ops = [
{
target: constants_js_1.SLOW_ADDRESS,
data: (0, viem_1.encodeFunctionData)({
abi: constants_js_1.slowAbi,
functionName: "approveTransfer",
args: [from, BigInt(transferId)],
}),
value: "0",
},
];
const walletClient = client.getWalletClient(chainId);
if (walletClient) {
const hash = await client.executeOps(ops, chainId);
return {
intent,
chain: chainId,
ops,
hash,
};
}
return {
intent,
chain: chainId,
ops,
};
},
});
exports.intentUnlockSlow = (0, client_js_1.createTool)({
name: "intentUnlockSlow",
description: "Unlock a time-locked transfer in SLOW contract",
supportedChains: constants_js_1.slowTransferChains,
parameters: zod_1.z.object({
chainId: zod_1.z.number().describe("The chainId to execute the intent on."),
transferId: zod_1.z.string().describe("The transfer ID to unlock"),
}),
execute: async (client, args) => {
const { chainId, transferId } = args;
const intent = `Unlock transfer with ID ${transferId}`;
const ops = [
{
target: constants_js_1.SLOW_ADDRESS,
data: (0, viem_1.encodeFunctionData)({
abi: constants_js_1.slowAbi,
functionName: "unlock",
args: [BigInt(transferId)],
}),
value: "0",
},
];
const walletClient = client.getWalletClient(chainId);
if (walletClient) {
const hash = await client.executeOps(ops, chainId);
return {
intent,
chain: chainId,
ops,
hash,
};
}
return {
intent,
chain: chainId,
ops,
};
},
});
exports.intentReverseSlowTransfer = (0, client_js_1.createTool)({
name: "intentReverseSlowTransfer",
description: "Reverse a pending transfer in SLOW contract",
supportedChains: constants_js_1.slowTransferChains,
parameters: zod_1.z.object({
chainId: zod_1.z.number().describe("The chainId to execute the intent on."),
transferId: zod_1.z.string().describe("The transfer ID to reverse"),
}),
execute: async (client, args) => {
const { chainId, transferId } = args;
const intent = `Reverse transfer with ID ${transferId}`;
const ops = [
{
target: constants_js_1.SLOW_ADDRESS,
data: (0, viem_1.encodeFunctionData)({
abi: constants_js_1.slowAbi,
functionName: "reverse",
args: [BigInt(transferId)],
}),
value: "0",
},
];
const walletClient = client.getWalletClient(chainId);
if (walletClient) {
const hash = await client.executeOps(ops, chainId);
return {
intent,
chain: chainId,
ops,
hash,
};
}
return {
intent,
chain: chainId,
ops,
};
},
});
//# sourceMappingURL=intents.js.map