0
# Network Information
1
2
The network information functionality provides access to Ethereum network and node details including protocol version, chain identification, synchronization status, and mining information.
3
4
## Protocol and Version Information
5
6
### getProtocolVersion
7
8
Returns the Ethereum protocol version supported by the connected node.
9
10
```typescript { .api }
11
getProtocolVersion(): Promise<string>;
12
```
13
14
**Usage Example:**
15
```typescript
16
const protocolVersion = await eth.getProtocolVersion();
17
console.log(`Node protocol version: ${protocolVersion}`);
18
// Example output: "63" or "64"
19
```
20
21
### getChainId
22
23
Returns the chain ID of the current Ethereum network.
24
25
```typescript { .api }
26
getChainId(): Promise<Numbers>;
27
```
28
29
**Usage Example:**
30
```typescript
31
const chainId = await eth.getChainId();
32
console.log(`Chain ID: ${chainId}`);
33
34
// Common chain IDs:
35
// 1 = Ethereum Mainnet
36
// 5 = Goerli Testnet
37
// 11155111 = Sepolia Testnet
38
// 137 = Polygon Mainnet
39
// 42161 = Arbitrum One
40
41
const networkNames = {
42
1: "Ethereum Mainnet",
43
5: "Goerli Testnet",
44
11155111: "Sepolia Testnet",
45
137: "Polygon Mainnet",
46
42161: "Arbitrum One"
47
};
48
49
const networkName = networkNames[Number(chainId)] || "Unknown Network";
50
console.log(`Connected to: ${networkName}`);
51
```
52
53
### getNodeInfo
54
55
Returns detailed information about the connected Ethereum node.
56
57
```typescript { .api }
58
getNodeInfo(): Promise<string>;
59
```
60
61
**Usage Example:**
62
```typescript
63
const nodeInfo = await eth.getNodeInfo();
64
console.log(`Node information: ${nodeInfo}`);
65
// Example: "Geth/v1.10.26-stable-e5eb32ac/linux-amd64/go1.18.5"
66
```
67
68
## Synchronization Status
69
70
### isSyncing
71
72
Checks if the connected node is currently synchronizing with the network.
73
74
```typescript { .api }
75
isSyncing(): Promise<SyncingStatusAPI | boolean>;
76
```
77
78
**Returns:**
79
- `false` if the node is fully synchronized
80
- `SyncingStatusAPI` object with sync progress if currently syncing
81
82
**Usage Example:**
83
```typescript
84
const syncStatus = await eth.isSyncing();
85
86
if (syncStatus === false) {
87
console.log("Node is fully synchronized");
88
} else {
89
console.log("Node is synchronizing:", {
90
startingBlock: syncStatus.startingBlock,
91
currentBlock: syncStatus.currentBlock,
92
highestBlock: syncStatus.highestBlock,
93
progress: ((Number(syncStatus.currentBlock) / Number(syncStatus.highestBlock)) * 100).toFixed(2) + '%'
94
});
95
96
// Optional additional sync info (if available)
97
if (syncStatus.knownStates && syncStatus.pulledStates) {
98
console.log("State sync progress:", {
99
knownStates: syncStatus.knownStates,
100
pulledStates: syncStatus.pulledStates,
101
stateProgress: ((Number(syncStatus.pulledStates) / Number(syncStatus.knownStates)) * 100).toFixed(2) + '%'
102
});
103
}
104
}
105
```
106
107
### Monitor Sync Progress
108
109
```typescript
110
// Monitor synchronization progress in real-time
111
async function monitorSyncProgress() {
112
console.log("Monitoring node synchronization...");
113
114
const checkSync = async () => {
115
const syncStatus = await eth.isSyncing();
116
117
if (syncStatus === false) {
118
console.log("β Node is fully synchronized");
119
return true; // Fully synced
120
} else {
121
const progress = ((Number(syncStatus.currentBlock) / Number(syncStatus.highestBlock)) * 100).toFixed(2);
122
console.log(`π Syncing: Block ${syncStatus.currentBlock}/${syncStatus.highestBlock} (${progress}%)`);
123
return false; // Still syncing
124
}
125
};
126
127
// Check every 10 seconds until fully synced
128
const syncInterval = setInterval(async () => {
129
const isFullySynced = await checkSync();
130
if (isFullySynced) {
131
clearInterval(syncInterval);
132
}
133
}, 10000);
134
135
// Initial check
136
await checkSync();
137
}
138
```
139
140
## Mining Information
141
142
### getCoinbase
143
144
Returns the coinbase address where mining rewards are sent.
145
146
```typescript { .api }
147
getCoinbase(): Promise<Address>;
148
```
149
150
**Usage Example:**
151
```typescript
152
try {
153
const coinbase = await eth.getCoinbase();
154
console.log(`Coinbase address: ${coinbase}`);
155
} catch (error) {
156
console.log("Node is not configured for mining or doesn't support mining");
157
}
158
```
159
160
### isMining
161
162
Checks if the connected node is currently mining blocks.
163
164
```typescript { .api }
165
isMining(): Promise<boolean>;
166
```
167
168
**Usage Example:**
169
```typescript
170
const isMining = await eth.isMining();
171
console.log(`Node is mining: ${isMining}`);
172
173
if (isMining) {
174
const coinbase = await eth.getCoinbase();
175
console.log(`Mining rewards going to: ${coinbase}`);
176
}
177
```
178
179
### getHashRate
180
181
Returns the current network hash rate (mining power).
182
183
```typescript { .api }
184
getHashRate(): Promise<Numbers>;
185
```
186
187
**Usage Example:**
188
```typescript
189
try {
190
const hashRate = await eth.getHashRate();
191
console.log(`Network hash rate: ${hashRate} hashes/second`);
192
193
// Convert to more readable format
194
const hashRateNum = Number(hashRate);
195
if (hashRateNum > 1e18) {
196
console.log(`Hash rate: ${(hashRateNum / 1e18).toFixed(2)} EH/s`);
197
} else if (hashRateNum > 1e15) {
198
console.log(`Hash rate: ${(hashRateNum / 1e15).toFixed(2)} PH/s`);
199
} else if (hashRateNum > 1e12) {
200
console.log(`Hash rate: ${(hashRateNum / 1e12).toFixed(2)} TH/s`);
201
}
202
} catch (error) {
203
console.log("Hash rate not available (non-PoW network or unsupported)");
204
}
205
```
206
207
## Network Health Checks
208
209
### Comprehensive Network Status
210
211
```typescript
212
// Get comprehensive network and node status
213
async function getNetworkStatus() {
214
try {
215
const [
216
chainId,
217
blockNumber,
218
syncStatus,
219
protocolVersion,
220
nodeInfo,
221
isMining
222
] = await Promise.all([
223
eth.getChainId(),
224
eth.getBlockNumber(),
225
eth.isSyncing(),
226
eth.getProtocolVersion(),
227
eth.getNodeInfo(),
228
eth.isMining().catch(() => false)
229
]);
230
231
const networkNames = {
232
1: "Ethereum Mainnet",
233
5: "Goerli Testnet",
234
11155111: "Sepolia Testnet",
235
137: "Polygon Mainnet",
236
42161: "Arbitrum One"
237
};
238
239
const status = {
240
network: {
241
chainId: Number(chainId),
242
name: networkNames[Number(chainId)] || "Unknown Network",
243
currentBlock: Number(blockNumber),
244
protocolVersion
245
},
246
node: {
247
info: nodeInfo,
248
isMining,
249
isFullySynced: syncStatus === false
250
},
251
sync: syncStatus === false ? null : {
252
startingBlock: Number(syncStatus.startingBlock),
253
currentBlock: Number(syncStatus.currentBlock),
254
highestBlock: Number(syncStatus.highestBlock),
255
progress: ((Number(syncStatus.currentBlock) / Number(syncStatus.highestBlock)) * 100).toFixed(2) + '%'
256
}
257
};
258
259
return status;
260
} catch (error) {
261
console.error("Error getting network status:", error);
262
throw error;
263
}
264
}
265
266
// Usage
267
const networkStatus = await getNetworkStatus();
268
console.log("Network Status:", JSON.stringify(networkStatus, null, 2));
269
```
270
271
### Connection Health Check
272
273
```typescript
274
// Check if connection to node is healthy
275
async function checkConnectionHealth(): Promise<boolean> {
276
try {
277
// Try multiple quick operations
278
const startTime = Date.now();
279
280
await Promise.all([
281
eth.getBlockNumber(),
282
eth.getChainId(),
283
eth.getProtocolVersion()
284
]);
285
286
const responseTime = Date.now() - startTime;
287
288
console.log(`Connection healthy - Response time: ${responseTime}ms`);
289
return responseTime < 5000; // Consider healthy if under 5 seconds
290
291
} catch (error) {
292
console.error("Connection health check failed:", error);
293
return false;
294
}
295
}
296
```
297
298
### Network Performance Monitoring
299
300
```typescript
301
// Monitor network performance metrics
302
async function monitorNetworkPerformance(intervalMs: number = 30000) {
303
console.log("Starting network performance monitoring...");
304
305
let lastBlockNumber = 0;
306
let lastCheckTime = Date.now();
307
308
const checkPerformance = async () => {
309
try {
310
const startTime = Date.now();
311
const [blockNumber, syncStatus] = await Promise.all([
312
eth.getBlockNumber(),
313
eth.isSyncing()
314
]);
315
const responseTime = Date.now() - startTime;
316
317
const currentTime = Date.now();
318
const timeDiff = (currentTime - lastCheckTime) / 1000; // seconds
319
const blockDiff = Number(blockNumber) - lastBlockNumber;
320
const blockRate = lastBlockNumber > 0 ? (blockDiff / timeDiff).toFixed(2) : "N/A";
321
322
console.log(`Performance Check:`, {
323
currentBlock: Number(blockNumber),
324
responseTime: `${responseTime}ms`,
325
blockRate: `${blockRate} blocks/sec`,
326
isFullySynced: syncStatus === false,
327
timestamp: new Date().toISOString()
328
});
329
330
lastBlockNumber = Number(blockNumber);
331
lastCheckTime = currentTime;
332
333
} catch (error) {
334
console.error("Performance check failed:", error);
335
}
336
};
337
338
// Initial check
339
await checkPerformance();
340
341
// Periodic checks
342
const interval = setInterval(checkPerformance, intervalMs);
343
344
// Return cleanup function
345
return () => clearInterval(interval);
346
}
347
```
348
349
## Core Types
350
351
```typescript { .api }
352
interface SyncingStatusAPI {
353
startingBlock: Numbers;
354
currentBlock: Numbers;
355
highestBlock: Numbers;
356
knownStates?: Numbers;
357
pulledStates?: Numbers;
358
}
359
360
type Numbers = HexString | number | bigint;
361
type Address = HexString20Bytes;
362
type HexString = string;
363
type HexString20Bytes = string;
364
type HexString32Bytes = string;
365
```