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
2 changes: 0 additions & 2 deletions examples/01_wallet_creation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/// - Generate and use mnemonic phrases
/// - Create HD wallets
/// - Encrypt and store wallets

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

Expand Down Expand Up @@ -163,4 +162,3 @@ pub fn main() !void {

std.debug.print("🎉 All wallet examples completed!\n\n", .{});
}

4 changes: 2 additions & 2 deletions examples/02_query_blockchain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ pub fn main() !void {
std.debug.print("✅ Gas price: {} wei\n", .{gas_price});

// Convert to gwei
const gas_u64 = gas_price.toU64();
const gwei = @as(f64, @floatFromInt(gas_u64)) / 1_000_000_000.0;
const gas_u64: u64 = @intCast(gas_price / 1_000_000_000);
const gwei = @as(f64, @floatFromInt(gas_u64));
std.debug.print(" Gas price: {d:.2} gwei\n\n", .{gwei});
}

Expand Down
22 changes: 11 additions & 11 deletions examples/03_send_transaction.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ pub fn main() !void {
var tx = zigeth.types.Transaction.newLegacy(
allocator,
to_address,
zigeth.primitives.U256.fromInt(1_000_000_000_000_000), // 0.001 ETH
@as(u256, 1_000_000_000_000_000), // 0.001 ETH
empty_data,
0, // nonce
21000, // gas_limit
zigeth.primitives.U256.fromInt(20_000_000_000), // 20 gwei gas_price
@as(u256, 20_000_000_000), // 20 gwei gas_price
);

tx.from = from_address;
Expand All @@ -76,12 +76,12 @@ pub fn main() !void {
var tx = zigeth.types.Transaction.newEip1559(
allocator,
to_address,
zigeth.primitives.U256.fromInt(1_000_000_000_000_000), // 0.001 ETH
@as(u256, 1_000_000_000_000_000), // 0.001 ETH
empty_data,
1, // nonce
21000, // gas_limit
zigeth.primitives.U256.fromInt(50_000_000_000), // 50 gwei max_fee
zigeth.primitives.U256.fromInt(2_000_000_000), // 2 gwei priority_fee
@as(u256, 50_000_000_000), // 50 gwei max_fee
@as(u256, 2_000_000_000), // 2 gwei priority_fee
11155111, // chain_id (Sepolia)
null, // access_list
);
Expand Down Expand Up @@ -122,12 +122,12 @@ pub fn main() !void {
var tx = zigeth.types.Transaction.newEip1559(
allocator,
to_address,
zigeth.primitives.U256.fromInt(1_000_000_000_000_000), // 0.001 ETH
@as(u256, 1_000_000_000_000_000), // 0.001 ETH
empty_data,
nonce,
21000, // initial gas_limit (will be estimated)
zigeth.primitives.U256.fromInt(50_000_000_000), // temp values
zigeth.primitives.U256.fromInt(2_000_000_000),
@as(u256, 50_000_000_000), // temp values
@as(u256, 2_000_000_000),
11155111, // Sepolia
null,
);
Expand Down Expand Up @@ -160,12 +160,12 @@ pub fn main() !void {
var tx = zigeth.types.Transaction.newEip1559(
allocator,
to_address,
zigeth.primitives.U256.fromInt(1_000_000_000_000_000), // 0.001 ETH
@as(u256, 1_000_000_000_000_000), // 0.001 ETH
empty_data,
0, // nonce
21000, // gas_limit
zigeth.primitives.U256.fromInt(50_000_000_000), // 50 gwei max_fee
zigeth.primitives.U256.fromInt(2_000_000_000), // 2 gwei priority_fee
@as(u256, 50_000_000_000), // 50 gwei max_fee
@as(u256, 2_000_000_000), // 2 gwei priority_fee
11155111, // Sepolia
null, // access_list
);
Expand Down
10 changes: 4 additions & 6 deletions examples/05_transaction_receipts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/// - Parse transaction data
/// - Calculate transaction fees
/// - Filter logs from receipts

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

Expand Down Expand Up @@ -75,16 +74,16 @@ pub fn main() !void {

// Simulated receipt data
const gas_used: u64 = 21000;
const gas_price = zigeth.primitives.U256.fromInt(50_000_000_000); // 50 gwei
const gas_price: u256 = 50_000_000_000; // 50 gwei

const fee = gas_price.mulScalar(gas_used);
const fee = gas_price * gas_used;

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});

// Convert to ETH
const fee_u64 = fee.toU64();
// Convert to ETH (simple cast since we know it's a small value)
const fee_u64: u64 = @intCast(fee); // Safe cast for this example
const fee_eth = @as(f64, @floatFromInt(fee_u64)) / 1_000_000_000_000_000_000.0;
std.debug.print(" Total fee: {d:.6} ETH\n\n", .{fee_eth});
}
Expand Down Expand Up @@ -179,4 +178,3 @@ pub fn main() !void {
std.debug.print("🎉 All receipt examples completed!\n", .{});
std.debug.print("💡 Tip: Always check receipt.status before considering TX successful\n\n", .{});
}

7 changes: 1 addition & 6 deletions examples/06_event_monitoring.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/// - Filter and parse event logs
/// - Track specific contract events
/// - Use real-time subscriptions

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

Expand Down Expand Up @@ -75,10 +74,7 @@ pub fn main() !void {
std.debug.print("───────────────────────────────────────\n", .{});
{
// USDC contract - simple string literal!
const usdc_address = try zigeth.primitives.Address.fromHex(

"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
);
const usdc_address = try zigeth.primitives.Address.fromHex("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");

const addr_hex = try usdc_address.toHex(allocator);
defer allocator.free(addr_hex);
Expand Down Expand Up @@ -183,4 +179,3 @@ pub fn main() !void {
std.debug.print("🎉 All event monitoring examples completed!\n", .{});
std.debug.print("💡 Tip: Use WebSocket for real-time, HTTP for historical data\n\n", .{});
}

8 changes: 4 additions & 4 deletions examples/07_complete_workflow.zig
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ pub fn main() !void {
var tx = zigeth.types.Transaction.newEip1559(
allocator,
to,
zigeth.primitives.U256.fromInt(100_000_000_000_000_000), // 0.1 ETH
@as(u256, 100_000_000_000_000_000), // 0.1 ETH
empty_data,
0, // nonce (would be set by middleware)
21000, // gas_limit
zigeth.primitives.U256.fromInt(50_000_000_000), // max_fee
zigeth.primitives.U256.fromInt(2_000_000_000), // priority_fee
@as(u256, 50_000_000_000), // max_fee
@as(u256, 2_000_000_000), // priority_fee
11155111, // Sepolia
null, // access_list
);
Expand Down Expand Up @@ -271,7 +271,7 @@ pub fn main() !void {
std.debug.print(" • zigeth.providers (Networks, HTTP, WebSocket)\n", .{});
std.debug.print(" • zigeth.middleware (Gas, Nonce, Signer)\n", .{});
std.debug.print(" • zigeth.types (Transaction, Receipt)\n", .{});
std.debug.print(" • zigeth.primitives (Address, Hash, U256)\n", .{});
std.debug.print(" • zigeth.primitives (Address, Hash, native u256)\n", .{});
std.debug.print(" • zigeth.utils (Units, Hex, Format)\n\n", .{});

std.debug.print("Benefits of using zigeth:\n", .{});
Expand Down
14 changes: 7 additions & 7 deletions src/abi/decode.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const Address = @import("../primitives/address.zig").Address;
const U256 = @import("../primitives/uint.zig").U256;
const types = @import("./types.zig");
const uint_utils = @import("../primitives/uint.zig");

/// ABI decoder for Ethereum smart contract responses
pub const Decoder = struct {
Expand All @@ -28,17 +28,17 @@ pub const Decoder = struct {
}

/// Decode a uint256
pub fn decodeUint256(self: *Decoder) !U256 {
pub fn decodeUint256(self: *Decoder) !u256 {
const bytes = try self.read32();
var arr: [32]u8 = undefined;
@memcpy(&arr, bytes);
return U256.fromBytes(arr);
return uint_utils.u256FromBytes(arr);
}

/// Decode a uint of any size
/// Alias for decodeUint256 for clarity
pub fn decodeUint(self: *Decoder) !u256 {
const bytes = try self.read32();
return std.mem.readInt(u256, bytes[0..32], .big);
return self.decodeUint256();
}

/// Decode an address
Expand Down Expand Up @@ -148,7 +148,7 @@ test "decode uint256" {
var decoder = Decoder.init(allocator, &data);
const value = try decoder.decodeUint256();

try std.testing.expect(value.eql(U256.fromInt(42)));
try std.testing.expectEqual(@as(u256, 42), value);
}

test "decode address" {
Expand Down Expand Up @@ -206,7 +206,7 @@ test "decode multiple values" {
var decoder = Decoder.init(allocator, &data);

const val1 = try decoder.decodeUint256();
try std.testing.expect(val1.eql(U256.fromInt(100)));
try std.testing.expectEqual(@as(u256, 100), val1);

const val2 = try decoder.decodeAddress();
try std.testing.expectEqual(@as(u8, 0xAB), val2.bytes[0]);
Expand Down
13 changes: 6 additions & 7 deletions src/abi/encode.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const std = @import("std");
const Address = @import("../primitives/address.zig").Address;
const U256 = @import("../primitives/uint.zig").U256;
const Bytes = @import("../primitives/bytes.zig").Bytes;
const types = @import("./types.zig");
const uint_utils = @import("../primitives/uint.zig");

/// ABI encoder for Ethereum smart contract calls
pub const Encoder = struct {
Expand All @@ -21,16 +21,15 @@ pub const Encoder = struct {
}

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

/// Encode a uint value of any size (padded to 32 bytes)
/// Alias for encodeUint256 for clarity
pub fn encodeUint(self: *Encoder, value: u256) !void {
var bytes: [32]u8 = undefined;
std.mem.writeInt(u256, &bytes, value, .big);
try self.buffer.appendSlice(&bytes);
try self.encodeUint256(value);
}

/// Encode an int256 value
Expand Down Expand Up @@ -183,7 +182,7 @@ test "encode uint256" {
var encoder = Encoder.init(allocator);
defer encoder.deinit();

const value = U256.fromInt(42);
const value: u256 = 42;
try encoder.encodeUint256(value);

const result = encoder.toSlice();
Expand Down
17 changes: 8 additions & 9 deletions src/abi/packed.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const Address = @import("../primitives/address.zig").Address;
const U256 = @import("../primitives/uint.zig").U256;
const Hash = @import("../primitives/hash.zig").Hash;
const uint_utils = @import("../primitives/uint.zig");

/// Packed ABI encoding (tightly packed, no padding)
/// Used for hashing and signature generation (e.g., EIP-712)
Expand All @@ -21,17 +21,16 @@ pub const PackedEncoder = struct {
}

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

/// Encode a uint of specific size
pub fn encodeUint(self: *PackedEncoder, value: u256, size_bytes: usize) !void {
if (size_bytes > 32) return error.InvalidSize;

var bytes: [32]u8 = undefined;
std.mem.writeInt(u256, &bytes, value, .big);
const bytes = uint_utils.u256ToBytes(value);

// Take only the required bytes from the end
const offset = 32 - size_bytes;
Expand Down Expand Up @@ -96,7 +95,7 @@ pub fn encodePacked(

/// Packed value (tightly packed, no padding)
pub const PackedValue = union(enum) {
uint256: U256,
uint256: u256,
uint: struct { value: u256, size_bytes: usize },
address: Address,
bool_val: bool,
Expand Down Expand Up @@ -135,7 +134,7 @@ test "packed encode uint256" {
var encoder = PackedEncoder.init(allocator);
defer encoder.deinit();

const value = U256.fromInt(42);
const value = @as(u256, 42);
try encoder.encodeUint256(value);

const result = encoder.toSlice();
Expand Down Expand Up @@ -191,7 +190,7 @@ test "packed encode multiple values" {

const values = [_]PackedValue{
.{ .address = Address.fromBytes([_]u8{0xAB} ** 20) },
.{ .uint256 = U256.fromInt(100) },
.{ .uint256 = @as(u256, 100) },
.{ .bool_val = true },
};

Expand Down Expand Up @@ -229,7 +228,7 @@ test "hash packed values" {

const values = [_]PackedValue{
.{ .string = "hello" },
.{ .uint256 = U256.fromInt(123) },
.{ .uint256 = @as(u256, 123) },
};

const hash = try hashPacked(allocator, &values);
Expand Down
3 changes: 1 addition & 2 deletions src/abi/types.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const std = @import("std");
const Address = @import("../primitives/address.zig").Address;
const U256 = @import("../primitives/uint.zig").U256;
const Bytes = @import("../primitives/bytes.zig").Bytes;

/// ABI type system for Solidity types
Expand Down Expand Up @@ -134,7 +133,7 @@ pub const AbiType = union(enum) {

/// ABI encoded value
pub const AbiValue = union(enum) {
uint: U256,
uint: u256,
int: i256,
address: Address,
bool_val: bool,
Expand Down
Loading
Loading