Skip to content

Commit b9ddb0c

Browse files
committed
feat: Replaced custom U256 type with the native one and added utility functions
1 parent 49a4f51 commit b9ddb0c

21 files changed

Lines changed: 324 additions & 427 deletions

‎examples/01_wallet_creation.zig‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
/// - Generate and use mnemonic phrases
77
/// - Create HD wallets
88
/// - Encrypt and store wallets
9-
109
const std = @import("std");
1110
const zigeth = @import("zigeth");
1211

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

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

‎examples/02_query_blockchain.zig‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ pub fn main() !void {
7171
std.debug.print("✅ Gas price: {} wei\n", .{gas_price});
7272

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

‎examples/03_send_transaction.zig‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ pub fn main() !void {
5050
var tx = zigeth.types.Transaction.newLegacy(
5151
allocator,
5252
to_address,
53-
zigeth.primitives.U256.fromInt(1_000_000_000_000_000), // 0.001 ETH
53+
@as(u256, 1_000_000_000_000_000), // 0.001 ETH
5454
empty_data,
5555
0, // nonce
5656
21000, // gas_limit
57-
zigeth.primitives.U256.fromInt(20_000_000_000), // 20 gwei gas_price
57+
@as(u256, 20_000_000_000), // 20 gwei gas_price
5858
);
5959

6060
tx.from = from_address;
@@ -76,12 +76,12 @@ pub fn main() !void {
7676
var tx = zigeth.types.Transaction.newEip1559(
7777
allocator,
7878
to_address,
79-
zigeth.primitives.U256.fromInt(1_000_000_000_000_000), // 0.001 ETH
79+
@as(u256, 1_000_000_000_000_000), // 0.001 ETH
8080
empty_data,
8181
1, // nonce
8282
21000, // gas_limit
83-
zigeth.primitives.U256.fromInt(50_000_000_000), // 50 gwei max_fee
84-
zigeth.primitives.U256.fromInt(2_000_000_000), // 2 gwei priority_fee
83+
@as(u256, 50_000_000_000), // 50 gwei max_fee
84+
@as(u256, 2_000_000_000), // 2 gwei priority_fee
8585
11155111, // chain_id (Sepolia)
8686
null, // access_list
8787
);
@@ -122,12 +122,12 @@ pub fn main() !void {
122122
var tx = zigeth.types.Transaction.newEip1559(
123123
allocator,
124124
to_address,
125-
zigeth.primitives.U256.fromInt(1_000_000_000_000_000), // 0.001 ETH
125+
@as(u256, 1_000_000_000_000_000), // 0.001 ETH
126126
empty_data,
127127
nonce,
128128
21000, // initial gas_limit (will be estimated)
129-
zigeth.primitives.U256.fromInt(50_000_000_000), // temp values
130-
zigeth.primitives.U256.fromInt(2_000_000_000),
129+
@as(u256, 50_000_000_000), // temp values
130+
@as(u256, 2_000_000_000),
131131
11155111, // Sepolia
132132
null,
133133
);
@@ -160,12 +160,12 @@ pub fn main() !void {
160160
var tx = zigeth.types.Transaction.newEip1559(
161161
allocator,
162162
to_address,
163-
zigeth.primitives.U256.fromInt(1_000_000_000_000_000), // 0.001 ETH
163+
@as(u256, 1_000_000_000_000_000), // 0.001 ETH
164164
empty_data,
165165
0, // nonce
166166
21000, // gas_limit
167-
zigeth.primitives.U256.fromInt(50_000_000_000), // 50 gwei max_fee
168-
zigeth.primitives.U256.fromInt(2_000_000_000), // 2 gwei priority_fee
167+
@as(u256, 50_000_000_000), // 50 gwei max_fee
168+
@as(u256, 2_000_000_000), // 2 gwei priority_fee
169169
11155111, // Sepolia
170170
null, // access_list
171171
);

‎examples/05_transaction_receipts.zig‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/// - Parse transaction data
66
/// - Calculate transaction fees
77
/// - Filter logs from receipts
8-
98
const std = @import("std");
109
const zigeth = @import("zigeth");
1110

@@ -75,16 +74,16 @@ pub fn main() !void {
7574

7675
// Simulated receipt data
7776
const gas_used: u64 = 21000;
78-
const gas_price = zigeth.primitives.U256.fromInt(50_000_000_000); // 50 gwei
77+
const gas_price: u256 = 50_000_000_000; // 50 gwei
7978

80-
const fee = gas_price.mulScalar(gas_used);
79+
const fee = gas_price * gas_used;
8180

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

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

‎examples/06_event_monitoring.zig‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/// - Filter and parse event logs
66
/// - Track specific contract events
77
/// - Use real-time subscriptions
8-
98
const std = @import("std");
109
const zigeth = @import("zigeth");
1110

@@ -75,10 +74,7 @@ pub fn main() !void {
7574
std.debug.print("───────────────────────────────────────\n", .{});
7675
{
7776
// USDC contract - simple string literal!
78-
const usdc_address = try zigeth.primitives.Address.fromHex(
79-
80-
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
81-
);
77+
const usdc_address = try zigeth.primitives.Address.fromHex("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
8278

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

‎examples/07_complete_workflow.zig‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ pub fn main() !void {
130130
var tx = zigeth.types.Transaction.newEip1559(
131131
allocator,
132132
to,
133-
zigeth.primitives.U256.fromInt(100_000_000_000_000_000), // 0.1 ETH
133+
@as(u256, 100_000_000_000_000_000), // 0.1 ETH
134134
empty_data,
135135
0, // nonce (would be set by middleware)
136136
21000, // gas_limit
137-
zigeth.primitives.U256.fromInt(50_000_000_000), // max_fee
138-
zigeth.primitives.U256.fromInt(2_000_000_000), // priority_fee
137+
@as(u256, 50_000_000_000), // max_fee
138+
@as(u256, 2_000_000_000), // priority_fee
139139
11155111, // Sepolia
140140
null, // access_list
141141
);
@@ -271,7 +271,7 @@ pub fn main() !void {
271271
std.debug.print(" • zigeth.providers (Networks, HTTP, WebSocket)\n", .{});
272272
std.debug.print(" • zigeth.middleware (Gas, Nonce, Signer)\n", .{});
273273
std.debug.print(" • zigeth.types (Transaction, Receipt)\n", .{});
274-
std.debug.print(" • zigeth.primitives (Address, Hash, U256)\n", .{});
274+
std.debug.print(" • zigeth.primitives (Address, Hash, native u256)\n", .{});
275275
std.debug.print(" • zigeth.utils (Units, Hex, Format)\n\n", .{});
276276

277277
std.debug.print("Benefits of using zigeth:\n", .{});

‎src/abi/decode.zig‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const std = @import("std");
22
const Address = @import("../primitives/address.zig").Address;
3-
const U256 = @import("../primitives/uint.zig").U256;
43
const types = @import("./types.zig");
4+
const uint_utils = @import("../primitives/uint.zig");
55

66
/// ABI decoder for Ethereum smart contract responses
77
pub const Decoder = struct {
@@ -28,17 +28,17 @@ pub const Decoder = struct {
2828
}
2929

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

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

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

151-
try std.testing.expect(value.eql(U256.fromInt(42)));
151+
try std.testing.expectEqual(@as(u256, 42), value);
152152
}
153153

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

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

211211
const val2 = try decoder.decodeAddress();
212212
try std.testing.expectEqual(@as(u8, 0xAB), val2.bytes[0]);

‎src/abi/encode.zig‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const std = @import("std");
22
const Address = @import("../primitives/address.zig").Address;
3-
const U256 = @import("../primitives/uint.zig").U256;
43
const Bytes = @import("../primitives/bytes.zig").Bytes;
54
const types = @import("./types.zig");
5+
const uint_utils = @import("../primitives/uint.zig");
66

77
/// ABI encoder for Ethereum smart contract calls
88
pub const Encoder = struct {
@@ -21,16 +21,15 @@ pub const Encoder = struct {
2121
}
2222

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

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

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

186-
const value = U256.fromInt(42);
185+
const value: u256 = 42;
187186
try encoder.encodeUint256(value);
188187

189188
const result = encoder.toSlice();

‎src/abi/packed.zig‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const std = @import("std");
22
const Address = @import("../primitives/address.zig").Address;
3-
const U256 = @import("../primitives/uint.zig").U256;
43
const Hash = @import("../primitives/hash.zig").Hash;
4+
const uint_utils = @import("../primitives/uint.zig");
55

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

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

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

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

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

9796
/// Packed value (tightly packed, no padding)
9897
pub const PackedValue = union(enum) {
99-
uint256: U256,
98+
uint256: u256,
10099
uint: struct { value: u256, size_bytes: usize },
101100
address: Address,
102101
bool_val: bool,
@@ -135,7 +134,7 @@ test "packed encode uint256" {
135134
var encoder = PackedEncoder.init(allocator);
136135
defer encoder.deinit();
137136

138-
const value = U256.fromInt(42);
137+
const value = @as(u256, 42);
139138
try encoder.encodeUint256(value);
140139

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

192191
const values = [_]PackedValue{
193192
.{ .address = Address.fromBytes([_]u8{0xAB} ** 20) },
194-
.{ .uint256 = U256.fromInt(100) },
193+
.{ .uint256 = @as(u256, 100) },
195194
.{ .bool_val = true },
196195
};
197196

@@ -229,7 +228,7 @@ test "hash packed values" {
229228

230229
const values = [_]PackedValue{
231230
.{ .string = "hello" },
232-
.{ .uint256 = U256.fromInt(123) },
231+
.{ .uint256 = @as(u256, 123) },
233232
};
234233

235234
const hash = try hashPacked(allocator, &values);

‎src/abi/types.zig‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const std = @import("std");
22
const Address = @import("../primitives/address.zig").Address;
3-
const U256 = @import("../primitives/uint.zig").U256;
43
const Bytes = @import("../primitives/bytes.zig").Bytes;
54

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

135134
/// ABI encoded value
136135
pub const AbiValue = union(enum) {
137-
uint: U256,
136+
uint: u256,
138137
int: i256,
139138
address: Address,
140139
bool_val: bool,

0 commit comments

Comments
 (0)