pyth-crosschain/aptos/examples/fetch_btc_price/sources/example.move

28 lines
1.1 KiB
Plaintext

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