queryPriceByRound
Retrieve historical price data for cryptocurrency pairs by specifying round ID and chain, enabling analysis of past market conditions through Chainlink's decentralized feeds.
Instructions
Queries the price for a given pair and round ID on a specified chain (placeholder due to historical data limitations)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- index.js:107-155 (handler)The main handler function for the 'queryPriceByRound' tool. It validates inputs, finds the price feed, connects to the blockchain provider, fetches decimals and latest round data (placeholder for specific round), formats the price, and returns structured data or error.async ({ roundId, pair, chain }) => { try { // Validate inputs const chainKey = chain.toLowerCase(); queryPriceSchema.parse({ roundId, pair, chain }); // Find feed by pair const feed = feedsData[chainKey].feeds.find((f) => f.name.toLowerCase() === pair.toLowerCase()); if (!feed) { throw new Error(`Pair ${pair} not found on chain ${chain}`); } // Initialize provider and contract const provider = new ethers.JsonRpcProvider(`${feedsData[chainKey].baseUrl}/${process.env.INFURA_API_KEY}`); const priceFeedContract = new ethers.Contract(feed.proxyAddress, priceFeedAbi, provider); // Note: getRoundData may not be supported for historical rounds const decimals = await priceFeedContract.decimals(); const roundData = await priceFeedContract.latestRoundData(); // Placeholder const price = ethers.formatUnits(roundData.answer, decimals); const timestamp = Number(roundData.updatedAt) * 1000; return { content: [{ type: 'text', text: JSON.stringify({ chain, pair, price: Number(price), decimals: Number(decimals), roundId, timestamp: new Date(timestamp).toISOString(), proxyAddress: feed.proxyAddress, feedCategory: feed.feedCategory }, null, 2) }] }; } catch (error) { return { content: [{ type: 'text', text: `Error: ${error.message}` }], isError: true }; } } );
- index.js:94-100 (schema)Zod schema defining the input parameters for the 'queryPriceByRound' tool: roundId (string number), pair (string), chain (validated against feedsData).const queryPriceSchema = z.object({ roundId: z.string().regex(/^\d+$/, 'Round ID must be a number').describe('The round ID for the price data'), pair: z.string().describe('The price feed pair, e.g., FIL/ETH or FDUSD/USD'), chain: z.string().refine((val) => feedsData[val.toLowerCase()], { message: 'Unsupported chain' }).describe('The blockchain network, e.g., ethereum or base') });
- index.js:103-156 (registration)The server.tool registration call that registers the 'queryPriceByRound' tool with name, description, schema, and handler function.server.tool( 'queryPriceByRound', 'Queries the price for a given pair and round ID on a specified chain (placeholder due to historical data limitations)', queryPriceSchema, async ({ roundId, pair, chain }) => { try { // Validate inputs const chainKey = chain.toLowerCase(); queryPriceSchema.parse({ roundId, pair, chain }); // Find feed by pair const feed = feedsData[chainKey].feeds.find((f) => f.name.toLowerCase() === pair.toLowerCase()); if (!feed) { throw new Error(`Pair ${pair} not found on chain ${chain}`); } // Initialize provider and contract const provider = new ethers.JsonRpcProvider(`${feedsData[chainKey].baseUrl}/${process.env.INFURA_API_KEY}`); const priceFeedContract = new ethers.Contract(feed.proxyAddress, priceFeedAbi, provider); // Note: getRoundData may not be supported for historical rounds const decimals = await priceFeedContract.decimals(); const roundData = await priceFeedContract.latestRoundData(); // Placeholder const price = ethers.formatUnits(roundData.answer, decimals); const timestamp = Number(roundData.updatedAt) * 1000; return { content: [{ type: 'text', text: JSON.stringify({ chain, pair, price: Number(price), decimals: Number(decimals), roundId, timestamp: new Date(timestamp).toISOString(), proxyAddress: feed.proxyAddress, feedCategory: feed.feedCategory }, null, 2) }] }; } catch (error) { return { content: [{ type: 'text', text: `Error: ${error.message}` }], isError: true }; } } );