-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheth_client.js
More file actions
63 lines (54 loc) · 1.9 KB
/
eth_client.js
File metadata and controls
63 lines (54 loc) · 1.9 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
const Web3 = require('web3');
// create a web3 instance
const web3 = new Web3(process.env.NODE_ENDPOINT);
async function getChainId(maxRetries = 5, delay = 1000) {
let retries = 0;
while (retries < maxRetries) {
try {
// Get the Chain ID
const chainId = await web3.eth.getChainId();
console.log('Chain ID:', chainId);
return chainId;
} catch (error) {
console.error('Error:', error);
retries++;
// Retry after a delay
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
return null;
}
// Define a function to retrieve a block by block number with retries in case of failure
async function getBlockWithRetries(blockNumber, maxRetries) {
let retries = 0;
while (retries < maxRetries) {
try {
const block = await web3.eth.getBlock(blockNumber, true);
return block;
} catch (error) {
console.error(`Error retrieving block ${blockNumber}. Retrying...`);
retries++;
}
}
console.error(`Max retries reached for block ${blockNumber}. Aborting...`);
return null;
}
async function getTransactionReceiptsWithRetries(block) {
let receipts = [];
let retries = 0;
let maxRetries = 3;
for (let i = 0; i < block.transactions.length; i++) {
try {
let hash = block.transactions[i].hash;
const receipt = await web3.eth.getTransactionReceipt(hash);
if (receipt != null) {
receipts.push(receipt);
}
} catch (error) {
console.error(`Error retrieving receipt ${block.transactions[i].transactionHash}. Retrying...`);
}
}
console.log(`fetched ${receipts.length} transaction receipts.`);
return receipts;
}
module.exports = { getBlockWithRetries, getTransactionReceiptsWithRetries, getChainId };