UNPKG

@agentek/tools

Version:

Blockchain tools for AI agents

124 lines 5.04 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createTallyVoteWithReasonIntent = exports.createTallyVoteIntent = void 0; const zod_1 = __importDefault(require("zod")); const client_js_1 = require("../client.js"); const viem_1 = require("viem"); const constants_js_1 = require("./constants.js"); const utils_js_1 = require("./utils.js"); const VoteSupport = zod_1.default.enum(["against", "for", "abstain"]); const voteSupportToNumber = (vote) => { switch (vote) { case "against": return 0; case "for": return 1; case "abstain": return 2; } }; const createTallyVoteIntentParams = zod_1.default.object({ space: zod_1.default.string(), vote: VoteSupport, proposalId: zod_1.default.number(), }); const createTallyVoteWithReasonIntentParams = zod_1.default.object({ space: zod_1.default.string(), vote: VoteSupport, proposalId: zod_1.default.number(), reason: zod_1.default.string(), }); const createTallyVoteIntent = (tallyApiKey) => { return (0, client_js_1.createTool)({ name: "intentGovernorVote", description: "Creates an intent to vote on a Governor bravo proposal", parameters: createTallyVoteIntentParams, execute: async (client, args) => { let { space, vote, proposalId } = args; const from = await client.getAddress(); const { chainId, address: spaceAddress } = (0, utils_js_1.parseCaip10)((await (0, utils_js_1.getGovernorBySlug)(space, tallyApiKey)).id); const voteNumber = voteSupportToNumber(vote); const ops = [ { target: spaceAddress, value: "0", data: (0, viem_1.encodeFunctionData)({ abi: constants_js_1.GovernorBravoDelegateAbi, functionName: "castVote", args: [BigInt(proposalId), voteNumber], }), }, ]; const walletClient = client.getWalletClient(chainId); if (!walletClient) { return { intent: `Vote ${vote} on proposal in space ${space}`, ops, chain: chainId, }; } else { if (!ops[0]) { throw new Error("Operations array is empty or undefined"); } const hash = await client.executeOps(ops, chainId); return { intent: `Vote ${vote.toUpperCase()} on proposal in ${space} from ${from}`, ops, chain: chainId, hash: hash, }; } }, }); }; exports.createTallyVoteIntent = createTallyVoteIntent; const createTallyVoteWithReasonIntent = (tallyApiKey) => { return (0, client_js_1.createTool)({ name: "intentGovernorVoteWithReason", description: "Creates an intent to vote on a Governor bravo proposal with a reason", parameters: createTallyVoteWithReasonIntentParams, execute: async (client, args) => { let { space, vote, proposalId, reason } = args; const from = await client.getAddress(); const { chainId, address: spaceAddress } = (0, utils_js_1.parseCaip10)((await (0, utils_js_1.getGovernorBySlug)(space, tallyApiKey)).id); const voteNumber = voteSupportToNumber(vote); const ops = [ { target: spaceAddress, value: "0", data: (0, viem_1.encodeFunctionData)({ abi: constants_js_1.GovernorBravoDelegateAbi, functionName: "castVoteWithReason", args: [BigInt(proposalId), voteNumber, reason], }), }, ]; const walletClient = client.getWalletClient(chainId); if (!walletClient) { return { intent: `Vote ${vote} on proposal in space ${space} with reason: "${reason}"`, ops, chain: chainId, }; } else { if (!ops[0]) { throw new Error("Operations array is empty or undefined"); } const hash = await client.executeOps(ops, chainId); return { intent: `Vote ${vote.toUpperCase()} on proposal in ${space} from ${from} with reason: ${reason}`, ops, chain: chainId, hash: hash, }; } }, }); }; exports.createTallyVoteWithReasonIntent = createTallyVoteWithReasonIntent; //# sourceMappingURL=intents.js.map