Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split up shared::contracts #3109

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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<BlockNumberHash> {
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.
Expand Down
3 changes: 1 addition & 2 deletions crates/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ required-features = ["bin"]
[features]
default = []
bin = [
"anyhow",
"ethcontract-generate",
"serde_json",
"tracing",
Expand All @@ -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 }
Expand Down
30 changes: 30 additions & 0 deletions crates/contracts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<u64> {
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;
Expand Down
13 changes: 0 additions & 13 deletions crates/contracts/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
};
}
46 changes: 0 additions & 46 deletions crates/shared/src/contracts.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading