|
| 1 | +import { |
| 2 | + ActionExample, |
| 3 | + Content, |
| 4 | + HandlerCallback, |
| 5 | + IAgentRuntime, |
| 6 | + Memory, |
| 7 | + ModelClass, |
| 8 | + State, |
| 9 | + type Action, |
| 10 | + elizaLogger, |
| 11 | + composeContext, |
| 12 | + generateObject, |
| 13 | +} from "@elizaos/core"; |
| 14 | +import { validateCronosZkevmConfig } from "../enviroment"; |
| 15 | + |
| 16 | +import { Web3 } from "web3"; |
| 17 | +import { |
| 18 | + ZKsyncPlugin, |
| 19 | + ZKsyncWallet, |
| 20 | + types, |
| 21 | + Web3ZKsyncL2, |
| 22 | +} from "web3-plugin-zksync"; |
| 23 | + |
| 24 | +export interface TransferContent extends Content { |
| 25 | + tokenAddress: string; |
| 26 | + recipient: string; |
| 27 | + amount: string | number; |
| 28 | +} |
| 29 | + |
| 30 | +export function isTransferContent( |
| 31 | + content: TransferContent |
| 32 | +): content is TransferContent { |
| 33 | + // Validate types |
| 34 | + const validTypes = |
| 35 | + typeof content.tokenAddress === "string" && |
| 36 | + typeof content.recipient === "string" && |
| 37 | + (typeof content.amount === "string" || |
| 38 | + typeof content.amount === "number"); |
| 39 | + if (!validTypes) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + // Validate addresses |
| 44 | + const validAddresses = |
| 45 | + content.tokenAddress.startsWith("0x") && |
| 46 | + content.tokenAddress.length === 42 && |
| 47 | + content.recipient.startsWith("0x") && |
| 48 | + content.recipient.length === 42; |
| 49 | + |
| 50 | + return validAddresses; |
| 51 | +} |
| 52 | + |
| 53 | +const transferTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined. |
| 54 | +
|
| 55 | +Here are several frequently used addresses. Use these for the corresponding tokens: |
| 56 | +- ZKCRO/zkCRO: 0x000000000000000000000000000000000000800A |
| 57 | +- USDC/usdc: 0xaa5b845f8c9c047779bedf64829601d8b264076c |
| 58 | +- ETH/eth: 0x898b3560affd6d955b1574d87ee09e46669c60ea |
| 59 | +
|
| 60 | +Example response: |
| 61 | +\`\`\`json |
| 62 | +{ |
| 63 | + "tokenAddress": "0xaa5b845f8c9c047779bedf64829601d8b264076c", |
| 64 | + "recipient": "0xCCa8009f5e09F8C5dB63cb0031052F9CB635Af62", |
| 65 | + "amount": "1000" |
| 66 | +} |
| 67 | +\`\`\` |
| 68 | +
|
| 69 | +{{recentMessages}} |
| 70 | +
|
| 71 | +Given the recent messages, extract the following information about the requested token transfer: |
| 72 | +- Token contract address |
| 73 | +- Recipient wallet address |
| 74 | +- Amount to transfer |
| 75 | +
|
| 76 | +Respond with a JSON markdown block containing only the extracted values.`; |
| 77 | + |
| 78 | +export default { |
| 79 | + name: "SEND_TOKEN", |
| 80 | + similes: [ |
| 81 | + "TRANSFER_TOKEN_ON_CRONOSZKEVM", |
| 82 | + "TRANSFER_TOKENS_ON_CRONOSZK", |
| 83 | + "SEND_TOKENS_ON_CRONOSZKEVM", |
| 84 | + "SEND_TOKENS_ON_CRONOSZK", |
| 85 | + "SEND_ETH_ON_CRONOSZKEVM", |
| 86 | + "SEND_ETH_ON_CRONOSZK", |
| 87 | + "PAY_ON_CRONOSZKEVM", |
| 88 | + "PAY_ON_CRONOSZK", |
| 89 | + ], |
| 90 | + validate: async (runtime: IAgentRuntime, message: Memory) => { |
| 91 | + await validateCronosZkevmConfig(runtime); |
| 92 | + return true; |
| 93 | + }, |
| 94 | + description: "Transfer tokens from the agent's wallet to another address", |
| 95 | + handler: async ( |
| 96 | + runtime: IAgentRuntime, |
| 97 | + message: Memory, |
| 98 | + state: State, |
| 99 | + _options: { [key: string]: unknown }, |
| 100 | + callback?: HandlerCallback |
| 101 | + ): Promise<boolean> => { |
| 102 | + elizaLogger.log("Starting SEND_TOKEN handler..."); |
| 103 | + |
| 104 | + // Initialize or update state |
| 105 | + if (!state) { |
| 106 | + state = (await runtime.composeState(message)) as State; |
| 107 | + } else { |
| 108 | + state = await runtime.updateRecentMessageState(state); |
| 109 | + } |
| 110 | + |
| 111 | + // Compose transfer context |
| 112 | + const transferContext = composeContext({ |
| 113 | + state, |
| 114 | + template: transferTemplate, |
| 115 | + }); |
| 116 | + |
| 117 | + // Generate transfer content |
| 118 | + const content = await generateObject({ |
| 119 | + runtime, |
| 120 | + context: transferContext, |
| 121 | + modelClass: ModelClass.SMALL, |
| 122 | + }); |
| 123 | + |
| 124 | + // Validate transfer content |
| 125 | + if (!isTransferContent(content)) { |
| 126 | + console.error("Invalid content for TRANSFER_TOKEN action."); |
| 127 | + if (callback) { |
| 128 | + callback({ |
| 129 | + text: "Unable to process transfer request. Invalid content provided.", |
| 130 | + content: { error: "Invalid transfer content" }, |
| 131 | + }); |
| 132 | + } |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + try { |
| 137 | + const PRIVATE_KEY = runtime.getSetting("CRONOSZKEVM_PRIVATE_KEY")!; |
| 138 | + const PUBLIC_KEY = runtime.getSetting("CRONOSZKEVM_ADDRESS")!; |
| 139 | + |
| 140 | + const web3: Web3 = new Web3(/* optional L1 provider */); |
| 141 | + |
| 142 | + web3.registerPlugin( |
| 143 | + new ZKsyncPlugin( |
| 144 | + new Web3ZKsyncL2("https://mainnet.zkevm.cronos.org") |
| 145 | + ) |
| 146 | + ); |
| 147 | + |
| 148 | + const smartAccount = new web3.ZKsync.SmartAccount({ |
| 149 | + address: PUBLIC_KEY, |
| 150 | + secret: "0x" + PRIVATE_KEY, |
| 151 | + }); |
| 152 | + |
| 153 | + const transferTx = await smartAccount.transfer({ |
| 154 | + to: content.recipient, |
| 155 | + token: content.tokenAddress, |
| 156 | + amount: web3.utils.toWei(content.amount, "ether"), |
| 157 | + }); |
| 158 | + |
| 159 | + const receipt = await transferTx.wait(); |
| 160 | + |
| 161 | + elizaLogger.success( |
| 162 | + "Transfer completed successfully! tx: " + |
| 163 | + receipt.transactionHash |
| 164 | + ); |
| 165 | + if (callback) { |
| 166 | + callback({ |
| 167 | + text: |
| 168 | + "Transfer completed successfully! tx: " + |
| 169 | + receipt.transactionHash, |
| 170 | + content: {}, |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + return true; |
| 175 | + } catch (error) { |
| 176 | + elizaLogger.error("Error during token transfer:", error); |
| 177 | + if (callback) { |
| 178 | + callback({ |
| 179 | + text: `Error transferring tokens: ${error.message}`, |
| 180 | + content: { error: error.message }, |
| 181 | + }); |
| 182 | + } |
| 183 | + return false; |
| 184 | + } |
| 185 | + }, |
| 186 | + |
| 187 | + examples: [ |
| 188 | + [ |
| 189 | + { |
| 190 | + user: "{{user1}}", |
| 191 | + content: { |
| 192 | + text: "Send 100 USDC to 0xCCa8009f5e09F8C5dB63cb0031052F9CB635Af62", |
| 193 | + }, |
| 194 | + }, |
| 195 | + { |
| 196 | + user: "{{agent}}", |
| 197 | + content: { |
| 198 | + text: "Sure, I'll send 100 USDC to that address now.", |
| 199 | + action: "SEND_TOKEN", |
| 200 | + }, |
| 201 | + }, |
| 202 | + { |
| 203 | + user: "{{agent}}", |
| 204 | + content: { |
| 205 | + text: "Successfully sent 100 USDC to 0xCCa8009f5e09F8C5dB63cb0031052F9CB635Af62\nTransaction: 0x4fed598033f0added272c3ddefd4d83a521634a738474400b27378db462a76ec", |
| 206 | + }, |
| 207 | + }, |
| 208 | + ], |
| 209 | + [ |
| 210 | + { |
| 211 | + user: "{{user1}}", |
| 212 | + content: { |
| 213 | + text: "Please send 100 ZKCRO tokens to 0xbD8679cf79137042214fA4239b02F4022208EE82", |
| 214 | + }, |
| 215 | + }, |
| 216 | + { |
| 217 | + user: "{{agent}}", |
| 218 | + content: { |
| 219 | + text: "Of course. Sending 100 ZKCRO to that address now.", |
| 220 | + action: "SEND_TOKEN", |
| 221 | + }, |
| 222 | + }, |
| 223 | + { |
| 224 | + user: "{{agent}}", |
| 225 | + content: { |
| 226 | + text: "Successfully sent 100 ZKCRO to 0xbD8679cf79137042214fA4239b02F4022208EE82\nTransaction: 0x0b9f23e69ea91ba98926744472717960cc7018d35bc3165bdba6ae41670da0f0", |
| 227 | + }, |
| 228 | + }, |
| 229 | + ], |
| 230 | + ] as ActionExample[][], |
| 231 | +} as Action; |
0 commit comments