Perp CityDocs

eth.zig

The Ethereum library for Zig -- comptime-first ABI encoding, signing, JSON-RPC, and wallets, with no package dependencies.

The Ethereum library for Zig. eth.zig covers what a Zig program needs to talk to Ethereum: ABI encoding and decoding, RLP, transaction signing, Keccak-256, HD wallets, EIP-712, JSON-RPC over HTTP and WebSocket, ERC-20/721, ENS, and more. It is the Ethereum client under the PerpCity Zig SDK.

The current release is v0.10.0, which requires Zig 0.17.0. Full documentation, the module reference and runnable examples live at ethzig.org.

Why eth.zig?

  • Comptime-first -- function selectors and event topics are computed at compile time.
  • No package dependencies -- build.zig.zon declares none. Performance-critical cryptography uses vendored C (libsecp256k1, blst, c-kzg-4844), compiled from source by zig build, with no system libraries to install.
  • Fast -- benchmarked against alloy.rs; see the benchmark results.

Example

Read the chain, compute a selector at compile time, and derive a wallet address:

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

pub fn main() !void {
    const allocator = std.heap.page_allocator;

    var transport = eth.http_transport.HttpTransport.init(allocator, "https://arb1.arbitrum.io/rpc", eth.runtime.blockingIo());
    defer transport.deinit();
    var provider = eth.provider.Provider.init(allocator, &transport);

    const block = try provider.getBlockNumber();
    const holder = try eth.primitives.addressFromHex("0xaf88d065e77c8cC2239327C5EDb3A432268e5831");
    const balance = try provider.getBalance(holder);
    std.debug.print("block {d}, balance {d} wei\n", .{ block, balance });

    const transfer_sel = comptime eth.keccak.selector("transfer(address,uint256)"); // a9059cbb
    std.debug.print("transfer selector {x}\n", .{transfer_sel});

    const private_key = try eth.hex.hexToBytesFixed(32, "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"); // well-known test key
    var wallet = eth.wallet.Wallet.initLocal(allocator, private_key, &provider);
    const addr = try wallet.address();
    std.debug.print("wallet {s}\n", .{eth.primitives.addressToChecksum(&addr)});
}

See Installation to add it to a project.

Resources

On this page