ethers-rs/book/providers/ipc.md

1.6 KiB

IPC provider

The IPC (Inter-Process Communication) transport allows our program to communicate with a node over a local Unix domain socket or Windows named pipe.

Using the IPC transport allows the ethers library to send JSON-RPC requests to the Ethereum client and receive responses, without the need for a network connection or HTTP server. This can be useful for interacting with a local Ethereum node that is running on the same network. Using IPC is faster than RPC, however you will need to have a local node that you can connect to.

Initializing an Ipc Provider

Below is an example of how to initialize a new Ipc provider.

use ethers::providers::Provider;

#[tokio::main]
async fn main() -> eyre::Result<()> {
    // Using a UNIX domain socket: `/path/to/ipc`
    #[cfg(unix)]
    let provider = Provider::connect_ipc("~/.ethereum/geth.ipc").await?;

    // Using a Windows named pipe: `\\<machine_address>\pipe\<pipe_name>`
    #[cfg(windows)]
    let provider = Provider::connect_ipc(r"\\.\pipe\geth").await?;

    Ok(())
}

Usage

The Ipc provider implements both JsonRpcClient and PubsubClient, just like Ws.

In this example, we monitor the WETH/USDC UniswapV2 pair reserves and print when they have changed.

{{#include ../../examples/providers/examples/ipc.rs}}