Skip to content

Commit

Permalink
CoW AMM: Implement archive node URL as an option to build CoW AMM Web3 (
Browse files Browse the repository at this point in the history
#3044)

# Description
CoW AMM currently relies on archive nodes. Add the possibility to build
`web3` for CoW AMM from an archive node URL.

# Changes
- Add new config for autopilot and driver in order to configure the
archive node URL
- If the archive node URL is present, build `web3` for CoW AMM from the
given URL

## How to test
1. Regression tests
  • Loading branch information
m-lord-renkse authored Oct 10, 2024
1 parent e7edc0c commit 42e4fe2
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 16 deletions.
6 changes: 6 additions & 0 deletions crates/autopilot/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ pub struct Arguments {
/// The maximum number of winners per auction. Each winner will be allowed
/// to settle their winning orders at the same time.
pub max_winners_per_auction: usize,

/// Archive node URL used to index CoW AMM
#[clap(long, env)]
pub archive_node_url: Option<Url>,
}

impl std::fmt::Display for Arguments {
Expand Down Expand Up @@ -283,6 +287,7 @@ impl std::fmt::Display for Arguments {
max_run_loop_delay,
run_loop_native_price_timeout,
max_winners_per_auction,
archive_node_url,
} = self;

write!(f, "{}", shared)?;
Expand Down Expand Up @@ -363,6 +368,7 @@ impl std::fmt::Display for Arguments {
run_loop_native_price_timeout
)?;
writeln!(f, "max_winners_per_auction: {:?}", max_winners_per_auction)?;
writeln!(f, "archive_node_url: {:?}", archive_node_url)?;
Ok(())
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,11 @@ pub async fn run(args: Arguments) {
skip_event_sync_start,
);

let mut cow_amm_registry = cow_amm::Registry::new(web3.clone());
let archive_node_web3 = args.archive_node_url.as_ref().map_or(web3.clone(), |url| {
boundary::web3_client(url, &args.shared.ethrpc)
});

let mut cow_amm_registry = cow_amm::Registry::new(archive_node_web3);
for config in &args.cow_amm_configs {
cow_amm_registry
.add_listener(config.index_start, config.factory, config.helper)
Expand Down
9 changes: 7 additions & 2 deletions crates/driver/src/infra/blockchain/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use {
crate::{domain::eth, infra::blockchain::Ethereum},
crate::{boundary, domain::eth, infra::blockchain::Ethereum},
ethcontract::dyns::DynWeb3,
ethrpc::block_stream::CurrentBlockWatcher,
thiserror::Error,
url::Url,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -30,6 +31,7 @@ impl Contracts {
chain: eth::ChainId,
addresses: Addresses,
block_stream: CurrentBlockWatcher,
archive_node_url: Option<&Url>,
) -> Result<Self, Error> {
let address_for = |contract: &ethcontract::Contract,
address: Option<eth::ContractAddress>| {
Expand Down Expand Up @@ -64,7 +66,10 @@ impl Contracts {
.0,
);

let mut cow_amm_registry = cow_amm::Registry::new(web3.clone());
let archive_node_web3 = archive_node_url
.as_ref()
.map_or(web3.clone(), |url| boundary::buffered_web3_client(url));
let mut cow_amm_registry = cow_amm::Registry::new(archive_node_web3);
for config in addresses.cow_amms {
cow_amm_registry
.add_listener(config.index_start, config.factory, config.helper)
Expand Down
13 changes: 10 additions & 3 deletions crates/driver/src/infra/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Ethereum {
rpc: Rpc,
addresses: contracts::Addresses,
gas: Arc<GasPriceEstimator>,
archive_node_url: Option<&Url>,
) -> Self {
let Rpc { web3, chain, url } = rpc;

Expand All @@ -82,9 +83,15 @@ impl Ethereum {
.await
.expect("couldn't initialize current block stream");

let contracts = Contracts::new(&web3, chain, addresses, current_block_stream.clone())
.await
.expect("could not initialize important smart contracts");
let contracts = Contracts::new(
&web3,
chain,
addresses,
current_block_stream.clone(),
archive_node_url,
)
.await
.expect("could not initialize important smart contracts");

Self {
inner: Arc::new(Inner {
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/infra/config/file/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,5 +331,6 @@ pub async fn load(chain: eth::ChainId, path: &Path) -> infra::Config {
disable_gas_simulation: config.disable_gas_simulation.map(Into::into),
gas_estimator: config.gas_estimator,
order_priority_strategies: config.order_priority_strategies,
archive_node_url: config.archive_node_url,
}
}
3 changes: 3 additions & 0 deletions crates/driver/src/infra/config/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ struct Config {
default = "default_order_priority_strategies"
)]
order_priority_strategies: Vec<OrderPriorityStrategy>,

/// Archive node URL used to index CoW AMM
archive_node_url: Option<Url>,
}

#[serde_as]
Expand Down
22 changes: 13 additions & 9 deletions crates/driver/src/infra/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::{
domain::eth,
infra::{
blockchain,
config::file::{GasEstimatorType, OrderPriorityStrategy},
liquidity,
mempool,
simulator,
solver,
use {
crate::{
domain::eth,
infra::{
blockchain,
config::file::{GasEstimatorType, OrderPriorityStrategy},
liquidity,
mempool,
simulator,
solver,
},
},
url::Url,
};

pub mod file;
Expand All @@ -24,4 +27,5 @@ pub struct Config {
pub mempools: Vec<mempool::Config>,
pub contracts: blockchain::contracts::Addresses,
pub order_priority_strategies: Vec<OrderPriorityStrategy>,
pub archive_node_url: Option<Url>,
}
8 changes: 7 additions & 1 deletion crates/driver/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ async fn ethereum(config: &infra::Config, ethrpc: blockchain::Rpc) -> Ethereum {
.await
.expect("initialize gas price estimator"),
);
Ethereum::new(ethrpc, config.contracts.clone(), gas).await
Ethereum::new(
ethrpc,
config.contracts.clone(),
gas,
config.archive_node_url.as_ref(),
)
.await
}

async fn solvers(config: &config::Config, eth: &Ethereum) -> Vec<Solver> {
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/tests/setup/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ impl Solver {
cow_amms: vec![],
},
gas,
None,
)
.await;

Expand Down

0 comments on commit 42e4fe2

Please sign in to comment.