Skip to content

Commit

Permalink
tighten slippage
Browse files Browse the repository at this point in the history
  • Loading branch information
anthontaylor committed Nov 21, 2024
1 parent 120be25 commit 403d8c2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub const USDC_MINT: &str = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
pub const USDC_DECIMALS: u8 = 6;
pub const STABLEBOND_DECIMALS: u8 = 6;
pub const MIN_USDC_AMOUNT: u64 = 1000000;
pub const MAX_USDC_AMOUNT_PER_TRADE: u64 = 1_000_000_000;
pub const MAX_USDC_AMOUNT_PER_TRADE: f64 = 1000.0;
pub const MAX_STABLEBOND_AMOUNT_PER_TRADE: u64 = 20_000_000_000;

// Strategy constants
Expand Down
2 changes: 1 addition & 1 deletion src/etherfuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,6 @@ impl ExchangeRateResponse {
self.usd_to_usd,
]
.into_iter()
.find(|&rate| rate > 0.0) // Changed from != 0.0 to > 0.0 for safety
.find(|&rate| rate > 0.0)
}
}
4 changes: 2 additions & 2 deletions src/jupiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl JupiterClient {
input_mint: stablebond_mint.clone(),
output_mint: Pubkey::from_str(USDC_MINT).unwrap(),
amount,
slippage_bps: Some(300),
slippage_bps: Some(100),
};
let quote = self.get_jupiter_quote(jupiter_quote_args).await?;
let jup_price_usd_to_token: f64 = quote.in_amount as f64 / quote.out_amount as f64;
Expand All @@ -122,7 +122,7 @@ impl JupiterClient {
input_mint: Pubkey::from_str(USDC_MINT).unwrap(),
output_mint: stablebond_mint.clone(),
amount,
slippage_bps: Some(300),
slippage_bps: Some(100),
};
let quote = self.get_jupiter_quote(jupiter_quote_args).await?;
let jup_price_token_to_usd: f64 = quote.in_amount as f64 / quote.out_amount as f64;
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ async fn main() -> Result<()> {
}
}

