From 0eb69dc93d9c38ce0eaae5cabc7fe46c4496cacb Mon Sep 17 00:00:00 2001 From: 0xbe1 <0xbetrue@gmail.com> Date: Mon, 2 Sep 2024 20:29:53 +0800 Subject: [PATCH] (feat) add get_price view functions to aptos contract --- .../aptos/contracts/sources/pyth.move | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/target_chains/aptos/contracts/sources/pyth.move b/target_chains/aptos/contracts/sources/pyth.move index a831643061..827f1f7cfd 100644 --- a/target_chains/aptos/contracts/sources/pyth.move +++ b/target_chains/aptos/contracts/sources/pyth.move @@ -424,6 +424,14 @@ module pyth::pyth { get_price_no_older_than(price_identifier, state::get_stale_price_threshold_secs()) } + /// A view function version of get_price(...) that's available in offchain programming environments + /// including aptos fullnode api, aptos cli, and aptos ts sdk. + #[view] + public fun get_price_by_feed_id(feed_id: vector): Price { + let price_identifier = price_identifier::from_byte_vec(feed_id); + get_price(price_identifier) + } + /// Get the latest available price cached for the given price identifier, if that price is /// no older than the given age. public fun get_price_no_older_than(price_identifier: PriceIdentifier, max_age_secs: u64): Price { @@ -433,6 +441,12 @@ module pyth::pyth { price } + #[view] + public fun get_price_no_older_than_by_feed_id(feed_id: vector, max_age_secs: u64): Price { + let price_identifier = price_identifier::from_byte_vec(feed_id); + get_price_no_older_than(price_identifier, max_age_secs) + } + /// Get the latest available price cached for the given price identifier. /// /// WARNING: the returned price can be from arbitrarily far in the past. @@ -446,6 +460,12 @@ module pyth::pyth { price_info::get_price_feed(&state::get_latest_price_info(price_identifier))) } + #[view] + public fun get_price_unsafe_by_feed_id(feed_id: vector): Price { + let price_identifier = price_identifier::from_byte_vec(feed_id); + get_price_unsafe(price_identifier) + } + fun abs_diff(x: u64, y: u64): u64 { if (x > y) { return x - y