|
| 1 | +import { |
| 2 | + ActionExample, |
| 3 | + HandlerCallback, |
| 4 | + IAgentRuntime, |
| 5 | + Memory, |
| 6 | + ModelClass, |
| 7 | + State, |
| 8 | + type Action, |
| 9 | + composeContext, |
| 10 | + generateObject, |
| 11 | +} from "@elizaos/core"; |
| 12 | +import { |
| 13 | + getQuaiAccount, |
| 14 | + isTransferContent, |
| 15 | + validateSettings, |
| 16 | +} from "../utils"; |
| 17 | +import { formatUnits, TransactionRequest } from "quais"; |
| 18 | + |
| 19 | +const transferTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined. |
| 20 | +
|
| 21 | +Example response: |
| 22 | +\`\`\`json |
| 23 | +{ |
| 24 | + "tokenAddress": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", |
| 25 | + "recipient": "0x0005C06bD1339c79700a8DAb35DE0a1b61dFBD71", |
| 26 | + "amount": "0.001" |
| 27 | +} |
| 28 | +\`\`\` |
| 29 | +
|
| 30 | +{{recentMessages}} |
| 31 | +
|
| 32 | +Given the recent messages, extract the following information about the requested token transfer: |
| 33 | +- Token contract address (if available) |
| 34 | +- Recipient wallet address |
| 35 | +- Amount to send |
| 36 | +
|
| 37 | +Respond with a JSON markdown block containing only the extracted values.`; |
| 38 | + |
| 39 | +export default { |
| 40 | + name: "SEND_TOKEN", |
| 41 | + similes: [ |
| 42 | + "TRANSFER_TOKEN_ON_QUAI", |
| 43 | + "TRANSFER_TOKENS_ON_QUAI", |
| 44 | + "SEND_TOKENS_ON_QUAI", |
| 45 | + "SEND_QUAI", |
| 46 | + "PAY_ON_QUAI", |
| 47 | + ], |
| 48 | + validate: async (runtime: IAgentRuntime, message: Memory) => { |
| 49 | + return validateSettings(runtime); |
| 50 | + }, |
| 51 | + description: |
| 52 | + "MUST use this action if the user requests send a token or transfer a token, the request might be varied, but it will always be a token transfer. If the user requests a transfer of lords, use this action.", |
| 53 | + handler: async ( |
| 54 | + runtime: IAgentRuntime, |
| 55 | + message: Memory, |
| 56 | + state: State, |
| 57 | + _options: { [key: string]: unknown }, |
| 58 | + callback?: HandlerCallback |
| 59 | + ): Promise<boolean> => { |
| 60 | + console.log("Starting TRANSFER_TOKEN handler..."); |
| 61 | + |
| 62 | + // Initialize or update state |
| 63 | + if (!state) { |
| 64 | + state = (await runtime.composeState(message)) as State; |
| 65 | + } else { |
| 66 | + state = await runtime.updateRecentMessageState(state); |
| 67 | + } |
| 68 | + |
| 69 | + // Compose transfer context |
| 70 | + const transferContext = composeContext({ |
| 71 | + state, |
| 72 | + template: transferTemplate, |
| 73 | + }); |
| 74 | + |
| 75 | + // Generate transfer content |
| 76 | + const content = await generateObject({ |
| 77 | + runtime, |
| 78 | + context: transferContext, |
| 79 | + modelClass: ModelClass.MEDIUM, |
| 80 | + }); |
| 81 | + |
| 82 | + console.log("Transfer content:", content); |
| 83 | + |
| 84 | + // Validate transfer content |
| 85 | + if (!isTransferContent(content)) { |
| 86 | + console.error("Invalid content for TRANSFER_TOKEN action."); |
| 87 | + if (callback) { |
| 88 | + callback({ |
| 89 | + text: "Not enough information to transfer tokens. Please respond with token address, recipient, and amount.", |
| 90 | + content: { error: "Invalid transfer content" }, |
| 91 | + }); |
| 92 | + } |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + const account = getQuaiAccount(runtime); |
| 98 | + const amount = formatUnits(content.amount, "wei"); |
| 99 | + |
| 100 | + var txObj: TransactionRequest = {}; |
| 101 | + if (content.tokenAddress) { |
| 102 | + // TODO: transfer QRC20s |
| 103 | + } else { |
| 104 | + txObj = { |
| 105 | + to: content.recipient, |
| 106 | + value: amount, |
| 107 | + from: account.address, |
| 108 | + }; |
| 109 | + |
| 110 | + console.log( |
| 111 | + "Transferring", |
| 112 | + amount, |
| 113 | + "QUAI", |
| 114 | + "to", |
| 115 | + content.recipient |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + const tx = await account.sendTransaction(txObj) |
| 120 | + |
| 121 | + console.log( |
| 122 | + "Transfer completed successfully! tx: " + tx.hash |
| 123 | + ); |
| 124 | + if (callback) { |
| 125 | + callback({ |
| 126 | + text: |
| 127 | + "Transfer completed successfully! tx: " + |
| 128 | + tx.hash, |
| 129 | + content: {}, |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + return true; |
| 134 | + } catch (error) { |
| 135 | + console.error("Error during token transfer:", error); |
| 136 | + if (callback) { |
| 137 | + callback({ |
| 138 | + text: `Error transferring tokens: ${error.message}`, |
| 139 | + content: { error: error.message }, |
| 140 | + }); |
| 141 | + } |
| 142 | + return false; |
| 143 | + } |
| 144 | + }, |
| 145 | + |
| 146 | + examples: [ |
| 147 | + [ |
| 148 | + { |
| 149 | + user: "{{user1}}", |
| 150 | + content: { |
| 151 | + text: "Send 10 QUAI to 0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", |
| 152 | + }, |
| 153 | + }, |
| 154 | + { |
| 155 | + user: "{{agent}}", |
| 156 | + content: { |
| 157 | + text: "I'll transfer 10 QUAI to that address right away. Let me process that for you.", |
| 158 | + }, |
| 159 | + }, |
| 160 | + ], |
| 161 | + [ |
| 162 | + { |
| 163 | + user: "{{user1}}", |
| 164 | + content: { |
| 165 | + text: "Please send 0.5 QUAI to 0x03fe2b97c1fd336e750087d68b9b867997fd64a2661ff3ca5a7c771641e8e7ac", |
| 166 | + }, |
| 167 | + }, |
| 168 | + { |
| 169 | + user: "{{agent}}", |
| 170 | + content: { |
| 171 | + text: "Got it, initiating transfer of 0.5 QUAI to the provided address. I'll confirm once it's complete.", |
| 172 | + }, |
| 173 | + }, |
| 174 | + ], |
| 175 | + ] as ActionExample[][], |
| 176 | +} as Action; |
0 commit comments