println!("Most profitable strategy: {:?}", most_profitable_strategy);
println!(
"==================================Most profitable strategy: {:?} ==================================",
most_profitable_strategy
);
let mut txs = most_profitable_strategy.txs;
if let Some(update_oracle_tx) = market_data.switchboard_update_tx {
txs.insert(0, update_oracle_tx);
Expand Down
18 changes: 11 additions & 7 deletions src/market_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::constants::{MAX_STABLEBOND_AMOUNT_PER_TRADE, MAX_USDC_AMOUNT_PER_TRADE, USDC_MINT};
use crate::constants::{MAX_STABLEBOND_AMOUNT_PER_TRADE, USDC_MINT};
use crate::etherfuse::EtherfuseClient;
use crate::{jito::JitoClient, math, switchboard::SwitchboardClient};
use anyhow::Result;
Expand Down Expand Up @@ -118,10 +118,8 @@ impl MarketDataBuilder {

pub async fn with_usdc_holdings_token_amount(mut self) -> Self {
let usdc_mint = Pubkey::from_str(&USDC_MINT).unwrap();
self.usdc_holdings_token_amount = Some(min(
self.get_spl_token_balance(&usdc_mint).await.unwrap_or(0),
MAX_USDC_AMOUNT_PER_TRADE,
));
self.usdc_holdings_token_amount =
Some(self.get_spl_token_balance(&usdc_mint).await.unwrap_or(0));
self
}

Expand All @@ -131,11 +129,17 @@ impl MarketDataBuilder {
}

pub async fn with_update_switchboard_oracle_tx(mut self, stablebond_mint: &Pubkey) -> Self {
let payment_feed = self
let payment_feed = match self
.etherfuse_client
.fetch_payment_feed(stablebond_mint)
.await
.unwrap();
{
Ok(payment_feed) => payment_feed,
Err(e) => {
println!("Error fetching payment feed: {:?}", e);
return self;
}
};

let switchboard_public_feed = if payment_feed.quote_price_feed == Pubkey::default() {
payment_feed.base_price_feed
Expand Down
47 changes: 21 additions & 26 deletions src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::math;
use crate::math::{TokenAmountExt, UiAmountExt};
use crate::{
constants::{
INITIAL_POINTS, MAX_RETRIES, MAX_TRADE_PERCENT, MIN_TRADE_PERCENT, MIN_USDC_AMOUNT,
RETRY_DELAY_MS, STABLEBOND_DECIMALS, USDC_DECIMALS,
INITIAL_POINTS, MAX_RETRIES, MAX_TRADE_PERCENT, MAX_USDC_AMOUNT_PER_TRADE,
MIN_TRADE_PERCENT, MIN_USDC_AMOUNT, RETRY_DELAY_MS, STABLEBOND_DECIMALS, USDC_DECIMALS,
},
jupiter::JupiterClient,
};
Expand Down Expand Up @@ -95,9 +95,6 @@ impl Strategy for BuyOnJupiterSellOnEtherfuse {
let sell_liquidity_usdc_amount = md
.sell_liquidity_usdc_amount
.ok_or_else(|| anyhow::anyhow!("Missing sell_liquidity_usdc_amount"))?;
let stablebond_holdings_token_amount = md
.stablebond_holdings_token_amount
.ok_or_else(|| anyhow::anyhow!("Missing stablebond_holdings_token_amount"))?;
let usdc_holdings_token_amount = md
.usdc_holdings_token_amount
.ok_or_else(|| anyhow::anyhow!("Missing usdc_holdings_token_amount"))?;
Expand All @@ -115,17 +112,10 @@ impl Strategy for BuyOnJupiterSellOnEtherfuse {
"Sell liquidity in USDC is required for this strategy"
));
}
let stablebond_holdings_in_usdc_ui_amount = math::checked_float_mul(
stablebond_holdings_token_amount.to_ui_amount(STABLEBOND_DECIMALS),
etherfuse_price_per_token,
)?;

let max_usdc_ui_amount_to_redeem = sell_liquidity_usdc_amount
.to_ui_amount(USDC_DECIMALS)
.min(stablebond_holdings_in_usdc_ui_amount);

let max_usdc_token_amount_to_redeem =
math::to_token_amount(max_usdc_ui_amount_to_redeem, USDC_DECIMALS)?;
let max_usdc_token_amount_to_redeem = (sell_liquidity_usdc_amount
.min(usdc_holdings_token_amount))
.min(MAX_USDC_AMOUNT_PER_TRADE.to_token_amount(USDC_DECIMALS));

let mut best_profit: f64 = 0.0;
let mut best_usdc_amount = 0;
Expand Down Expand Up @@ -184,8 +174,8 @@ impl Strategy for BuyOnJupiterSellOnEtherfuse {
(price_when_buying - etherfuse_price_per_token) / etherfuse_price_per_token;

let potential_profit = match math::profit_from_arb(
price_when_buying,
etherfuse_price_per_token,
price_when_buying,
stablebond_amount.to_ui_amount(STABLEBOND_DECIMALS),
) {
Ok(profit) => profit,
Expand All @@ -195,13 +185,14 @@ impl Strategy for BuyOnJupiterSellOnEtherfuse {
}
};

println!("\nTrade Analysis:");
println!("\nTrade Analysis for BuyOnJupiterSellOnEtherfuse:");
println!("Trade Size: {}% of max", trade_percent * 100.0);
println!("USDC Amount: {}", usdc_amount);
println!("USDC Amount: {}", usdc_amount.to_ui_amount(USDC_DECIMALS));
println!("Price Impact: {:.2}%", price_impact * 100.0);
println!("Potential Profit: {}", potential_profit);
println!("Buy Price: {}", price_when_buying);
println!("Base Price: {}", etherfuse_price_per_token);
println!("Buy price on jupiter: {}", price_when_buying);
println!("Sell price on etherfuse: {}", etherfuse_price_per_token);
println!("Stablebond: {:?}", stablebond_mint);

if potential_profit > best_profit {
println!("\n🎯 New best trade found!");
Expand Down Expand Up @@ -280,8 +271,11 @@ impl Strategy for BuyOnEtherfuseSellOnJupiter {
let purchase_liquidity_ui_amount_ =
purchase_liquidity_stablebond_amount.to_ui_amount(STABLEBOND_DECIMALS);
let max_usdc_to_purchase_ui_amount =
math::checked_float_mul(purchase_liquidity_ui_amount_, etherfuse_price_per_token)?
.min(usdc_holdings_token_amount.to_ui_amount(USDC_DECIMALS));
math::checked_float_mul(purchase_liquidity_ui_amount_, etherfuse_price_per_token)?.min(
usdc_holdings_token_amount
.to_ui_amount(USDC_DECIMALS)
.min(MAX_USDC_AMOUNT_PER_TRADE),
);
let max_usdc_to_purchase_token_amount =
max_usdc_to_purchase_ui_amount.to_token_amount(STABLEBOND_DECIMALS);

Expand Down Expand Up @@ -354,13 +348,14 @@ impl Strategy for BuyOnEtherfuseSellOnJupiter {
}
};

println!("\nTrade Analysis:");
println!("\nTrade Analysis for BuyOnEtherfuseSellOnJupiter:");
println!("Trade Size: {}% of max", trade_percent * 100.0);
println!("USDC Amount: {}", usdc_amount);
println!("USDC Amount: {}", usdc_amount.to_ui_amount(USDC_DECIMALS));
println!("Price Impact: {:.2}%", price_impact * 100.0);
println!("Potential Profit: {}", potential_profit);
println!("Sell Price: {}", price_per_token_when_selling);
println!("Base Price: {}", etherfuse_price_per_token);
println!("Buy price on etherfuse: {}", etherfuse_price_per_token);
println!("Sell price on jupiter: {}", price_per_token_when_selling);
println!("Stablebond: {:?}", stablebond_mint);

if potential_profit > best_profit {
println!("\n🎯 New best trade found!");
Expand Down

0 comments on commit 403d8c2

Please sign in to comment.