queryPriceByRound
Retrieve historical price data for a specific trading pair and round ID using Chainlink's decentralized price feeds, enabling precise analysis and integration into autonomous systems.
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-154 (handler)Handler function for queryPriceByRound tool. Fetches price data from Chainlink feed contract using ethers.js. Note: Currently uses latestRoundData as a placeholder instead of getRoundData(roundId).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 input schema for queryPriceByRound tool defining parameters: roundId, pair, chain with validation.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-155 (registration)Registers the 'queryPriceByRound' tool with MCP server using server.tool method, providing 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 }; } } );