Skip to content

Commit

Permalink
(feat) add get_price view functions to aptos contract
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbe1 committed Sep 2, 2024
1 parent f621690 commit bf851fd
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions target_chains/aptos/contracts/sources/pyth.move
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,14 @@ module pyth::pyth {
get_price_no_older_than(price_identifier, state::get_stale_price_threshold_secs())
}

#[view]
/// A view function version of get_price(...) that's available in offchain programming environments
/// including aptos fullnode api, aptos cli, and aptos ts sdk.
public fun get_price_by_feed_id(feed_id: vector<u8>): 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 {
Expand All @@ -433,6 +441,12 @@ module pyth::pyth {
price
}

#[view]
public fun get_price_no_older_than_by_feed_id(feed_id: vector<u8>, 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.
Expand All @@ -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<u8>): 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
Expand Down

0 comments on commit bf851fd

Please sign in to comment.