Liq: format, error handling, logging

This commit is contained in:
Christian Kamm 2022-07-13 13:02:25 +02:00
parent 0158239a56
commit 74873ad46b
1 changed files with 91 additions and 89 deletions

View File

@ -10,7 +10,7 @@ use mango_v4::state::{
};
use {
crate::chain_data::ChainData, anyhow::Context, fixed::types::I80F48, log::*,
crate::chain_data::ChainData, anyhow::Context, fixed::types::I80F48,
solana_sdk::account::AccountSharedData, solana_sdk::pubkey::Pubkey,
};
@ -112,25 +112,17 @@ pub fn new_health_cache_(
}
#[allow(clippy::too_many_arguments)]
pub fn process_accounts<'a>(
pub fn process_account(
mango_client: &MangoClient,
chain_data: &ChainData,
accounts: impl Iterator<Item = &'a Pubkey>,
mint_infos: &HashMap<TokenIndex, Pubkey>,
perp_markets: &HashMap<PerpMarketIndex, Pubkey>,
pubkey: &Pubkey,
) -> anyhow::Result<()> {
// TODO: configurable
let min_health_ratio = I80F48::from_num(50.0f64);
for pubkey in accounts {
let account_result = load_mango_account_from_chain::<MangoAccount>(chain_data, pubkey);
let account = match account_result {
Ok(account) => account,
Err(err) => {
warn!("could not load account {}: {:?}", pubkey, err);
continue;
}
};
let account = load_mango_account_from_chain::<MangoAccount>(chain_data, pubkey)?;
// compute maint health for account
let maint_health = new_health_cache_(chain_data, mint_infos, perp_markets, account)
@ -160,10 +152,7 @@ pub fn process_accounts<'a>(
.collect::<anyhow::Result<Vec<(TokenIndex, &Bank, I80F48)>>>()?;
tokens.sort_by(|a, b| a.2.cmp(&b.2));
if tokens.len() < 2 {
anyhow::bail!(format!(
"mango account {}, has less than 2 active tokens",
pubkey
));
anyhow::bail!("mango account {}, has less than 2 active tokens", pubkey);
}
let (asset_token_index, _asset_bank, _asset_price) = tokens.last().unwrap();
let (liab_token_index, _liab_bank, _liab_price) = tokens.first().unwrap();
@ -174,8 +163,8 @@ pub fn process_accounts<'a>(
&mango_client.mango_account_cache.0,
)?;
let health_cache = new_health_cache_(chain_data, mint_infos, perp_markets, liqor)
.expect("always ok");
let health_cache =
new_health_cache_(chain_data, mint_infos, perp_markets, liqor).expect("always ok");
health_cache.max_swap_source_for_health_ratio(
*liab_token_index,
@ -191,24 +180,37 @@ pub fn process_accounts<'a>(
// TODO: hook ChainData into MangoClient
// TODO: liq_token_with_token() re-gets the liqor account via rpc unnecessarily
//
let sig = mango_client.liq_token_with_token(
let sig = mango_client
.liq_token_with_token(
(pubkey, account),
*asset_token_index,
*liab_token_index,
max_liab_transfer,
);
match sig {
Ok(sig) => log::info!(
)
.context("sending liq_token_with_token")?;
log::info!(
"Liquidated {}..., maint_health was {}, tx sig {:?}",
&pubkey.to_string()[..3],
maint_health,
sig
),
Err(err) => {
log::error!("{:?}", err)
}
}
);
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub fn process_accounts<'a>(
mango_client: &MangoClient,
chain_data: &ChainData,
accounts: impl Iterator<Item = &'a Pubkey>,
mint_infos: &HashMap<TokenIndex, Pubkey>,
perp_markets: &HashMap<PerpMarketIndex, Pubkey>,
) -> anyhow::Result<()> {
for pubkey in accounts {
match process_account(mango_client, chain_data, mint_infos, perp_markets, pubkey) {
Err(err) => log::error!("error liquidating account {}: {:?}", pubkey, err),
_ => {}
};
}
Ok(())