On solana how can I verify that a pyth benchmark came from the expected price feed?
For example, say I have a contract that validates a benchmark price is within a certain range in order to settle a trade asynchronously.
I would have to have a guarantee that the benchmark came from the expected price feed.
I’m having trouble finding examples of this kind of interaction but I might just not understand how to integrate it correctly yet.
Basically I need to do a CPI from my solana program to the pyth solana program to verify the price, but I also need to know where the benchmark originated… hope that makes sense.
in the example below I’m having trouble understanding what the price feed consumer instruction actually does:
You don’t need to CPI from your contract to Pyth contract, the sdk creates instructions that come before your instruction which creates a PriceUpdateV2 account if the update data is verified (otherwise the whole tx reverts); later when you use the account in your instruction it is already verified. within the PriceUpdateV2 account, there are both feedId and timestamps and you can check them to make sure your requirements are met.
I came across this thread looking for a similar answer. Thanks for indicating that it creates a PriceUpdateV2 account. Our team is already using the PriceUpdateV2 account to read the latest price directly on-chain so that sounds like the change to Benchmarks should be pretty straight forward. However, we are still a little confused about the Benchmarks system.
This documentation - https://docs.pyth.network/benchmarks - makes it sound like rather than reading a PriceUpdateV2, we send the whole price object to parsePriceFeedUpdates. The only linked example is EVM.
Do you have SVM example code we can take a look at? Ideally both client side and Solana side?
hello.
these are frontend codes.
I cannot find a place where I can access those in the smart contract.
we want to query price at midnight.
so getting that done on front end is the way you shared with getPriceUpdatesAtTimestamp
and it will create a transaction that create or updates the account for a specific time?
but in the contract, how does that act?
I cannot find that
unless we use same get_price_no_older_than and specify the timestamp expected from the account passed in addPriceConsumerInstructions(address=>{ […] })
is that corect?
edit / add :
also,
I am very confused,
let’s say we have 20 different function that needs to query pricces onchain.
it means i need to maintain 20 separate function that are distinct that have the same start
to have addPriceConsumerInstructions and have the logic of my functions inside this ?
let say, start, renew and deploy have 3 different logics
it means these 3 functions shouldl start with getPriceUpdateDataFromOneDayAgo / newTransactionBuilder / addPostPriceUpdates so that I can unter the PDA account in addPriceConsumerInstructions?
After some tries I figured out.
But I still face the fact I have many functions that use pyth network and I would rather have
getPriceUpdateAccount cannot predict the account that will be used
or the function fails half way if it’s not initialized.
ultimately i would have
function that query the price accounts
function that builds instructions for my contract using addresses that will be updated
function that create and update price accounts, wrap IX from my contract then close the price accounts
maybe getPriceUpdateAccount can be available outside the addPriceConsumerInstructions function
or getPriceFeedAddressesdoesn’t throw an error if the account is not initialized?
Gm @Biboux , the price update account is a PDA derived from the feed ID and shard ID. You can see use this function on-chain to find the PDA from those (or this function off-chain).
ultimately i would have
function that query the price accounts
function that builds instructions for my contract using addresses that will be updated
function that create and update price accounts, wrap IX from my contract then close the price accounts
Can you help me understand why this flow doesn’t work for you using existing SDK methods?
For point 1, you can fetch the on-chain price with pythSolanaReceiver.fetchPriceFeedAccount.
Points 2 & 3 are handled by the SDK (you would provide feed ID and shard ID, not the price update account addresses.)