Skip to content

Commit

Permalink
Serge/deploy v24+ (#945)
Browse files Browse the repository at this point in the history
* liquidator: randomly select token/perps for rebalancing to avoid failing at every try if one token is having an issue (#921)

(cherry picked from commit e3a7ed9)

* liquidator: do not panic if token or perp rebalancing fails (#927)

(cherry picked from commit 0b7e62e)

* Liquidator: add Sanctum swap (#919)

liquidator: add sanctum swap
(cherry picked from commit 01d5237)

* liquidator: add more LST for sanctum swap (#944)

(cherry picked from commit c0b61b3)
  • Loading branch information
farnyser authored Apr 22, 2024
1 parent 4250e06 commit bf0d748
Show file tree
Hide file tree
Showing 16 changed files with 1,195 additions and 214 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions bin/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,31 @@ struct JupiterSwap {
rpc: Rpc,
}

#[derive(Args, Debug, Clone)]
struct SanctumSwap {
#[clap(long)]
account: String,

/// also pays for everything
#[clap(short, long)]
owner: String,

#[clap(long)]
input_mint: String,

#[clap(long)]
output_mint: String,

#[clap(short, long)]
amount: u64,

#[clap(short, long, default_value = "50")]
max_slippage_bps: u64,

#[clap(flatten)]
rpc: Rpc,
}

#[derive(ArgEnum, Clone, Debug)]
#[repr(u8)]
pub enum CliSide {
Expand Down Expand Up @@ -189,6 +214,7 @@ enum Command {
CreateAccount(CreateAccount),
Deposit(Deposit),
JupiterSwap(JupiterSwap),
SanctumSwap(SanctumSwap),
GroupAddress {
#[clap(short, long)]
creator: String,
Expand Down Expand Up @@ -312,6 +338,19 @@ async fn main() -> Result<(), anyhow::Error> {
.await?;
println!("{}", txsig);
}
Command::SanctumSwap(cmd) => {
let client = cmd.rpc.client(Some(&cmd.owner))?;
let account = pubkey_from_cli(&cmd.account);
let owner = Arc::new(keypair_from_cli(&cmd.owner));
let input_mint = pubkey_from_cli(&cmd.input_mint);
let output_mint = pubkey_from_cli(&cmd.output_mint);
let client = MangoClient::new_for_existing_account(client, account, owner).await?;
let txsig = client
.sanctum()
.swap(input_mint, output_mint, cmd.max_slippage_bps, cmd.amount)
.await?;
println!("{}", txsig);
}
Command::GroupAddress { creator, num } => {
let creator = pubkey_from_cli(&creator);
println!("{}", MangoClient::group_for_admin(creator, num));
Expand Down
5 changes: 4 additions & 1 deletion bin/liquidator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ shellexpand = "2.1.0"
solana-account-decoder = { workspace = true }
solana-client = { workspace = true }
solana-logger = { workspace = true }
solana-address-lookup-table-program = "~1.16.7"
solana-rpc = { workspace = true }
solana-sdk = { workspace = true }
tokio = { version = "1", features = ["full"] }
tokio-stream = { version = "0.1.9"}
tokio-tungstenite = "0.16.1"
tracing = "0.1"
regex = "1.9.5"
hdrhistogram = "7.5.4"
hdrhistogram = "7.5.4"
indexmap = "2.0.0"
borsh = { version = "0.10.3", features = ["const-generics"] }
42 changes: 38 additions & 4 deletions bin/liquidator/src/cli_args.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::trigger_tcs;
use anchor_lang::prelude::Pubkey;
use clap::Parser;
use mango_v4_client::{jupiter, priority_fees_cli};
use mango_v4_client::{priority_fees_cli, swap};
use std::collections::HashSet;

#[derive(Parser, Debug)]
Expand All @@ -28,11 +28,11 @@ pub(crate) enum JupiterVersionArg {
V6,
}

impl From<JupiterVersionArg> for jupiter::Version {
impl From<JupiterVersionArg> for swap::Version {
fn from(a: JupiterVersionArg) -> Self {
match a {
JupiterVersionArg::Mock => jupiter::Version::Mock,
JupiterVersionArg::V6 => jupiter::Version::V6,
JupiterVersionArg::Mock => swap::Version::Mock,
JupiterVersionArg::V6 => swap::Version::V6,
}
}
}
Expand Down Expand Up @@ -121,6 +121,12 @@ pub struct Cli {
#[clap(long, env, value_parser, value_delimiter = ',')]
pub(crate) rebalance_alternate_jupiter_route_tokens: Option<Vec<u16>>,

/// query sanctum for routes to and from these tokens
///
/// These routes will only be used when trying to rebalance a LST token
#[clap(long, env, value_parser, value_delimiter = ',')]
pub(crate) rebalance_alternate_sanctum_route_tokens: Option<Vec<u16>>,

/// When closing borrows, the rebalancer can't close token positions exactly.
/// Instead it purchases too much and then gets rid of the excess in a second step.
/// If this is 0.05, then it'll swap borrow_value * (1 + 0.05) quote token into borrow token.
Expand All @@ -136,6 +142,12 @@ pub struct Cli {
#[clap(long, env, value_enum, default_value = "true")]
pub(crate) take_tcs: BoolArg,

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

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

/// profit margin at which to take tcs orders
#[clap(long, env, default_value = "0.0005")]
pub(crate) tcs_profit_fraction: f64,
Expand Down Expand Up @@ -178,6 +190,10 @@ pub struct Cli {
#[clap(long, env, default_value = "https://quote-api.jup.ag/v6")]
pub(crate) jupiter_v6_url: String,

/// override the jupiter http request timeout
#[clap(long, env, default_value = "30")]
pub(crate) jupiter_timeout_secs: u64,

/// provide a jupiter token, currently only for jup v6
#[clap(long, env, default_value = "")]
pub(crate) jupiter_token: String,
Expand All @@ -191,6 +207,12 @@ pub struct Cli {
#[clap(long, env, value_enum, default_value = "true")]
pub(crate) telemetry: BoolArg,

/// if liquidation is enabled
///
/// might be used to run an instance of liquidator dedicated to TCS and another one for liquidation
#[clap(long, env, value_enum, default_value = "true")]
pub(crate) liquidation_enabled: BoolArg,

/// liquidation refresh timeout in secs
#[clap(long, env, default_value = "30")]
pub(crate) liquidation_refresh_timeout_secs: u8,
Expand All @@ -216,4 +238,16 @@ pub struct Cli {
/// how long should it wait before logging an oracle error again (for the same token)
#[clap(long, env, default_value = "30")]
pub(crate) skip_oracle_error_in_logs_duration_secs: u64,

/// Also use sanctum for rebalancing
#[clap(long, env, value_enum, default_value = "false")]
pub(crate) sanctum_enabled: BoolArg,

/// override the url to sanctum
#[clap(long, env, default_value = "https://api.sanctum.so/v1")]
pub(crate) sanctum_url: String,

/// override the sanctum http request timeout
#[clap(long, env, default_value = "30")]
pub(crate) sanctum_timeout_secs: u64,
}
13 changes: 11 additions & 2 deletions bin/liquidator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ async fn main() -> anyhow::Result<()> {
.commitment(commitment)
.fee_payer(Some(liqor_owner.clone()))
.timeout(rpc_timeout)
.jupiter_v6_url(cli.jupiter_v6_url)
.jupiter_token(cli.jupiter_token)
.jupiter_timeout(Duration::from_secs(cli.jupiter_timeout_secs))
.jupiter_v6_url(cli.jupiter_v6_url.clone())
.jupiter_token(cli.jupiter_token.clone())
.sanctum_url(cli.sanctum_url.clone())
.sanctum_timeout(Duration::from_secs(cli.sanctum_timeout_secs))
.transaction_builder_config(
TransactionBuilderConfig::builder()
.priority_fee_provider(prio_provider)
Expand Down Expand Up @@ -247,6 +250,11 @@ async fn main() -> anyhow::Result<()> {
alternate_jupiter_route_tokens: cli
.rebalance_alternate_jupiter_route_tokens
.unwrap_or_default(),
alternate_sanctum_route_tokens: cli
.rebalance_alternate_sanctum_route_tokens
.clone()
.unwrap_or_default(),
use_sanctum: cli.sanctum_enabled == BoolArg::True,
allow_withdraws: true,
};
rebalance_config.validate(&mango_client.context);
Expand All @@ -256,6 +264,7 @@ async fn main() -> anyhow::Result<()> {
account_fetcher: account_fetcher.clone(),
mango_account_address: cli.liqor_mango_account,
config: rebalance_config,
sanctum_supported_mints: HashSet::<Pubkey>::new(),
});

let mut liquidation = Box::new(LiquidationState {
Expand Down
Loading

0 comments on commit bf0d748

Please sign in to comment.