Skip to content

magicblock-labs/ephemeral-pricing-oracle

Repository files navigation

Ephemeral Pricing Oracle

This repository contains a Solana program designed to inject price feeds into ephemeral rollups. It includes a chain pusher that subscribes to and posts price updates on-chain, as well as an example of how to consume price data in a Solana program.

Currently supports:

Overview

The project is structured as follows:

  • Solana Program: A program that allow to create and update price feeds in the ephemeral rollups.
  • Chain Pusher: A component that subscribes to price updates from a price feed and posts these updates on-chain.
  • Example Consumer: An example demonstrating how to consume and utilize the price data within a Solana program.

Running the chain pusher

cargo run -- --auth_header "Bearer <your_auth_token>" --ws_url "ws_url" --cluster "https://devnet.magicblock.app"

Consuming Price Data in a Solana Program

  1. Add pyth sdk
cargo add pyth_solana_receiver_sdk
  1. Define the instruction context, passing the account as AccountInfo
#[derive(Accounts)]
pub struct Sample<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,
    /// CHECK: the correct price feed
    pub price_update: AccountInfo<'info>,
}
  1. Deserialize and use the price data
    pub fn sample(ctx: Context<Sample>) -> Result<()> {
        // Deserialize the price feed
        let price_update = PriceUpdateV2::try_deserialize_unchecked
            (&mut (*ctx.accounts.price_update.data.borrow()).as_ref(),
            ).map_err(Into::<Error>::into)?;

        // get_price_no_older_than will fail if the price update is more than 30 seconds old
        let maximum_age: u64 = 60;

        // Get the price feed id
        let feed_id: [u8; 32] = ctx.accounts.price_update.key().to_bytes();

        msg!("The price update is: {}", price_update.price_message.price);
        let price = price_update.get_price_no_older_than(&Clock::get()?, maximum_age, &feed_id)?;

        // Sample output:
        // The price is (7160106530699 ± 5129162301) * 10^-8
        msg!("The price is ({} ± {}) * 10^{}", price.price, price.conf, price.exponent);
        msg!("The price is: {}", price.price as f64 * 10_f64.powi(price.exponent));

        Ok(())
    }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published