Metal Price Feed: handling market closure

Hello,

I am developping a project, on Solana, where I am using the XAU:USD sponsored price feed.
Since gold is not quoted during week-ends, what is the right way to handle that in the Program?
I would like to get the last known price (with price feed id and shard), without checking it’s age (maybe conditionaly, based on the “market hours”) :

Metals From Sunday 6PM ET to Friday 5PM ET Daily maintenance window applies from 5PM ET to 6PM ET, Monday to Thursday. Spot gold and silver trading also follow CME holiday closures

For now, I am asking for a recent price, and if the provided price is not recent I am falling back to the sponsored price feed in shard 0. Is that a good way to proceed?

Here is my snippet:

    let ref_price = ctx.accounts.ref_price_update().get_price_no_older_than(&Clock::get()?, MAX_AGE, &ref_feed_id)
        .or_else(|_| {
            // Check that the price feed account is the one expected from shard 0.
            let expected_feed = Pubkey::from_str("2uPQGpm8X4ZkxMHxrAW1QuhXcse1AHEgPih6Xp9NuEWW")
            .expect("Invalid fallback feed key");
            
            msg!("Price feed is stale; using only fallback price (Shard 0).");
            require!(ctx.accounts.ref_price_update().key() == expected_feed, MyError::InvalidPriceFeed);
            Ok::<Price, anchor_lang::error::Error>(Price {
                price:          ctx.accounts.ref_price_update().price_message.price,
                conf:           ctx.accounts.ref_price_update().price_message.conf,
                exponent:       ctx.accounts.ref_price_update().price_message.exponent,
                publish_time:   ctx.accounts.ref_price_update().price_message.publish_time,
            })
        })?;

Thank you in advance ! :slightly_smiling_face:

Hi Cristophe,

Great question. For a feed like XAU there are market closures. If your program demands a recent price (for example at most 30 seconds all), it will be impossible to transact with it during the closures.

Instead you wrote some logic so that you can either:

  • pass a recent price with thresholds MAX_AGE.
  • pass the price feed account with shard 0 for XAU/USD.

The main danger I see in this approach is the age of the price in the price feed account is not checked. Although this feed is currently sponsored, you code is vulnerable to the price schedulers going down.

Yes, you are right.
Is there anything like schedules that could be fetched online, so we know that we are within market closure?
By the way, I could also add a second check, for example a maximum age of 1 week for this fallback logic, to mitigate the risk related to schedulers going down.

You can see the schedules in this API: https://hermes.pyth.network/v2/price_feeds (look for XAU).

For your second comment, perhaps make it 49h, since that seems to be the longest closure?