import axios from "axios";
import { AxiosResponse } from "axios";
import { baseUri, chainName } from "../../constants";
import z from "zod";
interface items {
extra_data: string;
gas_limit: number;
gas_used: number;
hash: string;
miner: string;
number: number;
parent_hash: string;
timestamp: string;
}
interface BlockHeightsResponse {
count: number;
cursor: string;
items: items[];
}
export default function(server: any) {
server.tool(
"getBlockHeights",
"Get all block heights between two dates",
{
chainName: z.literal(chainName),
startDate: z.string().describe("Start date in YYYY-MM-DD format"),
endDate: z.string().describe("End date in YYYY-MM-DD format"),
authToken: z
.string()
.describe("Authorization token for accessing the Somnia API"),
},
{
title: "Get Block Heights",
readOnlyHint: true,
description: "Fetch all block heights between two dates.",
destructiveHint: false,
},
async (args:any) => {
const { chainName, startDate, endDate, authToken } = args;
try {
const response: AxiosResponse<BlockHeightsResponse> = await axios.get(
`${baseUri}/${chainName}/v1/blocks/${startDate}/${endDate}`,
{
headers: {
"Authorization": `Bearer ${authToken}`,
"Content-Type": "application/json",
"Accept": "application/json",
},
params: {
startDate,
endDate,
},
}
);
return {
content: [
{
type: "text",
text: `Block Heights Data: ${JSON.stringify(response.data)}`,
},
],
_meta: {
blockHeightsData: response.data,
},
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error fetching block heights: ${error}`,
},
],
};
}
}
);
}