-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path02_query_blockchain.zig
More file actions
169 lines (137 loc) Β· 6.85 KB
/
Copy path02_query_blockchain.zig
File metadata and controls
169 lines (137 loc) Β· 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/// Example: Querying Blockchain Data
/// This example demonstrates how to:
/// - Connect to Ethereum networks
/// - Query account balances
/// - Get block information
/// - Retrieve transactions
/// - Check transaction receipts
/// - Query gas prices
const std = @import("std");
const zigeth = @import("zigeth");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
std.debug.print("\nπ Zigeth Blockchain Query Examples\n", .{});
std.debug.print("ββββββββββββββββββββββββββββββββββββ\n\n", .{});
// Connect to Ethereum mainnet via Etherspot
std.debug.print("π‘ Connecting to Ethereum mainnet...\n", .{});
var provider = try zigeth.providers.Networks.mainnet(allocator);
defer provider.deinit();
std.debug.print("β
Connected to Ethereum\n\n", .{});
// Example 1: Get current block number
std.debug.print("Example 1: Current Block Number\n", .{});
std.debug.print("ββββββββββββββββββββββββββββββββ\n", .{});
{
const block_number = try provider.getBlockNumber();
std.debug.print("β
Latest block: {d}\n\n", .{block_number});
}
// Example 2: Get chain information
std.debug.print("Example 2: Chain Information\n", .{});
std.debug.print("ββββββββββββββββββββββββββββ\n", .{});
{
const chain_id = try provider.getChainId();
std.debug.print("β
Chain ID: {d}\n", .{chain_id});
std.debug.print(" Network: Ethereum Mainnet\n\n", .{});
}
// Example 3: Query account balance
std.debug.print("Example 3: Account Balance\n", .{});
std.debug.print("ββββββββββββββββββββββββββ\n", .{});
{
// Vitalik's address - simple string literal!
const address = try zigeth.primitives.Address.fromHex("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const balance = try provider.getBalance(address);
const addr_hex = try address.toHex(allocator);
defer allocator.free(addr_hex);
std.debug.print("β
Address: {s}\n", .{addr_hex});
std.debug.print(" Balance: {d} wei\n", .{balance});
// Convert to ether
const ether = try zigeth.utils.units.weiToEther(balance);
std.debug.print(" Balance: {d:.4} ETH\n\n", .{ether});
}
// Example 4: Get current gas price
std.debug.print("Example 4: Gas Price\n", .{});
std.debug.print("ββββββββββββββββββββ\n", .{});
{
const gas_price = try provider.getGasPrice();
std.debug.print("β
Gas price: {d} wei\n", .{gas_price});
// Convert to gwei
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});
}
// Example 5: Get latest block details
std.debug.print("Example 5: Latest Block\n", .{});
std.debug.print("βββββββββββββββββββββββ\n", .{});
{
const block = try provider.getLatestBlock();
defer block.deinit();
const block_hash = try block.hash.toHex(allocator);
defer allocator.free(block_hash);
std.debug.print("β
Block #{d}\n", .{block.header.number});
std.debug.print(" Hash: {s}\n", .{block_hash});
std.debug.print(" Timestamp: {d}\n", .{block.header.timestamp});
std.debug.print(" Gas used: {d}\n", .{block.header.gas_used});
std.debug.print(" Gas limit: {d}\n", .{block.header.gas_limit});
std.debug.print(" Transactions: {d}\n", .{block.transactions.len});
if (block.header.base_fee_per_gas) |base_fee| {
std.debug.print(" Base fee: {d} wei\n", .{base_fee});
}
std.debug.print("\n", .{});
}
// Example 6: Get transaction count (nonce)
std.debug.print("Example 6: Transaction Count\n", .{});
std.debug.print("ββββββββββββββββββββββββββββ\n", .{});
{
// Vitalik's address
const address = try zigeth.primitives.Address.fromHex("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const nonce = try provider.getTransactionCount(address);
const addr_hex = try address.toHex(allocator);
defer allocator.free(addr_hex);
std.debug.print("β
Address: {s}\n", .{addr_hex});
std.debug.print(" Transaction count (nonce): {d}\n\n", .{nonce});
}
// Example 7: Check if address is a contract
std.debug.print("Example 7: Contract Detection\n", .{});
std.debug.print("ββββββββββββββββββββββββββββββ\n", .{});
{
// USDT contract on Ethereum
const usdt_address = try zigeth.primitives.Address.fromHex("0xdAC17F958D2ee523a2206206994597C13D831ec7");
const is_contract = try provider.isContract(usdt_address);
const addr_hex = try usdt_address.toHex(allocator);
defer allocator.free(addr_hex);
std.debug.print("β
Address: {s}\n", .{addr_hex});
std.debug.print(" Is contract: {}\n\n", .{is_contract});
}
// Example 8: Multi-chain queries
std.debug.print("Example 8: Multi-Chain Queries\n", .{});
std.debug.print("βββββββββββββββββββββββββββββββ\n", .{});
{
// Query Ethereum Mainnet
{
var eth_provider = try zigeth.providers.Networks.mainnet(allocator);
defer eth_provider.deinit();
const chain_id = try eth_provider.getChainId();
const block_num = try eth_provider.getBlockNumber();
std.debug.print("β
Ethereum: Chain ID {d}, Block #{d}\n", .{ chain_id, block_num });
}
// Query Polygon
{
var polygon_provider = try zigeth.providers.Networks.polygon(allocator);
defer polygon_provider.deinit();
const chain_id = try polygon_provider.getChainId();
const block_num = try polygon_provider.getBlockNumber();
std.debug.print("β
Polygon: Chain ID {d}, Block #{d}\n", .{ chain_id, block_num });
}
// Query Arbitrum
{
var arb_provider = try zigeth.providers.Networks.arbitrum(allocator);
defer arb_provider.deinit();
const chain_id = try arb_provider.getChainId();
const block_num = try arb_provider.getBlockNumber();
std.debug.print("β
Arbitrum: Chain ID {d}, Block #{d}\n", .{ chain_id, block_num });
}
std.debug.print("\n", .{});
}
std.debug.print("π All blockchain query examples completed!\n\n", .{});
}