Skip to content

Commit

Permalink
fix: pyth price feed
Browse files Browse the repository at this point in the history
  • Loading branch information
arihantbansal committed Jan 4, 2025
1 parent a4f990f commit 42ffa3d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/tools/pyth_fetch_price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export async function fetchPythPriceFeedID(

/**
* Fetch the price of a given price feed from Pyth
* @param agent SolanaAgentKit instance
* @param priceFeedID Price feed ID
* @returns Latest price value from feed
*
Expand All @@ -72,10 +71,19 @@ export async function fetchPythPrice(feedID: string): Promise<string> {
}

const price = new BN(parsedData[0].price.price);
const exponent = new BN(parsedData[0].price.expo);

const scaledPrice = price.div(new BN(10).pow(exponent));
const exponent = parsedData[0].price.expo;

if (exponent < 0) {
const adjustedPrice = price.mul(new BN(100));
const divisor = new BN(10).pow(new BN(-exponent));
const scaledPrice = adjustedPrice.div(divisor);

const priceStr = scaledPrice.toString();
const formattedPrice = `${priceStr.slice(0, -2)}.${priceStr.slice(-2)}`;
return formattedPrice.startsWith('.') ? `0${formattedPrice}` : formattedPrice;
}

const scaledPrice = price.div(new BN(10).pow(new BN(exponent)));
return scaledPrice.toString();
} catch (error: any) {
throw new Error(`Fetching price from Pyth failed: ${error.message}`);
Expand Down

0 comments on commit 42ffa3d

Please sign in to comment.