Skip to content

Commit

Permalink
Balancer SOR API V3 (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz authored Jul 4, 2024
1 parent 284de9b commit 98928ec
Show file tree
Hide file tree
Showing 14 changed files with 918 additions and 427 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ observe = { git = "https://github.com/cowprotocol/services.git", tag = "v2.265.0
shared = { git = "https://github.com/cowprotocol/services.git", tag = "v2.265.0", package = "shared" }
dto = { git = "https://github.com/cowprotocol/services.git", tag = "v2.265.0", package = "solvers-dto" }
rate-limit = { git = "https://github.com/cowprotocol/services.git", tag = "v2.265.0", package = "rate-limit" }
number = { git = "https://github.com/cowprotocol/services.git", tag = "v2.265.0", package = "number" }

[dev-dependencies]
glob = "0.3"
maplit = "1"
tempfile = "3"
5 changes: 2 additions & 3 deletions src/domain/solver/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ impl Dex {
order: &Order,
dex_order: &dex::Order,
tokens: &auction::Tokens,
gas_price: auction::GasPrice,
) -> Option<dex::Swap> {
let dex_err_handler = |err: infra::dex::Error| {
infra::metrics::solve_error(err.format_variant());
Expand Down Expand Up @@ -149,7 +148,7 @@ impl Dex {
let swap = async {
let slippage = self.slippage.relative(&dex_order.amount(), tokens);
self.dex
.swap(dex_order, &slippage, tokens, gas_price)
.swap(dex_order, &slippage, tokens)
.await
.map_err(dex_err_handler)
};
Expand Down Expand Up @@ -183,7 +182,7 @@ impl Dex {
) -> Option<solution::Solution> {
let order = order.get();
let dex_order = self.fills.dex_order(order, tokens)?;
let swap = self.try_solve(order, &dex_order, tokens, gas_price).await?;
let swap = self.try_solve(order, &dex_order, tokens).await?;
let sell = tokens.reference_price(&order.sell.token);
let Some(solution) = swap
.into_solution(
Expand Down
17 changes: 13 additions & 4 deletions src/infra/config/dex/balancer/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use {
crate::{
domain::eth,
infra::{config::dex::file, contracts, dex},
util::serialize,
},
ethereum_types::H160,
serde::Deserialize,
Expand All @@ -20,6 +21,15 @@ struct Config {
/// Optional Balancer V2 Vault contract address. If not specified, the
/// default Vault contract address will be used.
vault: Option<H160>,

/// Chain ID used to automatically determine contract addresses and send to
/// the SOR API.
#[serde_as(as = "serialize::ChainId")]
chain_id: eth::ChainId,

/// Whether to run `queryBatchSwap` to update the return amount with most
/// up-to-date on-chain values.
query_batch_swap: Option<bool>,
}

/// Load the driver configuration from a TOML file.
Expand All @@ -29,10 +39,7 @@ struct Config {
/// This method panics if the config is invalid or on I/O errors.
pub async fn load(path: &Path) -> super::Config {
let (base, config) = file::load::<Config>(path).await;

// Take advantage of the fact that deterministic deployment means that all
// CoW Protocol and Balancer Vault contracts have the same address.
let contracts = contracts::Contracts::for_chain(eth::ChainId::Mainnet);
let contracts = contracts::Contracts::for_chain(config.chain_id);

super::Config {
sor: dex::balancer::Config {
Expand All @@ -43,6 +50,8 @@ pub async fn load(path: &Path) -> super::Config {
.unwrap_or(contracts.balancer_vault),
settlement: base.contracts.settlement,
block_stream: base.block_stream.clone(),
chain_id: config.chain_id,
query_batch_swap: config.query_batch_swap.unwrap_or(false),
},
base,
}
Expand Down
Loading

0 comments on commit 98928ec

Please sign in to comment.