From 403d8c21e1680943db7cbffa19096c5694ed9786 Mon Sep 17 00:00:00 2001 From: AJ Taylor Date: Thu, 21 Nov 2024 08:53:34 -0700 Subject: [PATCH] tighten slippage --- src/constants.rs | 2 +- src/etherfuse.rs | 2 +- src/jupiter.rs | 4 ++-- src/main.rs | 5 ++++- src/market_data.rs | 18 +++++++++++------- src/strategy.rs | 47 +++++++++++++++++++++------------------------- 6 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 1cc5bf2..fb8ec94 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -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 diff --git a/src/etherfuse.rs b/src/etherfuse.rs index 18dce7e..bced377 100644 --- a/src/etherfuse.rs +++ b/src/etherfuse.rs @@ -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) } } diff --git a/src/jupiter.rs b/src/jupiter.rs index 641b071..c39cfd7 100644 --- a/src/jupiter.rs +++ b/src/jupiter.rs @@ -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; @@ -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; diff --git a/src/main.rs b/src/main.rs index 089493c..1d2e3b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); diff --git a/src/market_data.rs b/src/market_data.rs index 9d1f1fe..457864c 100644 --- a/src/market_data.rs +++ b/src/market_data.rs @@ -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; @@ -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 } @@ -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 diff --git a/src/strategy.rs b/src/strategy.rs index 51f752c..f13e4cc 100644 --- a/src/strategy.rs +++ b/src/strategy.rs @@ -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, }; @@ -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"))?; @@ -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; @@ -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, @@ -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!"); @@ -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); @@ -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!");