Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

liquidator: Allow excluding tokens from rebalance #774

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions bin/liquidator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ struct Cli {
#[clap(long, env, default_value = "100")]
rebalance_slippage_bps: u64,

/// tokens to not rebalance (in addition to USDC); use a comma separated list of names
#[clap(long, env, default_value = "")]
rebalance_skip_tokens: String,

/// if taking tcs orders is enabled
///
/// typically only disabled for tests where swaps are unavailable
Expand Down Expand Up @@ -312,6 +316,12 @@ async fn main() -> anyhow::Result<()> {
borrow_settle_excess: 1.05,
refresh_timeout: Duration::from_secs(30),
jupiter_version: cli.jupiter_version.into(),
skip_tokens: cli
.rebalance_skip_tokens
.split(',')
.filter(|v| !v.is_empty())
.map(|name| mango_client.context.token_by_name(name).token_index)
.collect(),
};

let rebalancer = Arc::new(rebalance::Rebalancer {
Expand Down
8 changes: 6 additions & 2 deletions bin/liquidator/src/rebalance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use itertools::Itertools;
use mango_v4::accounts_zerocopy::KeyedAccountSharedData;
use mango_v4::state::{
Bank, BookSide, MangoAccountValue, PerpPosition, PlaceOrderType, Side, QUOTE_TOKEN_INDEX,
Bank, BookSide, MangoAccountValue, PerpPosition, PlaceOrderType, Side, TokenIndex,
QUOTE_TOKEN_INDEX,
};
use mango_v4_client::{
chain_data, jupiter, perp_pnl, MangoClient, PerpMarketContext, TokenContext,
Expand All @@ -26,6 +27,7 @@ pub struct Config {
pub borrow_settle_excess: f64,
pub refresh_timeout: Duration,
pub jupiter_version: jupiter::Version,
pub skip_tokens: Vec<TokenIndex>,
}

fn token_bank(
Expand Down Expand Up @@ -298,7 +300,9 @@ impl Rebalancer {
for token_position in account.active_token_positions() {
let token_index = token_position.token_index;
let token = self.mango_client.context.token(token_index);
if token_index == quote_token.token_index {
if token_index == quote_token.token_index
|| self.config.skip_tokens.contains(&token_index)
{
continue;
}
let token_mint = token.mint_info.mint;
Expand Down
16 changes: 14 additions & 2 deletions lib/client/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,23 @@ impl MangoGroupContext {

pub fn token_by_mint(&self, mint: &Pubkey) -> anyhow::Result<&TokenContext> {
self.tokens
.iter()
.find_map(|(_, tc)| (tc.mint_info.mint == *mint).then(|| tc))
.values()
.find(|tc| tc.mint_info.mint == *mint)
.ok_or_else(|| anyhow::anyhow!("no token for mint {}", mint))
}

pub fn token_by_name(&self, name: &str) -> &TokenContext {
let mut tc_iter = self.tokens.values().filter(|tc| tc.name == name);
let tc = tc_iter.next();
assert!(
tc.is_some(),
"token {name} not found; names {:?}",
self.tokens.values().map(|tc| tc.name.clone()).collect_vec()
);
assert!(tc_iter.next().is_none(), "multiple token {name} found");
tc.unwrap()
}

pub async fn new_from_rpc(rpc: &RpcClientAsync, group: Pubkey) -> anyhow::Result<Self> {
let program = mango_v4::ID;

Expand Down
Loading