pub struct Provider<P> { /* private fields */ }
Expand description

An abstract provider for interacting with the Ethereum JSON RPC API. Must be instantiated with a data transport which implements the JsonRpcClient trait (e.g. HTTP, Websockets etc.)

Example

use ethers_providers::{Middleware, Provider, Http};
use std::convert::TryFrom;

let provider = Provider::<Http>::try_from(
    "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27"
).expect("could not instantiate HTTP Provider");

let block = provider.get_block(100u64).await?;
println!("Got block: {}", serde_json::to_string(&block)?);

Implementations§

Instantiate a new provider with a backend.

Returns the type of node we’re connected to, while also caching the value for use in other node-specific API calls, such as the get_block_receipts call.

Analogous to Middleware::call, but returns a CallBuilder that can either be .awaitd or used to override the parameters sent to eth_call.

See the call_raw::spoof for functions to construct state override parameters.

Note: this method does not send a transaction from your account

Example
let geth = Geth::new().spawn();
let provider = Provider::<Http>::try_from(geth.endpoint()).unwrap();

let adr1: Address = "0x6fC21092DA55B392b045eD78F4732bff3C580e2c".parse()?;
let adr2: Address = "0x295a70b2de5e3953354a6a8344e616ed314d7251".parse()?;
let pay_amt = parse_ether(1u64)?;

// Not enough ether to pay for the transaction
let tx = TransactionRequest::pay(adr2, pay_amt).from(adr1).into();

// override the sender's balance for the call
let mut state = spoof::balance(adr1, pay_amt * 2);
provider.call_raw(&tx).state(&state).await?;

Sets the ENS Address (default: mainnet)

Sets the default polling interval for event filters and pending transactions (default: 7 seconds)

Sets the default polling interval for event filters and pending transactions (default: 7 seconds)

Gets the polling interval which the provider currently uses for event filters and pending transactions (default: 7 seconds)

Direct connection to a websocket endpoint

Direct connection to an IPC socket.

The Url to which requests are made

Mutable access to the Url to which requests are made

Creates a new Provider with a RwClient

Provider that uses a quorum

Returns a Provider instantiated with an internal “mock” transport.

Example
use ethers_core::types::U64;
use ethers_providers::{Middleware, Provider};
// Instantiate the provider
let (provider, mock) = Provider::mocked();
// Push the mock response
mock.push(U64::from(12))?;
// Make the call
let blk = provider.get_block_number().await.unwrap();
// The response matches
assert_eq!(blk.as_u64(), 12);
// and the request as well!
mock.assert_request("eth_blockNumber", ()).unwrap();

Trait Implementations§

Converts this type into a shared reference of the (usually inferred) input type.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Returns the current client version using the web3_clientVersion RPC.

Gets the latest block number via the eth_BlockNumber API

Gets the block at block_hash_or_number (transaction hashes only)

Gets the block at block_hash_or_number (full transactions included)

Gets the block uncle count at block_hash_or_number

Gets the block uncle at block_hash_or_number and idx

Gets the transaction with transaction_hash

Gets the transaction receipt with transaction_hash

Returns all receipts for a block.

Note that this uses the eth_getBlockReceipts RPC, which is non-standard and currently supported by Erigon.

Returns all receipts for that block. Must be done on a parity node.

Gets the current gas price as estimated by the node

Gets a heuristic recommendation of max fee per gas and max priority fee per gas for EIP-1559 compatible transactions.

Gets the accounts on the node

Returns the nonce of the address

Returns the account’s balance

Returns the currently configured chain id, a value used in replay-protected transaction signing as introduced by EIP-155.

Return current client syncing status. If IsFalse sync is over.

Returns the network version.

Sends the read-only (constant) transaction to a single Ethereum node and return the result (as bytes) of executing it. This is free, since it does not change any state on the blockchain.

Sends a transaction to a single Ethereum node and return the estimated amount of gas required (as a U256) to send it This is free, but only an estimate. Providing too little gas will result in a transaction being rejected (while still consuming all provided gas).

