import axios, { AxiosResponse } from "axios";
import { z } from "zod";
import { baseUri, chainName } from "../../constants";
interface Transaction {
block_hash: string;
block_number: number;
block_timestamp: string;
contract_address: string;
cumulative_gas_used: string;
effective_gas_price: string;
from_address: string;
gas: string;
gas_price: string;
gas_used: string;
input: string;
l1_fee: string | null;
l1_fee_scalar: string | null;
l1_gas_price: string | null;
l1_gas_used: string | null;
max_fee_per_gas: string;
max_priority_fee_per_gas: string;
nonce: string;
status: number;
to_address: string;
transaction_hash: string;
transaction_index: number;
transaction_type: number;
value: string;
}
interface BlockTransactionsResponse {
cursor: string;
transactions: Transaction[];
}
export default function(server: any) {
server.tool(
"getBlockTransactions",
"Get transactions in a block",
{
chainName: z.literal(chainName),
blockNumber: z.string().describe("Block number to fetch transactions from"),
authToken: z
.string()
.describe("Authorization token for accessing the Somnia API"),
},
{
title: "Get Block Transactions",
readOnlyHint: true,
description: "Fetch transactions in a specific block by its number.",
destructiveHint: false,
},
async (args:any) => {
const { chainName, blockNumber, authToken } = args;
try {
const response: AxiosResponse<BlockTransactionsResponse> =
await axios.get(
`${baseUri}/${chainName}/v1/block/${blockNumber}/transactions`,
{
headers: {
"Authorization": `Bearer ${authToken}`,
"Content-Type": "application/json",
"Accept": "application/json",
},
}
);
return {
content: [
{
type: "text",
text: `Transaction Data: ${JSON.stringify(response.data)}`,
},
],
_meta: {
transactionData: response.data,
},
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error fetching transactions: ${error}`,
},
],
};
}
}
);
}