Skip to content
Merged
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
6 changes: 3 additions & 3 deletions examples/02_query_blockchain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn main() !void {
defer allocator.free(addr_hex);

std.debug.print("βœ… Address: {s}\n", .{addr_hex});
std.debug.print(" Balance: {} wei\n", .{balance});
std.debug.print(" Balance: {d} wei\n", .{balance});

// Convert to ether
const ether = try zigeth.utils.units.weiToEther(balance);
Expand All @@ -68,7 +68,7 @@ pub fn main() !void {
{
const gas_price = try provider.getGasPrice();

std.debug.print("βœ… Gas price: {} wei\n", .{gas_price});
std.debug.print("βœ… Gas price: {d} wei\n", .{gas_price});

// Convert to gwei
const gas_u64: u64 = @intCast(gas_price / 1_000_000_000);
Expand All @@ -94,7 +94,7 @@ pub fn main() !void {
std.debug.print(" Transactions: {d}\n", .{block.transactions.len});

if (block.header.base_fee_per_gas) |base_fee| {
std.debug.print(" Base fee: {} wei\n", .{base_fee});
std.debug.print(" Base fee: {d} wei\n", .{base_fee});
}
std.debug.print("\n", .{});
}
Expand Down
10 changes: 5 additions & 5 deletions examples/03_send_transaction.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ pub fn main() !void {

std.debug.print("βœ… Created legacy transaction\n", .{});
std.debug.print(" To: {any}\n", .{tx.to.?});
std.debug.print(" Value: {} wei\n", .{tx.value});
std.debug.print(" Value: {d} wei\n", .{tx.value});
std.debug.print(" Gas limit: {d}\n", .{tx.gas_limit});
std.debug.print(" Gas price: {?} wei\n\n", .{tx.gas_price});
std.debug.print(" Gas price: {d} wei\n\n", .{tx.gas_price.?});
}

// Example 2: Create EIP-1559 transaction
Expand All @@ -90,8 +90,8 @@ pub fn main() !void {

std.debug.print("βœ… Created EIP-1559 transaction\n", .{});
std.debug.print(" Type: EIP-1559\n", .{});
std.debug.print(" Max fee: {?} wei\n", .{tx.max_fee_per_gas});
std.debug.print(" Priority fee: {?} wei\n\n", .{tx.max_priority_fee_per_gas});
std.debug.print(" Max fee: {d} wei\n", .{tx.max_fee_per_gas.?});
std.debug.print(" Priority fee: {d} wei\n\n", .{tx.max_priority_fee_per_gas.?});
}

// Example 3: Using Middleware for Automatic Gas & Nonce
Expand Down Expand Up @@ -139,7 +139,7 @@ pub fn main() !void {
std.debug.print("βœ… Transaction configured with middleware\n", .{});
std.debug.print(" Nonce: {d} (auto-managed)\n", .{tx.nonce});
std.debug.print(" Gas limit: {d}\n", .{tx.gas_limit});
std.debug.print(" Max fee: {?}\n", .{tx.max_fee_per_gas});
std.debug.print(" Max fee: {any}\n", .{tx.max_fee_per_gas});

// Check if we have sufficient balance
const has_balance = try gas_middleware.checkSufficientBalance(
Expand Down
2 changes: 1 addition & 1 deletion examples/05_transaction_receipts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn main() !void {

std.debug.print(" Gas used: {d}\n", .{gas_used});
std.debug.print(" Gas price: 50 gwei\n", .{});
std.debug.print(" Total fee: {} wei\n", .{fee});
std.debug.print(" Total fee: {d} wei\n", .{fee});

// Convert to ETH (simple cast since we know it's a small value)
const fee_u64: u64 = @intCast(fee); // Safe cast for this example
Expand Down
4 changes: 2 additions & 2 deletions examples/07_complete_workflow.zig
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ pub fn main() !void {
std.debug.print(" if (receipt.isSuccess()) {{\n", .{});
std.debug.print(" std.debug.print(\"βœ… Transaction successful!\\n\", .{{}});\n", .{});
std.debug.print(" std.debug.print(\" Block: {{}}\\n\", .{{receipt.block_number}});\n", .{});
std.debug.print(" std.debug.print(\" Gas used: {{}}\\n\", .{{receipt.gas_used}});\n\n", .{});
std.debug.print(" std.debug.print(\" Gas used: {{d}}\\n\", .{{receipt.gas_used}});\n\n", .{});

std.debug.print(" // Calculate fee\n", .{});
std.debug.print(" const fee = receipt.calculateFee();\n", .{});
std.debug.print(" std.debug.print(\" Fee: {{}} wei\\n\", .{{fee}});\n\n", .{});
std.debug.print(" std.debug.print(\" Fee: {{d}} wei\\n\", .{{fee}});\n\n", .{});

std.debug.print(" // Remove from pending\n", .{});
std.debug.print(" nonce.removePendingTx(tx.from, tx.nonce);\n", .{});
Expand Down
61 changes: 35 additions & 26 deletions src/abi/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -93,42 +93,51 @@ pub const AbiType = union(enum) {
}

/// Convert AbiType to string representation for selector generation
pub fn toString(self: AbiType, allocator: std.mem.Allocator) ![]u8 {
/// For simple types, returns const string literals (no allocation)
/// For complex types (arrays), allocates and caller must free
pub fn toStringAlloc(self: AbiType, allocator: std.mem.Allocator) ![]const u8 {
return switch (self) {
.uint8 => try allocator.dupe(u8, "uint8"),
.uint16 => try allocator.dupe(u8, "uint16"),
.uint32 => try allocator.dupe(u8, "uint32"),
.uint64 => try allocator.dupe(u8, "uint64"),
.uint128 => try allocator.dupe(u8, "uint128"),
.uint256 => try allocator.dupe(u8, "uint256"),
.int8 => try allocator.dupe(u8, "int8"),
.int16 => try allocator.dupe(u8, "int16"),
.int32 => try allocator.dupe(u8, "int32"),
.int64 => try allocator.dupe(u8, "int64"),
.int128 => try allocator.dupe(u8, "int128"),
.int256 => try allocator.dupe(u8, "int256"),
.address => try allocator.dupe(u8, "address"),
.bool_type => try allocator.dupe(u8, "bool"),
.bytes1 => try allocator.dupe(u8, "bytes1"),
.bytes2 => try allocator.dupe(u8, "bytes2"),
.bytes4 => try allocator.dupe(u8, "bytes4"),
.bytes8 => try allocator.dupe(u8, "bytes8"),
.bytes16 => try allocator.dupe(u8, "bytes16"),
.bytes32 => try allocator.dupe(u8, "bytes32"),
.string => try allocator.dupe(u8, "string"),
.bytes => try allocator.dupe(u8, "bytes"),
// Return const string literals - NO allocation needed!
.uint8 => "uint8",
.uint16 => "uint16",
.uint32 => "uint32",
.uint64 => "uint64",
.uint128 => "uint128",
.uint256 => "uint256",
.int8 => "int8",
.int16 => "int16",
.int32 => "int32",
.int64 => "int64",
.int128 => "int128",
.int256 => "int256",
.address => "address",
.bool_type => "bool",
.bytes1 => "bytes1",
.bytes2 => "bytes2",
.bytes4 => "bytes4",
.bytes8 => "bytes8",
.bytes16 => "bytes16",
.bytes32 => "bytes32",
.string => "string",
.bytes => "bytes",
// Complex types need allocation
.array => |arr| {
const elem_str = try arr.element_type.toString(allocator);
defer allocator.free(elem_str);
const elem_str = try arr.element_type.toStringAlloc(allocator);
// Only free if it was actually allocated (for nested arrays)
if (arr.length) |len| {
return try std.fmt.allocPrint(allocator, "{s}[{d}]", .{ elem_str, len });
} else {
return try std.fmt.allocPrint(allocator, "{s}[]", .{elem_str});
}
},
.tuple => try allocator.dupe(u8, "tuple"),
.tuple => "tuple",
};
}

/// Legacy name for backwards compatibility
pub fn toString(self: AbiType, allocator: std.mem.Allocator) ![]const u8 {
return self.toStringAlloc(allocator);
}
};

/// ABI encoded value
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/gas.zig
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ pub const GasMiddleware = struct {
_ = base_fee; // Reserved for future use

// Try to get max priority fee from provider
const suggested_priority = self.provider.eth.maxPriorityFeePerGas() catch {
var eth = self.provider.getEth();
const suggested_priority = eth.maxPriorityFeePerGas() catch {
// Fallback: 2.5 gwei for standard, adjust for strategy
return @as(u256, 2_500_000_000);
};
Expand Down
22 changes: 11 additions & 11 deletions src/providers/http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub const HttpProvider = struct {
}

/// Free allocated memory
pub fn deinit(self: HttpProvider) void {
pub fn deinit(self: *HttpProvider) void {
self.provider.deinit();
}

Expand All @@ -22,39 +22,39 @@ pub const HttpProvider = struct {
}

/// Common provider methods (convenience wrappers)
pub fn getBlockNumber(self: HttpProvider) !u64 {
pub fn getBlockNumber(self: *HttpProvider) !u64 {
return try self.provider.getBlockNumber();
}

pub fn getBalance(self: HttpProvider, address: @import("../primitives/address.zig").Address) !u256 {
pub fn getBalance(self: *HttpProvider, address: @import("../primitives/address.zig").Address) !u256 {
return try self.provider.getBalance(address);
}

pub fn getChainId(self: HttpProvider) !u64 {
pub fn getChainId(self: *HttpProvider) !u64 {
return try self.provider.getChainId();
}

pub fn getTransactionCount(self: HttpProvider, address: @import("../primitives/address.zig").Address) !u64 {
pub fn getTransactionCount(self: *HttpProvider, address: @import("../primitives/address.zig").Address) !u64 {
return try self.provider.getTransactionCount(address);
}

pub fn getGasPrice(self: HttpProvider) !u256 {
pub fn getGasPrice(self: *HttpProvider) !u256 {
return try self.provider.getGasPrice();
}

pub fn getLatestBlock(self: HttpProvider) !@import("../types/block.zig").Block {
pub fn getLatestBlock(self: *HttpProvider) !@import("../types/block.zig").Block {
return try self.provider.getLatestBlock();
}

pub fn getTransaction(self: HttpProvider, hash: @import("../primitives/hash.zig").Hash) !@import("../types/transaction.zig").Transaction {
pub fn getTransaction(self: *HttpProvider, hash: @import("../primitives/hash.zig").Hash) !@import("../types/transaction.zig").Transaction {
return try self.provider.getTransaction(hash);
}

pub fn getTransactionReceipt(self: HttpProvider, hash: @import("../primitives/hash.zig").Hash) !@import("../types/receipt.zig").Receipt {
pub fn getTransactionReceipt(self: *HttpProvider, hash: @import("../primitives/hash.zig").Hash) !@import("../types/receipt.zig").Receipt {
return try self.provider.getTransactionReceipt(hash);
}

pub fn sendTransaction(self: HttpProvider, signed_tx: []const u8) !@import("../primitives/hash.zig").Hash {
pub fn sendTransaction(self: *HttpProvider, signed_tx: []const u8) !@import("../primitives/hash.zig").Hash {
return try self.provider.sendTransaction(signed_tx);
}

Expand All @@ -66,7 +66,7 @@ pub const HttpProvider = struct {
return try self.provider.waitForTransaction(tx_hash, timeout_ms, 1000);
}

pub fn isContract(self: HttpProvider, address: @import("../primitives/address.zig").Address) !bool {
pub fn isContract(self: *HttpProvider, address: @import("../primitives/address.zig").Address) !bool {
return try self.provider.isContract(address);
}
};
Expand Down
Loading
Loading