Skip to content

Commit 6aac766

Browse files
committed
Fetch product accounts in parallel
1 parent 4b1dc4d commit 6aac766

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/agent/solana/oracle.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use {
1010
Context,
1111
Result,
1212
},
13+
futures_util::future::join_all,
1314
pyth_sdk_solana::state::{
1415
load_mapping_account,
1516
load_price_account,
@@ -415,16 +416,30 @@ impl Poller {
415416
where
416417
A: IntoIterator<Item = &'a MappingAccount>,
417418
{
418-
let mut product_accounts = HashMap::new();
419+
let mut pubkeys = vec![];
420+
let mut futures = vec![];
419421

422+
// Fetch all product accounts in parallel
420423
for mapping_account in mapping_accounts {
421-
product_accounts.extend(
422-
self.fetch_product_accounts_from_mapping_account(mapping_account)
423-
.await?,
424-
);
424+
for account_key in mapping_account
425+
.products
426+
.iter()
427+
.filter(|pubkey| **pubkey != Pubkey::default())
428+
{
429+
pubkeys.push(account_key.clone());
430+
futures.push(self.fetch_product_account(account_key));
431+
}
425432
}
426433

427-
Ok(product_accounts)
434+
let product_accounts = join_all(futures)
435+
.await
436+
.into_iter()
437+
.collect::<Result<Vec<_>>>()?;
438+
439+
Ok(pubkeys
440+
.into_iter()
441+
.zip(product_accounts.into_iter())
442+
.collect())
428443
}
429444

430445
async fn fetch_price_accounts<'a, P>(
@@ -446,24 +461,6 @@ impl Poller {
446461
Ok(price_accounts)
447462
}
448463

449-
async fn fetch_product_accounts_from_mapping_account(
450-
&self,
451-
mapping_account: &MappingAccount,
452-
) -> Result<HashMap<Pubkey, ProductAccount>> {
453-
let mut product_accounts = HashMap::new();
454-
for account_key in mapping_account
455-
.products
456-
.iter()
457-
.filter(|pubkey| **pubkey != Pubkey::default())
458-
{
459-
// Update the price accounts
460-
let product_account = self.fetch_product_account(account_key).await?;
461-
product_accounts.insert(*account_key, product_account);
462-
}
463-
464-
Ok(product_accounts)
465-
}
466-
467464
async fn fetch_product_account(&self, product_account_key: &Pubkey) -> Result<ProductAccount> {
468465
// Fetch the product account
469466
let product_account = *load_product_account(

0 commit comments

Comments
 (0)