UNPKG

@agentek/tools

Version:

Blockchain tools for AI agents

213 lines 8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.intentVoteNaniProposal = exports.intentProposeNani = exports.intentUnstakeNani = exports.intentStakeNani = void 0; const zod_1 = __importDefault(require("zod")); const viem_1 = require("viem"); const chains_1 = require("viem/chains"); const abis_js_1 = require("./abis.js"); const constants_js_1 = require("./constants.js"); const client_js_1 = require("../client.js"); exports.intentStakeNani = (0, client_js_1.createTool)({ name: "intentStakeNani", description: "Stake NANI tokens to receive xNANI tokens, which can be used for governance", parameters: zod_1.default.object({ amount: zod_1.default.string().describe("The amount of NANI tokens to stake"), chainId: zod_1.default.number().describe("The chain to stake on"), }), supportedChains: [chains_1.mainnet, chains_1.base], execute: async (client, args) => { const { chainId, amount } = args; const publicClient = client.getPublicClient(chainId); if (!publicClient) { throw new Error("No public client found for " + chainId); } const walletClient = client.getWalletClient(chainId); const user = await client.getAddress(); const intent = `stake ${amount} NANI`; const allowance = await publicClient.readContract({ address: constants_js_1.NANI_TOKEN, abi: viem_1.erc20Abi, functionName: "allowance", args: [user, constants_js_1.XNANI_TOKEN], }); const amountBigInt = (0, viem_1.parseEther)(amount); let ops = []; if (allowance < amountBigInt) { ops.push({ target: constants_js_1.NANI_TOKEN, value: "0", data: (0, viem_1.encodeFunctionData)({ abi: viem_1.erc20Abi, functionName: "approve", args: [constants_js_1.XNANI_TOKEN, viem_1.maxUint256], }), }); } ops.push({ target: constants_js_1.XNANI_TOKEN, value: "0", data: (0, viem_1.encodeFunctionData)({ abi: abis_js_1.XNaniAbi, functionName: "stake", args: [amountBigInt], }), }); if (walletClient) { const hash = await client.executeOps(ops, chainId); return { intent, chain: chainId, ops, hash, }; } return { intent, chain: chainId, ops, }; }, }); exports.intentUnstakeNani = (0, client_js_1.createTool)({ name: "intentUnstakeNani", description: "Unstake xNANI tokens back to NANI tokens", parameters: zod_1.default.object({ amount: zod_1.default.string().describe("The amount of xNANI tokens to unstake"), chainId: zod_1.default.number().describe("The chain to unstake on"), }), supportedChains: [chains_1.mainnet, chains_1.base], execute: async (client, args) => { const { chainId, amount } = args; const publicClient = client.getPublicClient(chainId); if (!publicClient) { throw new Error("No public client found for " + chainId); } const walletClient = client.getWalletClient(chainId); const user = await client.getAddress(); const intent = `unstake ${amount} xNANI`; const amountBigInt = (0, viem_1.parseEther)(amount); const xNaniBalance = await publicClient.readContract({ address: constants_js_1.XNANI_TOKEN, abi: viem_1.erc20Abi, functionName: "balanceOf", args: [user], }); if (xNaniBalance < amountBigInt) { throw new Error("Insufficient xNANI balance for unstake"); } const ops = [ { target: constants_js_1.XNANI_TOKEN, value: "0", data: (0, viem_1.encodeFunctionData)({ abi: abis_js_1.XNaniAbi, functionName: "unstake", args: [(0, viem_1.parseEther)(amount)], }), }, ]; if (walletClient) { const hash = await client.executeOps(ops, chainId); return { intent, chain: chainId, ops, hash, }; } return { intent, chain: chainId, ops, }; }, }); exports.intentProposeNani = (0, client_js_1.createTool)({ name: "intentProposeNani", description: "Create a new governance proposal for NANIDAO", parameters: zod_1.default.object({ content: zod_1.default.string().describe("The proposal content/description"), chainId: zod_1.default.number().describe("The chain ID to propose on"), }), supportedChains: [chains_1.mainnet, chains_1.base], execute: async (client, args) => { const { chainId } = args; const publicClient = client.getPublicClient(chainId); if (!publicClient) { throw new Error("No public client found for " + chainId); } const walletClient = client.getWalletClient(chainId); const ops = [ { target: constants_js_1.SIGNALS_ADDRESS, value: "0", data: (0, viem_1.encodeFunctionData)({ abi: abis_js_1.SignalsAbi, functionName: "propose", args: [args.content], }), }, ]; if (walletClient) { const hash = await client.executeOps(ops, chainId); return { intent: `propose "${args.content}" on NANI`, chain: chainId, ops, hash, }; } return { intent: `propose "${args.content})" on NANI`, chain: chainId, ops, }; }, }); exports.intentVoteNaniProposal = (0, client_js_1.createTool)({ name: "intentVoteNaniProposal", description: "Vote on an existing NANIDAO governance proposal", parameters: zod_1.default.object({ proposalId: zod_1.default.number().describe("The ID of the proposal to vote on"), approve: zod_1.default.boolean().describe("True to vote yes, false to vote no"), chainId: zod_1.default.number().describe("The chain ID to vote on"), }), supportedChains: [chains_1.mainnet, chains_1.base], execute: async (client, args) => { const { chainId } = args; const publicClient = client.getPublicClient(chainId); if (!publicClient) { throw new Error("No public client found for " + chainId); } const walletClient = client.getWalletClient(chainId); const ops = [ { target: constants_js_1.SIGNALS_ADDRESS, value: "0", data: (0, viem_1.encodeFunctionData)({ abi: abis_js_1.SignalsAbi, functionName: "vote", args: [BigInt(args.proposalId), args.approve], }), }, ]; if (walletClient) { const hash = await client.executeOps(ops, chainId); return { intent: `vote ${args.approve ? "yes" : "no"} on NANI proposal ${args.proposalId}`, chain: chainId, ops, hash, }; } return { intent: `vote ${args.approve ? "yes" : "no"} on NANI proposal ${args.proposalId}`, chain: chainId, ops, }; }, }); //# sourceMappingURL=intents.js.map