Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) add get_price view functions to aptos contract #1861

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
0xbe1 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading