Skip to content
Merged
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
4 changes: 2 additions & 2 deletions crates/gem_evm/src/rpc/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ mod tests {
assert_eq!(tx.transaction_type, TransactionType::EarnDeposit);
assert_eq!(tx.asset_id, ETHEREUM_USDT_ASSET_ID.clone());
assert_eq!(tx.from, "0x8d7460E51bCf4eD26877cb77E56f3ce7E9f5EB8F");
assert_eq!(tx.to, YO_PROTOCOL_CONTRACT);
assert_eq!(tx.to, ETHEREUM_YO_PROTOCOL_CONTRACT);
assert_eq!(tx.value, "1466009");
}

Expand All @@ -580,7 +580,7 @@ mod tests {
assert_eq!(tx.transaction_type, TransactionType::EarnWithdraw);
assert_eq!(tx.asset_id, ETHEREUM_USDT_ASSET_ID.clone());
assert_eq!(tx.from, "0x8d7460E51bCf4eD26877cb77E56f3ce7E9f5EB8F");
assert_eq!(tx.to, YO_PROTOCOL_CONTRACT);
assert_eq!(tx.to, ETHEREUM_YO_PROTOCOL_CONTRACT);
assert_eq!(tx.value, "1466126");
}
}
13 changes: 5 additions & 8 deletions crates/swapper/src/across/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::{
approval::check_approval_erc20,
chainlink::ChainlinkPriceFeed,
client_factory::create_eth_client,
config::ReferralFee,
cross_chain::VaultAddresses,
eth_address,
fees::ReferralFee,
models::*,
};
use alloy_primitives::{
Expand Down Expand Up @@ -442,7 +442,7 @@ impl Swapper for Across {
let relayer_fee_percent = relayer_calc.capital_fee_percent(&BigInt::from_str(&request.value).unwrap(), cost_config);
let relayer_fee = fees::multiply(from_amount, relayer_fee_percent, cost_config.decimals);

let referral_config = request.options.fee.clone().unwrap_or_default().evm_bridge;
let referral_config = request.options.fee.clone().unwrap_or_default().evm;

// Calculate gas limit / price for relayer
let remain_amount = from_amount - lpfee - relayer_fee;
Expand Down Expand Up @@ -800,6 +800,7 @@ mod tests {
use crate::{
FetchQuoteData, NativeProvider, Options, QuoteRequest, SwapperError, SwapperMode,
config::{ReferralFee, ReferralFees},
fees::DEFAULT_STABLE_SWAP_REFERRAL_BPS,
};
use primitives::{AssetId, Chain, swap::SwapStatus};
use std::{sync::Arc, time::SystemTime};
Expand All @@ -808,19 +809,15 @@ mod tests {
async fn test_across_quote() -> Result<(), SwapperError> {
let network_provider = Arc::new(NativeProvider::default());
let swap_provider = Across::boxed(network_provider.clone());
let mut options = Options {
let options = Options {
slippage: 100.into(),
fee: Some(ReferralFees::evm(ReferralFee {
bps: 25,
bps: DEFAULT_STABLE_SWAP_REFERRAL_BPS,
address: "0x0D9DAB1A248f63B0a48965bA8435e4de7497a3dC".into(),
})),
preferred_providers: vec![],
use_max_amount: false,
};
options.fee.as_mut().unwrap().evm_bridge = ReferralFee {
bps: 25,
address: "0x0D9DAB1A248f63B0a48965bA8435e4de7497a3dC".into(),
};

let request = QuoteRequest {
from_asset: AssetId::from_chain(Chain::Optimism).into(),
Expand Down
8 changes: 6 additions & 2 deletions crates/swapper/src/chainflip/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ use super::{
};
use crate::{
FetchQuoteData, ProviderData, ProviderType, Quote, QuoteRequest, Route, SwapResult, Swapper, SwapperChainAsset, SwapperError, SwapperProvider, SwapperQuoteData,
alien::RpcProvider, amount_to_value, approval::check_approval_erc20, config::DEFAULT_CHAINFLIP_FEE_BPS, cross_chain::VaultAddresses, slippage,
alien::RpcProvider,
amount_to_value,
approval::check_approval_erc20,
cross_chain::VaultAddresses,
fees::{DEFAULT_CHAINFLIP_FEE_BPS, apply_slippage_in_bp},
};
use primitives::{ChainType, chain::Chain, swap::QuoteAsset};

Expand Down Expand Up @@ -218,7 +222,7 @@ where
})
} else if from_asset.chain.chain_type() == ChainType::Bitcoin {
let output_amount: U256 = quote.to_value.parse()?;
let min_output_amount = slippage::apply_slippage_in_bp(&output_amount, quote.data.slippage_bps);
let min_output_amount = apply_slippage_in_bp(&output_amount, quote.data.slippage_bps);
VaultSwapExtras::Bitcoin(VaultSwapBtcExtras {
chain,
min_output_amount: BigUint::from_bytes_le(&min_output_amount.to_le_bytes::<32>()),
Expand Down
111 changes: 7 additions & 104 deletions crates/swapper/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{SwapperSlippage, SwapperSlippageMode};
use crate::{
SwapperSlippage, SwapperSlippageMode,
fees::{ReferralFees, default_referral_fees},
};
use primitives::Chain;

pub const DEFAULT_SLIPPAGE_BPS: u32 = 100;
Expand All @@ -7,7 +10,6 @@ pub const DEFAULT_CHAINFLIP_FEE_BPS: u32 = 45;
pub const DEFAULT_STABLE_SWAP_REFERRAL_BPS: u32 = 25;

pub const API_BASE_URL: &str = "https://api.gemwallet.com/proxy/swap";
pub const EVM_REFERRAL_ADDRESS: &str = "0x0D9DAB1A248f63B0a48965bA8435e4de7497a3dC";

pub fn get_swap_api_url(path: &str) -> String {
format!("{API_BASE_URL}/{path}")
Expand All @@ -22,114 +24,15 @@ pub struct Config {
pub high_price_impact_percent: u32,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ReferralFees {
pub evm: ReferralFee,
pub evm_bridge: ReferralFee,
pub solana: ReferralFee,
pub thorchain: ReferralFee,
pub sui: ReferralFee,
pub ton: ReferralFee,
pub tron: ReferralFee,
pub near: ReferralFee,
pub aptos: ReferralFee,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ReferralFee {
pub address: String,
pub bps: u32,
}

impl ReferralFees {
pub fn evm(evm: ReferralFee) -> ReferralFees {
ReferralFees {
evm,
evm_bridge: ReferralFee::default(),
solana: ReferralFee::default(),
thorchain: ReferralFee::default(),
sui: ReferralFee::default(),
ton: ReferralFee::default(),
tron: ReferralFee::default(),
near: ReferralFee::default(),
aptos: ReferralFee::default(),
}
}

pub fn update_all_bps(&mut self, bps: u32) {
self.iter_mut().for_each(|fee| fee.update_bps(bps));
}

fn iter_mut(&mut self) -> impl Iterator<Item = &mut ReferralFee> {
[
&mut self.evm,
&mut self.evm_bridge,
&mut self.solana,
&mut self.thorchain,
&mut self.sui,
&mut self.ton,
&mut self.tron,
&mut self.near,
&mut self.aptos,
]
.into_iter()
}
}

impl ReferralFee {
pub fn update_bps(&mut self, bps: u32) {
if !self.address.is_empty() || self.bps > 0 {
self.bps = bps;
}
}
}

pub fn get_swap_config() -> Config {
Config {
default_slippage: SwapperSlippage {
bps: DEFAULT_SLIPPAGE_BPS,
mode: SwapperSlippageMode::Exact,
},
permit2_expiration: 60 * 60 * 24 * 30, // 30 days
permit2_sig_deadline: 60 * 30, // 30 minutes
referral_fee: ReferralFees {
evm: ReferralFee {
address: EVM_REFERRAL_ADDRESS.into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
evm_bridge: ReferralFee {
address: EVM_REFERRAL_ADDRESS.into(),
bps: DEFAULT_STABLE_SWAP_REFERRAL_BPS,
},
solana: ReferralFee {
address: "5fmLrs2GuhfDP1B51ziV5Kd1xtAr9rw1jf3aQ4ihZ2gy".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
thorchain: ReferralFee {
address: "g1".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
sui: ReferralFee {
address: "0x9d6b98b18fd26b5efeec68d020dcf1be7a94c2c315353779bc6b3aed44188ddf".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
ton: ReferralFee {
address: "UQDxJKarPSp0bCta9DFgp81Mpt5hpGbuVcSxwfeza0Bin201".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
tron: ReferralFee {
address: "TYeyZXywpA921LEtw2PF3obK4B8Jjgpp32".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
near: ReferralFee {
address: "0x0d9dab1a248f63b0a48965ba8435e4de7497a3dc".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
aptos: ReferralFee {
address: "0xc09d385527743bb03ed7847bb9180b5ff2263d38d5a93f1c9b3068f8505f6488".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
},
permit2_expiration: 2_592_000, // 30 days
permit2_sig_deadline: 1800, // 30 minutes
referral_fee: default_referral_fees(),
high_price_impact_percent: 10,
}
}
Expand Down
13 changes: 13 additions & 0 deletions crates/swapper/src/fees/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod referral;
mod reserve;
mod slippage;

pub use referral::{ReferralFee, ReferralFees, default_referral_fees};
pub use reserve::{RESERVED_NATIVE_FEES, reserved_tx_fees, resolve_max_quote_value};
pub use slippage::{BasisPointConvert, apply_slippage_in_bp};

pub const DEFAULT_SWAP_FEE_BPS: u32 = 50;
pub const DEFAULT_CHAINFLIP_FEE_BPS: u32 = 45;
pub const DEFAULT_STABLE_SWAP_REFERRAL_BPS: u32 = 25;
pub const DEFAULT_REFERRER: &str = "gemwallet";

88 changes: 88 additions & 0 deletions crates/swapper/src/fees/referral.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use super::DEFAULT_SWAP_FEE_BPS;

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ReferralFees {
pub evm: ReferralFee,
pub solana: ReferralFee,
pub thorchain: ReferralFee,
pub sui: ReferralFee,
pub ton: ReferralFee,
pub tron: ReferralFee,
pub near: ReferralFee,
pub aptos: ReferralFee,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct ReferralFee {
pub address: String,
pub bps: u32,
}

impl ReferralFees {
pub fn evm(evm: ReferralFee) -> Self {
Self { evm, ..Default::default() }
}

pub fn update_all_bps(&mut self, bps: u32) {
self.iter_mut().for_each(|fee| fee.update_bps(bps));
}

fn iter_mut(&mut self) -> impl Iterator<Item = &mut ReferralFee> {
[
&mut self.evm,
&mut self.solana,
&mut self.thorchain,
&mut self.sui,
&mut self.ton,
&mut self.tron,
&mut self.near,
&mut self.aptos,
]
.into_iter()
}
}

impl ReferralFee {
pub fn update_bps(&mut self, bps: u32) {
if !self.address.is_empty() || self.bps > 0 {
self.bps = bps;
}
}
}

pub fn default_referral_fees() -> ReferralFees {
ReferralFees {
evm: ReferralFee {
address: "0x0D9DAB1A248f63B0a48965bA8435e4de7497a3dC".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
solana: ReferralFee {
address: "5fmLrs2GuhfDP1B51ziV5Kd1xtAr9rw1jf3aQ4ihZ2gy".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
thorchain: ReferralFee {
address: "g1".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
sui: ReferralFee {
address: "0x9d6b98b18fd26b5efeec68d020dcf1be7a94c2c315353779bc6b3aed44188ddf".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
ton: ReferralFee {
address: "UQDxJKarPSp0bCta9DFgp81Mpt5hpGbuVcSxwfeza0Bin201".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
tron: ReferralFee {
address: "TYeyZXywpA921LEtw2PF3obK4B8Jjgpp32".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
near: ReferralFee {
address: "0x0d9dab1a248f63b0a48965ba8435e4de7497a3dc".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
aptos: ReferralFee {
address: "0xc09d385527743bb03ed7847bb9180b5ff2263d38d5a93f1c9b3068f8505f6488".into(),
bps: DEFAULT_SWAP_FEE_BPS,
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{collections::HashMap, str::FromStr, sync::LazyLock};

use crate::{QuoteRequest, SwapperError};

// Reserved fees represent approximately two simple native transfers per chain, in base units.
pub static RESERVED_NATIVE_FEES: LazyLock<HashMap<Chain, &'static str>> = LazyLock::new(|| {
HashMap::from([
(Chain::Near, "50000000000000000000000"), // 0.05 NEAR
Expand Down Expand Up @@ -33,6 +32,12 @@ pub static RESERVED_NATIVE_FEES: LazyLock<HashMap<Chain, &'static str>> = LazyLo
(Chain::Monad, "5000000000000000"), // 0.005 MON
(Chain::XLayer, "5000000000000000"), // 0.005 OKB
(Chain::Plasma, "5000000000000000"), // 0.005 XPL
(Chain::Cosmos, "39000"), // 0.039 ATOM
(Chain::Osmosis, "130000"), // 0.13 OSMO
(Chain::Celestia, "39000"), // 0.039 TIA
(Chain::Injective, "1300000000000000"), // 0.0013 INJ
(Chain::Sei, "1300000"), // 1.3 SEI
(Chain::Noble, "25000"), // 0.025 USDC
])
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,5 @@ mod tests {
assert_eq!(apply_slippage_in_bp(&1000_u64, 500), 950_u64);
assert_eq!(apply_slippage_in_bp(&U256::from(1000), 0), U256::from(1000));
assert_eq!(apply_slippage_in_bp(&U256::from(1000), HUNDRED_PERCENT_IN_BPS), U256::ZERO);
assert_eq!(apply_slippage_in_bp(&U256::from(1000), 12000), U256::ZERO);
assert_eq!(apply_slippage_in_bp(&U256::from(1000), 300 + 9800), U256::ZERO);
}
}
4 changes: 1 addition & 3 deletions crates/swapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod approval;
mod chainlink;
pub mod cross_chain;
mod eth_address;
pub(crate) mod fees;
pub mod fees;
mod swapper_trait;

#[cfg(test)]
Expand All @@ -20,9 +20,7 @@ pub mod models;
pub mod near_intents;
pub mod permit2_data;
pub mod proxy;
pub mod referrer;
pub mod relay;
pub mod slippage;
pub mod swapper;
pub mod thorchain;
pub mod uniswap;
Expand Down
5 changes: 1 addition & 4 deletions crates/swapper/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::permit2_data::Permit2Data;
use crate::{
SwapperMode, SwapperProvider, SwapperQuoteAsset, SwapperSlippage,
config::{DEFAULT_SLIPPAGE_BPS, ReferralFees},
};
use crate::{SwapperMode, SwapperProvider, SwapperQuoteAsset, SwapperSlippage, config::DEFAULT_SLIPPAGE_BPS, fees::ReferralFees};
pub use primitives::swap::SwapResult;
use primitives::{
AssetId, Chain,
Expand Down
2 changes: 1 addition & 1 deletion crates/swapper/src/near_intents/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ mod tests {
#[test]
fn test_get_asset_id() {
let asset = SwapperQuoteAsset {
id: "ethereum_0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".into(),
id: "ethereum_0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48".into(),
symbol: "USDC".into(),
decimals: 6,
};
Expand Down
Loading
Loading