Sends the transaction to the entire Ethereum network and returns the transaction’s hash This will consume gas from the account that signed the transaction.

Send the raw RLP encoded transaction to the entire Ethereum network and returns the transaction’s hash This will consume gas from the account that signed the transaction.

The JSON-RPC provider is at the bottom-most position in the middleware stack. Here we check if it has the key for the sender address unlocked, as well as supports the eth_sign call.

Signs data using a specific account. This account needs to be unlocked.

Sign a transaction via RPC call

Returns an array (possibly empty) of logs that match the filter

Streams matching filter logs

Streams new block hashes

Streams pending transactions

Creates a filter object, based on filter options, to notify when the state changes (logs). To check if the state has changed, call get_filter_changes with the filter id.

Uninstalls a filter

Polling method for a filter, which returns an array of logs which occurred since last poll.

This method must be called with one of the following return types, depending on the filter type:

  • eth_newBlockFilter: H256, returns block hashes
  • eth_newPendingTransactionFilter: H256, returns transaction hashes
  • eth_newFilter: Log, returns raw logs

If one of these types is not used, decoding will fail and the method will return an error.

Get the storage of an address for a particular slot location

Returns the deployed code at a given address

Returns the EIP-1186 proof response https://github.com/ethereum/EIPs/issues/1186

Returns the address that the ens_name resolves to (or None if not configured).

Panics

If the bytes returned from the ENS registrar/resolver cannot be interpreted as an address. This should theoretically never happen.

Returns the ENS name the address resolves to (or None if not configured).

Panics

If the bytes returned from the ENS registrar/resolver cannot be interpreted as a string. This should theoretically never happen.

Returns the avatar HTTP link of the avatar that the ens_name resolves to (or None if not configured)

Example
let avatar = provider.resolve_avatar("parishilton.eth").await.unwrap();
assert_eq!(avatar.to_string(), "https://i.imgur.com/YW3Hzph.jpg");
Panics

If the bytes returned from the ENS registrar/resolver cannot be interpreted as a string. This should theoretically never happen.

Returns the URL (not necesserily HTTP) of the image behind a token.

Example
let token = ethers_providers::erc::ERCNFT::from_str("erc721:0xc92ceddfb8dd984a89fb494c376f9a48b999aafc/9018").unwrap();
let token_image = provider.resolve_nft(token).await.unwrap();
assert_eq!(token_image.to_string(), "https://creature.mypinata.cloud/ipfs/QmNwj3aUzXfG4twV3no7hJRYxLLAWNPk6RrfQaqJ6nVJFa/9018.jpg");
Panics

If the bytes returned from the ENS registrar/resolver cannot be interpreted as a string. This should theoretically never happen.

Fetch a field for the ens_name (no None if not configured).

Panics

If the bytes returned from the ENS registrar/resolver cannot be interpreted as a string. This should theoretically never happen.

Returns the details of all transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future execution only. Ref: Here

Returns a summary of all the transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future execution only. Ref: Here

Returns the number of transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future execution only. Ref: Here

Executes the given call and returns a number of possible traces for it

Executes the given call and returns a number of possible traces for it

Executes given calls and returns a number of possible traces for each call

Traces a call to eth_sendRawTransaction without making the call, returning the traces

Replays a transaction, returning the traces

Replays all transactions in a block returning the requested traces for each transaction

Returns traces created at given block

Return traces matching the given filter

Returns trace at the given position

Returns all traces of a given transaction

The next middleware in the stack
The HTTP or Websocket provider.
Fill necessary details of a transaction for dispatch Read more
Returns a stream of logs are loaded in pages of given page size
Send a transaction with a simple escalation policy. Read more
The error type that can occur when creating a provider
Try to create a new Provider
Customized Provider settings for chain
Creates a new instance connected to the given url, exit on error
Customize Provider settings for chain. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca) Read more
Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA) Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more