Skip to content

Commit

Permalink
0x Excluded Sources Configuration (#246)
Browse files Browse the repository at this point in the history
Co-authored-by: josojo <[email protected]>
  • Loading branch information
Nicholas Rodrigues Lordello and josojo committed Jun 2, 2022
1 parent 519f957 commit b645caf
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
5 changes: 4 additions & 1 deletion crates/orderbook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,10 @@ async fn main() {
token_info_fetcher.clone(),
args.shared.disabled_paraswap_dexs.clone(),
)),
PriceEstimatorType::ZeroEx => Box::new(ZeroExPriceEstimator::new(zeroex_api.clone())),
PriceEstimatorType::ZeroEx => Box::new(ZeroExPriceEstimator::new(
zeroex_api.clone(),
args.shared.disabled_zeroex_sources.clone(),
)),
PriceEstimatorType::Quasimodo => Box::new(QuasimodoPriceEstimator::new(
Arc::new(DefaultHttpSolverApi {
name: "quasimodo-price-estimator".to_string(),
Expand Down
4 changes: 4 additions & 0 deletions crates/shared/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ pub struct Arguments {
/// The 1Inch REST API URL to use.
#[structopt(long, env, default_value = "https://api.1inch.exchange/")]
pub one_inch_url: Url,

/// The list of disabled 0x sources.
#[clap(long, env, use_value_delimiter = true)]
pub disabled_zeroex_sources: Vec<String>,
}

pub fn parse_unbounded_factor(s: &str) -> Result<f64> {
Expand Down
8 changes: 7 additions & 1 deletion crates/shared/src/price_estimation/zeroex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use std::sync::Arc;
pub struct ZeroExPriceEstimator {
api: Arc<dyn ZeroExApi>,
sharing: RequestSharing<Query, BoxFuture<'static, Result<SwapResponse, PriceEstimationError>>>,
excluded_sources: Vec<String>,
}

impl ZeroExPriceEstimator {
pub fn new(api: Arc<dyn ZeroExApi>) -> Self {
pub fn new(api: Arc<dyn ZeroExApi>, excluded_sources: Vec<String>) -> Self {
Self {
api,
sharing: Default::default(),
excluded_sources,
}
}

Expand All @@ -34,6 +36,7 @@ impl ZeroExPriceEstimator {
sell_amount,
buy_amount,
slippage_percentage: Default::default(),
excluded_sources: self.excluded_sources.clone(),
};
let api = self.api.clone();
let swap_future = async move {
Expand Down Expand Up @@ -108,6 +111,7 @@ mod tests {
let estimator = ZeroExPriceEstimator {
api: Arc::new(zeroex_api),
sharing: Default::default(),
excluded_sources: Default::default(),
};

let est = estimator
Expand Down Expand Up @@ -154,6 +158,7 @@ mod tests {
let estimator = ZeroExPriceEstimator {
api: Arc::new(zeroex_api),
sharing: Default::default(),
excluded_sources: Default::default(),
};

let est = estimator
Expand All @@ -179,6 +184,7 @@ mod tests {
let estimator = ZeroExPriceEstimator {
api: Arc::new(DefaultZeroExApi::with_default_url(Client::new())),
sharing: Default::default(),
excluded_sources: Default::default(),
};

let result = estimator
Expand Down
39 changes: 37 additions & 2 deletions crates/shared/src/zeroex_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn addr2str(addr: H160) -> String {
///
/// These parameters are currently incomplete, and missing parameters can be
/// added incrementally as needed.
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Debug, Default)]
pub struct SwapQuery {
/// Contract address of a token to sell.
pub sell_token: H160,
Expand All @@ -45,6 +45,8 @@ pub struct SwapQuery {
pub buy_amount: Option<U256>,
/// Limit of price slippage you are willing to accept.
pub slippage_percentage: Slippage,
/// List of sources to exclude.
pub excluded_sources: Vec<String>,
}

impl SwapQuery {
Expand All @@ -67,6 +69,10 @@ impl SwapQuery {
url.query_pairs_mut()
.append_pair("buyAmount", &amount.to_string());
}
if !self.excluded_sources.is_empty() {
url.query_pairs_mut()
.append_pair("excludedSources", &self.excluded_sources.join(","));
}
url.query_pairs_mut()
.append_pair("affiliateAddress", AFFILIATE_ADDRESS);
// We do not provide a takerAddress so validation does not make sense.
Expand Down Expand Up @@ -472,6 +478,7 @@ mod tests {
sell_amount: Some(U256::from_f64_lossy(1e18)),
buy_amount: None,
slippage_percentage: Slippage(0.1_f64),
excluded_sources: Vec::new(),
};

let price_response = zeroex_client.get_swap(swap_query).await;
Expand All @@ -491,16 +498,44 @@ mod tests {
sell_amount: Some(U256::from_f64_lossy(1e18)),
buy_amount: None,
slippage_percentage: Slippage(0.1_f64),
excluded_sources: Vec::new(),
};

let price_response = zeroex_client.get_price(swap_query).await;
let price_response = zeroex_client.get_price(swap_query.clone()).await;
dbg!(&price_response);
assert!(price_response.is_ok());
let swap_response = zeroex_client.get_swap(swap_query).await;
dbg!(&swap_response);
assert!(swap_response.is_ok());
}

#[tokio::test]
#[ignore]
async fn excluded_sources() {
let zeroex = DefaultZeroExApi::default();
let query = SwapQuery {
sell_token: testlib::tokens::WETH,
buy_token: addr!("c011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f"), // SNX
sell_amount: Some(U256::from_f64_lossy(1000e18)),
buy_amount: None,
slippage_percentage: Slippage(0.1_f64),
excluded_sources: Vec::new(),
};

let swap = zeroex.get_swap(query.clone()).await;
dbg!(&swap);
assert!(swap.is_ok());

let swap = zeroex
.get_swap(SwapQuery {
excluded_sources: vec!["Balancer_V2".to_string()],
..query
})
.await;
dbg!(&swap);
assert!(swap.is_ok());
}

#[tokio::test]
#[ignore]
async fn test_get_orders() {
Expand Down
1 change: 1 addition & 0 deletions crates/solver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ async fn main() {
metrics.clone(),
zeroex_api.clone(),
args.zeroex_slippage_bps,
args.shared.disabled_zeroex_sources,
args.oneinch_slippage_bps,
args.shared.quasimodo_uses_internal_buffers,
args.shared.mip_uses_internal_buffers,
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,
disabled_zeroex_sources: Vec<String>,
oneinch_slippage_bps: u32,
quasimodo_uses_internal_buffers: bool,
mip_uses_internal_buffers: bool,
Expand Down Expand Up @@ -322,6 +323,7 @@ pub fn create(
chain_id,
zeroex_api.clone(),
zeroex_slippage_bps,
disabled_zeroex_sources.clone(),
)
.unwrap();
Ok(shared(SingleOrderSolver::new(
Expand Down
12 changes: 11 additions & 1 deletion crates/solver/src/solver/zeroex_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct ZeroExSolver {
api: Arc<dyn ZeroExApi>,
allowance_fetcher: Box<dyn AllowanceManaging>,
zeroex_slippage_bps: u32,
excluded_sources: Vec<String>,
}

/// Chain ID for Mainnet.
Expand All @@ -61,6 +62,7 @@ impl ZeroExSolver {
chain_id: u64,
api: Arc<dyn ZeroExApi>,
zeroex_slippage_bps: u32,
excluded_sources: Vec<String>,
) -> Result<Self> {
ensure!(
chain_id == MAINNET_CHAIN_ID,
Expand All @@ -72,6 +74,7 @@ impl ZeroExSolver {
allowance_fetcher: Box::new(allowance_fetcher),
api,
zeroex_slippage_bps,
excluded_sources,
})
}
}
Expand All @@ -94,6 +97,7 @@ impl SingleOrderSolving for ZeroExSolver {
buy_amount,
slippage_percentage: Slippage::number_from_basis_points(self.zeroex_slippage_bps)
.unwrap(),
excluded_sources: self.excluded_sources.clone(),
};
let swap = self.api.get_swap(query).await?;

Expand Down Expand Up @@ -185,6 +189,7 @@ mod tests {
chain_id,
Arc::new(DefaultZeroExApi::default()),
10u32,
Default::default(),
)
.unwrap();
let settlement = solver
Expand Down Expand Up @@ -226,6 +231,7 @@ mod tests {
chain_id,
Arc::new(DefaultZeroExApi::default()),
10u32,
Default::default(),
)
.unwrap();
let settlement = solver
Expand Down Expand Up @@ -294,6 +300,7 @@ mod tests {
api: Arc::new(client),
allowance_fetcher,
zeroex_slippage_bps: 10u32,
excluded_sources: Default::default(),
};

let buy_order_passing_limit = LimitOrder {
Expand Down Expand Up @@ -383,7 +390,8 @@ mod tests {
settlement,
chain_id,
Arc::new(DefaultZeroExApi::default()),
10u32
10u32,
Default::default(),
)
.is_err())
}
Expand Down Expand Up @@ -440,6 +448,7 @@ mod tests {
api: Arc::new(client),
allowance_fetcher,
zeroex_slippage_bps: 10u32,
excluded_sources: Default::default(),
};

let order = LimitOrder {
Expand Down Expand Up @@ -498,6 +507,7 @@ mod tests {
api: Arc::new(client),
allowance_fetcher,
zeroex_slippage_bps: 10u32,
excluded_sources: Default::default(),
};

let order = LimitOrder {
Expand Down

0 comments on commit b645caf

Please sign in to comment.