|
| 1 | +import { |
| 2 | + elizaLogger, |
| 3 | + composeContext, |
| 4 | + Content, |
| 5 | + HandlerCallback, |
| 6 | + ModelClass, |
| 7 | + generateObjectV2, |
| 8 | + type IAgentRuntime, |
| 9 | + type Memory, |
| 10 | + type State, |
| 11 | +} from "@ai16z/eliza"; |
| 12 | +import { z } from "zod"; |
| 13 | + |
| 14 | +import { initWalletProvider, WalletProvider, nativeWalletProvider } from "../providers/wallet"; |
| 15 | +import { internal } from "@ton/ton"; |
| 16 | + |
| 17 | +export interface TransferContent extends Content { |
| 18 | + recipient: string; |
| 19 | + amount: string | number; |
| 20 | +} |
| 21 | + |
| 22 | +function isTransferContent(content: Content): content is TransferContent { |
| 23 | + console.log("Content for transfer", content); |
| 24 | + return ( |
| 25 | + typeof content.recipient === "string" && |
| 26 | + (typeof content.amount === "string" || |
| 27 | + typeof content.amount === "number") |
| 28 | + ); |
| 29 | +} |
| 30 | + |
| 31 | +const transferTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined. |
| 32 | +
|
| 33 | +Example response: |
| 34 | +\`\`\`json |
| 35 | +{ |
| 36 | + "recipient": "EQCGScrZe1xbyWqWDvdI6mzP-GAcAWFv6ZXuaJOuSqemxku4", |
| 37 | + "amount": "1" |
| 38 | +} |
| 39 | +\`\`\` |
| 40 | +
|
| 41 | +{{recentMessages}} |
| 42 | +
|
| 43 | +Given the recent messages, extract the following information about the requested token transfer: |
| 44 | +- Recipient wallet address |
| 45 | +- Amount to transfer |
| 46 | +
|
| 47 | +Respond with a JSON markdown block containing only the extracted values.`; |
| 48 | + |
| 49 | +export class TransferAction { |
| 50 | + constructor(private walletProvider: WalletProvider) {} |
| 51 | + |
| 52 | + async transfer(params: TransferContent): Promise<string> { |
| 53 | + console.log( |
| 54 | + `Transferring: ${params.amount} tokens to (${params.recipient})` |
| 55 | + ); |
| 56 | + |
| 57 | + const walletClient = this.walletProvider.getWalletClient(); |
| 58 | + const contract = walletClient.open(this.walletProvider.wallet); |
| 59 | + |
| 60 | + try { |
| 61 | + // Create a transfer |
| 62 | + const seqno: number = await contract.getSeqno(); |
| 63 | + const transfer = await contract.createTransfer({ |
| 64 | + seqno, |
| 65 | + secretKey: this.walletProvider.keypair.secretKey, |
| 66 | + messages: [internal({ |
| 67 | + value: params.amount.toString(), |
| 68 | + to: params.recipient, |
| 69 | + body: 'eliza ton wallet plugin', |
| 70 | + })] |
| 71 | + }); |
| 72 | + |
| 73 | + await contract.send(transfer); |
| 74 | + |
| 75 | + // await this.waitForTransaction(seqno, contract); |
| 76 | + |
| 77 | + return transfer.hash().toString('hex'); |
| 78 | + |
| 79 | + } catch (error) { |
| 80 | + throw new Error(`Transfer failed: ${error.message}`); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const buildTransferDetails = async ( |
| 86 | + runtime: IAgentRuntime, |
| 87 | + message: Memory, |
| 88 | + state: State, |
| 89 | +): Promise<TransferContent> => { |
| 90 | + const walletInfo = await nativeWalletProvider.get(runtime, message, state); |
| 91 | + state.walletInfo = walletInfo; |
| 92 | + |
| 93 | + // Initialize or update state |
| 94 | + if (!state) { |
| 95 | + state = (await runtime.composeState(message)) as State; |
| 96 | + } else { |
| 97 | + state = await runtime.updateRecentMessageState(state); |
| 98 | + } |
| 99 | + |
| 100 | + // Define the schema for the expected output |
| 101 | + const transferSchema = z.object({ |
| 102 | + recipient: z.string(), |
| 103 | + amount: z.union([z.string(), z.number()]), |
| 104 | + }); |
| 105 | + |
| 106 | + // Compose transfer context |
| 107 | + const transferContext = composeContext({ |
| 108 | + state, |
| 109 | + template: transferTemplate, |
| 110 | + }); |
| 111 | + |
| 112 | + // Generate transfer content with the schema |
| 113 | + const content = await generateObjectV2({ |
| 114 | + runtime, |
| 115 | + context: transferContext, |
| 116 | + schema: transferSchema, |
| 117 | + modelClass: ModelClass.SMALL, |
| 118 | + }); |
| 119 | + |
| 120 | + const transferContent = content.object as TransferContent; |
| 121 | + |
| 122 | + return transferContent; |
| 123 | +}; |
| 124 | + |
| 125 | +export default { |
| 126 | + name: "SEND_TOKEN", |
| 127 | + similes: [ |
| 128 | + "SEND_TOKENS", |
| 129 | + "TOKEN_TRANSFER", |
| 130 | + "MOVE_TOKENS", |
| 131 | + "SEND_TON" |
| 132 | + ], |
| 133 | + description: "Transfer tokens from the agent's wallet to another", |
| 134 | + handler: async ( |
| 135 | + runtime: IAgentRuntime, |
| 136 | + message: Memory, |
| 137 | + state: State, |
| 138 | + options: any, |
| 139 | + callback?: HandlerCallback |
| 140 | + ) => { |
| 141 | + elizaLogger.log("Starting SEND_TOKEN handler..."); |
| 142 | + |
| 143 | + const transferDetails = await buildTransferDetails( |
| 144 | + runtime, |
| 145 | + message, |
| 146 | + state, |
| 147 | + ); |
| 148 | + |
| 149 | + // Validate transfer content |
| 150 | + if (!isTransferContent(transferDetails)) { |
| 151 | + console.error("Invalid content for TRANSFER_TOKEN action."); |
| 152 | + if (callback) { |
| 153 | + callback({ |
| 154 | + text: "Unable to process transfer request. Invalid content provided.", |
| 155 | + content: { error: "Invalid transfer content" }, |
| 156 | + }); |
| 157 | + } |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + try { |
| 162 | + |
| 163 | + const walletProvider = await initWalletProvider(runtime); |
| 164 | + const action = new TransferAction(walletProvider); |
| 165 | + const hash = await action.transfer(transferDetails); |
| 166 | + |
| 167 | + if (callback) { |
| 168 | + callback({ |
| 169 | + text: `Successfully transferred ${transferDetails.amount} TON to ${transferDetails.recipient}, Transaction: ${hash}`, |
| 170 | + content: { |
| 171 | + success: true, |
| 172 | + hash: hash, |
| 173 | + amount: transferDetails.amount, |
| 174 | + recipient: transferDetails.recipient, |
| 175 | + }, |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + return true; |
| 180 | + } catch (error) { |
| 181 | + console.error("Error during token transfer:", error); |
| 182 | + if (callback) { |
| 183 | + callback({ |
| 184 | + text: `Error transferring tokens: ${error.message}`, |
| 185 | + content: { error: error.message }, |
| 186 | + }); |
| 187 | + } |
| 188 | + return false; |
| 189 | + } |
| 190 | + }, |
| 191 | + template: transferTemplate, |
| 192 | + validate: async (runtime: IAgentRuntime) => { |
| 193 | + //console.log("Validating TON transfer from user:", message.userId); |
| 194 | + return true; |
| 195 | + }, |
| 196 | + examples: [ |
| 197 | + [ |
| 198 | + { |
| 199 | + user: "{{user1}}", |
| 200 | + content: { |
| 201 | + text: "Send 1 TON tokens to EQCGScrZe1xbyWqWDvdI6mzP-GAcAWFv6ZXuaJOuSqemxku4", |
| 202 | + action: "SEND_TOKENS", |
| 203 | + }, |
| 204 | + }, |
| 205 | + { |
| 206 | + user: "{{user2}}", |
| 207 | + content: { |
| 208 | + text: "I'll send 1 TON tokens now...", |
| 209 | + action: "SEND_TOKENS", |
| 210 | + }, |
| 211 | + }, |
| 212 | + { |
| 213 | + user: "{{user2}}", |
| 214 | + content: { |
| 215 | + text: "Successfully sent 1 TON tokens to EQCGScrZe1xbyWqWDvdI6mzP-GAcAWFv6ZXuaJOuSqemxku4, Transaction: c8ee4a2c1bd070005e6cd31b32270aa461c69b927c3f4c28b293c80786f78b43", |
| 216 | + }, |
| 217 | + }, |
| 218 | + ], |
| 219 | + ], |
| 220 | +}; |
0 commit comments