MCPcopy Create free account
hub / github.com/Bot80926/ethers-scripts / Provider

Class Provider

utils/provider.js:3–134  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1const Ethers = require("ethers");
2
3class Provider {
4 constructor(props = {}) {
5 this.props = props;
6 this.chain = props.chain;
7 this.chainId = props.chainId;
8 this.fullnode = props.fullnode;
9 this.provider = new Ethers.providers.JsonRpcProvider(this.fullnode, this.chainId);
10 }
11
12 getProvider() {
13 return this.provider;
14 }
15
16 async getBlockNumber() {
17 return await this.provider.getBlockNumber();
18 }
19
20 async getBlock(blockNumber) {
21 // const block = await this.provider.getBlock(blockNumber);
22 const block = await this.provider.send("eth_getBlockByNumber", [Decimal.toHex(blockNumber), false]);
23
24 return {
25 hash: block.hash,
26 parentHash: block.parentHash,
27 number: lodash.toInteger(Decimal.fromHex(block.number)),
28 timestamp: lodash.toInteger(Decimal.fromHex(block.timestamp)),
29 nonce: block.nonce,
30 difficulty: lodash.toInteger(Decimal.fromHex(block.difficulty)),
31 gasLimit: Decimal.fromHex(block.gasLimit), //
32 gasUsed: Decimal.fromHex(block.gasUsed), //
33 miner: block.miner,
34 extraData: block.extraData,
35 transactions: block.transactions,
36 logsBloom: block.logsBloom,
37 baseFeePerGas: lodash.isUndefined(block.baseFeePerGas) ? null : Decimal.fromHex(block.baseFeePerGas), //
38 };
39 }
40
41 async getChainBalance(address, blockTag = "latest") {
42 return await this.provider.getBalance(address, blockTag);
43 }
44
45 async getTokenBalance(address, contract, blockTag = "latest") {
46 const balanceOfAbi = [{
47 "inputs": [{
48 "internalType": "address",
49 "name": "account",
50 "type": "address",
51 }, ],
52 "name": "balanceOf",
53 "outputs": [{
54 "internalType": "uint256",
55 "name": "",
56 "type": "uint256",
57 }, ],
58 "stateMutability": "view",
59 "type": "function",
60 }];

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected