|
| 1 | +import { |
| 2 | + ActionExample, |
| 3 | + Content, |
| 4 | + HandlerCallback, |
| 5 | + IAgentRuntime, |
| 6 | + Memory, |
| 7 | + ModelClass, |
| 8 | + State, |
| 9 | + type Action, |
| 10 | + elizaLogger, |
| 11 | + composeContext, |
| 12 | + generateObject, |
| 13 | + generateObjectDeprecated, |
| 14 | +} from "@elizaos/core"; |
| 15 | +import { validateAvailConfig } from "../environment"; |
| 16 | +import { |
| 17 | + getDecimals, |
| 18 | + initialize, |
| 19 | + formatNumberToBalance, |
| 20 | + getKeyringFromSeed, |
| 21 | + isValidAddress, |
| 22 | +} from "avail-js-sdk"; |
| 23 | +import { ISubmittableResult } from "@polkadot/types/types/extrinsic"; |
| 24 | +import { H256 } from "@polkadot/types/interfaces/runtime"; |
| 25 | + |
| 26 | +export interface DataContent extends Content { |
| 27 | + data: string; |
| 28 | +} |
| 29 | + |
| 30 | +export function isDataContent(content: DataContent): content is DataContent { |
| 31 | + // Validate types |
| 32 | + const validTypes = typeof content.data === "string"; |
| 33 | + if (!validTypes) { |
| 34 | + return false; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +const submitDataTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined. |
| 39 | +
|
| 40 | +
|
| 41 | +Example response: |
| 42 | +\`\`\`json |
| 43 | +{ |
| 44 | + "data": "Hello World, this is the data I submitted" |
| 45 | +} |
| 46 | +\`\`\` |
| 47 | +
|
| 48 | +{{recentMessages}} |
| 49 | +
|
| 50 | +Given the recent messages, extract the following information about the requested AVAIL token transfer: |
| 51 | +- Data to be submitted |
| 52 | +
|
| 53 | +Respond with a JSON markdown block containing only the extracted values.`; |
| 54 | + |
| 55 | +export default { |
| 56 | + name: "SUBMIT_DATA", |
| 57 | + similes: [ |
| 58 | + "SUBMIT_DATA_TO_AVAIL", |
| 59 | + "SEND_DATA", |
| 60 | + "SEND_DATA_TO_AVAIL", |
| 61 | + "POST_DATA", |
| 62 | + "POST_DATA_TO_AVAIL", |
| 63 | + "POST_DATA_ON_AVAIL_NETWORK", |
| 64 | + "POST_DATA_TO_AVAIL_NETWORK", |
| 65 | + "SEND_DATA_ON_AVAIL_NETWORK", |
| 66 | + "SEND_DATA_TO_AVAIL_NETWORK", |
| 67 | + "SUBMIT_DATA_ON_AVAIL_NETWORK", |
| 68 | + "SUBMIT_DATA_TO_AVAIL_NETWORK", |
| 69 | + ], |
| 70 | + validate: async (runtime: IAgentRuntime, message: Memory) => { |
| 71 | + await validateAvailConfig(runtime); |
| 72 | + return true; |
| 73 | + }, |
| 74 | + description: "Submit data to Avail as per user command", |
| 75 | + handler: async ( |
| 76 | + runtime: IAgentRuntime, |
| 77 | + message: Memory, |
| 78 | + state: State, |
| 79 | + _options: { [key: string]: unknown }, |
| 80 | + callback?: HandlerCallback |
| 81 | + ): Promise<boolean> => { |
| 82 | + elizaLogger.log("Starting SUBMIT_DATA handler..."); |
| 83 | + |
| 84 | + // Initialize or update state |
| 85 | + if (!state) { |
| 86 | + state = (await runtime.composeState(message)) as State; |
| 87 | + } else { |
| 88 | + state = await runtime.updateRecentMessageState(state); |
| 89 | + } |
| 90 | + |
| 91 | + // Compose transfer context |
| 92 | + const submitDataContext = composeContext({ |
| 93 | + state, |
| 94 | + template: submitDataTemplate, |
| 95 | + }); |
| 96 | + |
| 97 | + // Generate transfer content |
| 98 | + const content = await generateObjectDeprecated({ |
| 99 | + runtime, |
| 100 | + context: submitDataContext, |
| 101 | + modelClass: ModelClass.SMALL, |
| 102 | + }); |
| 103 | + |
| 104 | + // Validate transfer content |
| 105 | + // if (!isDataContent(content)) { |
| 106 | + // console.log(content + typeof(content.data)) |
| 107 | + // console.error("Invalid content for SUBMIT_DATA action."); |
| 108 | + // if (callback) { |
| 109 | + // callback({ |
| 110 | + // text: "Unable to process submit data request. Invalid content provided.", |
| 111 | + // content: { error: "Invalid data content" }, |
| 112 | + // }); |
| 113 | + // } |
| 114 | + // return false; |
| 115 | + // } |
| 116 | + if (content.data != null) { |
| 117 | + try { |
| 118 | + const SEED = runtime.getSetting("AVAIL_SEED")!; |
| 119 | + const ACCOUNT = runtime.getSetting("AVAIL_ADDRESS")!; |
| 120 | + const ENDPOINT = runtime.getSetting("AVAIL_RPC_URL"); |
| 121 | + const APP_ID = runtime.getSetting("AVAIL_APP_ID"); |
| 122 | + |
| 123 | + const api = await initialize(ENDPOINT); |
| 124 | + const keyring = getKeyringFromSeed(SEED); |
| 125 | + const options = { app_id: APP_ID, nonce: -1 }; |
| 126 | + const decimals = getDecimals(api); |
| 127 | + const data = content.data; |
| 128 | + |
| 129 | + const submitDataInfo = await api.tx.dataAvailability |
| 130 | + .submitData(data) |
| 131 | + .paymentInfo(keyring); |
| 132 | + //print estimated fees |
| 133 | + elizaLogger.log(`Transaction Fee for Submit Data: |
| 134 | + class=${submitDataInfo.class.toString()}, |
| 135 | + weight=${submitDataInfo.weight.toString()}, |
| 136 | + partialFee=${submitDataInfo.partialFee.toHuman()} |
| 137 | + `); |
| 138 | + |
| 139 | + //submit data |
| 140 | + const txResult = await new Promise<ISubmittableResult>( |
| 141 | + (res) => { |
| 142 | + api.tx.dataAvailability |
| 143 | + .submitData(data) |
| 144 | + .signAndSend( |
| 145 | + keyring, |
| 146 | + options, |
| 147 | + (result: ISubmittableResult) => { |
| 148 | + elizaLogger.log( |
| 149 | + `Tx status: ${result.status}` |
| 150 | + ); |
| 151 | + if (result.isFinalized || result.isError) { |
| 152 | + res(result); |
| 153 | + } |
| 154 | + } |
| 155 | + ); |
| 156 | + } |
| 157 | + ); |
| 158 | + |
| 159 | + // Rejected Transaction handling |
| 160 | + if (txResult.isError) { |
| 161 | + console.log(`Transaction was not executed`); |
| 162 | + } |
| 163 | + |
| 164 | + // Failed Transaction handling |
| 165 | + const error = txResult.dispatchError; |
| 166 | + if (error != undefined) { |
| 167 | + if (error.isModule) { |
| 168 | + const decoded = api.registry.findMetaError( |
| 169 | + error.asModule |
| 170 | + ); |
| 171 | + const { docs, name, section } = decoded; |
| 172 | + console.log(`${section}.${name}: ${docs.join(" ")}`); |
| 173 | + } else { |
| 174 | + console.log(error.toString()); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + elizaLogger.success( |
| 179 | + "Data submitted successfully! tx: \n " + |
| 180 | + `Tx Hash: ${txResult.txHash as H256}, Block Hash: ${txResult.status.asFinalized as H256}` |
| 181 | + ); |
| 182 | + if (callback) { |
| 183 | + callback({ |
| 184 | + text: `Data submitted successfully! tx hash: ${txResult.txHash as H256} Block Hash: ${txResult.status.asFinalized as H256} `, |
| 185 | + content: {}, |
| 186 | + }); |
| 187 | + } |
| 188 | + |
| 189 | + return true; |
| 190 | + } catch (error) { |
| 191 | + elizaLogger.error("Error during data submission:", error); |
| 192 | + if (callback) { |
| 193 | + callback({ |
| 194 | + text: `Error submitting data: ${error.message}`, |
| 195 | + content: { error: error.message }, |
| 196 | + }); |
| 197 | + } |
| 198 | + return false; |
| 199 | + } |
| 200 | + } else { |
| 201 | + elizaLogger.log("No data mentioned to be submitted"); |
| 202 | + } |
| 203 | + }, |
| 204 | + |
| 205 | + examples: [ |
| 206 | + [ |
| 207 | + { |
| 208 | + user: "{{user1}}", |
| 209 | + content: { |
| 210 | + text: "Submit the following data to Avail 'Hello World!'", |
| 211 | + }, |
| 212 | + }, |
| 213 | + { |
| 214 | + user: "{{agent}}", |
| 215 | + content: { |
| 216 | + text: "Sure, I'll send the data 'Hello World!' to Avail now.", |
| 217 | + action: "SUBMIT_DATA", |
| 218 | + }, |
| 219 | + }, |
| 220 | + { |
| 221 | + user: "{{agent}}", |
| 222 | + content: { |
| 223 | + text: "Successfully submitted the data 'Hello World!' to Avail \nTransaction: 0x748057951ff79cea6de0e13b2ef70a1e9f443e9c83ed90e5601f8b45144a4ed4", |
| 224 | + }, |
| 225 | + }, |
| 226 | + ], |
| 227 | + [ |
| 228 | + { |
| 229 | + user: "{{user1}}", |
| 230 | + content: { |
| 231 | + text: "Submit 'Don't Fight, Unite!' to Avail", |
| 232 | + }, |
| 233 | + }, |
| 234 | + { |
| 235 | + user: "{{agent}}", |
| 236 | + content: { |
| 237 | + text: "Sure, I'll send the data 'Don't Fight, Unite!' to Avail now.", |
| 238 | + action: "SUBMIT_DATA", |
| 239 | + }, |
| 240 | + }, |
| 241 | + { |
| 242 | + user: "{{agent}}", |
| 243 | + content: { |
| 244 | + text: "Successfully submitted the data 'Don't Fight, Unite!' to Avail \nTransaction: 0x748057951ff79cea6de0e13b2ef70a1e9f443e9c83ed90e5601f8b45144a4ed4", |
| 245 | + }, |
| 246 | + }, |
| 247 | + ], |
| 248 | + ] as ActionExample[][], |
| 249 | +} as Action; |
0 commit comments