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 rebalance: limit amount of perp traded #955

Open
wants to merge 3 commits into
base: deploy
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions bin/liquidator/src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ pub struct Cli {
#[clap(long, env, default_value = "30")]
pub(crate) rebalance_refresh_timeout_secs: u64,

#[clap(long, env, default_value = "5")]
pub(crate) rebalance_perp_twap_interval_secs: u64,

#[clap(long, env, default_value = "100_000")]
pub(crate) rebalance_perp_twap_max_quote: f64,

/// if taking tcs orders is enabled
///
/// typically only disabled for tests where swaps are unavailable
Expand Down
3 changes: 3 additions & 0 deletions bin/liquidator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ async fn main() -> anyhow::Result<()> {
.unwrap_or_default(),
use_sanctum: cli.sanctum_enabled == BoolArg::True,
allow_withdraws: true,
perp_twap_interval: Duration::from_secs(cli.rebalance_perp_twap_interval_secs),
perp_twap_max_quote: (cli.rebalance_perp_twap_max_quote * 1_000_000.0).floor() as u64,
};
rebalance_config.validate(&mango_client.context);

Expand All @@ -265,6 +267,7 @@ async fn main() -> anyhow::Result<()> {
mango_account_address: cli.liqor_mango_account,
config: rebalance_config,
sanctum_supported_mints: HashSet::<Pubkey>::new(),
state: Arc::new(RwLock::new(Default::default())),
});

let mut liquidation = Box::new(LiquidationState {
Expand Down
74 changes: 69 additions & 5 deletions bin/liquidator/src/rebalance.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use itertools::Itertools;
use mango_v4::accounts_zerocopy::KeyedAccountSharedData;
use mango_v4::i80f48::ClampToInt;
use mango_v4::state::{
Bank, BookSide, MangoAccountValue, OracleAccountInfos, PerpMarket, PerpPosition,
PlaceOrderType, Side, TokenIndex, QUOTE_TOKEN_INDEX,
Bank, BookSide, MangoAccountValue, OracleAccountInfos, PerpMarket, PerpMarketIndex,
PerpPosition, PlaceOrderType, Side, TokenIndex, QUOTE_TOKEN_INDEX,
};
use mango_v4_client::{
chain_data, perp_pnl, swap, MangoClient, MangoGroupContext, PerpMarketContext, TokenContext,
Expand All @@ -15,8 +16,8 @@ use {fixed::types::I80F48, solana_sdk::pubkey::Pubkey};
use solana_sdk::signature::Signature;
use std::collections::{HashMap, HashSet};
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use tracing::*;

#[derive(Clone)]
Expand All @@ -35,6 +36,8 @@ pub struct Config {
pub alternate_sanctum_route_tokens: Vec<TokenIndex>,
pub allow_withdraws: bool,
pub use_sanctum: bool,
pub perp_twap_interval: Duration,
pub perp_twap_max_quote: u64,
}

impl Config {
Expand All @@ -56,12 +59,47 @@ fn token_bank(
account_fetcher.fetch::<Bank>(&token.first_bank())
}

struct PerpTwapState {
start: Instant,
used_quote: u64,
}

#[derive(Default)]
pub struct RebalancerState {
perp_twap: HashMap<PerpMarketIndex, PerpTwapState>,
}

impl RebalancerState {
fn expire_old_perp_twaps(&mut self, interval: Duration) {
self.perp_twap
.retain(|_, twap| twap.start.elapsed() <= interval);
}

fn used_perp_twap(&self, perp_market_index: PerpMarketIndex) -> u64 {
self.perp_twap
.get(&perp_market_index)
.map(|twap| twap.used_quote)
.unwrap_or(0)
}

fn add_perp_twap(&mut self, perp_market_index: PerpMarketIndex, quote: u64) {
self.perp_twap
.entry(perp_market_index)
.or_insert_with(|| PerpTwapState {
start: Instant::now(),
used_quote: 0,
})
.used_quote += quote;
}
}

pub struct Rebalancer {
pub mango_client: Arc<MangoClient>,
pub account_fetcher: Arc<chain_data::AccountFetcher>,
pub mango_account_address: Pubkey,
pub config: Config,
pub sanctum_supported_mints: HashSet<Pubkey>,
pub state: Arc<RwLock<RebalancerState>>,
}

impl Rebalancer {
Expand Down Expand Up @@ -574,6 +612,14 @@ impl Rebalancer {
.duration_since(std::time::UNIX_EPOCH)?
.as_secs();

let allowed_quote = {
let mut state = self.state.write().unwrap();
state.expire_old_perp_twaps(self.config.perp_twap_interval);
self.config
.perp_twap_max_quote
.saturating_sub(state.used_perp_twap(perp.perp_market_index))
};

let base_lots = perp_position.base_position_lots();
let effective_lots = perp_position.effective_base_position_lots();
let quote_native = perp_position.quote_position_native();
Expand All @@ -599,8 +645,11 @@ impl Rebalancer {
perp_position.bids_base_lots,
)
};
let allowed_base_lots =
(I80F48::from(allowed_quote) / order_price / I80F48::from(perp.base_lot_size))
.clamp_to_i64();
let price_lots = perp_market.native_price_to_lot(order_price);
let max_base_lots = effective_lots.abs() - oo_lots;
let max_base_lots = (effective_lots.abs() - oo_lots).min(allowed_base_lots);
if max_base_lots <= 0 {
warn!(?side, oo_lots, "cannot place reduce-only order",);
return Ok(true);
Expand Down Expand Up @@ -660,6 +709,21 @@ impl Rebalancer {
if !self.refresh_mango_account_after_tx(txsig).await? {
return Ok(false);
}

// Update the twap amount limits
let after_base_lots = self
.mango_account()?
.perp_position(perp.perp_market_index)
.map(|position| position.base_position_lots())
.unwrap_or(0);
let base_reduction_lots = (base_lots.abs() - after_base_lots.abs()).max(0);
let quote_reduction = (I80F48::from(base_reduction_lots * perp.base_lot_size)
* order_price)
.clamp_to_u64();
self.state
.write()
.unwrap()
.add_perp_twap(perp.perp_market_index, quote_reduction);
} else if base_lots == 0 && quote_native != 0 {
// settle pnl
let direction = if quote_native > 0 {
Expand Down
Loading