Raiden1411/zabi
Interact with ethereum and EVM based chains via Zig!
A zig library to interact with EVM blockchains
Zabi aims to add support for interacting with ethereum or any compatible EVM based chain.
Zabi will follow the master branch of ziglang as best as possible.
You can install the latest version of zig here or you can also use a version manager like zvm to manage your zig version.
In the build.zig.zon
file, add the following to the dependencies object.
.zabi = .{
.url = "https://github.com/Raiden1411/zabi/archive/VERSION_NUMBER.tar.gz",
}
The compiler will produce a hash mismatch error, add the .hash
field to build.zig.zon
with the hash the compiler tells you it found.
You can also use zig fetch
to automatically do the above steps.
zig fetch --save https://github.com/Raiden1411/zabi/archive/VERSION_NUMBER.tar.gz
zig fetch --save git+https://github.com/Raiden1411/zabi.git#LATEST_COMMIT
To install zabi with the latest zig version you can install it like so
zig fetch --save git+https://github.com/Raiden1411/zabi.git#zig_version_0.14.0
Zabi will only maintain the latest version available. So when 0.15.0 eventually releases it use that and 0.14.0 will no longer be supported and so on. This should only be until zig reaches 1.0.
Then in your build.zig
file add the following to the exe
section for the executable where you wish to have zabi
available.
const zabi_module = b.dependency("zabi", .{}).module("zabi");
// for exe, lib, tests, etc.
exe.root_module.addImport("zabi", zabi_module);
Now in the code, you can import components like this:
const zabi = @import("zabi");
const meta = zabi.meta;
const encoder = zabi.encoder;
Zabi is a modular library meaning that you can import separete modules if you just need some components of zabi.
const zabi = b.dependency("zabi", .{});
// for exe, lib, tests, etc.
exe.root_module.addImport("zabi-evm", zabi.module("zabi-evm"));
Now in the code, you can import components like this:
const zabi_evm = @import("zabi-evm");
Currently these are all of the modules available for you to use in zabi
:
You can check of the examples in the example/ folder but for a simple introduction you can checkout the bellow example.
const args_parser = @import("zabi").utils.args;
const std = @import("std");
const clients = @import("zabi").clients;
const HttpProvider = clients.Provider.HttpProvider;
const Wallet = clients.Wallet;
const CliOptions = struct {
priv_key: [32]u8,
url: []const u8,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var iter = try std.process.argsWithAllocator(gpa.allocator());
defer iter.deinit();
const parsed = args_parser.parseArgs(CliOptions, gpa.allocator(), &iter);
const uri = try std.Uri.parse(parsed.url);
var provider = try HttpProvider.init(.{
.allocator = gpa.allocator(),
.network_config = .{ .endpoint = .{ .uri = uri } },
});
defer provider.deinit();
var wallet = try Wallet.init(parsed.priv_key, gpa.allocator(), &provider.provider, false);
defer wallet.deinit();
const message = try wallet.signEthereumMessage("Hello World");
const hexed = try message.toHex(wallet.allocator);
defer gpa.allocator().free(hexed);
std.debug.print("Ethereum message: {s}\n", .{hexed});
}
And a lot more yet to come...
The goal of zabi is to be one of the best library to use by the ethereum ecosystem and to expose to more people to the zig programming language.
Contributions to Zabi are greatly appreciated! If you're interested in contributing to ZAbi, feel free to create a pull request with a feature or a bug fix.
You can also read the contributing guide before submitting a pull request
If you find Zabi useful or use it for work, please consider supporting development on GitHub Sponsors or sending crypto to zzabi.eth or interacting with the drip platform where 40% of the revenue gets sent to zabi's dependencies. Thank you 🙏