@agentek/tools
Version:
Blockchain tools for AI agents
268 lines • 10.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.intentAaveRepay = exports.intentAaveBorrow = exports.intentAaveWithdraw = exports.intentAaveDeposit = void 0;
const client_js_1 = require("../client.js");
const zod_1 = require("zod");
const viem_1 = require("viem");
const constants_js_1 = require("./constants.js");
// Intent: Deposit tokens into Aave (i.e. supply to the pool)
exports.intentAaveDeposit = (0, client_js_1.createTool)({
name: "intentAaveDeposit",
description: "Deposits tokens into the Aave protocol to supply liquidity and earn interest.",
supportedChains: constants_js_1.supportedChains,
parameters: zod_1.z.object({
chainId: zod_1.z.number().describe("Chain ID for the deposit."),
asset: zod_1.z.string().describe("Token contract address to deposit."),
amount: zod_1.z
.string()
.describe("Amount of tokens to deposit (in human-readable format)."),
}),
async execute(client, args) {
const walletClient = client.getWalletClient(args.chainId);
const publicClient = client.getPublicClient(args.chainId);
const userAddress = await client.getAddress();
const decimals = await publicClient.readContract({
address: args.asset,
abi: viem_1.erc20Abi,
functionName: "decimals",
});
const amountBigInt = (0, viem_1.parseUnits)(args.amount, decimals);
const poolAddress = (0, constants_js_1.getAavePoolAddress)(args.chainId);
const currentAllowance = await publicClient.readContract({
address: args.asset,
abi: viem_1.erc20Abi,
functionName: "allowance",
args: [userAddress, poolAddress],
});
let ops = [];
if (currentAllowance < amountBigInt) {
const approvalData = (0, viem_1.encodeFunctionData)({
abi: viem_1.erc20Abi,
functionName: "approve",
args: [poolAddress, viem_1.maxUint256],
});
ops.push({
target: args.asset,
value: "0",
data: approvalData,
});
}
const supplyData = (0, viem_1.encodeFunctionData)({
abi: constants_js_1.aavePoolAbi,
functionName: "supply",
args: [args.asset, amountBigInt, userAddress, 0],
});
ops.push({
target: poolAddress,
value: "0",
data: supplyData,
});
const intentDescription = `Deposit ${args.amount} tokens into Aave on chain ${args.chainId}`;
if (!walletClient) {
return {
intent: intentDescription,
ops,
chain: args.chainId,
};
}
else {
const hash = await client.executeOps(ops, args.chainId);
return {
intent: intentDescription,
ops,
chain: args.chainId,
hash,
};
}
},
});
// Intent: Withdraw tokens from Aave
exports.intentAaveWithdraw = (0, client_js_1.createTool)({
name: "intentAaveWithdraw",
description: "Withdraws tokens from Aave, redeeming your supplied assets (aTokens).",
supportedChains: constants_js_1.supportedChains,
parameters: zod_1.z.object({
chainId: zod_1.z.number().describe("Chain ID for the withdrawal."),
asset: zod_1.z.string().describe("Token contract address to withdraw."),
amount: zod_1.z
.string()
.describe("Amount of tokens to withdraw (in human-readable format)."),
}),
async execute(client, args) {
const walletClient = client.getWalletClient(args.chainId);
const publicClient = client.getPublicClient(args.chainId);
const userAddress = await client.getAddress();
const decimals = await publicClient.readContract({
address: args.asset,
abi: viem_1.erc20Abi,
functionName: "decimals",
});
const amountBigInt = (0, viem_1.parseUnits)(args.amount, decimals);
const poolAddress = (0, constants_js_1.getAavePoolAddress)(args.chainId);
const withdrawData = (0, viem_1.encodeFunctionData)({
abi: constants_js_1.aavePoolAbi,
functionName: "withdraw",
args: [args.asset, amountBigInt, userAddress],
});
const ops = [
{
target: poolAddress,
value: "0",
data: withdrawData,
},
];
const intentDescription = `Withdraw ${args.amount} tokens from Aave on chain ${args.chainId}`;
if (!walletClient) {
return {
intent: intentDescription,
ops,
chain: args.chainId,
};
}
else {
const hash = await client.executeOps(ops, args.chainId);
return {
intent: intentDescription,
ops,
chain: args.chainId,
hash,
};
}
},
});
exports.intentAaveBorrow = (0, client_js_1.createTool)({
name: "intentAaveBorrow",
description: "Borrows tokens from Aave using your supplied collateral. By default, the variable rate mode (2) is used.",
supportedChains: constants_js_1.supportedChains,
parameters: zod_1.z.object({
chainId: zod_1.z.number().describe("Chain ID for borrowing."),
asset: zod_1.z.string().describe("Token contract address to borrow."),
amount: zod_1.z
.string()
.describe("Amount of tokens to borrow (in human-readable format)."),
interestRateMode: zod_1.z
.number()
.optional()
.describe("1 for stable, 2 for variable (default is variable)."),
}),
async execute(client, args) {
const walletClient = client.getWalletClient(args.chainId);
const publicClient = client.getPublicClient(args.chainId);
const userAddress = await client.getAddress();
const decimals = await publicClient.readContract({
address: args.asset,
abi: viem_1.erc20Abi,
functionName: "decimals",
});
const amountBigInt = (0, viem_1.parseUnits)(args.amount, decimals);
const interestRateMode = args.interestRateMode ?? 2;
const poolAddress = (0, constants_js_1.getAavePoolAddress)(args.chainId);
const borrowData = (0, viem_1.encodeFunctionData)({
abi: constants_js_1.aavePoolAbi,
functionName: "borrow",
args: [args.asset, amountBigInt, interestRateMode, 0, userAddress],
});
const ops = [
{
target: poolAddress,
value: "0",
data: borrowData,
},
];
const intentDescription = `Borrow ${args.amount} tokens on Aave (using ${interestRateMode === 1 ? "stable" : "variable"} rate) on chain ${args.chainId}`;
if (!walletClient) {
return {
intent: intentDescription,
ops,
chain: args.chainId,
};
}
else {
const hash = await client.executeOps(ops, args.chainId);
return {
intent: intentDescription,
ops,
chain: args.chainId,
hash,
};
}
},
});
// Intent: Repay borrowed tokens on Aave
exports.intentAaveRepay = (0, client_js_1.createTool)({
name: "intentAaveRepay",
description: "Repays your Aave debt. By default, the variable rate mode (2) is used for repayment.",
supportedChains: constants_js_1.supportedChains,
parameters: zod_1.z.object({
chainId: zod_1.z.number().describe("Chain ID for repayment."),
asset: zod_1.z.string().describe("Token contract address of the asset to repay."),
amount: zod_1.z
.string()
.describe("Amount of tokens to repay (in human-readable format)."),
rateMode: zod_1.z
.number()
.optional()
.describe("1 for stable, 2 for variable (default is variable)."),
}),
async execute(client, args) {
const walletClient = client.getWalletClient(args.chainId);
const publicClient = client.getPublicClient(args.chainId);
const userAddress = await client.getAddress();
const decimals = await publicClient.readContract({
address: args.asset,
abi: viem_1.erc20Abi,
functionName: "decimals",
});
const amountBigInt = (0, viem_1.parseUnits)(args.amount, decimals);
const rateMode = args.rateMode ?? 2;
const poolAddress = (0, constants_js_1.getAavePoolAddress)(args.chainId);
let ops = [];
const currentAllowance = await publicClient.readContract({
address: args.asset,
abi: viem_1.erc20Abi,
functionName: "allowance",
args: [userAddress, poolAddress],
});
if (currentAllowance < amountBigInt) {
const approvalData = (0, viem_1.encodeFunctionData)({
abi: viem_1.erc20Abi,
functionName: "approve",
args: [poolAddress, viem_1.maxUint256],
});
ops.push({
target: args.asset,
value: "0",
data: approvalData,
});
}
// repay(address asset, uint256 amount, uint256 rateMode, address onBehalfOf)
const repayData = (0, viem_1.encodeFunctionData)({
abi: constants_js_1.aavePoolAbi,
functionName: "repay",
args: [args.asset, amountBigInt, rateMode, userAddress],
});
ops.push({
target: poolAddress,
value: "0",
data: repayData,
});
const intentDescription = `Repay ${args.amount} tokens on Aave (using ${rateMode === 1 ? "stable" : "variable"} rate) on chain ${args.chainId}`;
if (!walletClient) {
return {
intent: intentDescription,
ops,
chain: args.chainId,
};
}
else {
const hash = await client.executeOps(ops, args.chainId);
return {
intent: intentDescription,
ops,
chain: args.chainId,
hash,
};
}
},
});
//# sourceMappingURL=intents.js.map