1 unstable release

new 0.15.0 Nov 6, 2024

#120 in Magic Beans

Download history 119/week @ 2024-11-04

119 downloads per month
Used in 5 crates (via mwc_wallet_libwallet)

MIT license

420KB
9K SLoC

web3

Ethereum JSON-RPC multi-transport client. Rust implementation of Web3.js library.

Build Status Crates.io

Documentation: crates.io

Usage

First, add this to your Cargo.toml:

[dependencies]
web3 = "0.15.0"

Example

#[tokio::main]
async fn main() -> web3::Result<()> {
    let transport = web3::transports::Http::new("http://localhost:8545")?;
    let web3 = web3::Web3::new(transport);

    println!("Calling accounts.");
    let mut accounts = web3.eth().accounts().await?;
    println!("Accounts: {:?}", accounts);
    accounts.push("00a329c0648769a73afac7f9381e08fb43dbea72".parse().unwrap());

    println!("Calling balance.");
    for account in accounts {
        let balance = web3.eth().balance(account, None).await?;
        println!("Balance of {:?}: {}", account, balance);
    }

    Ok(())
}

If you want to deploy smart contracts you have written you can do something like this (make sure you have the solidity compiler installed):

solc -o build --bin --abi contracts/*.sol

The solidity compiler is generating the binary and abi code for the smart contracts in a directory called contracts and is being output to a directory called build.

For more see examples folder.

Opt-out Features

  • http - Enables HTTP transport (requires tokio runtime, because of hyper).
  • http-tls - Enables TLS support for HTTP transport (implies http).
  • ws - Enables WS transport.
  • ws-tls - Enables TLS support for WS transport (implies ws).

Futures migration

  • Get rid of parking_lot (replace with async-aware locks if really needed).
  • Consider getting rid of Unpin requirements. (#361)
  • WebSockets: TLS support (#360)
  • WebSockets: Reconnecting & Pings
  • Consider using tokio instead of async-std for ws.rs transport (issue with test).
  • Restore IPC Transport

General

  • More flexible API (accept Into<X>)
  • Contract calls (ABI encoding; debris/ethabi)
  • Batch Requests

Transports

  • HTTP transport
  • IPC transport
  • WebSockets transport

Types

  • Types for U256,H256,Address(H160)
  • Index type (numeric, encoded to hex)
  • Transaction type (Transaction from Parity)
  • Transaction receipt type (TransactionReceipt from Parity)
  • Block type (RichBlock from Parity)
  • Work type (Work from Parity)
  • Syncing type (SyncStats from Parity)

APIs

  • Eth: eth_*
  • Eth filters: eth_*
  • Eth pubsub: eth_*
  • net_*
  • web3_*
  • personal_*
  • traces_*

Parity-specific APIs

  • Parity read-only: parity_*

  • Parity accounts: parity_* (partially implemented)

  • Parity set: parity_*

  • signer_*

  • Own APIs (Extendable)

let web3 = Web3::new(transport);
web3.api::<CustomNamespace>().custom_method().wait().unwrap()

Installation on Windows

Currently, Windows does not support IPC, which is enabled in the library by default. To compile, you need to disable the IPC feature:

web3 = { version = "0.14.0", default-features = false, features = ["http"] }

Cargo Features

The library supports following features:

  • http - Enables http transport.
  • http-tls - Enables http over TLS (https) transport support. Implies http.
  • ipc-tokio - Enables ipc transport (tokio runtime). *NIX only!
  • ws-tokio - Enables ws tranport (tokio runtime).
  • ws-tls-tokio - Enables wss tranport (tokio runtime).
  • ws-async-std - Enables ws tranport (async-std runtime).
  • ws-tls-async-std - Enables wss tranport (async-std runtime).

By default http-tls and ws-tls-tokio are enabled.

Dependencies

~4–20MB
~271K SLoC