import { z } from "zod";
import { api } from "../services/api.js";
import { getPrice } from "../services/clob-public.js";
const schema = z.object({
slug: z.string().describe("Market slug identifier"),
});
export const getMarketDetailsTool = {
name: "get_market_details",
description:
"Get complete market information including metadata, tokens, volume, liquidity, and livePrice by token_id. livePrice may be null when tokens are closed or have no active CLOB.",
parameters: schema,
execute: async (args: z.infer<typeof schema>) => {
try {
const market: any = await api.getMarketBySlug(args.slug);
// Try to get live prices from CLOB for each token
let prices: Record<string, any> = {};
try {
const tokenIds = typeof market.clobTokenIds === "string"
? JSON.parse(market.clobTokenIds)
: market.clobTokenIds;
if (Array.isArray(tokenIds)) {
const priceResults = await Promise.allSettled(
tokenIds.map((id: string) => getPrice(id)),
);
for (let i = 0; i < tokenIds.length; i++) {
const r = priceResults[i];
prices[tokenIds[i]] = r.status === "fulfilled" ? r.value : null;
}
}
} catch {
// Price fetch optional
}
return JSON.stringify({ ...market, livePrice: prices }, null, 2);
} catch (error) {
return JSON.stringify({ error: error instanceof Error ? error.message : String(error) });
}
},
};