Pyth
Pyth est un Oracle utilisé pour obtenir des données financières et des données sur le marché des crypto-monnaies dans le monde réel. L'Oracle de Pyth peut être utilisé par les programmes on-chain afin d'utiliser des données dans divers cas d'utilisation.
Comment utiliser Pyth dans le Client
Pyth fournit une bibliothèque JavaScript/TypeScript appelée @pythnetwork/client. Cette bibliothèque peut être utilisée pour lire les données on-chain de Pyth pour des applications off-chain, comme l'affichage du prix du jeton Pyth sur un site web. Plus d'informations à ce sujet ici
Press </> button to view full source
import * as web3 from "@solana/web3.js";
import * as pyth from "@pythnetwork/client";
(async () => {
  const connection = new web3.Connection(
    web3.clusterApiUrl("devnet"),
    "confirmed"
  );
  const pythConnection = new pyth.PythConnection(
    connection,
    pyth.getPythProgramKeyForCluster("devnet")
  );
  pythConnection.onPriceChange((product, price) => {
    if (price.price && price.confidence) {
      console.log(
        `${product.symbol}: $${price.price} \xB1$${price.confidence}`
      );
    } else {
      console.log(
        `${product.symbol}: price currently unavailable. status is ${price.status}`
      );
    }
  });
  pythConnection.start();
})();
const pythConnection = new pyth.PythConnection(
  connection,
  pyth.getPythProgramKeyForCluster("devnet")
);
pythConnection.onPriceChange((product, price) => {
  if (price.price && price.confidence) {
    console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence}`);
  } else {
    console.log(
      `${product.symbol}: price currently unavailable. status is ${price.status}`
    );
  }
});
pythConnection.start();
Comment utiliser Pyth dans Anchor
Pyth fournit un Crate Rust qui peut être utilisé par des programmes on-chain ou des applications off-chain pour utiliser les données de Pyth.
Press </> button to view full source
use anchor_lang::prelude::*;
use pyth_client::{self, load_price, Price};
declare_id!("6B7XgKFmo73geJY8ZboSpLhkTumvwXeCXBpeP7nCT35w");
#[program]
pub mod pyth_test {
    use super::*;
    pub fn get_sol_price(ctx: Context<SolPrice>) -> Result<()> {
        let pyth_price_info = &ctx.accounts.pyth_account;
        let pyth_price_data = &pyth_price_info.try_borrow_data()?;
        let price_account: Price = *load_price(pyth_price_data).unwrap();
        msg!("price_account .. {:?}", pyth_price_info.key);
        msg!("price_type ... {:?}", price_account.ptype);
        msg!("price ........ {}", price_account.agg.price);
        Ok(())
    }
}
#[derive(Accounts)]
pub struct SolPrice<'info> {
    #[account(mut)]
    pub user_account: Signer<'info>,
    pub pyth_account: UncheckedAccount<'info>,
    pub system_program: UncheckedAccount<'info>,
    pub rent: Sysvar<'info, Rent>,
}
let pyth_price_info = &ctx.accounts.pyth_account;
let pyth_price_data = &pyth_price_info.try_borrow_data()?;
let price_account: Price = *load_price(pyth_price_data).unwrap();