Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ defer wallet.deinit();
// Create transaction
var tx = zigeth.types.Transaction.newEip1559(allocator);
tx.to = try zigeth.primitives.Address.fromHex("0x...");
tx.value = zigeth.primitives.U256.fromInt(100_000_000_000_000_000); // 0.1 ETH
tx.value = 100_000_000_000_000_000; // 0.1 ETH
tx.nonce = try provider.getTransactionCount(wallet.address);
tx.gas_limit = 21000;
tx.max_fee_per_gas = 30_000_000_000; // 30 gwei
Expand Down
58 changes: 35 additions & 23 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ pub fn build(b: *std.Build) void {
const secp256k1_artifact = secp256k1_dep.artifact("secp256k1");

// Build static library
const lib = b.addStaticLibrary(.{
const lib = b.addLibrary(.{
.name = "zigeth",
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.root_module = zigeth_mod,
});

// Link libc (required for some std library functions)
Expand All @@ -45,9 +43,11 @@ pub fn build(b: *std.Build) void {
// Build executable (CLI tool)
const exe = b.addExecutable(.{
.name = "zigeth",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});

exe.root_module.addImport("zigeth", zigeth_mod);
Expand All @@ -68,21 +68,26 @@ pub fn build(b: *std.Build) void {

// Unit tests for library
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
}),
});

lib_unit_tests.root_module.addImport("secp256k1", secp256k1_mod);
lib_unit_tests.linkLibC();
lib_unit_tests.linkLibrary(secp256k1_artifact);

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

// Unit tests for executable
const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});

exe_unit_tests.root_module.addImport("zigeth", zigeth_mod);
Expand Down Expand Up @@ -146,9 +151,11 @@ pub fn build(b: *std.Build) void {

const example_exe = b.addExecutable(.{
.name = example_name,
.root_source_file = b.path(example_path),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path(example_path),
.target = target,
.optimize = optimize,
}),
});

