-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description Autopilot `RunLoop` is slowly becoming a new `Driver` and `Solver` class with a million arguments. Let's try to classify things into `infra` for the start. Initially, I recognized `blockchain`, `config` and `driver` as something we already have. https://github.com/cowprotocol/services/pull/2200/files introduces s3 which should also go to `infra`. - [x] Added `blockchain::Ethereum` for everything blockchain related
- Loading branch information
Showing
6 changed files
with
166 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
pub use shared::ethrpc::Web3; | ||
use url::Url; | ||
/// Builds a web3 client that bufferes requests and sends them in a | ||
/// batch call. | ||
pub fn buffered_web3_client(ethrpc: &Url) -> Web3 { | ||
let ethrpc_args = shared::ethrpc::Arguments { | ||
ethrpc_max_batch_size: 20, | ||
ethrpc_max_concurrent_requests: 10, | ||
ethrpc_batch_delay: Default::default(), | ||
}; | ||
let http_factory = | ||
shared::http_client::HttpClientFactory::new(&shared::http_client::Arguments { | ||
http_timeout: std::time::Duration::from_secs(10), | ||
}); | ||
shared::ethrpc::web3(ðrpc_args, &http_factory, ethrpc, "base") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use { | ||
crate::boundary, | ||
ethcontract::dyns::DynWeb3, | ||
ethrpc::current_block::CurrentBlockStream, | ||
primitive_types::{H256, U256}, | ||
std::sync::Arc, | ||
thiserror::Error, | ||
web3::types::TransactionReceipt, | ||
}; | ||
|
||
/// Chain ID as defined by EIP-155. | ||
/// | ||
/// https://eips.ethereum.org/EIPS/eip-155 | ||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub struct ChainId(pub U256); | ||
|
||
impl From<U256> for ChainId { | ||
fn from(value: U256) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NetworkId(pub String); | ||
|
||
impl From<String> for NetworkId { | ||
fn from(value: String) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
/// An Ethereum RPC connection. | ||
pub struct Rpc { | ||
web3: DynWeb3, | ||
network: Network, | ||
} | ||
|
||
/// Network information for an Ethereum blockchain connection. | ||
#[derive(Clone, Debug)] | ||
pub struct Network { | ||
pub id: NetworkId, | ||
pub chain: ChainId, | ||
} | ||
|
||
impl Rpc { | ||
/// Instantiate an RPC client to an Ethereum (or Ethereum-compatible) node | ||
/// at the specifed URL. | ||
pub async fn new(url: &url::Url) -> Result<Self, Error> { | ||
let web3 = boundary::buffered_web3_client(url); | ||
let id = web3.net().version().await?.into(); | ||
let chain = web3.eth().chain_id().await?.into(); | ||
|
||
Ok(Self { | ||
web3, | ||
network: Network { id, chain }, | ||
}) | ||
} | ||
|
||
/// Returns the network information for the RPC connection. | ||
pub fn network(&self) -> &Network { | ||
&self.network | ||
} | ||
|
||
/// Returns a reference to the underlying web3 client. | ||
pub fn web3(&self) -> &DynWeb3 { | ||
&self.web3 | ||
} | ||
} | ||
|
||
/// The Ethereum blockchain. | ||
#[derive(Clone)] | ||
pub struct Ethereum { | ||
web3: DynWeb3, | ||
network: Network, | ||
current_block: CurrentBlockStream, | ||
} | ||
|
||
impl Ethereum { | ||
/// Access the Ethereum blockchain through an RPC API. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Since this type is essential for the program this method will panic on | ||
/// any initialization error. | ||
pub async fn new(rpc: Rpc) -> Self { | ||
let Rpc { web3, network } = rpc; | ||
|
||
Self { | ||
current_block: ethrpc::current_block::current_block_stream( | ||
Arc::new(web3.clone()), | ||
std::time::Duration::from_millis(500), | ||
) | ||
.await | ||
.expect("couldn't initialize current block stream"), | ||
web3, | ||
network, | ||
} | ||
} | ||
|
||
pub fn network(&self) -> &Network { | ||
&self.network | ||
} | ||
|
||
/// Returns a stream that monitors the block chain to inform about the | ||
/// current and new blocks. | ||
pub fn current_block(&self) -> &CurrentBlockStream { | ||
&self.current_block | ||
} | ||
|
||
pub async fn transaction_receipt( | ||
&self, | ||
hash: H256, | ||
) -> Result<Option<TransactionReceipt>, Error> { | ||
self.web3 | ||
.eth() | ||
.transaction_receipt(hash) | ||
.await | ||
.map_err(Into::into) | ||
} | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error("web3 error: {0:?}")] | ||
Web3(#[from] web3::error::Error), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod blockchain; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters