Skip to content

Commit

Permalink
Unify networks and add Base support
Browse files Browse the repository at this point in the history
  • Loading branch information
m-lord-renkse committed Oct 29, 2024
1 parent 2a3c2a4 commit 03d4104
Show file tree
Hide file tree
Showing 35 changed files with 623 additions and 281 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/autopilot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ itertools = { workspace = true }
maplit = { workspace = true }
mimalloc = { workspace = true }
model = { path = "../model" }
network = { path = "../network" }
num = { workspace = true }
number = { path = "../number" }
order-validation = { path = "../order-validation" }
Expand Down
18 changes: 4 additions & 14 deletions crates/autopilot/src/domain/settlement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ impl Settlement {
pub async fn new(
settled: Transaction,
persistence: &infra::Persistence,
chain: &infra::blockchain::Id,
network: &network::Network,
) -> Result<Self, Error> {
let auction = persistence.get_auction(settled.auction_id).await?;

if settled.block > auction.block + max_settlement_age(chain) {
if settled.block > auction.block + max_settlement_age(network) {
// A settled transaction references a VERY old auction.
//
// A hacky way to detect processing of production settlements in the staging
Expand Down Expand Up @@ -138,23 +138,13 @@ impl Settlement {
}
}

const MAINNET_BLOCK_TIME: u64 = 13_000; // ms
const GNOSIS_BLOCK_TIME: u64 = 5_000; // ms
const SEPOLIA_BLOCK_TIME: u64 = 13_000; // ms
const ARBITRUM_ONE_BLOCK_TIME: u64 = 100; // ms

/// How old (in terms of blocks) a settlement should be, to be considered as a
/// settlement from another environment.
///
/// Currently set to ~6h
fn max_settlement_age(chain: &infra::blockchain::Id) -> u64 {
fn max_settlement_age(network: &network::Network) -> u64 {
const TARGET_AGE: u64 = 6 * 60 * 60 * 1000; // 6h in ms
match chain {
infra::blockchain::Id::Mainnet => TARGET_AGE / MAINNET_BLOCK_TIME,
infra::blockchain::Id::Gnosis => TARGET_AGE / GNOSIS_BLOCK_TIME,
infra::blockchain::Id::Sepolia => TARGET_AGE / SEPOLIA_BLOCK_TIME,
infra::blockchain::Id::ArbitrumOne => TARGET_AGE / ARBITRUM_ONE_BLOCK_TIME,
}
network.number_of_blocks_in(TARGET_AGE).round() as u64
}

#[derive(Debug, thiserror::Error)]
Expand Down
2 changes: 1 addition & 1 deletion crates/autopilot/src/domain/settlement/observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Observer {
let settlement = match settlement::Settlement::new(
transaction,
&self.persistence,
self.eth.chain(),
self.eth.network(),
)
.await
{
Expand Down
9 changes: 3 additions & 6 deletions crates/autopilot/src/infra/blockchain/authenticator.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use {
crate::{
domain::{self, eth},
infra::blockchain::{
self,
contracts::{deployment_address, Contracts},
},
infra::blockchain::contracts::{deployment_address, Contracts},
},
ethcontract::{dyns::DynWeb3, GasPrice},
};
Expand All @@ -25,13 +22,13 @@ impl Manager {
/// Creates an authenticator which can remove solvers from the allow-list
pub async fn new(
web3: DynWeb3,
chain: blockchain::Id,
network: &network::Network,
contracts: Contracts,
authenticator_pk: eth::H256,
) -> Self {
let authenticator_role = contracts::Roles::at(
&web3,
deployment_address(contracts::Roles::raw_contract(), &chain).expect("roles address"),
deployment_address(contracts::Roles::raw_contract(), &network).expect("roles address"),
);

Self {
Expand Down
14 changes: 5 additions & 9 deletions crates/autopilot/src/infra/blockchain/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use {
crate::{domain, infra::blockchain},
ethcontract::dyns::DynWeb3,
primitive_types::H160,
};
use {crate::domain, ethcontract::dyns::DynWeb3, primitive_types::H160};

#[derive(Debug, Clone)]
pub struct Contracts {
Expand All @@ -24,10 +20,10 @@ pub struct Addresses {
}

impl Contracts {
pub async fn new(web3: &DynWeb3, chain: &blockchain::Id, addresses: Addresses) -> Self {
pub async fn new(web3: &DynWeb3, network: &network::Network, addresses: Addresses) -> Self {
let address_for = |contract: &ethcontract::Contract, address: Option<H160>| {
address
.or_else(|| deployment_address(contract, chain))
.or_else(|| deployment_address(contract, network))
.unwrap()
};

Expand Down Expand Up @@ -104,10 +100,10 @@ impl Contracts {
/// there is no known deployment for the contract on that network.
pub fn deployment_address(
contract: &ethcontract::Contract,
chain: &blockchain::Id,
network: &network::Network,
) -> Option<H160> {
contract
.networks
.get(chain.network_id())
.get(&network.chain_id().to_string())
.map(|network| network.address)
}
43 changes: 0 additions & 43 deletions crates/autopilot/src/infra/blockchain/id.rs

This file was deleted.

26 changes: 12 additions & 14 deletions crates/autopilot/src/infra/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ use {

pub mod authenticator;
pub mod contracts;
pub mod id;

pub use id::Id;

/// An Ethereum RPC connection.
pub struct Rpc {
web3: DynWeb3,
chain: Id,
network: network::Network,
url: Url,
}

Expand All @@ -30,18 +27,19 @@ impl Rpc {
ethrpc_args: &shared::ethrpc::Arguments,
) -> Result<Self, Error> {
let web3 = boundary::web3_client(url, ethrpc_args);
let chain = Id::new(web3.eth().chain_id().await?).map_err(|_| Error::UnsupportedChain)?;
let network = network::Network::try_from(web3.eth().chain_id().await?)
.map_err(|_| Error::UnsupportedChain)?;

Ok(Self {
web3,
chain,
network,
url: url.clone(),
})
}

/// Returns the chain id for the RPC connection.
pub fn chain(&self) -> Id {
self.chain
pub fn network(&self) -> network::Network {
self.network
}

/// Returns a reference to the underlying web3 client.
Expand All @@ -59,7 +57,7 @@ impl Rpc {
#[derive(Clone)]
pub struct Ethereum {
web3: DynWeb3,
chain: Id,
network: network::Network,
current_block: CurrentBlockWatcher,
contracts: Contracts,
}
Expand All @@ -73,25 +71,25 @@ impl Ethereum {
/// any initialization error.
pub async fn new(
web3: DynWeb3,
chain: Id,
network: &network::Network,
url: Url,
addresses: contracts::Addresses,
poll_interval: Duration,
) -> Self {
let contracts = Contracts::new(&web3, &chain, addresses).await;
let contracts = Contracts::new(&web3, &network, addresses).await;

Self {
current_block: ethrpc::block_stream::current_block_stream(url, poll_interval)
.await
.expect("couldn't initialize current block stream"),
web3,
chain,
network: network.clone(),
contracts,
}
}

pub fn chain(&self) -> &Id {
&self.chain
pub fn network(&self) -> &network::Network {
&self.network
}

/// Returns a stream that monitors the block chain to inform about the
Expand Down
26 changes: 12 additions & 14 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ async fn ethrpc(url: &Url, ethrpc_args: &shared::ethrpc::Arguments) -> infra::bl

async fn ethereum(
web3: DynWeb3,
chain: infra::blockchain::Id,
network: &network::Network,
url: Url,
contracts: infra::blockchain::contracts::Addresses,
poll_interval: Duration,
) -> infra::Ethereum {
infra::Ethereum::new(web3, chain, url, contracts, poll_interval).await
infra::Ethereum::new(web3, network, url, contracts, poll_interval).await
}

pub async fn start(args: impl Iterator<Item = String>) {
Expand Down Expand Up @@ -156,7 +156,7 @@ pub async fn run(args: Arguments) {
}

let ethrpc = ethrpc(&args.shared.node_url, &args.shared.ethrpc).await;
let chain = ethrpc.chain();
let chain = ethrpc.network();
let web3 = ethrpc.web3().clone();
let url = ethrpc.url().clone();
let contracts = infra::blockchain::contracts::Addresses {
Expand All @@ -165,7 +165,7 @@ pub async fn run(args: Arguments) {
};
let eth = ethereum(
web3.clone(),
chain,
&chain,
url,
contracts.clone(),
args.shared.current_block.block_stream_poll_interval,
Expand Down Expand Up @@ -197,12 +197,11 @@ pub async fn run(args: Arguments) {
other => Some(other.unwrap()),
};

let network_name = shared::network::network_name(chain_id);
let network = network::Network::try_from(chain_id).unwrap();

let signature_validator = signature_validator::validator(
&web3,
signature_validator::Contracts {
chain_id,
settlement: eth.contracts().settlement().address(),
vault_relayer,
},
Expand All @@ -211,7 +210,6 @@ pub async fn run(args: Arguments) {
let balance_fetcher = account_balances::cached(
&web3,
account_balances::Contracts {
chain_id,
settlement: eth.contracts().settlement().address(),
vault_relayer,
vault: vault.as_ref().map(|contract| contract.address()),
Expand All @@ -230,10 +228,11 @@ pub async fn run(args: Arguments) {
.expect("failed to create gas price estimator"),
);

let baseline_sources = args.shared.baseline_sources.clone().unwrap_or_else(|| {
shared::sources::defaults_for_chain(chain_id)
.expect("failed to get default baseline sources")
});
let baseline_sources = args
.shared
.baseline_sources
.clone()
.unwrap_or_else(|| shared::sources::defaults_for_network(&network));
tracing::info!(?baseline_sources, "using baseline sources");
let univ2_sources = baseline_sources
.iter()
Expand Down Expand Up @@ -261,7 +260,7 @@ pub async fn run(args: Arguments) {
let finder = token_owner_finder::init(
&args.token_owner_finder,
web3.clone(),
chain_id,
&network,
&http_factory,
&pair_providers,
vault.as_ref(),
Expand Down Expand Up @@ -312,8 +311,7 @@ pub async fn run(args: Arguments) {
factory::Network {
web3: web3.clone(),
simulation_web3,
name: network_name.to_string(),
chain_id,
network,
native_token: eth.contracts().weth().address(),
settlement: eth.contracts().settlement().address(),
authenticator: eth
Expand Down
Loading

0 comments on commit 03d4104

Please sign in to comment.