Skip to content

Commit

Permalink
add get_slot to account_fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Lou-Kamades committed Jan 11, 2024
1 parent a0d8fb2 commit 7756368
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
12 changes: 12 additions & 0 deletions lib/client/src/account_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub trait AccountFetcher: Sync + Send {
&self,
keys: &[Pubkey],
) -> anyhow::Result<Vec<(Pubkey, AccountSharedData)>>;

async fn get_slot(
&self,
) -> anyhow::Result<u64>;
}

// Can't be in the trait, since then it would no longer be object-safe...
Expand Down Expand Up @@ -114,6 +118,10 @@ impl AccountFetcher for RpcAccountFetcher {
) -> anyhow::Result<Vec<(Pubkey, AccountSharedData)>> {
gpa::fetch_multiple_accounts(&self.rpc, keys).await
}

async fn get_slot(&self) -> anyhow::Result<u64> {
Ok(self.rpc.get_slot().await?)
}
}

struct CoalescedAsyncJob<Key, Output> {
Expand Down Expand Up @@ -282,4 +290,8 @@ impl<T: AccountFetcher + 'static> AccountFetcher for CachedAccountFetcher<T> {
) -> anyhow::Result<Vec<(Pubkey, AccountSharedData)>> {
self.fetcher.fetch_multiple_accounts(keys).await
}

async fn get_slot(&self) -> anyhow::Result<u64> {
self.fetcher.get_slot().await
}
}
18 changes: 16 additions & 2 deletions lib/client/src/chain_data_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::{Arc, RwLock};
use std::thread;
use std::time::{Duration, Instant};

use crate::{chain_data::*, gpa};
use crate::chain_data::*;

use anchor_lang::Discriminator;

Expand Down Expand Up @@ -247,6 +247,20 @@ impl crate::AccountFetcher for AccountFetcher {
&self,
keys: &[Pubkey],
) -> anyhow::Result<Vec<(Pubkey, AccountSharedData)>> {
gpa::fetch_multiple_accounts(&self.rpc, keys).await
let chain_data = self.chain_data.read().unwrap();
Ok(chain_data
.iter_accounts()
.filter_map(|(pk, data)| {
if !keys.contains(pk) {
return None;
}
Some((*pk, data.account.clone()))
})
.collect::<Vec<_>>())
}

async fn get_slot(&self) -> anyhow::Result<u64> {
let chain_data = self.chain_data.read().unwrap();
Ok(chain_data.newest_processed_slot())
}
}
13 changes: 7 additions & 6 deletions lib/client/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anchor_client::ClientError;
use anchor_lang::__private::bytemuck;

use mango_v4::{
accounts_zerocopy::KeyedAccountReader,
accounts_zerocopy::{KeyedAccountReader, KeyedAccountSharedData},
state::{
determine_oracle_type, load_whirlpool_state, oracle_state_unchecked, Group,
MangoAccountValue, OracleAccountInfos, OracleConfig, OracleConfigParams, OracleType,
Expand Down Expand Up @@ -284,8 +284,9 @@ impl MangoGroupContext {
token.name = bank.name().into();
token.decimals = bank.mint_decimals;
token.oracle_config = bank.oracle_config;
let (key, acc_info) = fallback_oracle_accounts[index].clone();
token.fallback_context.quote_key =
get_fallback_quote_key(&fallback_oracle_accounts[index]);
get_fallback_quote_key(&KeyedAccountSharedData::new(key, acc_info));
}
assert!(tokens.values().all(|t| t.decimals != u8::MAX));

Expand Down Expand Up @@ -661,13 +662,13 @@ impl MangoGroupContext {
let oracle_accounts = account_fetcher
.fetch_multiple_accounts(&oracle_keys)
.await?;
let now_slot = rpc.get_slot().await?;
let now_slot = account_fetcher.get_slot().await?;

let mut stale_oracles_with_fallbacks = vec![];
for acc in oracle_accounts {
let token = tokens_by_oracle.get(&acc.0).unwrap();
for (key, acc) in oracle_accounts {
let token = tokens_by_oracle.get(&key).unwrap();
let state = oracle_state_unchecked(
&OracleAccountInfos::from_reader(&acc.1),
&OracleAccountInfos::from_reader(&KeyedAccountSharedData::new(key, acc)),
token.decimals,
)?;
let oracle_is_valid = state
Expand Down

0 comments on commit 7756368

Please sign in to comment.