Skip to content

Commit 57c83f4

Browse files
committed
Only fetch price accounts once
1 parent 6aac766 commit 57c83f4

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

src/agent/solana/oracle.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use {
3535
HashMap,
3636
HashSet,
3737
},
38+
iter::zip,
3839
time::Duration,
3940
},
4041
tokio::{
@@ -374,10 +375,9 @@ impl Poller {
374375
let mapping_accounts = self
375376
.fetch_mapping_accounts(self.mapping_account_key)
376377
.await?;
377-
let product_accounts = self
378-
.fetch_product_accounts(mapping_accounts.values())
378+
let (product_accounts, price_accounts) = self
379+
.fetch_product_and_price_accounts(mapping_accounts.values())
379380
.await?;
380-
let price_accounts = self.fetch_price_accounts(product_accounts.values()).await?;
381381

382382
Ok(Data::new(
383383
mapping_accounts,
@@ -409,10 +409,13 @@ impl Poller {
409409
Ok(accounts)
410410
}
411411

412-
async fn fetch_product_accounts<'a, A>(
412+
async fn fetch_product_and_price_accounts<'a, A>(
413413
&self,
414414
mapping_accounts: A,
415-
) -> Result<HashMap<Pubkey, ProductAccount>>
415+
) -> Result<(
416+
HashMap<Pubkey, ProductAccount>,
417+
HashMap<Pubkey, PriceAccount>,
418+
)>
416419
where
417420
A: IntoIterator<Item = &'a MappingAccount>,
418421
{
@@ -431,37 +434,32 @@ impl Poller {
431434
}
432435
}
433436

434-
let product_accounts = join_all(futures)
437+
let future_results = join_all(futures)
435438
.await
436439
.into_iter()
437440
.collect::<Result<Vec<_>>>()?;
438441

439-
Ok(pubkeys
440-
.into_iter()
441-
.zip(product_accounts.into_iter())
442-
.collect())
443-
}
444-
445-
async fn fetch_price_accounts<'a, P>(
446-
&self,
447-
product_accounts: P,
448-
) -> Result<HashMap<Pubkey, PriceAccount>>
449-
where
450-
P: IntoIterator<Item = &'a ProductAccount>,
451-
{
452-
let mut price_accounts = HashMap::new();
442+
let product_accounts = zip(
443+
pubkeys.into_iter(),
444+
future_results
445+
.clone()
446+
.into_iter()
447+
.map(|(product_account, _)| product_account),
448+
)
449+
.collect();
453450

454-
for product_account in product_accounts {
455-
for price_account_key in &product_account.price_accounts {
456-
let price_account = self.fetch_price_account(price_account_key).await?;
457-
price_accounts.insert(*price_account_key, price_account);
458-
}
459-
}
451+
let price_accounts = future_results
452+
.into_iter()
453+
.flat_map(|(_, price_accounts)| price_accounts.into_iter())
454+
.collect();
460455

461-
Ok(price_accounts)
456+
Ok((product_accounts, price_accounts))
462457
}
463458

464-
async fn fetch_product_account(&self, product_account_key: &Pubkey) -> Result<ProductAccount> {
459+
async fn fetch_product_account(
460+
&self,
461+
product_account_key: &Pubkey,
462+
) -> Result<(ProductAccount, HashMap<Pubkey, PriceAccount>)> {
465463
// Fetch the product account
466464
let product_account = *load_product_account(
467465
&self
@@ -487,7 +485,7 @@ impl Poller {
487485
price_accounts: price_accounts.keys().cloned().collect(),
488486
};
489487

490-
Ok(product_account)
488+
Ok((product_account, price_accounts))
491489
}
492490

493491
async fn fetch_price_account(&self, price_account_key: &Pubkey) -> Result<PriceAccount> {

0 commit comments

Comments
 (0)