Crate ethers_middleware[][src]

Expand description

Ethers uses a middleware-based architecture. You start the middleware stack with a Provider, and wrap it with additional middleware functionalities that you need.

Available Middleware

  • Signer: Signs transactions locally, with a private key or a hardware wallet
  • Nonce Manager: Manages nonces locally, allowing the rapid broadcast of transactions without having to wait for them to be submitted
  • Gas Escalator: Bumps transaction gas prices in the background
  • Gas Oracle: Allows getting your gas price estimates from places other than eth_gasPrice.
  • Transformer: Allows intercepting and transforming a transaction to be broadcasted via a proxy wallet, e.g. DSProxy.

Example of a middleware stack

use ethers_providers::{Provider, Http};
use ethers_signers::{LocalWallet, Signer};
use ethers_middleware::{
    gas_escalator::{GasEscalatorMiddleware, GeometricGasPrice, Frequency},
    gas_oracle::{GasOracleMiddleware, EthGasStation, GasCategory},
use ethers_core::rand;
use std::convert::TryFrom;

// 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);

// Use EthGasStation as the gas oracle
let gas_oracle = EthGasStation::new(None);
let provider = GasOracleMiddleware::new(provider, gas_oracle);

// Manage nonces locally
let provider = NonceManagerMiddleware::new(provider, address);

// ... do something with the provider


pub use nonce_manager::NonceManagerMiddleware;
pub use signer::SignerMiddleware;
pub use policy::PolicyMiddleware;
pub use timelag::TimeLag;


The Gas Escalator middleware is used to re-broadcast transactions with an increasing gas price to guarantee their timely inclusion.

The gas oracle middleware is used to get the gas price from a list of gas oracles instead of using eth_gasPrice. For usage examples, refer to the GasOracle trait.

The Nonce Manager is used to locally calculate nonces instead of using eth_getTransactionCount

The Policy is used to ensure transactions comply with the rules configured in the PolicyMiddleware before sending them.

The Signer is used to locally sign transactions and messages instead of using eth_sendTransaction and eth_sign

The TimeLag provides safety against reorgs by querying state N blocks before the chain tip

The Transformer is used to intercept transactions and transform them to be sent via various supported transformers, e.g., DSProxy