|
| 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 { validateAbstractConfig } from "../environment"; |
| 15 | + |
| 16 | +import { Address, createWalletClient, erc20Abi, http, parseEther } from "viem"; |
| 17 | +import { abstractTestnet } from "viem/chains"; |
| 18 | +import { privateKeyToAccount } from "viem/accounts"; |
| 19 | +import { eip712WalletActions } from "viem/zksync"; |
| 20 | +import { z } from "zod"; |
| 21 | + |
| 22 | +const TransferSchema = z.object({ |
| 23 | + tokenAddress: z.string(), |
| 24 | + recipient: z.string(), |
| 25 | + amount: z.string(), |
| 26 | +}); |
| 27 | + |
| 28 | +export interface TransferContent extends Content { |
| 29 | + tokenAddress: string; |
| 30 | + recipient: string; |
| 31 | + amount: string | number; |
| 32 | +} |
| 33 | + |
| 34 | +export function isTransferContent( |
| 35 | + content: TransferContent |
| 36 | +): content is TransferContent { |
| 37 | + // Validate types |
| 38 | + const validTypes = |
| 39 | + typeof content.tokenAddress === "string" && |
| 40 | + typeof content.recipient === "string" && |
| 41 | + (typeof content.amount === "string" || |
| 42 | + typeof content.amount === "number"); |
| 43 | + if (!validTypes) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // Validate addresses |
| 48 | + const validAddresses = |
| 49 | + content.tokenAddress.startsWith("0x") && |
| 50 | + content.tokenAddress.length === 42 && |
| 51 | + content.recipient.startsWith("0x") && |
| 52 | + content.recipient.length === 42; |
| 53 | + |
| 54 | + return validAddresses; |
| 55 | +} |
| 56 | + |
| 57 | +const transferTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined. |
| 58 | +
|
| 59 | +Here are several frequently used addresses. Use these for the corresponding tokens: |
| 60 | +- ETH/eth: 0x000000000000000000000000000000000000800A |
| 61 | +- USDC/usdc: 0xe4c7fbb0a626ed208021ccaba6be1566905e2dfc |
| 62 | +
|
| 63 | +Example response: |
| 64 | +\`\`\`json |
| 65 | +{ |
| 66 | + "tokenAddress": "0x5A7d6b2F92C77FAD6CCaBd7EE0624E64907Eaf3E", |
| 67 | + "recipient": "0xCCa8009f5e09F8C5dB63cb0031052F9CB635Af62", |
| 68 | + "amount": "1000" |
| 69 | +} |
| 70 | +\`\`\` |
| 71 | +
|
| 72 | +{{recentMessages}} |
| 73 | +
|
| 74 | +Given the recent messages, extract the following information about the requested token transfer: |
| 75 | +- Token contract address |
| 76 | +- Recipient wallet address |
| 77 | +- Amount to transfer |
| 78 | +
|
| 79 | +Respond with a JSON markdown block containing only the extracted values.`; |
| 80 | + |
| 81 | +const ETH_ADDRESS = "0x000000000000000000000000000000000000800A"; |
| 82 | +const ERC20_OVERRIDE_INFO = { |
| 83 | + "0xe4c7fbb0a626ed208021ccaba6be1566905e2dfc": { |
| 84 | + name: "USDC", |
| 85 | + decimals: 6, |
| 86 | + }, |
| 87 | +}; |
| 88 | + |
| 89 | +export default { |
| 90 | + name: "SEND_TOKEN", |
| 91 | + similes: [ |
| 92 | + "TRANSFER_TOKEN_ON_ABSTRACT", |
| 93 | + "TRANSFER_TOKENS_ON_ABSTRACT", |
| 94 | + "SEND_TOKENS_ON_ABSTRACT", |
| 95 | + "SEND_ETH_ON_ABSTRACT", |
| 96 | + "PAY_ON_ABSTRACT", |
| 97 | + "MOVE_TOKENS_ON_ABSTRACT", |
| 98 | + "MOVE_ETH_ON_ABSTRACT", |
| 99 | + ], |
| 100 | + validate: async (runtime: IAgentRuntime, message: Memory) => { |
| 101 | + await validateAbstractConfig(runtime); |
| 102 | + return true; |
| 103 | + }, |
| 104 | + description: "Transfer tokens from the agent's wallet to another address", |
| 105 | + handler: async ( |
| 106 | + runtime: IAgentRuntime, |
| 107 | + message: Memory, |
| 108 | + state: State, |
| 109 | + _options: { [key: string]: unknown }, |
| 110 | + callback?: HandlerCallback |
| 111 | + ): Promise<boolean> => { |
| 112 | + elizaLogger.log("Starting Abstract SEND_TOKEN handler..."); |
| 113 | + |
| 114 | + // Initialize or update state |
| 115 | + if (!state) { |
| 116 | + state = (await runtime.composeState(message)) as State; |
| 117 | + } else { |
| 118 | + state = await runtime.updateRecentMessageState(state); |
| 119 | + } |
| 120 | + |
| 121 | + // Compose transfer context |
| 122 | + const transferContext = composeContext({ |
| 123 | + state, |
| 124 | + template: transferTemplate, |
| 125 | + }); |
| 126 | + |
| 127 | + // Generate transfer content |
| 128 | + const content = ( |
| 129 | + await generateObject({ |
| 130 | + runtime, |
| 131 | + context: transferContext, |
| 132 | + modelClass: ModelClass.SMALL, |
| 133 | + schema: TransferSchema, |
| 134 | + }) |
| 135 | + ).object as unknown as TransferContent; |
| 136 | + |
| 137 | + // Validate transfer content |
| 138 | + if (!isTransferContent(content)) { |
| 139 | + console.error("Invalid content for TRANSFER_TOKEN action."); |
| 140 | + if (callback) { |
| 141 | + callback({ |
| 142 | + text: "Unable to process transfer request. Invalid content provided.", |
| 143 | + content: { error: "Invalid transfer content" }, |
| 144 | + }); |
| 145 | + } |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + try { |
| 150 | + const PRIVATE_KEY = runtime.getSetting("ABSTRACT_PRIVATE_KEY")!; |
| 151 | + const account = privateKeyToAccount(`0x${PRIVATE_KEY}`); |
| 152 | + |
| 153 | + const walletClient = createWalletClient({ |
| 154 | + chain: abstractTestnet, |
| 155 | + transport: http(), |
| 156 | + }).extend(eip712WalletActions()); |
| 157 | + |
| 158 | + let hash; |
| 159 | + if ( |
| 160 | + content.tokenAddress.toLowerCase() !== ETH_ADDRESS.toLowerCase() |
| 161 | + ) { |
| 162 | + // Convert amount to proper token decimals |
| 163 | + const tokenInfo = |
| 164 | + ERC20_OVERRIDE_INFO[content.tokenAddress.toLowerCase()]; |
| 165 | + const decimals = tokenInfo?.decimals ?? 18; // Default to 18 decimals if not specified |
| 166 | + const tokenAmount = |
| 167 | + BigInt(content.amount) * BigInt(10 ** decimals); |
| 168 | + |
| 169 | + // Execute ERC20 transfer |
| 170 | + hash = await walletClient.writeContract({ |
| 171 | + account, |
| 172 | + chain: abstractTestnet, |
| 173 | + address: content.tokenAddress as Address, |
| 174 | + abi: erc20Abi, |
| 175 | + functionName: "transfer", |
| 176 | + args: [content.recipient as Address, tokenAmount], |
| 177 | + }); |
| 178 | + } else { |
| 179 | + hash = await walletClient.sendTransaction({ |
| 180 | + account: account, |
| 181 | + chain: abstractTestnet, |
| 182 | + to: content.recipient as Address, |
| 183 | + value: parseEther(content.amount.toString()), |
| 184 | + kzg: undefined, |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + elizaLogger.success( |
| 189 | + "Transfer completed successfully! Transaction hash: " + hash |
| 190 | + ); |
| 191 | + if (callback) { |
| 192 | + callback({ |
| 193 | + text: |
| 194 | + "Transfer completed successfully! Transaction hash: " + |
| 195 | + hash, |
| 196 | + content: {}, |
| 197 | + }); |
| 198 | + } |
| 199 | + |
| 200 | + return true; |
| 201 | + } catch (error) { |
| 202 | + elizaLogger.error("Error during token transfer:", error); |
| 203 | + if (callback) { |
| 204 | + callback({ |
| 205 | + text: `Error transferring tokens: ${error.message}`, |
| 206 | + content: { error: error.message }, |
| 207 | + }); |
| 208 | + } |
| 209 | + return false; |
| 210 | + } |
| 211 | + }, |
| 212 | + |
| 213 | + examples: [ |
| 214 | + [ |
| 215 | + { |
| 216 | + user: "{{user1}}", |
| 217 | + content: { |
| 218 | + text: "Send 100 USDC to 0xCCa8009f5e09F8C5dB63cb0031052F9CB635Af62", |
| 219 | + }, |
| 220 | + }, |
| 221 | + { |
| 222 | + user: "{{agent}}", |
| 223 | + content: { |
| 224 | + text: "Sure, I'll send 100 USDC to that address now.", |
| 225 | + action: "SEND_TOKEN", |
| 226 | + }, |
| 227 | + }, |
| 228 | + { |
| 229 | + user: "{{agent}}", |
| 230 | + content: { |
| 231 | + text: "Successfully sent 100 USDC to 0xCCa8009f5e09F8C5dB63cb0031052F9CB635Af62\nTransaction: 0x4fed598033f0added272c3ddefd4d83a521634a738474400b27378db462a76ec", |
| 232 | + }, |
| 233 | + }, |
| 234 | + ], |
| 235 | + [ |
| 236 | + { |
| 237 | + user: "{{user1}}", |
| 238 | + content: { |
| 239 | + text: "Please send 0.1 ETH to 0xbD8679cf79137042214fA4239b02F4022208EE82", |
| 240 | + }, |
| 241 | + }, |
| 242 | + { |
| 243 | + user: "{{agent}}", |
| 244 | + content: { |
| 245 | + text: "Of course. Sending 0.1 ETH to that address now.", |
| 246 | + action: "SEND_TOKEN", |
| 247 | + }, |
| 248 | + }, |
| 249 | + { |
| 250 | + user: "{{agent}}", |
| 251 | + content: { |
| 252 | + text: "Successfully sent 0.1 ETH to 0xbD8679cf79137042214fA4239b02F4022208EE82\nTransaction: 0x0b9f23e69ea91ba98926744472717960cc7018d35bc3165bdba6ae41670da0f0", |
| 253 | + }, |
| 254 | + }, |
| 255 | + ], |
| 256 | + ] as ActionExample[][], |
| 257 | +} as Action; |
0 commit comments