Skip to content

Commit

Permalink
More auto slippage refinements (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xh3rman authored Jan 23, 2025
1 parent f58a9f4 commit cb94fa2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
24 changes: 22 additions & 2 deletions gemstone/src/config/swap_config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::swapper::{GemSlippage, SlippageMode};
use primitives::Chain;

pub static DEFAULT_SLIPPAGE_BPS: u32 = 100;

#[derive(uniffi::Record, Debug, Clone, PartialEq)]
pub struct SwapConfig {
pub default_slippage_bps: u32,
pub default_slippage: GemSlippage,
pub referral_fee: SwapReferralFees,
}

Expand Down Expand Up @@ -35,7 +38,10 @@ impl SwapReferralFees {

pub fn get_swap_config() -> SwapConfig {
SwapConfig {
default_slippage_bps: DEFAULT_SLIPPAGE_BPS,
default_slippage: GemSlippage {
bps: DEFAULT_SLIPPAGE_BPS,
mode: SlippageMode::Exact,
},
referral_fee: SwapReferralFees {
evm: SwapReferralFee {
address: "0x0D9DAB1A248f63B0a48965bA8435e4de7497a3dC".into(),
Expand All @@ -57,3 +63,17 @@ pub fn get_swap_config() -> SwapConfig {
},
}
}

#[uniffi::export]
pub fn get_default_slippage(chain: &Chain) -> GemSlippage {
match chain {
Chain::Solana => GemSlippage {
bps: DEFAULT_SLIPPAGE_BPS * 3,
mode: SlippageMode::Auto,
},
_ => GemSlippage {
bps: DEFAULT_SLIPPAGE_BPS,
mode: SlippageMode::Exact,
},
}
}
9 changes: 9 additions & 0 deletions gemstone/src/swapper/jupiter/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct QuoteResponse {
pub other_amount_threshold: String,
pub swap_mode: String,
pub slippage_bps: u32,
pub computed_auto_slippage: Option<u32>,
pub platform_fee: PlatformFee,
pub price_impact_pct: String,
pub route_plan: Vec<Route>,
Expand Down Expand Up @@ -61,6 +62,12 @@ pub struct QuoteDataResponse {
pub swap_transaction: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DynamicSlippage {
pub max_bps: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QuoteDataRequest {
Expand All @@ -69,4 +76,6 @@ pub struct QuoteDataRequest {
pub fee_account: String,
pub quote_response: QuoteResponse,
pub prioritization_fee_lamports: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub dynamic_slippage: Option<DynamicSlippage>,
}
19 changes: 14 additions & 5 deletions gemstone/src/swapper/jupiter/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ impl GemSwapProvider for Jupiter {
let slippage_bps = swap_options.slippage.bps;
let platform_fee_bps = swap_options.fee.unwrap_or_default().solana_jupiter.bps;

let (auto_slippage, max_auto_slippage_bps) = match swap_options.slippage.mode {
SlippageMode::Auto => (true, slippage_bps * 3),
SlippageMode::Exact => (false, slippage_bps),
let auto_slippage = match swap_options.slippage.mode {
SlippageMode::Auto => true,
SlippageMode::Exact => false,
};

let quote_request = QuoteRequest {
Expand All @@ -125,10 +125,11 @@ impl GemSwapProvider for Jupiter {
platform_fee_bps,
slippage_bps,
auto_slippage,
max_auto_slippage_bps,
max_auto_slippage_bps: slippage_bps,
};
let client = JupiterClient::new(self.get_endpoint(), provider.clone());
let swap_quote = client.get_swap_quote(quote_request).await?;
let computed_auto_slippage = swap_quote.computed_auto_slippage.unwrap_or(swap_quote.slippage_bps);

let quote = SwapQuote {
from_value: request.value.clone(),
Expand All @@ -141,7 +142,7 @@ impl GemSwapProvider for Jupiter {
route_data: serde_json::to_string(&swap_quote).unwrap_or_default(),
gas_estimate: None,
}],
slippage_bps: swap_quote.slippage_bps,
slippage_bps: computed_auto_slippage,
},
approval: ApprovalType::None,
request: request.clone(),
Expand All @@ -162,11 +163,19 @@ impl GemSwapProvider for Jupiter {
.fetch_fee_account(&quote.request.mode, &quote.request.options, &input_mint, &output_mint, provider.clone())
.await?;

let dynamic_slippage = match quote.request.options.slippage.mode {
SlippageMode::Auto => Some(DynamicSlippage {
max_bps: quote.request.options.slippage.bps,
}),
SlippageMode::Exact => None,
};

let request = QuoteDataRequest {
user_public_key: quote.request.wallet_address.clone(),
fee_account,
quote_response,
prioritization_fee_lamports: 500_000,
dynamic_slippage,
};

let client = JupiterClient::new(self.get_endpoint(), provider);
Expand Down
4 changes: 2 additions & 2 deletions gemstone/src/swapper/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ pub struct SwapQuoteRequest {
pub options: GemSwapOptions,
}

#[derive(Debug, Clone, uniffi::Record)]
#[derive(Debug, Clone, PartialEq, uniffi::Record)]
pub struct GemSlippage {
pub bps: u32,
pub mode: SlippageMode,
}

#[derive(Debug, Clone, uniffi::Enum)]
#[derive(Debug, Clone, PartialEq, uniffi::Enum)]
pub enum SlippageMode {
Auto,
Exact,
Expand Down
5 changes: 1 addition & 4 deletions gemstone/tests/ios/GemTest/GemTest/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import Gemstone

let SWAP_OPTIONS = GemSwapOptions(
slippage: GemSlippage(
bps: 100,
mode: .exact
),
slippage: getDefaultSlippage(chain: "solana"),
fee: SwapReferralFees(
evm: .init(address: "0x514BCb1F9AAbb904e6106Bd1052B66d2706dBbb7", bps: 50),
evmBridge: .init(address: "0x514BCb1F9AAbb904e6106Bd1052B66d2706dBbb7", bps: 25),
Expand Down

0 comments on commit cb94fa2

Please sign in to comment.