diff --git a/crates/autopilot/src/database/onchain_order_events/ethflow_events.rs b/crates/autopilot/src/database/onchain_order_events/ethflow_events.rs index 8f74c16893..d8d552b4d9 100644 --- a/crates/autopilot/src/database/onchain_order_events/ethflow_events.rs +++ b/crates/autopilot/src/database/onchain_order_events/ethflow_events.rs @@ -3,9 +3,13 @@ use { crate::database::events::meta_to_event_index, anyhow::{anyhow, Context, Result}, chrono::Duration, - contracts::cowswap_onchain_orders::{ - event_data::OrderPlacement as ContractOrderPlacement, - Event as ContractEvent, + contracts::{ + cowswap_onchain_orders::{ + event_data::OrderPlacement as ContractOrderPlacement, + Event as ContractEvent, + }, + deployment_block, + GPv2Settlement, }, database::{ byte_array::ByteArray, @@ -22,9 +26,9 @@ use { }, hex_literal::hex, model::time::now_in_epoch_seconds, - shared::contracts::settlement_deployment_block_number_hash, sqlx::types::BigDecimal, std::{collections::HashMap, convert::TryInto}, + web3::types::U64, }; // 4c84c1c8 is the identifier of the following function: @@ -145,6 +149,16 @@ fn convert_to_quote_id_and_user_valid_to( Ok((quote_id, user_valid_to)) } +async fn settlement_deployment_block_number_hash( + web3: &Web3, + chain_id: u64, +) -> Result { + let block_number = deployment_block(GPv2Settlement::raw_contract(), chain_id)?; + block_number_to_block_number_hash(web3, U64::from(block_number).into()) + .await + .ok_or_else(|| anyhow!("Deployment block not found")) +} + /// The block from which to start indexing eth-flow events. Note that this /// function is expected to be used at the start of the services and will panic /// if it cannot retrieve the information it needs. diff --git a/crates/contracts/Cargo.toml b/crates/contracts/Cargo.toml index f50c7760bb..a1300bc86a 100644 --- a/crates/contracts/Cargo.toml +++ b/crates/contracts/Cargo.toml @@ -15,7 +15,6 @@ required-features = ["bin"] [features] default = [] bin = [ - "anyhow", "ethcontract-generate", "serde_json", "tracing", @@ -27,7 +26,7 @@ ethcontract = { workspace = true } serde = { workspace = true } # [bin-dependencies] -anyhow = { workspace = true, optional = true } +anyhow = { workspace = true } ethcontract-generate = { workspace = true, optional = true, features = ["http"] } serde_json = { workspace = true, optional = true } tracing = { workspace = true, optional = true } diff --git a/crates/contracts/src/lib.rs b/crates/contracts/src/lib.rs index a2f0d750f1..cfdf66ef95 100644 --- a/crates/contracts/src/lib.rs +++ b/crates/contracts/src/lib.rs @@ -1,6 +1,36 @@ #![allow(clippy::let_unit_value)] pub use ethcontract; +use { + anyhow::{anyhow, bail, Result}, + ethcontract::{ + common::{contract::Network, DeploymentInformation}, + Contract, + }, +}; + +pub fn deployment(contract: &Contract, chain_id: u64) -> Result<&Network> { + contract + .networks + .get(&chain_id.to_string()) + // Note that we are conflating network IDs with chain IDs. In general + // they cannot be considered the same, but for the networks that we + // support (xDAI, Görli and Mainnet) they are. + .ok_or_else(|| anyhow!("missing {} deployment for {}", contract.name, chain_id)) +} + +pub fn deployment_block(contract: &Contract, chain_id: u64) -> Result { + let deployment_info = deployment(contract, chain_id)? + .deployment_information + .ok_or_else(|| anyhow!("missing deployment information for {}", contract.name))?; + + match deployment_info { + DeploymentInformation::BlockNumber(block) => Ok(block), + DeploymentInformation::TransactionHash(tx) => { + bail!("missing deployment block number for {}", tx) + } + } +} #[macro_use] mod macros; diff --git a/crates/contracts/src/macros.rs b/crates/contracts/src/macros.rs index 97595a9108..98186194a4 100644 --- a/crates/contracts/src/macros.rs +++ b/crates/contracts/src/macros.rs @@ -21,16 +21,3 @@ macro_rules! deployed_bytecode { .unwrap() }; } - -#[macro_export] -macro_rules! deployment_block { - ($contract:ident) => { - match $contract.deployment_information() { - Some(ethcontract::common::DeploymentInformation::TransactionHash(_)) => { - panic!("no block number in deployment info") - } - Some(ethcontract::common::DeploymentInformation::BlockNumber(block)) => Some(block), - None => None, - } - }; -} diff --git a/crates/shared/src/contracts.rs b/crates/shared/src/contracts.rs deleted file mode 100644 index e29e65bfd5..0000000000 --- a/crates/shared/src/contracts.rs +++ /dev/null @@ -1,46 +0,0 @@ -use { - anyhow::{anyhow, bail, Result}, - contracts::GPv2Settlement, - ethcontract::{ - common::{contract::Network, DeploymentInformation}, - Contract, - }, - ethrpc::{ - block_stream::{block_number_to_block_number_hash, BlockNumberHash}, - Web3, - }, - web3::types::U64, -}; - -pub fn deployment(contract: &Contract, chain_id: u64) -> Result<&Network> { - contract - .networks - .get(&chain_id.to_string()) - // Note that we are conflating network IDs with chain IDs. In general - // they cannot be considered the same, but for the networks that we - // support (xDAI, Görli and Mainnet) they are. - .ok_or_else(|| anyhow!("missing {} deployment for {}", contract.name, chain_id)) -} - -pub async fn deployment_block(contract: &Contract, chain_id: u64) -> Result { - let deployment_info = deployment(contract, chain_id)? - .deployment_information - .ok_or_else(|| anyhow!("missing deployment information for {}", contract.name))?; - - match deployment_info { - DeploymentInformation::BlockNumber(block) => Ok(block), - DeploymentInformation::TransactionHash(tx) => { - bail!("missing deployment block number for {}", tx) - } - } -} - -pub async fn settlement_deployment_block_number_hash( - web3: &Web3, - chain_id: u64, -) -> Result { - let block_number = deployment_block(GPv2Settlement::raw_contract(), chain_id).await?; - block_number_to_block_number_hash(web3, U64::from(block_number).into()) - .await - .ok_or_else(|| anyhow!("Deployment block not found")) -} diff --git a/crates/shared/src/lib.rs b/crates/shared/src/lib.rs index c61ebbff98..6cdd79c504 100644 --- a/crates/shared/src/lib.rs +++ b/crates/shared/src/lib.rs @@ -7,7 +7,6 @@ pub mod bad_token; pub mod baseline_solver; pub mod code_fetching; pub mod code_simulation; -pub mod contracts; pub mod conversions; pub mod current_block; pub mod db_order_conversions;