|
| 1 | + |
| 2 | +import { elizaLogger, } from "@elizaos/core"; |
| 3 | +import { |
| 4 | + ActionExample, |
| 5 | + Content, |
| 6 | + HandlerCallback, |
| 7 | + IAgentRuntime, |
| 8 | + Memory, |
| 9 | + ModelClass, |
| 10 | + State, |
| 11 | + type Action, |
| 12 | +} from "@elizaos/core"; |
| 13 | +import { composeContext } from "@elizaos/core"; |
| 14 | +import { generateObjectDeprecated } from "@elizaos/core"; |
| 15 | +import { ACTIONS } from "solana-agent-kit"; |
| 16 | +import { getSAK } from "../client"; |
| 17 | + |
| 18 | +const GET_TOKEN_INFO_ACTION = ACTIONS.GET_TOKEN_DATA_ACTION; |
| 19 | + |
| 20 | +export interface GetTokenInfoContent extends Content { |
| 21 | + tokenAddress: string; |
| 22 | +} |
| 23 | + |
| 24 | +function isGetTokenInfoContent( |
| 25 | + runtime: IAgentRuntime, |
| 26 | + content: any |
| 27 | +): content is GetTokenInfoContent { |
| 28 | + elizaLogger.log("Content for transfer", content); |
| 29 | + return ( |
| 30 | + typeof content.tokenAddress === "string" |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +const getTokenInfoTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined. |
| 35 | +
|
| 36 | +Example response: |
| 37 | +\`\`\`json |
| 38 | +{ |
| 39 | + "tokenAddress": "SENDdRQtYMWaQrBroBrJ2Q53fgVuq95CV9UPGEvpCxa", |
| 40 | +} |
| 41 | +\`\`\` |
| 42 | +
|
| 43 | +{{recentMessages}} |
| 44 | +
|
| 45 | +Given the recent messages, extract the following information about the requested token: |
| 46 | +- Token contract address |
| 47 | +
|
| 48 | +Respond with a JSON markdown block containing only the extracted values.`; |
| 49 | + |
| 50 | +export default { |
| 51 | + name: GET_TOKEN_INFO_ACTION.name, |
| 52 | + similes: GET_TOKEN_INFO_ACTION.similes, |
| 53 | + validate: async (runtime: IAgentRuntime, message: Memory) => { |
| 54 | + elizaLogger.log("Validating get token info from user:", message.userId); |
| 55 | + |
| 56 | + return false; |
| 57 | + }, |
| 58 | + description: GET_TOKEN_INFO_ACTION.description, |
| 59 | + handler: async ( |
| 60 | + runtime: IAgentRuntime, |
| 61 | + message: Memory, |
| 62 | + state: State, |
| 63 | + _options: { [key: string]: unknown }, |
| 64 | + callback?: HandlerCallback |
| 65 | + ): Promise<boolean> => { |
| 66 | + elizaLogger.log("Starting GET_TOKEN_INFO handler..."); |
| 67 | + const sak = await getSAK(runtime); |
| 68 | + |
| 69 | + // Initialize or update state |
| 70 | + if (!state) { |
| 71 | + state = (await runtime.composeState(message)) as State; |
| 72 | + } else { |
| 73 | + state = await runtime.updateRecentMessageState(state); |
| 74 | + } |
| 75 | + |
| 76 | + // Compose get token info context |
| 77 | + const getTokenInfoContext = composeContext({ |
| 78 | + state, |
| 79 | + template: getTokenInfoTemplate, |
| 80 | + }); |
| 81 | + |
| 82 | + // Generate get token info content |
| 83 | + const content = await generateObjectDeprecated({ |
| 84 | + runtime, |
| 85 | + context: getTokenInfoContext, |
| 86 | + modelClass: ModelClass.LARGE, |
| 87 | + }); |
| 88 | + |
| 89 | + // Validate get token info content |
| 90 | + if (!isGetTokenInfoContent(runtime, content)) { |
| 91 | + elizaLogger.error("Invalid content for GET_TOKEN_INFO action."); |
| 92 | + if (callback) { |
| 93 | + callback({ |
| 94 | + text: "Unable to process get token info request. Invalid content provided.", |
| 95 | + content: { error: "Invalid get token info content" }, |
| 96 | + }); |
| 97 | + } |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + |
| 103 | + const tokenData = await sak.getTokenDataByAddress(content.tokenAddress) |
| 104 | + |
| 105 | + console.log("Token data:", tokenData); |
| 106 | + |
| 107 | + if (callback) { |
| 108 | + callback({ |
| 109 | + text: `Successfully retrieved token data for ${content.tokenAddress}`, |
| 110 | + content: { |
| 111 | + success: true, |
| 112 | + tokenData: tokenData, |
| 113 | + }, |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + return true; |
| 118 | + } catch (error) { |
| 119 | + elizaLogger.error("Error during get token info:", error); |
| 120 | + if (callback) { |
| 121 | + callback({ |
| 122 | + text: `Error getting token info: ${error.message}`, |
| 123 | + content: { error: error.message }, |
| 124 | + }); |
| 125 | + } |
| 126 | + return false; |
| 127 | + } |
| 128 | + }, |
| 129 | + |
| 130 | + examples: [ |
| 131 | + [ |
| 132 | + { |
| 133 | + user: "{{user1}}", |
| 134 | + content: { |
| 135 | + text: "Get token info for SENDdRQtYMWaQrBroBrJ2Q53fgVuq95CV9UPGEvpCxa", |
| 136 | + }, |
| 137 | + }, |
| 138 | + { |
| 139 | + user: "{{user2}}", |
| 140 | + content: { |
| 141 | + text: "Get token info for SENDdRQtYMWaQrBroBrJ2Q53fgVuq95CV9UPGEvpCxa", |
| 142 | + action: "GET_TOKEN_INFO", |
| 143 | + }, |
| 144 | + }, |
| 145 | + { |
| 146 | + user: "{{user2}}", |
| 147 | + content: { |
| 148 | + text: "Successfully retrieved token info for SENDdRQtYMWaQrBroBrJ2Q53fgVuq95CV9UPGEvpCxa", |
| 149 | + }, |
| 150 | + }, |
| 151 | + ], |
| 152 | + ] as ActionExample[][], |
| 153 | +} as Action; |
0 commit comments