essential-eth 0.4.10 → 0.4.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
1
  export declare function post(url: string, body: Record<string, unknown>): Promise<any>;
2
- declare type RPCMethodName = 'eth_getBlockByNumber' | 'eth_call' | 'eth_chainId' | 'eth_gasPrice';
3
- export declare function buildRPCPostBody(method: RPCMethodName, params: any[]): {
2
+ declare type RPCMethodName = 'eth_getBlockByNumber' | 'eth_call' | 'eth_chainId' | 'eth_gasPrice' | 'eth_getBalance';
3
+ export declare function buildRPCPostBody(method: RPCMethodName, params: unknown[]): {
4
4
  jsonrpc: string;
5
5
  id: number;
6
6
  method: RPCMethodName;
7
- params: any[];
7
+ params: unknown[];
8
8
  };
9
9
  export {};
@@ -1,17 +1,20 @@
1
1
  import { TinyBig } from '../shared/tiny-big/tiny-big';
2
- import { Block } from '../types/Block.types';
2
+ import { Block, BlockTag } from '../types/Block.types';
3
3
  import { Network } from '../types/Network.types';
4
4
  export declare class JsonRpcProvider {
5
5
  /**
6
- * The URL to your Eth node. Consider POKT or Infura
6
+ * @ignore
7
7
  */
8
8
  readonly _rpcUrl: string;
9
+ /**
10
+ * @param rpcUrl The URL to your Eth node. Consider POKT or Infura
11
+ */
9
12
  constructor(rpcUrl?: string);
10
13
  /**
11
14
  * Returns the block requested
12
15
  * Same as `web3.eth.getBlock`
13
16
  */
14
- getBlock(timeFrame: 'latest' | 'earliest' | 'pending' | number, returnTransactionObjects?: boolean): Promise<Block>;
17
+ getBlock(timeFrame: BlockTag, returnTransactionObjects?: boolean): Promise<Block>;
15
18
  /**
16
19
  * Returns the network this provider is connected to
17
20
  */
@@ -21,6 +24,20 @@ export declare class JsonRpcProvider {
21
24
  * Same as `ethers.provider.getGasPrice`
22
25
  */
23
26
  getGasPrice(): Promise<TinyBig>;
27
+ /**
28
+ * Returns the balance of the account in wei as TinyBig
29
+ * Same as `ethers.provider.getBalance`
30
+ * Same as `web3.eth.getBalance`
31
+ *
32
+ * @example
33
+ * ```js
34
+ * await provider
35
+ * .getBalance('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8')
36
+ * .then((balance) => console.log(balance.toString()));
37
+ * // "28798127851528138"
38
+ * ```
39
+ */
40
+ getBalance(address: string, blockTag?: BlockTag): Promise<TinyBig>;
24
41
  }
25
42
  /**
26
43
  * Helper function to avoid "new"
@@ -19,6 +19,9 @@ const hex_to_decimal_1 = require("../classes/utils/hex-to-decimal");
19
19
  const tiny_big_1 = require("../shared/tiny-big/tiny-big");
20
20
  const chains_info_1 = __importDefault(require("./utils/chains-info"));
21
21
  class JsonRpcProvider {
22
+ /**
23
+ * @param rpcUrl The URL to your Eth node. Consider POKT or Infura
24
+ */
22
25
  constructor(rpcUrl) {
23
26
  this._rpcUrl = rpcUrl || 'https://free-eth-node.com/api/eth';
24
27
  }
@@ -26,7 +29,7 @@ class JsonRpcProvider {
26
29
  * Returns the block requested
27
30
  * Same as `web3.eth.getBlock`
28
31
  */
29
- getBlock(timeFrame /* block number as integer */, returnTransactionObjects = false) {
32
+ getBlock(timeFrame, returnTransactionObjects = false) {
30
33
  return __awaiter(this, void 0, void 0, function* () {
31
34
  let rpcTimeFrame;
32
35
  if (typeof timeFrame === 'number') {
@@ -37,12 +40,12 @@ class JsonRpcProvider {
37
40
  // "latest", "earliest", and "pending" require no manipulation
38
41
  rpcTimeFrame = timeFrame;
39
42
  }
40
- const req = () => __awaiter(this, void 0, void 0, function* () {
41
- return yield (0, fetchers_1.post)(this._rpcUrl, (0, fetchers_1.buildRPCPostBody)('eth_getBlockByNumber', [
43
+ const req = () => {
44
+ return (0, fetchers_1.post)(this._rpcUrl, (0, fetchers_1.buildRPCPostBody)('eth_getBlockByNumber', [
42
45
  rpcTimeFrame,
43
46
  returnTransactionObjects,
44
47
  ]));
45
- });
48
+ };
46
49
  const nodeResponse = (yield req());
47
50
  return (0, clean_block_1.cleanBlock)(nodeResponse, returnTransactionObjects);
48
51
  });
@@ -52,9 +55,9 @@ class JsonRpcProvider {
52
55
  */
53
56
  getNetwork() {
54
57
  return __awaiter(this, void 0, void 0, function* () {
55
- const req = () => __awaiter(this, void 0, void 0, function* () {
56
- return yield (0, fetchers_1.post)(this._rpcUrl, (0, fetchers_1.buildRPCPostBody)('eth_chainId', []));
57
- });
58
+ const req = () => {
59
+ return (0, fetchers_1.post)(this._rpcUrl, (0, fetchers_1.buildRPCPostBody)('eth_chainId', []));
60
+ };
58
61
  const nodeResponse = (yield req());
59
62
  const chainId = (0, hex_to_decimal_1.hexToDecimal)(nodeResponse);
60
63
  const info = chains_info_1.default[chainId];
@@ -71,9 +74,31 @@ class JsonRpcProvider {
71
74
  */
72
75
  getGasPrice() {
73
76
  return __awaiter(this, void 0, void 0, function* () {
74
- const req = () => __awaiter(this, void 0, void 0, function* () {
75
- return yield (0, fetchers_1.post)(this._rpcUrl, (0, fetchers_1.buildRPCPostBody)('eth_gasPrice', []));
76
- });
77
+ const req = () => {
78
+ return (0, fetchers_1.post)(this._rpcUrl, (0, fetchers_1.buildRPCPostBody)('eth_gasPrice', []));
79
+ };
80
+ const nodeResponse = (yield req()); /* '0x153cfb1ad0' */
81
+ return (0, tiny_big_1.tinyBig)((0, hex_to_decimal_1.hexToDecimal)(nodeResponse));
82
+ });
83
+ }
84
+ /**
85
+ * Returns the balance of the account in wei as TinyBig
86
+ * Same as `ethers.provider.getBalance`
87
+ * Same as `web3.eth.getBalance`
88
+ *
89
+ * @example
90
+ * ```js
91
+ * await provider
92
+ * .getBalance('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8')
93
+ * .then((balance) => console.log(balance.toString()));
94
+ * // "28798127851528138"
95
+ * ```
96
+ */
97
+ getBalance(address, blockTag = 'latest') {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ const req = () => {
100
+ return (0, fetchers_1.post)(this._rpcUrl, (0, fetchers_1.buildRPCPostBody)('eth_getBalance', [address, blockTag]));
101
+ };
77
102
  const nodeResponse = (yield req()); /* '0x153cfb1ad0' */
78
103
  return (0, tiny_big_1.tinyBig)((0, hex_to_decimal_1.hexToDecimal)(nodeResponse));
79
104
  });
@@ -5,4 +5,6 @@ export declare const rpcUrls: {
5
5
  mainnet: string;
6
6
  gno: string;
7
7
  bnb: string;
8
+ arb1: string;
9
+ gor: string;
8
10
  };
@@ -8,4 +8,6 @@ exports.rpcUrls = {
8
8
  mainnet: `${process.env.RPC_ORIGIN}/api/eth`,
9
9
  gno: `${process.env.RPC_ORIGIN}/api/gno`,
10
10
  bnb: `${process.env.RPC_ORIGIN}/api/bnb`,
11
+ arb1: `${process.env.RPC_ORIGIN}/api/arb1`,
12
+ gor: `${process.env.RPC_ORIGIN}/api/gor`,
11
13
  };
@@ -262,6 +262,7 @@ declare const _default: {
262
262
  "16000": string[];
263
263
  "16001": string[];
264
264
  "19845": string[];
265
+ "21337": string[];
265
266
  "21816": string[];
266
267
  "24484": string[];
267
268
  "24734": string[];
@@ -587,7 +587,7 @@ exports.default = {
587
587
  "mrock"
588
588
  ],
589
589
  "1337": [
590
- "cennz-a"
590
+ "cennz-old"
591
591
  ],
592
592
  "1618": [
593
593
  "cate"
@@ -796,6 +796,9 @@ exports.default = {
796
796
  "19845": [
797
797
  "btcix"
798
798
  ],
799
+ "21337": [
800
+ "cennz-a"
801
+ ],
799
802
  "21816": [
800
803
  "oml"
801
804
  ],
@@ -45,3 +45,4 @@ export interface RPCBlock {
45
45
  transactionsRoot: string;
46
46
  uncles: unknown[];
47
47
  }
48
+ export declare type BlockTag = 'latest' | 'earliest' | 'pending' | number | string;
@@ -1,9 +1,9 @@
1
1
  export declare function post(url: string, body: Record<string, unknown>): Promise<any>;
2
- declare type RPCMethodName = 'eth_getBlockByNumber' | 'eth_call' | 'eth_chainId' | 'eth_gasPrice';
3
- export declare function buildRPCPostBody(method: RPCMethodName, params: any[]): {
2
+ declare type RPCMethodName = 'eth_getBlockByNumber' | 'eth_call' | 'eth_chainId' | 'eth_gasPrice' | 'eth_getBalance';
3
+ export declare function buildRPCPostBody(method: RPCMethodName, params: unknown[]): {
4
4
  jsonrpc: string;
5
5
  id: number;
6
6
  method: RPCMethodName;
7
- params: any[];
7
+ params: unknown[];
8
8
  };
9
9
  export {};
@@ -1,11 +1,12 @@
1
1
  import { TinyBig } from '../shared/tiny-big/tiny-big';
2
- import { Block } from '../types/Block.types';
2
+ import { Block, BlockTag } from '../types/Block.types';
3
3
  import { Network } from '../types/Network.types';
4
4
  export declare class JsonRpcProvider {
5
5
  readonly _rpcUrl: string;
6
6
  constructor(rpcUrl?: string);
7
- getBlock(timeFrame: 'latest' | 'earliest' | 'pending' | number, returnTransactionObjects?: boolean): Promise<Block>;
7
+ getBlock(timeFrame: BlockTag, returnTransactionObjects?: boolean): Promise<Block>;
8
8
  getNetwork(): Promise<Network>;
9
9
  getGasPrice(): Promise<TinyBig>;
10
+ getBalance(address: string, blockTag?: BlockTag): Promise<TinyBig>;
10
11
  }
11
12
  export declare function jsonRpcProvider(rpcUrl?: string): JsonRpcProvider;
@@ -25,21 +25,21 @@ export class JsonRpcProvider {
25
25
  else {
26
26
  rpcTimeFrame = timeFrame;
27
27
  }
28
- const req = () => __awaiter(this, void 0, void 0, function* () {
29
- return yield post(this._rpcUrl, buildRPCPostBody('eth_getBlockByNumber', [
28
+ const req = () => {
29
+ return post(this._rpcUrl, buildRPCPostBody('eth_getBlockByNumber', [
30
30
  rpcTimeFrame,
31
31
  returnTransactionObjects,
32
32
  ]));
33
- });
33
+ };
34
34
  const nodeResponse = (yield req());
35
35
  return cleanBlock(nodeResponse, returnTransactionObjects);
36
36
  });
37
37
  }
38
38
  getNetwork() {
39
39
  return __awaiter(this, void 0, void 0, function* () {
40
- const req = () => __awaiter(this, void 0, void 0, function* () {
41
- return yield post(this._rpcUrl, buildRPCPostBody('eth_chainId', []));
42
- });
40
+ const req = () => {
41
+ return post(this._rpcUrl, buildRPCPostBody('eth_chainId', []));
42
+ };
43
43
  const nodeResponse = (yield req());
44
44
  const chainId = hexToDecimal(nodeResponse);
45
45
  const info = chainsInfo[chainId];
@@ -52,9 +52,18 @@ export class JsonRpcProvider {
52
52
  }
53
53
  getGasPrice() {
54
54
  return __awaiter(this, void 0, void 0, function* () {
55
- const req = () => __awaiter(this, void 0, void 0, function* () {
56
- return yield post(this._rpcUrl, buildRPCPostBody('eth_gasPrice', []));
57
- });
55
+ const req = () => {
56
+ return post(this._rpcUrl, buildRPCPostBody('eth_gasPrice', []));
57
+ };
58
+ const nodeResponse = (yield req());
59
+ return tinyBig(hexToDecimal(nodeResponse));
60
+ });
61
+ }
62
+ getBalance(address, blockTag = 'latest') {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ const req = () => {
65
+ return post(this._rpcUrl, buildRPCPostBody('eth_getBalance', [address, blockTag]));
66
+ };
58
67
  const nodeResponse = (yield req());
59
68
  return tinyBig(hexToDecimal(nodeResponse));
60
69
  });
@@ -5,4 +5,6 @@ export declare const rpcUrls: {
5
5
  mainnet: string;
6
6
  gno: string;
7
7
  bnb: string;
8
+ arb1: string;
9
+ gor: string;
8
10
  };
@@ -5,4 +5,6 @@ export const rpcUrls = {
5
5
  mainnet: `${process.env.RPC_ORIGIN}/api/eth`,
6
6
  gno: `${process.env.RPC_ORIGIN}/api/gno`,
7
7
  bnb: `${process.env.RPC_ORIGIN}/api/bnb`,
8
+ arb1: `${process.env.RPC_ORIGIN}/api/arb1`,
9
+ gor: `${process.env.RPC_ORIGIN}/api/gor`,
8
10
  };
@@ -262,6 +262,7 @@ declare const _default: {
262
262
  "16000": string[];
263
263
  "16001": string[];
264
264
  "19845": string[];
265
+ "21337": string[];
265
266
  "21816": string[];
266
267
  "24484": string[];
267
268
  "24734": string[];
@@ -583,7 +583,7 @@ export default {
583
583
  "mrock"
584
584
  ],
585
585
  "1337": [
586
- "cennz-a"
586
+ "cennz-old"
587
587
  ],
588
588
  "1618": [
589
589
  "cate"
@@ -792,6 +792,9 @@ export default {
792
792
  "19845": [
793
793
  "btcix"
794
794
  ],
795
+ "21337": [
796
+ "cennz-a"
797
+ ],
795
798
  "21816": [
796
799
  "oml"
797
800
  ],
@@ -45,3 +45,4 @@ export interface RPCBlock {
45
45
  transactionsRoot: string;
46
46
  uncles: unknown[];
47
47
  }
48
+ export declare type BlockTag = 'latest' | 'earliest' | 'pending' | number | string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "essential-eth",
3
3
  "description": "Ultralight JS library for Ethereum utilities",
4
- "version": "0.4.10",
4
+ "version": "0.4.11",
5
5
  "license": "MIT",
6
6
  "sideEffects": false,
7
7
  "main": "./lib/cjs/index.js",
package/readme.md CHANGED
@@ -264,6 +264,33 @@ provider.getGasPrice().toNumber();
264
264
 
265
265
  <br/>
266
266
 
267
+ #### `getBalance`
268
+
269
+ Returns the balance of an address at a given block
270
+
271
+ ```typescript
272
+ // Same API as ethers.providers.getBalance
273
+ // Same API as web3.eth.getBalance
274
+ getBalance(address: string, blockTag?: BlockTag): Promise<TinyBig>
275
+ ```
276
+
277
+ <details>
278
+ <summary>View Example</summary>
279
+
280
+ ```typescript
281
+ import { JsonRpcProvider } from 'essential-eth';
282
+
283
+ const provider = new JsonRpcProvider();
284
+ await provider
285
+ .getBalance('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8')
286
+ .then((balance) => console.log(balance.toString()));
287
+ // "28798127851528138"
288
+ ```
289
+
290
+ </details>
291
+
292
+ <br/>
293
+
267
294
  #### `getNetwork`
268
295
 
269
296
  Returns a Network