near: use Seconds alias to document units, fix calc

This commit is contained in:
Reisen 2023-04-03 15:37:22 +02:00 committed by Reisen
parent 5b7ada59bd
commit 177b5af94b
1 changed files with 10 additions and 3 deletions

View File

@ -72,6 +72,9 @@ enum StorageKeys {
Prices, Prices,
} }
/// Alias to document time unit Pyth expects data to be in.
type Seconds = u64;
/// The `State` contains all persisted state for the contract. This includes runtime configuration. /// The `State` contains all persisted state for the contract. This includes runtime configuration.
/// ///
/// There is no valid Default state for this contract, so we derive PanicOnDefault to force /// There is no valid Default state for this contract, so we derive PanicOnDefault to force
@ -461,7 +464,11 @@ impl Pyth {
/// Get the latest available price cached for the given price identifier, if that price is /// Get the latest available price cached for the given price identifier, if that price is
/// no older than the given age. /// no older than the given age.
pub fn get_price_no_older_than(&self, price_id: PriceIdentifier, age: u64) -> Option<Price> { pub fn get_price_no_older_than(
&self,
price_id: PriceIdentifier,
age: Seconds,
) -> Option<Price> {
self.prices.get(&price_id).and_then(|feed| { self.prices.get(&price_id).and_then(|feed| {
let block_timestamp = env::block_timestamp() / 1_000_000_000; let block_timestamp = env::block_timestamp() / 1_000_000_000;
let price_timestamp = feed.price.publish_time; let price_timestamp = feed.price.publish_time;
@ -491,10 +498,10 @@ impl Pyth {
pub fn get_ema_price_no_older_than( pub fn get_ema_price_no_older_than(
&self, &self,
price_id: PriceIdentifier, price_id: PriceIdentifier,
age: u64, age: Seconds,
) -> Option<Price> { ) -> Option<Price> {
self.prices.get(&price_id).and_then(|feed| { self.prices.get(&price_id).and_then(|feed| {
let block_timestamp = env::block_timestamp(); let block_timestamp = env::block_timestamp() / 1_000_000_000;
let price_timestamp = feed.ema_price.publish_time; let price_timestamp = feed.ema_price.publish_time;
// - If Price older than STALENESS_THRESHOLD, set status to Unknown. // - If Price older than STALENESS_THRESHOLD, set status to Unknown.