Pyth
Pythは、現実世界の金融および暗号市場データを取得するために使用されるOracleです。Pyth Oracleは、さまざまなユースケースでデータを消費する際にオンチェーンプログラムで使用できます。
クライアントでPythを使用する方法
Pythは**@pythnetwork/client**として呼び出されるJavaScript/TypeScriptライブラリを提供します。このライブラリを使用して、ウェブサイトでのPyth価格の表示などといった、オフチェーンアプリケーションに使用するオンチェーンPyth データを読み取ることができます。詳細はこちら
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();
AnchorでPythを使用する方法
Pythは、オンチェーンプログラムまたはオフチェーンアプリケーションがpythのデータを消費するために使用できるRust Crateを提供します。
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();