|
| 1 | +import type { IAgentRuntime, Memory, State, HandlerCallback } from "@elizaos/core"; |
| 2 | +import { WalletProvider } from "../providers/wallet"; |
| 3 | +import { executeProposalTemplate } from "../templates"; |
| 4 | +import type { ExecuteProposalParams, SupportedChain, Transaction } from "../types"; |
| 5 | +import governorArtifacts from "../contracts/artifacts/OZGovernor.json"; |
| 6 | +import { |
| 7 | + type ByteArray, |
| 8 | + type Hex, |
| 9 | + type Address, |
| 10 | + encodeFunctionData, |
| 11 | + keccak256, |
| 12 | + stringToHex, |
| 13 | +} from "viem"; |
| 14 | + |
| 15 | +export { executeProposalTemplate }; |
| 16 | + |
| 17 | +export class ExecuteAction { |
| 18 | + constructor(private walletProvider: WalletProvider) { |
| 19 | + this.walletProvider = walletProvider; |
| 20 | + } |
| 21 | + |
| 22 | + async execute(params: ExecuteProposalParams): Promise<Transaction> { |
| 23 | + const walletClient = this.walletProvider.getWalletClient(params.chain); |
| 24 | + |
| 25 | + const descriptionHash = keccak256(stringToHex(params.description)); |
| 26 | + |
| 27 | + const txData = encodeFunctionData({ |
| 28 | + abi: governorArtifacts.abi, |
| 29 | + functionName: "execute", |
| 30 | + args: [ |
| 31 | + params.targets, |
| 32 | + params.values, |
| 33 | + params.calldatas, |
| 34 | + descriptionHash, |
| 35 | + ], |
| 36 | + }); |
| 37 | + |
| 38 | + try { |
| 39 | + const chainConfig = this.walletProvider.getChainConfigs( |
| 40 | + params.chain |
| 41 | + ); |
| 42 | + |
| 43 | + // Log current block before sending transaction |
| 44 | + const publicClient = this.walletProvider.getPublicClient( |
| 45 | + params.chain |
| 46 | + ); |
| 47 | + |
| 48 | + const hash = await walletClient.sendTransaction({ |
| 49 | + account: walletClient.account, |
| 50 | + to: params.governor, |
| 51 | + value: BigInt(0), |
| 52 | + data: txData as Hex, |
| 53 | + chain: chainConfig, |
| 54 | + kzg: { |
| 55 | + blobToKzgCommitment: (_blob: ByteArray): ByteArray => { |
| 56 | + throw new Error("Function not implemented."); |
| 57 | + }, |
| 58 | + computeBlobKzgProof: ( |
| 59 | + _blob: ByteArray, |
| 60 | + _commitment: ByteArray |
| 61 | + ): ByteArray => { |
| 62 | + throw new Error("Function not implemented."); |
| 63 | + }, |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + const receipt = await publicClient.waitForTransactionReceipt({ |
| 68 | + hash, |
| 69 | + }); |
| 70 | + |
| 71 | + return { |
| 72 | + hash, |
| 73 | + from: walletClient.account.address, |
| 74 | + to: params.governor, |
| 75 | + value: BigInt(0), |
| 76 | + data: txData as Hex, |
| 77 | + chainId: this.walletProvider.getChainConfigs(params.chain).id, |
| 78 | + logs: receipt.logs, |
| 79 | + }; |
| 80 | + } catch (error) { |
| 81 | + throw new Error(`Vote failed: ${error.message}`); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export const executeAction = { |
| 87 | + name: "execute", |
| 88 | + description: "Execute a DAO governance proposal", |
| 89 | + handler: async ( |
| 90 | + runtime: IAgentRuntime, |
| 91 | + _message: Memory, |
| 92 | + _state: State, |
| 93 | + options: Record<string, unknown>, |
| 94 | + callback?: HandlerCallback |
| 95 | + ) => { |
| 96 | + try { |
| 97 | + // Validate required fields |
| 98 | + if (!options.chain || !options.governor || !options.proposalId || |
| 99 | + !options.targets || !options.values || !options.calldatas || !options.description) { |
| 100 | + throw new Error("Missing required parameters for execute proposal"); |
| 101 | + } |
| 102 | + |
| 103 | + // Convert options to ExecuteProposalParams |
| 104 | + const executeParams: ExecuteProposalParams = { |
| 105 | + chain: options.chain as SupportedChain, |
| 106 | + governor: options.governor as Address, |
| 107 | + proposalId: String(options.proposalId), |
| 108 | + targets: options.targets as Address[], |
| 109 | + values: (options.values as string[]).map(v => BigInt(v)), |
| 110 | + calldatas: options.calldatas as `0x${string}`[], |
| 111 | + description: String(options.description) |
| 112 | + }; |
| 113 | + |
| 114 | + const privateKey = runtime.getSetting( |
| 115 | + "EVM_PRIVATE_KEY" |
| 116 | + ) as `0x${string}`; |
| 117 | + const walletProvider = new WalletProvider( |
| 118 | + privateKey, |
| 119 | + runtime.cacheManager |
| 120 | + ); |
| 121 | + const action = new ExecuteAction(walletProvider); |
| 122 | + return await action.execute(executeParams); |
| 123 | + } catch (error) { |
| 124 | + console.error("Error in execute handler:", error.message); |
| 125 | + if (callback) { |
| 126 | + callback({ text: `Error: ${error.message}` }); |
| 127 | + } |
| 128 | + return false; |
| 129 | + } |
| 130 | + }, |
| 131 | + template: executeProposalTemplate, |
| 132 | + validate: async (runtime: IAgentRuntime) => { |
| 133 | + const privateKey = runtime.getSetting("EVM_PRIVATE_KEY"); |
| 134 | + return typeof privateKey === "string" && privateKey.startsWith("0x"); |
| 135 | + }, |
| 136 | + examples: [ |
| 137 | + [ |
| 138 | + { |
| 139 | + user: "user", |
| 140 | + content: { |
| 141 | + text: "Execute proposal 123 on the governor at 0x1234567890123456789012345678901234567890 on Ethereum", |
| 142 | + action: "EXECUTE_PROPOSAL", |
| 143 | + }, |
| 144 | + }, |
| 145 | + ], |
| 146 | + ], |
| 147 | + similes: ["EXECUTE_PROPOSAL", "GOVERNANCE_EXECUTE"], |
| 148 | +}; // TODO: add more examples |
0 commit comments