Using priceInfo in unit tests on aptos move

:pushpin: Before Posting:
– Please select the appropriate product category from the dropdown when creating a topic.
– Use this category only for help topics related Pyth Price Feeds, Hermes, Price Pusher.
– You can remove these instructions when submitting your post.

:books: Check the Pyth Documentation for guides, known issues, and integration examples β€” your answer might already be there.

:hammer_and_wrench: When asking for support, please include:

β€’ :link: Chain: Aptos mainnet

β€’ :stopwatch: Timestamp of the issue (block time or UTC)

β€’ :test_tube: Steps to reproduce the issue

β€’ :receipt: Any relevant code snippet you can share

fun test_get_oracle_price(){

        let price_infos = vector<PriceInfo>[
            price_info::new(
                1663680747,
                1663074349,
                price_feed::new(
                    price_identifier::from_byte_vec(PRICE_ID),
                    price::new(i64::new(1557, false), 7, i64::new(5, true), 1663680740),
                    price::new(i64::new(1500, false), 3, i64::new(5, true), 1663680740),
                ),
            )
        ];

        pyth_test::update_cache_for_test(price_infos);
        let price = pyth::get_price_no_older_than(price_identifier::from_byte_vec(PRICE_ID), 60);
        std::debug::print(&format1(&b"price   =  {}", price));

β€’ Name of the dApp/Project that you are building

Xyralabs

β€’ Contact (Telegram/Email/Discord) @OxAnt0

The more context you provide, the faster we can help.

I’m trying to initialise custom pyth oracle price for unit testing on aptos move. I’m using the above approach to create custom price . However I’m getting this error while reading price

β”Œβ”€β”€ test_get_oracle_price ──────
β”‚ error[E11001]: test failure
β”‚     β”Œβ”€ /Users/anto/.move/https___github_com_0xAnto_pyth-crosschain_git_mainnet/target_chains/aptos/contracts/sources/state.move:126:33
β”‚     β”‚
β”‚ 125 β”‚     public fun price_info_cached(price_identifier: PriceIdentifier): bool acquires LatestPriceInfo {
β”‚     β”‚                ----------------- In this function in 0x7e783b349d3e89cf5931af376ebeadbfab855b3fa239b7ada8f5a92fbea6b387::state
β”‚ 126 β”‚         let latest_price_info = borrow_global<LatestPriceInfo>(@pyth);
β”‚     β”‚                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test was not expected to error, but it gave a MISSING_DATA (code 4008) error with error message: "Failed to borrow global resource from 7e783b349d3e89cf5931af376ebeadbfab855b3fa239b7ada8f5a92fbea6b387". Error originating in the module 7e783b349d3e89cf5931af376ebeadbfab855b3fa239b7ada8f5a92fbea6b387::state rooted here
β”‚ 
β”‚ 
β”‚ stack trace
β”‚       pyth::price_feed_exists(/Users/anto/.move/https___github_com_0xAnto_pyth-crosschain_git_mainnet/target_chains/aptos/contracts/sources/pyth.move:406)
β”‚       pyth::is_fresh_update(/Users/anto/.move/https___github_com_0xAnto_pyth-crosschain_git_mainnet/target_chains/aptos/contracts/sources/pyth.move:390)
β”‚       pyth::update_cache(/Users/anto/.move/https___github_com_0xAnto_pyth-crosschain_git_mainnet/target_chains/aptos/contracts/sources/pyth.move:311)
β”‚       pyth_test::update_cache_for_test(/Users/anto/.move/https___github_com_0xAnto_pyth-crosschain_git_mainnet/target_chains/aptos/contracts/sources/pyth.move:649)
β”‚       oracle::test_get_oracle_price(/Users/anto/Documents/Kana/Code/perpetual-contracts-v2/sources/oracle.move:320)
β”‚ 
└──────────────────

Test result: FAILED. Total tests: 1; passed: 0; failed: 1
{
  "Error": "Move unit tests failed"
}

Hey, Thank you for posting.

Which hermes endpoint are you using to fetch the priceData?

Just want to make sure that you are using hermes stable feeds for Aptos mainnet.

https://docs.pyth.network/price-feeds/price-feeds

Since aptos testnet uses beta feeds, developers have a tendency to forget to migrate from beta to stable while moving from testnet to aptos mainnet.

Thanks for the quick reply. I’m talking about unit testing in Aptos Move contracts. I don’t see how price feed endpoints are related. I want to test how my module works at different oracle prices. For example, how can I set the oracle price to $10 in a Move unit test to check if liquidation works correctly? @Aditya520