eth.zig

eth.zig

The fastest Ethereum library, in pure Zig.

The fastest Ethereum library.

eth.zig provides everything you need to interact with Ethereum from Zig -- signing transactions, encoding ABI calls, managing HD wallets, reading ERC-20 tokens, talking to nodes over JSON-RPC, and more.

Why eth.zig?

  • Fast, and kept fast -- vendored bitcoin-core secp256k1, XKCP Keccak, limb-native u256 math and allocation-light ABI/RLP codecs, with ten hot-path benchmarks tracked release over release.
  • Comptime-first -- Function selectors and event topics are computed at compile time with zero runtime cost. The compiler does the hashing so your program doesn't have to.

Quick Start

const std = @import("std");
const eth = @import("eth");

pub fn main() !void {
    var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    // WARNING: This is the default Anvil/Hardhat test key. Never use it for real funds.
    const private_key = try eth.hex.hexToBytesFixed(32, "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80");
    const signer = eth.signer.Signer.init(private_key);
    const addr = try signer.address();
    const checksum = eth.primitives.addressToChecksum(&addr);
    _ = checksum;

    // Sign and send a transaction
    var transport = eth.http_transport.HttpTransport.init(allocator, "https://rpc.example.com", eth.runtime.blockingIo());
    defer transport.deinit();
    var provider = eth.provider.Provider.init(allocator, &transport);

    const recipient_address = try eth.hex.hexToBytesFixed(20, "0000000000000000000000000000000000000000");
    var wallet = eth.wallet.Wallet.initLocal(allocator, private_key, &provider);
    const tx_hash = try wallet.sendTransaction(.{
        .to = recipient_address,
        .value = eth.units.parseEther(1.0),
    });
    _ = tx_hash;
}

Guides

Resources

On this page