Skip to content

Commit b672613

Browse files
MagicGordonReisen
andauthored
feat(target_chains/near): added batch price fetching functions (#1578)
* Added batch price fetching functions * style(target_chains/near): rustfmt --------- Co-authored-by: Reisen <[email protected]>
1 parent 2df53c9 commit b672613

File tree

5 files changed

+223
-20
lines changed

5 files changed

+223
-20
lines changed

target_chains/near/receiver/Cargo.lock

Lines changed: 45 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

target_chains/near/receiver/src/ext.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use {
1414
ext_contract,
1515
json_types::U128,
1616
},
17+
std::collections::HashMap,
1718
};
1819

1920
/// Defines the external contract API we care about for interacting with Wormhole. Note that
@@ -44,4 +45,28 @@ pub trait Pyth {
4445
fn get_ema_price(&self, price_id: PriceIdentifier) -> Option<Price>;
4546
fn get_ema_price_unsafe(&self, price_id: PriceIdentifier) -> Option<Price>;
4647
fn get_ema_price_no_older_than(&self, price_id: PriceIdentifier, age: u64) -> Option<Price>;
48+
fn list_prices(
49+
&self,
50+
price_ids: Vec<PriceIdentifier>,
51+
) -> HashMap<PriceIdentifier, Option<Price>>;
52+
fn list_prices_unsafe(
53+
&self,
54+
price_ids: Vec<PriceIdentifier>,
55+
) -> HashMap<PriceIdentifier, Option<Price>>;
56+
fn list_prices_no_older_than(
57+
&self,
58+
price_ids: Vec<PriceIdentifier>,
59+
) -> HashMap<PriceIdentifier, Option<Price>>;
60+
fn list_ema_prices(
61+
&self,
62+
price_ids: Vec<PriceIdentifier>,
63+
) -> HashMap<PriceIdentifier, Option<Price>>;
64+
fn list_ema_prices_unsafe(
65+
&self,
66+
price_ids: Vec<PriceIdentifier>,
67+
) -> HashMap<PriceIdentifier, Option<Price>>;
68+
fn list_ema_prices_no_older_than(
69+
&self,
70+
price_ids: Vec<PriceIdentifier>,
71+
) -> HashMap<PriceIdentifier, Option<Price>>;
4772
}

target_chains/near/receiver/src/lib.rs

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ use {
5454
Source,
5555
Vaa,
5656
},
57-
std::io::{
58-
Cursor,
59-
Read,
57+
std::{
58+
collections::HashMap,
59+
io::{
60+
Cursor,
61+
Read,
62+
},
6063
},
6164
};
6265

@@ -511,6 +514,50 @@ impl Pyth {
511514
})
512515
}
513516

517+
/// Batch version of `get_price`.
518+
pub fn list_prices(
519+
&self,
520+
price_ids: Vec<PriceIdentifier>,
521+
) -> HashMap<PriceIdentifier, Option<Price>> {
522+
self.list_prices_no_older_than(price_ids, self.stale_threshold)
523+
}
524+
525+
/// Batch version of `get_price_unsafe`.
526+
pub fn list_prices_unsafe(
527+
&self,
528+
price_ids: Vec<PriceIdentifier>,
529+
) -> HashMap<PriceIdentifier, Option<Price>> {
530+
self.list_prices_no_older_than(price_ids, u64::MAX)
531+
}
532+
533+
/// Batch version of `get_price_no_older_than`.
534+
pub fn list_prices_no_older_than(
535+
&self,
536+
price_ids: Vec<PriceIdentifier>,
537+
age: Seconds,
538+
) -> HashMap<PriceIdentifier, Option<Price>> {
539+
price_ids
540+
.into_iter()
541+
.map(|price_id| {
542+
if let Some(feed) = self.prices.get(&price_id) {
543+
let block_timestamp = env::block_timestamp() / 1_000_000_000;
544+
let price_timestamp = feed.price.publish_time;
545+
546+
// - If Price older than STALENESS_THRESHOLD, set status to Unknown.
547+
// - If Price newer than now by more than STALENESS_THRESHOLD, set status to Unknown.
548+
// - Any other price around the current time is considered valid.
549+
if u64::abs_diff(block_timestamp, price_timestamp.try_into().unwrap()) > age {
550+
(price_id, None)
551+
} else {
552+
(price_id, Some(feed.price))
553+
}
554+
} else {
555+
(price_id, None)
556+
}
557+
})
558+
.collect()
559+
}
560+
514561
/// EMA version of `get_price`.
515562
pub fn get_ema_price(&self, price_id: PriceIdentifier) -> Option<Price> {
516563
self.get_ema_price_no_older_than(price_id, self.get_stale_threshold())
@@ -541,6 +588,50 @@ impl Pyth {
541588
Some(feed.ema_price)
542589
})
543590
}
591+
592+
/// EMA version of `list_prices`.
593+
pub fn list_ema_prices(
594+
&self,
595+
price_ids: Vec<PriceIdentifier>,
596+
) -> HashMap<PriceIdentifier, Option<Price>> {
597+
self.list_ema_prices_no_older_than(price_ids, self.get_stale_threshold())
598+
}
599+
600+
/// EMA version of `list_prices_unsafe`.
601+
pub fn list_ema_prices_unsafe(
602+
&self,
603+
price_ids: Vec<PriceIdentifier>,
604+
) -> HashMap<PriceIdentifier, Option<Price>> {
605+
self.list_ema_prices_no_older_than(price_ids, u64::MAX)
606+
}
607+
608+
/// EMA version of `list_prices_no_older_than`.
609+
pub fn list_ema_prices_no_older_than(
610+
&self,
611+
price_ids: Vec<PriceIdentifier>,
612+
age: Seconds,
613+
) -> HashMap<PriceIdentifier, Option<Price>> {
614+
price_ids
615+
.into_iter()
616+
.map(|price_id| {
617+
if let Some(feed) = self.prices.get(&price_id) {
618+
let block_timestamp = env::block_timestamp() / 1_000_000_000;
619+
let price_timestamp = feed.ema_price.publish_time;
620+
621+
// - If Price older than STALENESS_THRESHOLD, set status to Unknown.
622+
// - If Price newer than now by more than STALENESS_THRESHOLD, set status to Unknown.
623+
// - Any other price around the current time is considered valid.
624+
if u64::abs_diff(block_timestamp, price_timestamp.try_into().unwrap()) > age {
625+
(price_id, None)
626+
} else {
627+
(price_id, Some(feed.ema_price))
628+
}
629+
} else {
630+
(price_id, None)
631+
}
632+
})
633+
.collect()
634+
}
544635
}
545636

546637
/// This second `impl Pyth` block contains only private methods that are called internally that

target_chains/near/receiver/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub type WormholeSignature = [u8; 65];
2525
/// Type alias for Wormhole's cross-chain 32-byte address.
2626
pub type WormholeAddress = [u8; 32];
2727

28-
#[derive(BorshDeserialize, BorshSerialize)]
28+
#[derive(BorshDeserialize, BorshSerialize, PartialEq, Eq, Hash)]
2929
#[repr(transparent)]
3030
pub struct PriceIdentifier(pub [u8; 32]);
3131

0 commit comments

Comments
 (0)