import { z } from "zod";
import axios, { AxiosResponse } from "axios";
import { baseUri, chainName } from "../../constants";
interface HistoricalBalanceResponse {
contract: {
address: string;
decimals: number | null;
erc_type: "ERC-20" | "ERC-721" | "ERC-1155";
logoUri: string | null;
name: string;
symbol: string;
};
holdings: {
balance: string;
raw_balance: string;
timestamp: string;
}[];
}
export default function (server: any) {
server.tool(
"getTokenBalancePortfolio",
"Commonly used to render a daily portfolio balance for an address broken down by the token. The timeframe is user-configurable, defaults to 30 days.",
{
chainName: z.literal(chainName),
walletAddress: z
.string()
.describe("Wallet address to fetch token balance for"),
authToken: z
.string()
.describe("Authorization token for accessing the Somnia API"),
},
{
title: "Get Token Balance Portfolio",
readOnlyHint: true,
description:
"Fetch the token balance portfolio for a specific wallet address.",
destructiveHint: false,
idempotentHint: true,
},
async (args:any) => {
const { chainName, walletAddress, authToken } = args;
try {
const response: AxiosResponse<HistoricalBalanceResponse> =
await axios.get(
`${baseUri}/${chainName}/v1/address/${walletAddress}/balance/portfolio`,
{
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
Accept: "application/json",
},
}
);
return {
content: [
{
type: "text",
text: `Token Balance Portfolio Data: ${JSON.stringify(
response.data
)}`,
},
],
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error fetching token balance portfolio: ${error}`,
},
],
};
}
}
);
}