Skip to content

Commit

Permalink
making oneinch slippage configurabel (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
josojo authored May 11, 2022
1 parent b028a87 commit c346f3a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/shared/src/solver_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Slippage {
}

/// Creates a slippage amount from the specified basis points.
pub fn percentage_from_basis_points(basis_points: u16) -> Result<Self> {
pub fn percentage_from_basis_points(basis_points: u32) -> Result<Self> {
let percent = (basis_points as f64) / 100.;
Slippage::percentage(percent)
}
Expand Down
5 changes: 5 additions & 0 deletions crates/solver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ struct Arguments {
#[clap(long, env, default_value = "10")]
zeroex_slippage_bps: u32,

/// The slippage tolerance we apply to the price quoted by oneInchSolver
#[clap(long, env, default_value = "10")]
oneinch_slippage_bps: u32,

/// How to to submit settlement transactions.
/// Expected to contain either:
/// 1. One value equal to TransactionStrategyArg::DryRun or
Expand Down Expand Up @@ -507,6 +511,7 @@ async fn main() {
metrics.clone(),
zeroex_api.clone(),
args.zeroex_slippage_bps,
args.oneinch_slippage_bps,
args.shared.quasimodo_uses_internal_buffers,
args.shared.mip_uses_internal_buffers,
args.shared.one_inch_url,
Expand Down
2 changes: 2 additions & 0 deletions crates/solver/src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub fn create(
solver_metrics: Arc<dyn SolverMetrics>,
zeroex_api: Arc<dyn ZeroExApi>,
zeroex_slippage_bps: u32,
oneinch_slippage_bps: u32,
quasimodo_uses_internal_buffers: bool,
mip_uses_internal_buffers: bool,
one_inch_url: Url,
Expand Down Expand Up @@ -318,6 +319,7 @@ pub fn create(
disabled_one_inch_protocols.clone(),
client.clone(),
one_inch_url.clone(),
oneinch_slippage_bps,
)?,
solver_metrics.clone(),
))),
Expand Down
10 changes: 8 additions & 2 deletions crates/solver/src/solver/oneinch_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{
use crate::{
encoding::EncodedInteraction,
interactions::allowances::{AllowanceManager, AllowanceManaging, ApprovalRequest},
liquidity::{slippage::MAX_SLIPPAGE_BPS, LimitOrder},
liquidity::LimitOrder,
settlement::{Interaction, Settlement},
};
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -40,6 +40,7 @@ pub struct OneInchSolver {
#[derivative(Debug = "ignore")]
allowance_fetcher: Box<dyn AllowanceManaging>,
protocol_cache: ProtocolCache,
oneinch_slippage_bps: u32,
}

impl From<RestError> for SettlementError {
Expand All @@ -53,6 +54,7 @@ impl From<RestError> for SettlementError {

impl OneInchSolver {
/// Creates a new 1Inch solver with a list of disabled protocols.
#[allow(clippy::too_many_arguments)]
pub fn with_disabled_protocols(
account: Account,
web3: Web3,
Expand All @@ -61,6 +63,7 @@ impl OneInchSolver {
disabled_protocols: impl IntoIterator<Item = String>,
client: Client,
one_inch_url: Url,
oneinch_slippage_bps: u32,
) -> Result<Self> {
let settlement_address = settlement_contract.address();
Ok(Self {
Expand All @@ -70,6 +73,7 @@ impl OneInchSolver {
client: Box::new(OneInchClientImpl::new(one_inch_url, client, chain_id)?),
allowance_fetcher: Box::new(AllowanceManager::new(web3, settlement_address)),
protocol_cache: ProtocolCache::default(),
oneinch_slippage_bps,
})
}
}
Expand Down Expand Up @@ -104,7 +108,7 @@ impl OneInchSolver {
order.sell_amount,
self.settlement_contract.address(),
protocols,
Slippage::percentage_from_basis_points(MAX_SLIPPAGE_BPS).unwrap(),
Slippage::percentage_from_basis_points(self.oneinch_slippage_bps).unwrap(),
);

tracing::debug!("querying 1Inch swap api with {:?}", query);
Expand Down Expand Up @@ -196,6 +200,7 @@ mod tests {
client: Box::new(client),
allowance_fetcher: Box::new(allowance_fetcher),
protocol_cache: ProtocolCache::default(),
oneinch_slippage_bps: 10u32,
}
}

Expand Down Expand Up @@ -426,6 +431,7 @@ mod tests {
vec!["PMM1".to_string()],
Client::new(),
OneInchClientImpl::DEFAULT_URL.try_into().unwrap(),
10u32,
)
.unwrap();
let settlement = solver
Expand Down

0 comments on commit c346f3a

Please sign in to comment.