example_exe.root_module.addImport("zigeth", zigeth_mod);
Expand Down Expand Up @@ -198,12 +205,15 @@ pub fn build(b: *std.Build) void {
lint_step.dependOn(&fmt_check.step);

// 2. Build library with warnings
const lint_lib = b.addStaticLibrary(.{
const lint_lib = b.addLibrary(.{
.name = "zigeth-lint",
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = .Debug,
.root_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = .Debug,
}),
});
lint_lib.root_module.addImport("secp256k1", secp256k1_mod);
lint_lib.linkLibC();

const lint_lib_check = b.addInstallArtifact(lint_lib, .{
Expand All @@ -214,9 +224,11 @@ pub fn build(b: *std.Build) void {
// 3. Build executable with warnings
const lint_exe = b.addExecutable(.{
.name = "zigeth-lint",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = .Debug,
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = .Debug,
}),
});
lint_exe.root_module.addImport("zigeth", zigeth_mod);
lint_exe.linkLibC();
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// To add a new dependency, use: zig fetch --save <url>
.dependencies = .{
.zig_eth_secp256k1 = .{
.url = "https://github.com/jsign/zig-eth-secp256k1/archive/refs/heads/main.tar.gz",
.hash = "zig_eth_secp256k1-0.1.0-_U97sGzoDACkTnLX6ggeDfs_MhZHyxapYfhPPAtoQJJG",
.url = "https://github.com/DaviRain-Su/zig-eth-secp256k1/archive/refs/heads/main.tar.gz",
.hash = "zig_eth_secp256k1-0.1.0-_U97sLbpDAATA_3x8cFERYszQ8DPzDck3ZiffzwQOcnt",
},
},
.paths = .{
Expand Down
10 changes: 5 additions & 5 deletions examples/04_smart_contracts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ pub fn main() !void {
const selectors = zigeth.sol.Selectors;

std.debug.print("✅ Common ERC-20 selectors:\n", .{});
std.debug.print(" balanceOf: 0x{x:0>8}\n", .{selectors.ERC20_BALANCE_OF});
std.debug.print(" transfer: 0x{x:0>8}\n", .{selectors.ERC20_TRANSFER});
std.debug.print(" approve: 0x{x:0>8}\n", .{selectors.ERC20_APPROVE});
std.debug.print(" transferFrom: 0x{x:0>8}\n", .{selectors.ERC20_TRANSFER_FROM});
std.debug.print(" allowance: 0x{x:0>8}\n\n", .{selectors.ERC20_ALLOWANCE});
std.debug.print(" balanceOf: {s}\n", .{selectors.ERC20_BALANCE_OF});
std.debug.print(" transfer: {s}\n", .{selectors.ERC20_TRANSFER});
std.debug.print(" approve: {s}\n", .{selectors.ERC20_APPROVE});
std.debug.print(" transferFrom: {s}\n", .{selectors.ERC20_TRANSFER_FROM});
std.debug.print(" allowance: {s}\n\n", .{selectors.ERC20_ALLOWANCE});
}

// Example 6: ABI encoding patterns
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ var signer = try zigeth.middleware.SignerMiddleware.init(allocator, private_key,
var tx = zigeth.types.Transaction.newEip1559(allocator);
tx.from = from_address;
tx.to = try zigeth.primitives.Address.fromHex(allocator, "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb");
tx.value = zigeth.primitives.U256.fromInt(100_000_000_000_000_000); // 0.1 ETH
tx.value = 100_000_000_000_000_000; // 0.1 ETH
tx.nonce = try provider.getTransactionCount(from_address);
tx.gas_limit = 21000;

Expand Down
8 changes: 4 additions & 4 deletions src/abi/decode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ pub fn decodeFunctionReturn(
output_types: []const types.Parameter,
) ![]types.AbiValue {
var decoder = Decoder.init(allocator, data);
var results = std.ArrayList(types.AbiValue).init(allocator);
defer results.deinit();
var results = try std.ArrayList(types.AbiValue).initCapacity(allocator, 0);
defer results.deinit(allocator);

for (output_types) |param| {
const value = try decodeValue(&decoder, param.type);
try results.append(value);
try results.append(allocator, value);
}

return try results.toOwnedSlice();
return try results.toOwnedSlice(allocator);
}

/// Decode a single value
Expand Down
36 changes: 18 additions & 18 deletions src/abi/encode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ pub const Encoder = struct {
allocator: std.mem.Allocator,
buffer: std.ArrayList(u8),

pub fn init(allocator: std.mem.Allocator) Encoder {
pub fn init(allocator: std.mem.Allocator) !Encoder {
return .{
.allocator = allocator,
.buffer = std.ArrayList(u8).init(allocator),
.buffer = try std.ArrayList(u8).initCapacity(allocator, 0),
};
}

pub fn deinit(self: *Encoder) void {
self.buffer.deinit();
self.buffer.deinit(self.allocator);
}

/// Encode a uint256 value
pub fn encodeUint256(self: *Encoder, value: u256) !void {
const bytes = uint_utils.u256ToBytes(value);
try self.buffer.appendSlice(&bytes);
try self.buffer.appendSlice(self.allocator, &bytes);
}

/// Encode a uint value of any size (padded to 32 bytes)
Expand All @@ -36,21 +36,21 @@ pub const Encoder = struct {
pub fn encodeInt256(self: *Encoder, value: i256) !void {
var bytes: [32]u8 = undefined;
std.mem.writeInt(i256, &bytes, value, .big);
try self.buffer.appendSlice(&bytes);
try self.buffer.appendSlice(self.allocator, &bytes);
}

/// Encode an address (padded to 32 bytes, left-padded with zeros)
pub fn encodeAddress(self: *Encoder, addr: Address) !void {
var bytes: [32]u8 = [_]u8{0} ** 32;
@memcpy(bytes[12..32], &addr.bytes);
try self.buffer.appendSlice(&bytes);
try self.buffer.appendSlice(self.allocator, &bytes);
}

/// Encode a boolean (0 or 1, padded to 32 bytes)
pub fn encodeBool(self: *Encoder, value: bool) !void {
var bytes: [32]u8 = [_]u8{0} ** 32;
bytes[31] = if (value) 1 else 0;
try self.buffer.appendSlice(&bytes);
try self.buffer.appendSlice(self.allocator, &bytes);
}

/// Encode fixed-size bytes (right-padded with zeros to 32 bytes)
Expand All @@ -59,7 +59,7 @@ pub const Encoder = struct {

var bytes: [32]u8 = [_]u8{0} ** 32;
@memcpy(bytes[0..data.len], data);
try self.buffer.appendSlice(&bytes);
try self.buffer.appendSlice(self.allocator, &bytes);
}

/// Encode dynamic bytes (length + data, padded)
Expand All @@ -68,12 +68,12 @@ pub const Encoder = struct {
try self.encodeUint(data.len);

// Encode data with padding
try self.buffer.appendSlice(data);
try self.buffer.appendSlice(self.allocator, data);

// Pad to multiple of 32 bytes
const padding = (32 - (data.len % 32)) % 32;
if (padding > 0) {
try self.buffer.appendNTimes(0, padding);
try self.buffer.appendNTimes(self.allocator, 0, padding);
}
}

Expand All @@ -89,7 +89,7 @@ pub const Encoder = struct {

/// Get owned encoded data
pub fn toOwnedSlice(self: *Encoder) ![]u8 {
return try self.buffer.toOwnedSlice();
return try self.buffer.toOwnedSlice(self.allocator);
}

/// Reset the encoder for reuse
Expand All @@ -108,13 +108,13 @@ pub fn encodeFunctionCall(
return error.ArgumentCountMismatch;
}

var encoder = Encoder.init(allocator);
var encoder = try Encoder.init(allocator);
defer encoder.deinit();

// Add function selector (first 4 bytes)
const selector = try function.getSelector(allocator);
defer allocator.free(selector);
try encoder.buffer.appendSlice(selector);
try encoder.buffer.appendSlice(allocator, selector);

// Encode arguments
for (args, function.inputs) |arg, param| {
Expand Down Expand Up @@ -179,7 +179,7 @@ pub fn padLeft(allocator: std.mem.Allocator, data: []const u8) ![]u8 {
test "encode uint256" {
const allocator = std.testing.allocator;

var encoder = Encoder.init(allocator);
var encoder = try Encoder.init(allocator);
defer encoder.deinit();

const value: u256 = 42;
Expand All @@ -193,7 +193,7 @@ test "encode uint256" {
test "encode address" {
const allocator = std.testing.allocator;

var encoder = Encoder.init(allocator);
var encoder = try Encoder.init(allocator);
defer encoder.deinit();

const addr = Address.fromBytes([_]u8{0x12} ** 20);
Expand All @@ -212,7 +212,7 @@ test "encode address" {
test "encode bool" {
const allocator = std.testing.allocator;

var encoder = Encoder.init(allocator);
var encoder = try Encoder.init(allocator);
defer encoder.deinit();

try encoder.encodeBool(true);
Expand All @@ -230,7 +230,7 @@ test "encode bool" {
test "encode string" {
const allocator = std.testing.allocator;

var encoder = Encoder.init(allocator);
var encoder = try Encoder.init(allocator);
defer encoder.deinit();

try encoder.encodeString("hello");
Expand All @@ -247,7 +247,7 @@ test "encode string" {
test "encode fixed bytes" {
const allocator = std.testing.allocator;

var encoder = Encoder.init(allocator);
var encoder = try Encoder.init(allocator);
defer encoder.deinit();

const data = [_]u8{ 0xde, 0xad, 0xbe, 0xef };
Expand Down
Loading