|
| 1 | +import { elizaLogger } from "@ai16z/eliza"; |
| 2 | +import { |
| 3 | + ActionExample, |
| 4 | + Content, |
| 5 | + HandlerCallback, |
| 6 | + IAgentRuntime, |
| 7 | + Memory, |
| 8 | + ModelClass, |
| 9 | + State, |
| 10 | + type Action, |
| 11 | +} from "@ai16z/eliza"; |
| 12 | +import { composeContext } from "@ai16z/eliza"; |
| 13 | +import { generateObject } from "@ai16z/eliza"; |
| 14 | +import { |
| 15 | + Account, |
| 16 | + Aptos, |
| 17 | + AptosConfig, |
| 18 | + Ed25519PrivateKey, |
| 19 | + Network, |
| 20 | + PrivateKey, |
| 21 | + PrivateKeyVariants, |
| 22 | +} from "@aptos-labs/ts-sdk"; |
| 23 | +import { walletProvider } from "../providers/wallet"; |
| 24 | + |
| 25 | +export interface TransferContent extends Content { |
| 26 | + recipient: string; |
| 27 | + amount: string | number; |
| 28 | +} |
| 29 | + |
| 30 | +function isTransferContent(content: any): content is TransferContent { |
| 31 | + console.log("Content for transfer", content); |
| 32 | + return ( |
| 33 | + typeof content.recipient === "string" && |
| 34 | + (typeof content.amount === "string" || |
| 35 | + typeof content.amount === "number") |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +const transferTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined. |
| 40 | +
|
| 41 | +Example response: |
| 42 | +\`\`\`json |
| 43 | +{ |
| 44 | + "recipient": "0x2badda48c062e861ef17a96a806c451fd296a49f45b272dee17f85b0e32663fd", |
| 45 | + "amount": "1000" |
| 46 | +} |
| 47 | +\`\`\` |
| 48 | +
|
| 49 | +{{recentMessages}} |
| 50 | +
|
| 51 | +Given the recent messages, extract the following information about the requested token transfer: |
| 52 | +- Recipient wallet address |
| 53 | +- Amount to transfer |
| 54 | +
|
| 55 | +Respond with a JSON markdown block containing only the extracted values.`; |
| 56 | + |
| 57 | +export default { |
| 58 | + name: "SEND_TOKEN", |
| 59 | + similes: [ |
| 60 | + "TRANSFER_TOKEN", |
| 61 | + "TRANSFER_TOKENS", |
| 62 | + "SEND_TOKENS", |
| 63 | + "SEND_APT", |
| 64 | + "PAY", |
| 65 | + ], |
| 66 | + validate: async (runtime: IAgentRuntime, message: Memory) => { |
| 67 | + console.log("Validating apt transfer from user:", message.userId); |
| 68 | + //add custom validate logic here |
| 69 | + /* |
| 70 | + const adminIds = runtime.getSetting("ADMIN_USER_IDS")?.split(",") || []; |
| 71 | + //console.log("Admin IDs from settings:", adminIds); |
| 72 | +
|
| 73 | + const isAdmin = adminIds.includes(message.userId); |
| 74 | +
|
| 75 | + if (isAdmin) { |
| 76 | + //console.log(`Authorized transfer from user: ${message.userId}`); |
| 77 | + return true; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + //console.log(`Unauthorized transfer attempt from user: ${message.userId}`); |
| 82 | + return false; |
| 83 | + } |
| 84 | + */ |
| 85 | + return false; |
| 86 | + }, |
| 87 | + description: "Transfer tokens from the agent's wallet to another address", |
| 88 | + handler: async ( |
| 89 | + runtime: IAgentRuntime, |
| 90 | + message: Memory, |
| 91 | + state: State, |
| 92 | + _options: { [key: string]: unknown }, |
| 93 | + callback?: HandlerCallback |
| 94 | + ): Promise<boolean> => { |
| 95 | + elizaLogger.log("Starting SEND_TOKEN handler..."); |
| 96 | + |
| 97 | + const walletInfo = await walletProvider.get(runtime, message, state); |
| 98 | + state.walletInfo = walletInfo; |
| 99 | + |
| 100 | + // Initialize or update state |
| 101 | + if (!state) { |
| 102 | + state = (await runtime.composeState(message)) as State; |
| 103 | + } else { |
| 104 | + state = await runtime.updateRecentMessageState(state); |
| 105 | + } |
| 106 | + |
| 107 | + // Compose transfer context |
| 108 | + const transferContext = composeContext({ |
| 109 | + state, |
| 110 | + template: transferTemplate, |
| 111 | + }); |
| 112 | + |
| 113 | + // Generate transfer content |
| 114 | + const content = await generateObject({ |
| 115 | + runtime, |
| 116 | + context: transferContext, |
| 117 | + modelClass: ModelClass.SMALL, |
| 118 | + }); |
| 119 | + |
| 120 | + // Validate transfer content |
| 121 | + if (!isTransferContent(content)) { |
| 122 | + console.error("Invalid content for TRANSFER_TOKEN action."); |
| 123 | + if (callback) { |
| 124 | + callback({ |
| 125 | + text: "Unable to process transfer request. Invalid content provided.", |
| 126 | + content: { error: "Invalid transfer content" }, |
| 127 | + }); |
| 128 | + } |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + try { |
| 133 | + const privateKey = runtime.getSetting("APTOS_PRIVATE_KEY"); |
| 134 | + const aptosAccount = Account.fromPrivateKey({ |
| 135 | + privateKey: new Ed25519PrivateKey( |
| 136 | + PrivateKey.formatPrivateKey( |
| 137 | + privateKey, |
| 138 | + PrivateKeyVariants.Ed25519 |
| 139 | + ) |
| 140 | + ), |
| 141 | + }); |
| 142 | + const network = runtime.getSetting("APTOS_NETWORK") as Network; |
| 143 | + const aptosClient = new Aptos( |
| 144 | + new AptosConfig({ |
| 145 | + network, |
| 146 | + }) |
| 147 | + ); |
| 148 | + |
| 149 | + const APT_DECIMALS = 8; |
| 150 | + const adjustedAmount = BigInt( |
| 151 | + Number(content.amount) * Math.pow(10, APT_DECIMALS) |
| 152 | + ); |
| 153 | + console.log( |
| 154 | + `Transferring: ${content.amount} tokens (${adjustedAmount} base units)` |
| 155 | + ); |
| 156 | + |
| 157 | + const tx = await aptosClient.transaction.build.simple({ |
| 158 | + sender: aptosAccount.accountAddress.toStringLong(), |
| 159 | + data: { |
| 160 | + function: "0x1::aptos_account::transfer", |
| 161 | + typeArguments: [], |
| 162 | + functionArguments: [content.recipient, adjustedAmount], |
| 163 | + }, |
| 164 | + }); |
| 165 | + const committedTransaction = |
| 166 | + await aptosClient.signAndSubmitTransaction({ |
| 167 | + signer: aptosAccount, |
| 168 | + transaction: tx, |
| 169 | + }); |
| 170 | + const executedTransaction = await aptosClient.waitForTransaction({ |
| 171 | + transactionHash: committedTransaction.hash, |
| 172 | + }); |
| 173 | + |
| 174 | + console.log("Transfer successful:", executedTransaction.hash); |
| 175 | + |
| 176 | + if (callback) { |
| 177 | + callback({ |
| 178 | + text: `Successfully transferred ${content.amount} APT to ${content.recipient}, Transaction: ${executedTransaction.hash}`, |
| 179 | + content: { |
| 180 | + success: true, |
| 181 | + hash: executedTransaction.hash, |
| 182 | + amount: content.amount, |
| 183 | + recipient: content.recipient, |
| 184 | + }, |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + return true; |
| 189 | + } catch (error) { |
| 190 | + console.error("Error during token transfer:", error); |
| 191 | + if (callback) { |
| 192 | + callback({ |
| 193 | + text: `Error transferring tokens: ${error.message}`, |
| 194 | + content: { error: error.message }, |
| 195 | + }); |
| 196 | + } |
| 197 | + return false; |
| 198 | + } |
| 199 | + }, |
| 200 | + |
| 201 | + examples: [ |
| 202 | + [ |
| 203 | + { |
| 204 | + user: "{{user1}}", |
| 205 | + content: { |
| 206 | + text: "Send 69 APT tokens to 0x4f2e63be8e7fe287836e29cde6f3d5cbc96eefd0c0e3f3747668faa2ae7324b0", |
| 207 | + }, |
| 208 | + }, |
| 209 | + { |
| 210 | + user: "{{user2}}", |
| 211 | + content: { |
| 212 | + text: "I'll send 69 APT tokens now...", |
| 213 | + action: "SEND_TOKEN", |
| 214 | + }, |
| 215 | + }, |
| 216 | + { |
| 217 | + user: "{{user2}}", |
| 218 | + content: { |
| 219 | + text: "Successfully sent 69 APT tokens to 0x4f2e63be8e7fe287836e29cde6f3d5cbc96eefd0c0e3f3747668faa2ae7324b0, Transaction: 0x39a8c432d9bdad993a33cc1faf2e9b58fb7dd940c0425f1d6db3997e4b4b05c0", |
| 220 | + }, |
| 221 | + }, |
| 222 | + ], |
| 223 | + ] as ActionExample[][], |
| 224 | +} as Action; |
0 commit comments