example.move 1.1 KB

123456789101112131415161718192021222324252627
  1. module example::example {
  2. use pyth::pyth;
  3. use pyth::price::Price;
  4. use pyth::price_identifier;
  5. use aptos_framework::coin;
  6. /// Updates the Pyth price feeds using the given pyth_update_data, and then returns
  7. /// the BTC/USD price.
  8. ///
  9. /// https://github.com/pyth-network/pyth-js/tree/main/pyth-aptos-js should be used to
  10. /// fetch the pyth_update_data off-chain and pass it in. More information about how this
  11. /// works can be found at https://docs.pyth.network/consume-data
  12. public fun get_btc_usd_price(user: &signer, pyth_update_data: vector<vector<u8>>): Price {
  13. // First update the Pyth price feeds
  14. let coins = coin::withdraw(user, pyth::get_update_fee(&pyth_update_data));
  15. pyth::update_price_feeds(pyth_update_data, coins);
  16. // Price Feed Identifier of BTC/USD in Testnet
  17. let btc_price_identifier = x"f9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b";
  18. // Now we can use the prices which we have just updated
  19. let btc_usd_price_id = price_identifier::from_byte_vec(btc_price_identifier);
  20. pyth::get_price(btc_usd_price_id)
  21. }
  22. }