mango-v4/lib/client/src/health_cache.rs

69 lines
2.5 KiB
Rust
Raw Normal View History

use crate::{AccountFetcher, MangoGroupContext};
use anyhow::Context;
use futures::{stream, StreamExt, TryStreamExt};
use mango_v4::accounts_zerocopy::KeyedAccountSharedData;
2022-12-08 04:12:43 -08:00
use mango_v4::health::{FixedOrderAccountRetriever, HealthCache};
use mango_v4::state::MangoAccountValue;
pub async fn new(
context: &MangoGroupContext,
account_fetcher: &impl AccountFetcher,
account: &MangoAccountValue,
) -> anyhow::Result<HealthCache> {
let active_token_len = account.active_token_positions().count();
let active_perp_len = account.active_perp_positions().count();
let (metas, _health_cu) =
context.derive_health_check_remaining_account_metas(account, vec![], vec![], vec![])?;
let accounts: anyhow::Result<Vec<KeyedAccountSharedData>> = stream::iter(metas.iter())
.then(|meta| async {
Ok(KeyedAccountSharedData::new(
meta.pubkey,
account_fetcher.fetch_raw_account(&meta.pubkey).await?,
))
})
.try_collect()
.await;
let retriever = FixedOrderAccountRetriever {
ais: accounts?,
n_banks: active_token_len,
n_perps: active_perp_len,
begin_perp: active_token_len * 2,
2022-11-02 10:13:25 -07:00
begin_serum3: active_token_len * 2 + active_perp_len * 2,
staleness_slot: None,
};
2022-12-08 04:12:43 -08:00
mango_v4::health::new_health_cache(&account.borrow(), &retriever).context("make health cache")
}
pub fn new_sync(
context: &MangoGroupContext,
account_fetcher: &crate::chain_data::AccountFetcher,
account: &MangoAccountValue,
) -> anyhow::Result<HealthCache> {
let active_token_len = account.active_token_positions().count();
let active_perp_len = account.active_perp_positions().count();
let (metas, _health_cu) =
context.derive_health_check_remaining_account_metas(account, vec![], vec![], vec![])?;
let accounts = metas
.iter()
.map(|meta| {
Ok(KeyedAccountSharedData::new(
meta.pubkey,
account_fetcher.fetch_raw(&meta.pubkey)?,
))
})
.collect::<anyhow::Result<Vec<_>>>()?;
let retriever = FixedOrderAccountRetriever {
ais: accounts,
n_banks: active_token_len,
n_perps: active_perp_len,
begin_perp: active_token_len * 2,
begin_serum3: active_token_len * 2 + active_perp_len * 2,
staleness_slot: None,
};
mango_v4::health::new_health_cache(&account.borrow(), &retriever).context("make health cache")
}