Apologies for the delay. I’m not able to reproduce your issue, I can execute pnpm exec ts-node examples/update_price_feed.ts to run the @pythnetwork/pyth-solana-receiver examples. Can you try that?
If the issue comes from importing PythSolanaReceiver into your own test code, please provide source code snippets and output logs. It sounds like there’s some kind of compilation configuration issue going on in your setup.
This works. I’m looking to get the price details for different tokens, where I can have the price feed account upfront—similar to how I can get the Solana price using the 7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE price update account. Where can I find the corresponding price feed accounts for other tokens so that I can supply them directly to the program?
I am using npx-create-solana-dapp@4.6.0 scaffold for building my solana dapp,using npm@latest , where it uses Codama.js to generate client interface and then calls instructions using those interface. The test file in the new scaffold uses @solana/kit and solana-kite for testing instead of @coral-xyz/anchor and @solana/web3.js libraries.
This is my UpdateGoldTokenPrice.rs Instruction file where I want to fetch XAU/USD PriceFeed for on-chain calculations.
use crate::error::ErrorCode;
use crate::events::*;
use crate::state::*;
use anchor_lang::prelude::*;
use pyth_solana_receiver_sdk::price_update::get_feed_id_from_hex;
use pyth_solana_receiver_sdk::price_update::PriceUpdateV2;
// The instruction logic is updated to fetch the price
pub fn update_gold_price(ctx: Context<UpdateGoldPrice>) -> Result<()> {
require!(!ctx.accounts.gold_state.is_paused, ErrorCode::ProgramPaused);
let price_update = &mut ctx.accounts.price_update;
let maximum_age: u64 = 30;
let feed_id: [u8; 32] =
get_feed_id_from_hex("765d2ba906dbc32ca17cc11f5310a89e9ee1f6420508c63861f2f8ba4ee34bb2")?;
let current_price =
price_update.get_price_no_older_than(&Clock::get()?, maximum_age, &feed_id)?;
let exponent_adjustment = 10u64.pow(current_price.exponent.abs() as u32 - 2);
let price_in_cents = (current_price.price as u64) / exponent_adjustment;
require!(price_in_cents > 0, ErrorCode::InvalidPrice);
let gold_state = &mut ctx.accounts.gold_state;
gold_state.gold_price_usd = price_in_cents;
gold_state.last_price_update = Clock::get()?.unix_timestamp;
emit!(PriceUpdated {
new_price: price_in_cents,
timestamp: gold_state.last_price_update,
});
msg!(
"Gold price updated to {} cents per ounce from Pyth.",
price_in_cents
);
Ok(())
}
#[derive(Accounts)]
pub struct UpdateGoldPrice<'info> {
#[account(
mut,
has_one = oracle_authority @ ErrorCode::UnauthorizedOracle,
)]
pub gold_state: Account<'info, GoldState>,
// The admin who is authorized to trigger the price update
pub oracle_authority: Signer<'info>,
pub price_update: Account<'info, PriceUpdateV2>,
}
Now, how can I create a test function for the same , since the new @solana/kit works differently.