|
| 1 | +import { |
| 2 | + composeContext, |
| 3 | + generateObjectDeprecated, |
| 4 | + HandlerCallback, |
| 5 | + IAgentRuntime, |
| 6 | + Memory, |
| 7 | + ModelClass, |
| 8 | + State, |
| 9 | +} from "@ai16z/eliza"; |
| 10 | +import { initWalletChainsData } from "../../providers/wallet/utils"; |
| 11 | +import { cosmosTransferTemplate } from "../../templates"; |
| 12 | +import { CosmosTransferActionService } from "./services/cosmos-transfer-action-service"; |
| 13 | +import type { CosmosTransferParams } from "./types"; |
| 14 | +import type { |
| 15 | + ICosmosPluginOptions, |
| 16 | + ICosmosWalletChains, |
| 17 | +} from "../../shared/interfaces"; |
| 18 | + |
| 19 | +export const createTransferAction = (pluginOptions: ICosmosPluginOptions) => ({ |
| 20 | + name: "COSMOS_TRANSFER", |
| 21 | + description: "Transfer tokens between addresses on the same chain", |
| 22 | + handler: async ( |
| 23 | + _runtime: IAgentRuntime, |
| 24 | + _message: Memory, |
| 25 | + state: State, |
| 26 | + _options: { [key: string]: unknown }, |
| 27 | + _callback?: HandlerCallback |
| 28 | + ) => { |
| 29 | + const cosmosTransferContext = composeContext({ |
| 30 | + state: state, |
| 31 | + template: cosmosTransferTemplate, |
| 32 | + templatingEngine: "handlebars", |
| 33 | + }); |
| 34 | + |
| 35 | + const cosmosTransferContent = await generateObjectDeprecated({ |
| 36 | + runtime: _runtime, |
| 37 | + context: cosmosTransferContext, |
| 38 | + modelClass: ModelClass.SMALL, |
| 39 | + }); |
| 40 | + |
| 41 | + const paramOptions: CosmosTransferParams = { |
| 42 | + chainName: cosmosTransferContent.chainName, |
| 43 | + symbol: cosmosTransferContent.symbol, |
| 44 | + amount: cosmosTransferContent.amount, |
| 45 | + toAddress: cosmosTransferContent.toAddress, |
| 46 | + }; |
| 47 | + |
| 48 | + try { |
| 49 | + const walletProvider: ICosmosWalletChains = |
| 50 | + await initWalletChainsData(_runtime); |
| 51 | + |
| 52 | + const action = new CosmosTransferActionService(walletProvider); |
| 53 | + |
| 54 | + const customAssets = (pluginOptions?.customChainData ?? []).map( |
| 55 | + (chainData) => chainData.assets |
| 56 | + ); |
| 57 | + |
| 58 | + const transferResp = await action.execute( |
| 59 | + paramOptions, |
| 60 | + customAssets |
| 61 | + ); |
| 62 | + |
| 63 | + if (_callback) { |
| 64 | + await _callback({ |
| 65 | + text: `Successfully transferred ${paramOptions.amount} tokens to ${paramOptions.toAddress}\nGas paid: ${transferResp.gasPaid}\nTransaction Hash: ${transferResp.txHash}`, |
| 66 | + content: { |
| 67 | + success: true, |
| 68 | + hash: transferResp.txHash, |
| 69 | + amount: paramOptions.amount, |
| 70 | + recipient: transferResp.to, |
| 71 | + chain: cosmosTransferContent.fromChain, |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + const newMemory: Memory = { |
| 76 | + userId: _message.agentId, |
| 77 | + agentId: _message.agentId, |
| 78 | + roomId: _message.roomId, |
| 79 | + content: { |
| 80 | + text: `Transaction ${paramOptions.amount} ${paramOptions.symbol} to address ${paramOptions.toAddress} on chain ${paramOptions.toAddress} was successfully transfered.\n Gas paid: ${transferResp.gasPaid}. Tx hash: ${transferResp.txHash}`, |
| 81 | + }, |
| 82 | + }; |
| 83 | + |
| 84 | + await _runtime.messageManager.createMemory(newMemory); |
| 85 | + } |
| 86 | + return true; |
| 87 | + } catch (error) { |
| 88 | + console.error("Error during token transfer:", error); |
| 89 | + |
| 90 | + if (_callback) { |
| 91 | + await _callback({ |
| 92 | + text: `Error transferring tokens: ${error.message}`, |
| 93 | + content: { error: error.message }, |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + const newMemory: Memory = { |
| 98 | + userId: _message.agentId, |
| 99 | + agentId: _message.agentId, |
| 100 | + roomId: _message.roomId, |
| 101 | + content: { |
| 102 | + text: `Transaction ${paramOptions.amount} ${paramOptions.symbol} to address ${paramOptions.toAddress} on chain ${paramOptions.toAddress} was unsuccessful.`, |
| 103 | + }, |
| 104 | + }; |
| 105 | + |
| 106 | + await _runtime.messageManager.createMemory(newMemory); |
| 107 | + |
| 108 | + return false; |
| 109 | + } |
| 110 | + }, |
| 111 | + template: cosmosTransferTemplate, |
| 112 | + validate: async (runtime: IAgentRuntime) => { |
| 113 | + const mnemonic = runtime.getSetting("COSMOS_RECOVERY_PHRASE"); |
| 114 | + const availableChains = runtime.getSetting("COSMOS_AVAILABLE_CHAINS"); |
| 115 | + const availableChainsArray = availableChains?.split(","); |
| 116 | + |
| 117 | + return !(mnemonic && availableChains && availableChainsArray.length); |
| 118 | + }, |
| 119 | + examples: [ |
| 120 | + [ |
| 121 | + { |
| 122 | + user: "{{user1}}", |
| 123 | + content: { |
| 124 | + text: "Make transfer {{0.0001 OM}} to {{mantra1pcnw46km8m5amvf7jlk2ks5std75k73aralhcf}} on {{mantrachaintestnet2}}", |
| 125 | + action: "COSMOS_TRANSFER", |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + user: "{{user2}}", |
| 130 | + content: { |
| 131 | + text: "Do you confirm the transfer action?", |
| 132 | + action: "COSMOS_TRANSFER", |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + user: "{{user1}}", |
| 137 | + content: { |
| 138 | + text: "Yes", |
| 139 | + action: "COSMOS_TRANSFER", |
| 140 | + }, |
| 141 | + }, |
| 142 | + { |
| 143 | + user: "{{user2}}", |
| 144 | + content: { |
| 145 | + text: "", |
| 146 | + action: "COSMOS_TRANSFER", |
| 147 | + }, |
| 148 | + }, |
| 149 | + ], |
| 150 | + [ |
| 151 | + { |
| 152 | + user: "{{user1}}", |
| 153 | + content: { |
| 154 | + text: "Send {{10 OSMO}} to {{osmo13248w8dtnn07sxc3gq4l3ts4rvfyat6f4qkdd6}} on {{osmosistestnet}}", |
| 155 | + action: "COSMOS_TRANSFER", |
| 156 | + }, |
| 157 | + }, |
| 158 | + { |
| 159 | + user: "{{user2}}", |
| 160 | + content: { |
| 161 | + text: "Do you confirm the transfer action?", |
| 162 | + action: "COSMOS_TRANSFER", |
| 163 | + }, |
| 164 | + }, |
| 165 | + { |
| 166 | + user: "{{user1}}", |
| 167 | + content: { |
| 168 | + text: "Yes", |
| 169 | + action: "COSMOS_TRANSFER", |
| 170 | + }, |
| 171 | + }, |
| 172 | + { |
| 173 | + user: "{{user2}}", |
| 174 | + content: { |
| 175 | + text: "", |
| 176 | + action: "COSMOS_TRANSFER", |
| 177 | + }, |
| 178 | + }, |
| 179 | + ], |
| 180 | + [ |
| 181 | + { |
| 182 | + user: "{{user1}}", |
| 183 | + content: { |
| 184 | + text: "Send {{0.0001 OM}} on {{mantrachaintestnet2}} to {{mantra1pcnw46km8m5amvf7jlk2ks5std75k73aralhcf}}.", |
| 185 | + action: "COSMOS_TRANSFER", |
| 186 | + }, |
| 187 | + }, |
| 188 | + { |
| 189 | + user: "{{user2}}", |
| 190 | + content: { |
| 191 | + text: "Do you confirm the transfer action?", |
| 192 | + action: "COSMOS_TRANSFER", |
| 193 | + }, |
| 194 | + }, |
| 195 | + { |
| 196 | + user: "{{user1}}", |
| 197 | + content: { |
| 198 | + text: "Yes", |
| 199 | + action: "COSMOS_TRANSFER", |
| 200 | + }, |
| 201 | + }, |
| 202 | + { |
| 203 | + user: "{{user2}}", |
| 204 | + content: { |
| 205 | + text: "", |
| 206 | + action: "COSMOS_TRANSFER", |
| 207 | + }, |
| 208 | + }, |
| 209 | + ], |
| 210 | + ], |
| 211 | + similes: [ |
| 212 | + "COSMOS_SEND_TOKENS", |
| 213 | + "COSMOS_TOKEN_TRANSFER", |
| 214 | + "COSMOS_MOVE_TOKENS", |
| 215 | + ], |
| 216 | +}); |
0 commit comments