-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.js
46 lines (34 loc) · 1.37 KB
/
rpc.js
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
const { ethers } = require('ethers');
const logger = require('./logger');
const { Command } = require('commander');
require('dotenv').config();
const program = new Command();
program
.option('-r, --rpc <url>', 'RPC provider URL')
.option('-a, --address <address>', 'Address to check balance')
.parse(process.argv);
const options = program.opts();
if (!options.rpc) {
console.error('RPC provider URL is required (--rpc)');
process.exit(1);
}
async function testRpcCalls(rpc, address) {
const provider = new ethers.providers.JsonRpcProvider(rpc);
try {
const latestBlockNumber = await provider.getBlockNumber();
logger.info(`Latest block number: ${latestBlockNumber}`);
const gasPrice = await provider.getGasPrice();
logger.info(`Current gas price: ${ethers.utils.formatUnits(gasPrice, 'gwei')} gwei`);
if (address) {
const balance = await provider.getBalance(address);
logger.info(`Balance of ${address}: ${ethers.utils.formatEther(balance)} SHM`);
}
const latestBlock = await provider.getBlock(latestBlockNumber);
logger.info(`Latest block details: ${JSON.stringify(latestBlock, null, 2)}`);
const network = await provider.getNetwork();
logger.info(`Network details: ${JSON.stringify(network, null, 2)}`);
} catch (error) {
logger.error(`Error in RPC calls: ${error}`);
}
}
testRpcCalls(options.rpc, options.address);