|
| 1 | +import { ByteArray, formatEther, parseEther, type Hex } from "viem"; |
| 2 | +import { |
| 3 | + composeContext, |
| 4 | + generateObjectDeprecated, |
| 5 | + HandlerCallback, |
| 6 | + ModelClass, |
| 7 | + type IAgentRuntime, |
| 8 | + type Memory, |
| 9 | + type State, |
| 10 | +} from "@elizaos/core"; |
| 11 | + |
| 12 | +import { initWalletProvider, WalletProvider } from "../providers/wallet"; |
| 13 | +import type { Transaction, TransferParams } from "../types"; |
| 14 | +import { transferTemplate } from "../templates"; |
| 15 | + |
| 16 | +export { transferTemplate }; |
| 17 | + |
| 18 | +// Exported for tests |
| 19 | +export class TransferAction { |
| 20 | + constructor(private walletProvider: WalletProvider) {} |
| 21 | + |
| 22 | + async transfer(params: TransferParams): Promise<Transaction> { |
| 23 | + const walletClient = this.walletProvider.getWalletClient( |
| 24 | + params.fromChain |
| 25 | + ); |
| 26 | + |
| 27 | + console.log( |
| 28 | + `Transferring: ${params.amount} tokens from (${walletClient.account.address} to (${params.toAddress} on ${params.fromChain})` |
| 29 | + ); |
| 30 | + |
| 31 | + if (!params.data) { |
| 32 | + params.data = "0x"; |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + const hash = await walletClient.sendTransaction({ |
| 37 | + account: walletClient.account, |
| 38 | + to: params.toAddress, |
| 39 | + value: parseEther(params.amount), |
| 40 | + data: params.data as Hex, |
| 41 | + kzg: { |
| 42 | + blobToKzgCommitment: function (_: ByteArray): ByteArray { |
| 43 | + throw new Error("Function not implemented."); |
| 44 | + }, |
| 45 | + computeBlobKzgProof: function ( |
| 46 | + _blob: ByteArray, |
| 47 | + _commitment: ByteArray |
| 48 | + ): ByteArray { |
| 49 | + throw new Error("Function not implemented."); |
| 50 | + }, |
| 51 | + }, |
| 52 | + chain: undefined, |
| 53 | + }); |
| 54 | + |
| 55 | + return { |
| 56 | + hash, |
| 57 | + from: walletClient.account.address, |
| 58 | + to: params.toAddress, |
| 59 | + value: parseEther(params.amount), |
| 60 | + data: params.data as Hex, |
| 61 | + }; |
| 62 | + } catch (error) { |
| 63 | + throw new Error(`Transfer failed: ${error.message}`); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const buildTransferDetails = async ( |
| 69 | + state: State, |
| 70 | + runtime: IAgentRuntime, |
| 71 | + wp: WalletProvider |
| 72 | +): Promise<TransferParams> => { |
| 73 | + const context = composeContext({ |
| 74 | + state, |
| 75 | + template: transferTemplate, |
| 76 | + }); |
| 77 | + |
| 78 | + const chains = Object.keys(wp.chains); |
| 79 | + |
| 80 | + const contextWithChains = context.replace( |
| 81 | + "SUPPORTED_CHAINS", |
| 82 | + chains.map((item) => `"${item}"`).join("|") |
| 83 | + ); |
| 84 | + |
| 85 | + const transferDetails = (await generateObjectDeprecated({ |
| 86 | + runtime, |
| 87 | + context: contextWithChains, |
| 88 | + modelClass: ModelClass.SMALL, |
| 89 | + })) as TransferParams; |
| 90 | + |
| 91 | + const existingChain = wp.chains[transferDetails.fromChain]; |
| 92 | + |
| 93 | + if (!existingChain) { |
| 94 | + throw new Error( |
| 95 | + "The chain " + |
| 96 | + transferDetails.fromChain + |
| 97 | + " not configured yet. Add the chain or choose one from configured: " + |
| 98 | + chains.toString() |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + return transferDetails; |
| 103 | +}; |
| 104 | + |
| 105 | +export const transferAction = { |
| 106 | + name: "transfer", |
| 107 | + description: "Transfer tokens between addresses on the same chain", |
| 108 | + handler: async ( |
| 109 | + runtime: IAgentRuntime, |
| 110 | + _message: Memory, |
| 111 | + state: State, |
| 112 | + _options: Record<string, unknown>, |
| 113 | + callback?: HandlerCallback |
| 114 | + ) => { |
| 115 | + console.log("Transfer action handler called"); |
| 116 | + const walletProvider = initWalletProvider(runtime); |
| 117 | + const action = new TransferAction(walletProvider); |
| 118 | + |
| 119 | + // Compose transfer context |
| 120 | + const paramOptions = await buildTransferDetails( |
| 121 | + state, |
| 122 | + runtime, |
| 123 | + walletProvider |
| 124 | + ); |
| 125 | + |
| 126 | + try { |
| 127 | + const transferResp = await action.transfer(paramOptions); |
| 128 | + if (callback) { |
| 129 | + callback({ |
| 130 | + text: `Successfully transferred ${paramOptions.amount} tokens to ${paramOptions.toAddress}\nTransaction Hash: ${transferResp.hash}`, |
| 131 | + content: { |
| 132 | + success: true, |
| 133 | + hash: transferResp.hash, |
| 134 | + amount: formatEther(transferResp.value), |
| 135 | + recipient: transferResp.to, |
| 136 | + chain: paramOptions.fromChain, |
| 137 | + }, |
| 138 | + }); |
| 139 | + } |
| 140 | + return true; |
| 141 | + } catch (error) { |
| 142 | + console.error("Error during token transfer:", error); |
| 143 | + if (callback) { |
| 144 | + callback({ |
| 145 | + text: `Error transferring tokens: ${error.message}`, |
| 146 | + content: { error: error.message }, |
| 147 | + }); |
| 148 | + } |
| 149 | + return false; |
| 150 | + } |
| 151 | + }, |
| 152 | + template: transferTemplate, |
| 153 | + validate: async (runtime: IAgentRuntime) => { |
| 154 | + const privateKey = runtime.getSetting("ARTHERA_PRIVATE_KEY"); |
| 155 | + return typeof privateKey === "string" && privateKey.startsWith("0x"); |
| 156 | + }, |
| 157 | + examples: [ |
| 158 | + [ |
| 159 | + { |
| 160 | + user: "assistant", |
| 161 | + content: { |
| 162 | + text: "I'll help you transfer 1 AA to 0x742d35Cc6634C0532925a3b844Bc454e4438f44e", |
| 163 | + action: "SEND_TOKENS", |
| 164 | + }, |
| 165 | + }, |
| 166 | + { |
| 167 | + user: "user", |
| 168 | + content: { |
| 169 | + text: "Transfer 1 AA to 0x742d35Cc6634C0532925a3b844Bc454e4438f44e", |
| 170 | + action: "SEND_TOKENS", |
| 171 | + }, |
| 172 | + }, |
| 173 | + ], |
| 174 | + ], |
| 175 | + similes: ["SEND_TOKENS", "TOKEN_TRANSFER", "MOVE_TOKENS"], |
| 176 | +}; |
0 commit comments