2020-10-08 15:56:36 +00:00
|
|
|
//! # Ethers Middleware
|
2020-09-24 21:33:09 +00:00
|
|
|
//!
|
2020-12-31 19:08:12 +00:00
|
|
|
//! Ethers uses a middleware-based architecture. You start the middleware stack with
|
2020-10-08 15:56:36 +00:00
|
|
|
//! a [`Provider`](ethers_providers::Provider), and wrap it with additional
|
|
|
|
//! middleware functionalities that you need.
|
2020-09-24 21:33:09 +00:00
|
|
|
//!
|
2020-10-08 15:56:36 +00:00
|
|
|
//! ## Available Middleware
|
2020-12-31 19:08:12 +00:00
|
|
|
//! - [`Signer`](crate::SignerMiddleware): Signs transactions locally, with a private
|
|
|
|
//! key or a hardware wallet
|
|
|
|
//! - [`Nonce Manager`](crate::NonceManagerMiddleware): Manages nonces locally, allowing
|
|
|
|
//! the rapid broadcast of transactions without having to wait for them to be submitted
|
|
|
|
//! - [`Gas Escalator`](crate::gas_escalator::GasEscalatorMiddleware): Bumps transaction
|
|
|
|
//! gas prices in the background
|
|
|
|
//! - [`Gas Oracle`](crate::gas_oracle): Allows getting your gas price estimates from
|
|
|
|
//! places other than `eth_gasPrice`.
|
2021-01-22 09:25:22 +00:00
|
|
|
//! - [`Transformer`](crate::transformer): Allows intercepting and transforming a transaction to
|
|
|
|
//! be broadcasted via a proxy wallet, e.g. [`DSProxy`](crate::transformer::DsProxy).
|
2020-09-24 21:33:09 +00:00
|
|
|
//!
|
2020-10-08 15:56:36 +00:00
|
|
|
//! ## Example of a middleware stack
|
2020-09-24 21:33:09 +00:00
|
|
|
//!
|
2020-10-08 15:56:36 +00:00
|
|
|
//! ```no_run
|
2021-08-28 21:06:29 +00:00
|
|
|
//! use ethers_providers::{Provider, Http};
|
|
|
|
//! use ethers_signers::{LocalWallet, Signer};
|
|
|
|
//! use ethers_middleware::{
|
|
|
|
//! gas_escalator::{GasEscalatorMiddleware, GeometricGasPrice, Frequency},
|
2021-10-15 11:05:54 +00:00
|
|
|
//! gas_oracle::{GasOracleMiddleware, EthGasStation, GasCategory},
|
2021-08-28 21:06:29 +00:00
|
|
|
//! signer::SignerMiddleware,
|
|
|
|
//! nonce_manager::NonceManagerMiddleware,
|
2020-10-08 15:56:36 +00:00
|
|
|
//! };
|
2021-08-28 21:06:29 +00:00
|
|
|
//! use ethers_core::rand;
|
2020-10-08 15:56:36 +00:00
|
|
|
//! use std::convert::TryFrom;
|
2020-09-24 21:33:09 +00:00
|
|
|
//!
|
2020-10-08 15:56:36 +00:00
|
|
|
//! // Start the stack
|
|
|
|
//! let provider = Provider::<Http>::try_from("http://localhost:8545").unwrap();
|
|
|
|
//!
|
|
|
|
//! // Escalate gas prices
|
|
|
|
//! let escalator = GeometricGasPrice::new(1.125, 60u64, None::<u64>);
|
|
|
|
//! let provider =
|
|
|
|
//! GasEscalatorMiddleware::new(provider, escalator, Frequency::PerBlock);
|
|
|
|
//!
|
|
|
|
//! // Sign transactions with a private key
|
|
|
|
//! let signer = LocalWallet::new(&mut rand::thread_rng());
|
|
|
|
//! let address = signer.address();
|
|
|
|
//! let provider = SignerMiddleware::new(provider, signer);
|
|
|
|
//!
|
2021-10-15 11:05:54 +00:00
|
|
|
//! // Use EthGasStation as the gas oracle
|
|
|
|
//! let gas_oracle = EthGasStation::new(None);
|
2020-10-08 15:56:36 +00:00
|
|
|
//! let provider = GasOracleMiddleware::new(provider, gas_oracle);
|
|
|
|
//!
|
|
|
|
//! // Manage nonces locally
|
|
|
|
//! let provider = NonceManagerMiddleware::new(provider, address);
|
|
|
|
//!
|
|
|
|
//! // ... do something with the provider
|
|
|
|
//! ```
|
|
|
|
|
2020-12-31 19:08:12 +00:00
|
|
|
/// The [Gas Escalator middleware](crate::gas_escalator::GasEscalatorMiddleware)
|
|
|
|
/// is used to re-broadcast transactions with an increasing gas price to guarantee
|
|
|
|
/// their timely inclusion.
|
2020-10-08 15:56:36 +00:00
|
|
|
pub mod gas_escalator;
|
|
|
|
|
|
|
|
/// The gas oracle middleware is used to get the gas price from a list of gas oracles
|
2020-12-31 19:08:12 +00:00
|
|
|
/// instead of using eth_gasPrice. For usage examples, refer to the
|
|
|
|
/// [`GasOracle`](crate::gas_oracle::GasOracle) trait.
|
2020-09-24 21:33:09 +00:00
|
|
|
pub mod gas_oracle;
|
|
|
|
|
2021-10-29 12:29:35 +00:00
|
|
|
/// The [Nonce Manager](crate::NonceManagerMiddleware) is used to locally calculate nonces instead
|
|
|
|
/// of using eth_getTransactionCount
|
2020-10-08 15:56:36 +00:00
|
|
|
pub mod nonce_manager;
|
2020-12-31 19:08:12 +00:00
|
|
|
pub use nonce_manager::NonceManagerMiddleware;
|
2020-09-24 21:33:09 +00:00
|
|
|
|
2021-01-22 09:25:22 +00:00
|
|
|
/// The [Transformer](crate::TransformerMiddleware) is used to intercept transactions and transform
|
|
|
|
/// them to be sent via various supported transformers, e.g., [DSProxy](crate::transformer::DsProxy)
|
|
|
|
pub mod transformer;
|
|
|
|
|
2020-12-31 19:08:12 +00:00
|
|
|
/// The [Signer](crate::SignerMiddleware) is used to locally sign transactions and messages
|
2020-10-08 15:56:36 +00:00
|
|
|
/// instead of using eth_sendTransaction and eth_sign
|
|
|
|
pub mod signer;
|
|
|
|
pub use signer::SignerMiddleware;
|
2021-08-23 08:30:26 +00:00
|
|
|
|
|
|
|
/// The [Policy](crate::PolicyMiddleware) is used to ensure transactions comply with the rules
|
|
|
|
/// configured in the `PolicyMiddleware` before sending them.
|
|
|
|
pub mod policy;
|
|
|
|
pub use policy::PolicyMiddleware;
|
2021-09-17 17:38:35 +00:00
|
|
|
|
|
|
|
/// The [TimeLag](crate::TimeLag) provides safety against reorgs by querying state N blocks
|
|
|
|
/// before the chain tip
|
|
|
|
pub mod timelag;
|
|
|
|
pub use timelag::TimeLag;
|