import { tool } from 'ai'; import { z } from "zod"; import axios from 'axios'; const getTransactionDetails = async (txHash) => { const url = `https://pro-api.solscan.io/v2.0/transaction/detail?tx=${txHash}`; const options = { method: 'GET', url: url, headers: { 'token': process.env.SOLSCAN_API_TOKEN, }, }; try { const response = await axios.request(options); const { log_message, address_table_lookup, signer, list_signer, status, compute_units_consumed, confirmations, version, priority_fee, tx_hash } = response.data.data; return { log_message, address_table_lookup, signer, list_signer, status, compute_units_consumed, confirmations, version, priority_fee, tx_hash }; } catch (error) { throw new Error('Error fetching tx details from API 1. The tx may not exist'); } }; const getTransactionActions = async (txHash) => { const url = `https://pro-api.solscan.io/v2.0/transaction/actions?tx=${txHash}`; const options = { method: 'GET', url: url, headers: { 'token': process.env.SOLSCAN_API_TOKEN, }, }; try { const response = await axios.request(options); const { tx_hash, block_id, block_time, fee, transfers, activities } = response.data.data; return { tx_hash, block_id, block_time, fee, transfers, activities }; } catch (error) { throw new Error('Error fetching transaction actions from API 2. The tx may not exist'); } }; export const getTransactionTool = (userId, chatId,responseId, dataStream) => tool({ description: `Retrieve parsed transaction details by its hash. You will get the transaction logs and parsed instructions. lookup tables, and a few generic details. A transaction thats using a lookup table for creating a token (especially on pump.fun) is probably a bundled transaction. Warn the user about interacting with the created token. Take a deep breath and think about everything provided in the provided data. The user doesnt care about all the details, unless the users asks for it, dont data dump everything you see back to the user. Keep your responses concise and explain only when necessary `, parameters: z.object({ txHash: z.string().describe("The transaction hash (tx) to fetch details for"), }), execute: async ({ txHash }) => { try { console.log(`${userId} ${chatId} called getTransactionTool with txHash: ${txHash}`); const transactionDetails = await getTransactionDetails(txHash); const transactionActions = await getTransactionActions(txHash); const combinedResponse = { ...transactionDetails, ...transactionActions }; if (transactionDetails) { dataStream.writeMessageAnnotation({ id: responseId, tool_type: 'tx', content: txHash.toString(), }); } return JSON.stringify(combinedResponse); } catch (error) { console.error(`Error executing getTransactionTool for txHash ${txHash}:`, error.message); return `Failed to fetch transaction details for txHash: ${txHash}. It may not exist`; } } });