2022-09-23 02:59:18 -07:00
|
|
|
use crate::{AccountFetcher, MangoGroupContext};
|
|
|
|
use anyhow::Context;
|
2022-12-16 04:10:46 -08:00
|
|
|
use futures::{stream, StreamExt, TryStreamExt};
|
2022-09-23 02:59:18 -07:00
|
|
|
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;
|
2022-09-23 02:59:18 -07:00
|
|
|
|
2022-12-16 04:10:46 -08:00
|
|
|
pub async fn new(
|
2022-09-23 02:59:18 -07:00
|
|
|
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 = context.derive_health_check_remaining_account_metas(account, vec![], false)?;
|
2022-12-16 04:10:46 -08:00
|
|
|
let accounts: anyhow::Result<Vec<KeyedAccountSharedData>> = stream::iter(metas.iter())
|
|
|
|
.then(|meta| async {
|
2022-09-23 02:59:18 -07:00
|
|
|
Ok(KeyedAccountSharedData::new(
|
|
|
|
meta.pubkey,
|
2022-12-16 04:10:46 -08:00
|
|
|
account_fetcher.fetch_raw_account(&meta.pubkey).await?,
|
2022-09-23 02:59:18 -07:00
|
|
|
))
|
|
|
|
})
|
2022-12-16 04:10:46 -08:00
|
|
|
.try_collect()
|
|
|
|
.await;
|
2022-09-23 02:59:18 -07:00
|
|
|
|
|
|
|
let retriever = FixedOrderAccountRetriever {
|
2022-12-16 04:10:46 -08:00
|
|
|
ais: accounts?,
|
2022-09-23 02:59:18 -07:00
|
|
|
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,
|
2022-11-10 06:47:11 -08:00
|
|
|
staleness_slot: None,
|
2022-09-23 02:59:18 -07:00
|
|
|
};
|
2022-12-08 04:12:43 -08:00
|
|
|
mango_v4::health::new_health_cache(&account.borrow(), &retriever).context("make health cache")
|
2022-09-23 02:59:18 -07:00
|
|
|
}
|