How to fetch price without maintaining any update accounts?

Hello guys, please help me. I want my program to fetch the price. Like I call getSolPrice in my program, and it returns the price. I just don’t wanna update the price. Tried this function

fn calculate_sol_price(price_update: &Account<PriceUpdateV2>) -> Result<PriceData> {
    let price = price_update.get_price_no_older_than(
        &Clock::get()?,
        MAXIMUM_AGE,
        &get_feed_id_from_hex(FEED_ID)?,
    )?;

    require!(price.price > 0, LockerError::InvalidPrice);

    let normalized_price = if price.exponent >= 0 {
        price.price as u64 * 10_u64.pow(price.exponent as u32)
    } else {
        (price.price as u64) / 10_u64.pow((-price.exponent) as u32)
    };

    Ok(PriceData {
        price: price.price,
        exponent: price.exponent,
        normalized_price,
        publish_time: price.publish_time,
        confidence: price.conf,
    })
}

But it needs a price update account, I can’t find one, nor do I want to create/maintain one. For reference: This is a solidity function, I want something similar.

  function getEthUsdPrice() public view returns (uint256) {
        (, int256 price,,,) = ethUsdPriceFeed.latestRoundData();

        require(price > 0, "Invalid price");
        return uint256(price); // 8 decimals
    }

There are two ways of using oracle prices on blockchain:

  • pass the signed price payload in your instruction data (or calldata) and verify it in a smart contract
  • read it from some on-chain state (account or smart contract) where someone has previously placed it

On Solana, Pyth payloads are too big to be included in instruction data so the first way is not available. You therefore need to read the price from an account.

You can either maintain your own price update accounts or read from the sponsored ones https://docs.pyth.network/price-feeds/sponsored-feeds/solana.

1 Like