-
-
Save ranimth0707/66811078923d1608834a961d511e284b to your computer and use it in GitHub Desktop.
RektoMeter — Pyth Network Price Feeds Integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * RektoMeter — Pyth Network Price Feeds Integration | |
| * https://rektometer.click | |
| * | |
| * This gist shows how to fetch real-time crypto prices | |
| * from Pyth Network's Hermes API for a portfolio tracker. | |
| */ | |
| // Step 1: Fetch all available crypto feeds from Pyth | |
| async function getAllCryptoFeeds() { | |
| const res = await fetch( | |
| 'https://hermes.pyth.network/v2/price_feeds?asset_type=crypto' | |
| ); | |
| const data = await res.json(); | |
| // Filter for USD pairs only and map to usable format | |
| return data | |
| .filter(f => | |
| f.attributes?.base && | |
| f.attributes?.quote_currency === 'USD' | |
| ) | |
| .map(f => ({ | |
| id: f.id, | |
| symbol: f.attributes.base, | |
| display: `${f.attributes.base}/USD`, | |
| })) | |
| .sort((a, b) => a.symbol.localeCompare(b.symbol)); | |
| } | |
| // Step 2: Fetch live prices for multiple assets at once | |
| async function getLivePrices(feedIds) { | |
| const params = new URLSearchParams(); | |
| feedIds.forEach(id => params.append('ids[]', id)); | |
| const res = await fetch( | |
| `https://hermes.pyth.network/v2/updates/price/latest?${params}` | |
| ); | |
| if (!res.ok) throw new Error(`Pyth API error: ${res.status}`); | |
| const data = await res.json(); | |
| // Parse prices using expo to get actual USD value | |
| const prices = {}; | |
| data.parsed.forEach(item => { | |
| prices[item.id] = parseFloat(item.price.price) | |
| * Math.pow(10, item.price.expo); | |
| }); | |
| return prices; | |
| } | |
| // Step 3: Calculate P&L for a position | |
| function calculatePnL(qty, buyPrice, currentPrice) { | |
| const modalCost = qty * buyPrice; | |
| const currentValue = qty * currentPrice; | |
| const unrealizedPnl = currentValue - modalCost; | |
| const unrealizedPct = modalCost > 0 | |
| ? (unrealizedPnl / modalCost) * 100 | |
| : 0; | |
| return { modalCost, currentValue, unrealizedPnl, unrealizedPct }; | |
| } | |
| // Example usage | |
| async function main() { | |
| // Get all available crypto feeds | |
| const feeds = await getAllCryptoFeeds(); | |
| console.log(`Found ${feeds.length} crypto feeds on Pyth`); | |
| // Find BTC and ETH feed IDs | |
| const btc = feeds.find(f => f.symbol === 'BTC'); | |
| const eth = feeds.find(f => f.symbol === 'ETH'); | |
| // Fetch live prices | |
| const prices = await getLivePrices([btc.id, eth.id]); | |
| const btcPrice = prices[btc.id]; | |
| const ethPrice = prices[eth.id]; | |
| console.log(`BTC/USD: $${btcPrice.toLocaleString()}`); | |
| console.log(`ETH/USD: $${ethPrice.toLocaleString()}`); | |
| // Calculate P&L example | |
| // Bought 1 BTC at $70,000 | |
| const pnl = calculatePnL(1, 70000, btcPrice); | |
| console.log(`Unrealized P&L: $${pnl.unrealizedPnl.toFixed(2)}`); | |
| console.log(`ROI: ${pnl.unrealizedPct.toFixed(2)}%`); | |
| } | |
| main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment