essential-eth 0.4.9-beta.4 → 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.
- package/lib/cjs/classes/Contract.js +3 -8
- package/lib/cjs/classes/utils/fetchers.d.ts +3 -3
- package/lib/cjs/classes/utils/fetchers.js +21 -3
- package/lib/cjs/index.d.ts +3 -2
- package/lib/cjs/index.js +5 -26
- package/lib/cjs/providers/JsonRpcProvider.d.ts +36 -6
- package/lib/cjs/providers/JsonRpcProvider.js +56 -28
- package/lib/cjs/providers/test/rpc-urls.d.ts +10 -0
- package/lib/cjs/providers/test/rpc-urls.js +13 -0
- package/lib/cjs/providers/utils/chains-info.d.ts +23 -1
- package/lib/cjs/providers/utils/chains-info.js +69 -3
- package/lib/cjs/shared/tiny-big/tiny-big.d.ts +6 -0
- package/lib/cjs/shared/tiny-big/tiny-big.js +6 -0
- package/lib/cjs/types/Block.types.d.ts +1 -0
- package/lib/cjs/utils/ether-to-gwei.d.ts +26 -0
- package/lib/cjs/utils/ether-to-gwei.js +34 -0
- package/lib/cjs/utils/ether-to-wei.d.ts +4 -2
- package/lib/cjs/utils/ether-to-wei.js +4 -2
- package/lib/cjs/utils/gwei-to-ether.d.ts +26 -0
- package/lib/cjs/utils/gwei-to-ether.js +34 -0
- package/lib/cjs/utils/is-address.d.ts +19 -0
- package/lib/cjs/utils/is-address.js +19 -0
- package/lib/cjs/utils/to-checksum-address.d.ts +2 -2
- package/lib/cjs/utils/to-checksum-address.js +2 -2
- package/lib/cjs/utils/wei-to-ether.d.ts +12 -12
- package/lib/cjs/utils/wei-to-ether.js +11 -11
- package/lib/esm/classes/Contract.js +3 -8
- package/lib/esm/classes/utils/fetchers.d.ts +3 -3
- package/lib/esm/classes/utils/fetchers.js +21 -3
- package/lib/esm/index.d.ts +3 -2
- package/lib/esm/index.js +3 -2
- package/lib/esm/providers/JsonRpcProvider.d.ts +7 -5
- package/lib/esm/providers/JsonRpcProvider.js +27 -27
- package/lib/esm/providers/test/rpc-urls.d.ts +10 -0
- package/lib/esm/providers/test/rpc-urls.js +10 -0
- package/lib/esm/providers/utils/chains-info.d.ts +23 -1
- package/lib/esm/providers/utils/chains-info.js +69 -3
- package/lib/esm/types/Block.types.d.ts +1 -0
- package/lib/esm/utils/ether-to-gwei.d.ts +3 -0
- package/lib/esm/utils/ether-to-gwei.js +7 -0
- package/lib/esm/utils/gwei-to-ether.d.ts +3 -0
- package/lib/esm/utils/gwei-to-ether.js +7 -0
- package/lib/esm/utils/wei-to-ether.d.ts +1 -1
- package/package.json +8 -12
- package/readme.md +112 -9
- package/lib/cjs/utils/index.d.ts +0 -5
- package/lib/cjs/utils/index.js +0 -11
- package/lib/esm/utils/index.d.ts +0 -5
- package/lib/esm/utils/index.js +0 -5
|
@@ -15,5 +15,11 @@ export declare class TinyBig extends Big {
|
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
17
|
* Helper factory function so that you don't have to type "new" when instantiating a new TinyBig
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```javascript
|
|
21
|
+
* tinyBig(10).times(3).toNumber()
|
|
22
|
+
* // 30
|
|
23
|
+
* ```
|
|
18
24
|
*/
|
|
19
25
|
export declare function tinyBig(value: string | number | TinyBig | Big): TinyBig;
|
|
@@ -34,6 +34,12 @@ class TinyBig extends big_js_1.default {
|
|
|
34
34
|
exports.TinyBig = TinyBig;
|
|
35
35
|
/**
|
|
36
36
|
* Helper factory function so that you don't have to type "new" when instantiating a new TinyBig
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```javascript
|
|
40
|
+
* tinyBig(10).times(3).toNumber()
|
|
41
|
+
* // 30
|
|
42
|
+
* ```
|
|
37
43
|
*/
|
|
38
44
|
function tinyBig(value) {
|
|
39
45
|
return new TinyBig(value);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
import { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
3
|
+
/**
|
|
4
|
+
* Convert from Ether to Gwei
|
|
5
|
+
*
|
|
6
|
+
* No direct equivalent in ether.js; requires multiple functions to achieve.
|
|
7
|
+
*
|
|
8
|
+
* No direct equivalent in web3; requires multiple functions to achieve.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```javascript
|
|
12
|
+
* etherToGwei('1000').toString()
|
|
13
|
+
* // '1000000000000'
|
|
14
|
+
* etherToGwei(1000).toString()
|
|
15
|
+
* // '1000000000000'
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```javascript
|
|
20
|
+
* etherToGwei('1000').toNumber()
|
|
21
|
+
* // 1000000000000
|
|
22
|
+
* etherToGwei(1000).toNumber()
|
|
23
|
+
* // 1000000000000
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function etherToGwei(etherQuantity: string | number | TinyBig | Big): TinyBig;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.etherToGwei = void 0;
|
|
4
|
+
const tiny_big_1 = require("../shared/tiny-big/tiny-big");
|
|
5
|
+
const validate_type_1 = require("../shared/validate-type");
|
|
6
|
+
/**
|
|
7
|
+
* Convert from Ether to Gwei
|
|
8
|
+
*
|
|
9
|
+
* No direct equivalent in ether.js; requires multiple functions to achieve.
|
|
10
|
+
*
|
|
11
|
+
* No direct equivalent in web3; requires multiple functions to achieve.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```javascript
|
|
15
|
+
* etherToGwei('1000').toString()
|
|
16
|
+
* // '1000000000000'
|
|
17
|
+
* etherToGwei(1000).toString()
|
|
18
|
+
* // '1000000000000'
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```javascript
|
|
23
|
+
* etherToGwei('1000').toNumber()
|
|
24
|
+
* // 1000000000000
|
|
25
|
+
* etherToGwei(1000).toNumber()
|
|
26
|
+
* // 1000000000000
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function etherToGwei(etherQuantity) {
|
|
30
|
+
(0, validate_type_1.validateType)(etherQuantity, ['string', 'number', 'object']);
|
|
31
|
+
const result = (0, tiny_big_1.tinyBig)(etherQuantity).times('1000000000');
|
|
32
|
+
return (0, tiny_big_1.tinyBig)(result);
|
|
33
|
+
}
|
|
34
|
+
exports.etherToGwei = etherToGwei;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import Big from 'big.js';
|
|
2
2
|
import { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Convert Ether to Wei
|
|
5
5
|
*
|
|
6
|
-
* Similar to ["
|
|
6
|
+
* Similar to ["parseEther" in ethers.js](https://docs.ethers.io/v5/api/utils/display-logic/#utils-parseEther)
|
|
7
|
+
*
|
|
8
|
+
* Similar to ["toWei" in web3.js](https://web3js.readthedocs.io/en/v1.7.1/web3-utils.html#towei)
|
|
7
9
|
*
|
|
8
10
|
* @example
|
|
9
11
|
* ```javascript
|
|
@@ -4,9 +4,11 @@ exports.etherToWei = void 0;
|
|
|
4
4
|
const tiny_big_1 = require("../shared/tiny-big/tiny-big");
|
|
5
5
|
const validate_type_1 = require("../shared/validate-type");
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* Convert Ether to Wei
|
|
8
8
|
*
|
|
9
|
-
* Similar to ["
|
|
9
|
+
* Similar to ["parseEther" in ethers.js](https://docs.ethers.io/v5/api/utils/display-logic/#utils-parseEther)
|
|
10
|
+
*
|
|
11
|
+
* Similar to ["toWei" in web3.js](https://web3js.readthedocs.io/en/v1.7.1/web3-utils.html#towei)
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
12
14
|
* ```javascript
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
import { TinyBig } from './../shared/tiny-big/tiny-big';
|
|
3
|
+
/**
|
|
4
|
+
* Convert from Gwei to Ether
|
|
5
|
+
*
|
|
6
|
+
* No direct equivalent in ethers.js; requires multiple functions to achieve.
|
|
7
|
+
*
|
|
8
|
+
* No direct equivalent in web3; requires multiple functions to achieve.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```javascript
|
|
12
|
+
* gweiToEther('1000000000000').toString()
|
|
13
|
+
* // '1000'
|
|
14
|
+
* gweiToEther(1000000000000).toString()
|
|
15
|
+
* // '1000'
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```javascript
|
|
20
|
+
* gweiToEther('1000000000000').toNumber()
|
|
21
|
+
* // 1000
|
|
22
|
+
* gweiToEther(1000000000000).toNumber()
|
|
23
|
+
* // 1000
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function gweiToEther(gweiQuantity: string | number | TinyBig | Big): TinyBig;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.gweiToEther = void 0;
|
|
4
|
+
const tiny_big_1 = require("../shared/tiny-big/tiny-big");
|
|
5
|
+
const validate_type_1 = require("../shared/validate-type");
|
|
6
|
+
/**
|
|
7
|
+
* Convert from Gwei to Ether
|
|
8
|
+
*
|
|
9
|
+
* No direct equivalent in ethers.js; requires multiple functions to achieve.
|
|
10
|
+
*
|
|
11
|
+
* No direct equivalent in web3; requires multiple functions to achieve.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```javascript
|
|
15
|
+
* gweiToEther('1000000000000').toString()
|
|
16
|
+
* // '1000'
|
|
17
|
+
* gweiToEther(1000000000000).toString()
|
|
18
|
+
* // '1000'
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```javascript
|
|
23
|
+
* gweiToEther('1000000000000').toNumber()
|
|
24
|
+
* // 1000
|
|
25
|
+
* gweiToEther(1000000000000).toNumber()
|
|
26
|
+
* // 1000
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function gweiToEther(gweiQuantity) {
|
|
30
|
+
(0, validate_type_1.validateType)(gweiQuantity, ['string', 'number', 'object']);
|
|
31
|
+
const result = (0, tiny_big_1.tinyBig)(gweiQuantity).div('1000000000');
|
|
32
|
+
return (0, tiny_big_1.tinyBig)(result);
|
|
33
|
+
}
|
|
34
|
+
exports.gweiToEther = gweiToEther;
|
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Returns a boolean as to whether the input is a valid address.
|
|
3
3
|
* Does NOT support ICAP addresses
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```javascript
|
|
7
|
+
* isAddress('0xc0deaf6bd3f0c6574a6a625ef2f22f62a5150eab');
|
|
8
|
+
* // true
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```javascript
|
|
13
|
+
* isAddress('bad');
|
|
14
|
+
* // false
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```javascript
|
|
19
|
+
* // Does NOT support ENS.
|
|
20
|
+
* isAddress('vitalik.eth');
|
|
21
|
+
* // false
|
|
22
|
+
* ```
|
|
4
23
|
*/
|
|
5
24
|
export declare function isAddress(address: string): boolean;
|
|
@@ -6,6 +6,25 @@ const validate_type_1 = require("../shared/validate-type");
|
|
|
6
6
|
/**
|
|
7
7
|
* Returns a boolean as to whether the input is a valid address.
|
|
8
8
|
* Does NOT support ICAP addresses
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```javascript
|
|
12
|
+
* isAddress('0xc0deaf6bd3f0c6574a6a625ef2f22f62a5150eab');
|
|
13
|
+
* // true
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```javascript
|
|
18
|
+
* isAddress('bad');
|
|
19
|
+
* // false
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```javascript
|
|
24
|
+
* // Does NOT support ENS.
|
|
25
|
+
* isAddress('vitalik.eth');
|
|
26
|
+
* // false
|
|
27
|
+
* ```
|
|
9
28
|
*/
|
|
10
29
|
function isAddress(address) {
|
|
11
30
|
(0, validate_type_1.validateType)(address, ['string']);
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
* // '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359'
|
|
11
11
|
* ```
|
|
12
12
|
*
|
|
13
|
-
* Similar to ["getAddress" in ethers.js](https://docs.ethers.io/
|
|
13
|
+
* Similar to ["getAddress" in ethers.js](https://docs.ethers.io/v5/api/utils/address/#utils-getAddress)
|
|
14
14
|
*
|
|
15
|
-
* Similar to ["toChecksumAddress" in web3](https://web3js.readthedocs.io/en/v1.
|
|
15
|
+
* Similar to ["toChecksumAddress" in web3.js](https://web3js.readthedocs.io/en/v1.7.1/web3-utils.html#tochecksumaddress)
|
|
16
16
|
*/
|
|
17
17
|
export declare function toChecksumAddress(address: string): string;
|
|
@@ -15,9 +15,9 @@ const validate_type_1 = require("../shared/validate-type");
|
|
|
15
15
|
* // '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359'
|
|
16
16
|
* ```
|
|
17
17
|
*
|
|
18
|
-
* Similar to ["getAddress" in ethers.js](https://docs.ethers.io/
|
|
18
|
+
* Similar to ["getAddress" in ethers.js](https://docs.ethers.io/v5/api/utils/address/#utils-getAddress)
|
|
19
19
|
*
|
|
20
|
-
* Similar to ["toChecksumAddress" in web3](https://web3js.readthedocs.io/en/v1.
|
|
20
|
+
* Similar to ["toChecksumAddress" in web3.js](https://web3js.readthedocs.io/en/v1.7.1/web3-utils.html#tochecksumaddress)
|
|
21
21
|
*/
|
|
22
22
|
function toChecksumAddress(address) {
|
|
23
23
|
(0, validate_type_1.validateType)(address, ['string']);
|
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import Big from 'big.js';
|
|
2
|
-
import { TinyBig } from '
|
|
2
|
+
import { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
3
3
|
/**
|
|
4
|
-
* Convert from
|
|
4
|
+
* Convert from Wei to Ether
|
|
5
5
|
*
|
|
6
|
-
* Similar to ["formatEther" in ethers.js](https://docs.ethers.io/
|
|
6
|
+
* Similar to ["formatEther" in ethers.js](https://docs.ethers.io/v5/api/utils/display-logic/#utils-formatEther)
|
|
7
7
|
*
|
|
8
|
-
* Similar to ["fromWei" in web3](https://web3js.readthedocs.io/en/v1.
|
|
8
|
+
* Similar to ["fromWei" in web3.js](https://web3js.readthedocs.io/en/v1.7.1/web3-utils.html#fromwei)
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* ```javascript
|
|
12
|
-
*
|
|
13
|
-
* // '
|
|
14
|
-
*
|
|
15
|
-
* '
|
|
12
|
+
* weiToEther('1000000000000000000000').toString()
|
|
13
|
+
* // '1000'
|
|
14
|
+
* weiToEther(1000000000000000000000).toString()
|
|
15
|
+
* // '1000'
|
|
16
16
|
* ```
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
19
|
* ```javascript
|
|
20
|
-
*
|
|
21
|
-
* //
|
|
22
|
-
*
|
|
23
|
-
* //
|
|
20
|
+
* weiToEther('1000000000000000000000').toNumber()
|
|
21
|
+
* // 1000
|
|
22
|
+
* weiToEther(1000000000000000000000).toNumber()
|
|
23
|
+
* // 1000
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
26
|
export declare function weiToEther(weiQuantity: string | number | TinyBig | Big): TinyBig;
|
|
@@ -4,26 +4,26 @@ exports.weiToEther = void 0;
|
|
|
4
4
|
const tiny_big_1 = require("../shared/tiny-big/tiny-big");
|
|
5
5
|
const validate_type_1 = require("../shared/validate-type");
|
|
6
6
|
/**
|
|
7
|
-
* Convert from
|
|
7
|
+
* Convert from Wei to Ether
|
|
8
8
|
*
|
|
9
|
-
* Similar to ["formatEther" in ethers.js](https://docs.ethers.io/
|
|
9
|
+
* Similar to ["formatEther" in ethers.js](https://docs.ethers.io/v5/api/utils/display-logic/#utils-formatEther)
|
|
10
10
|
*
|
|
11
|
-
* Similar to ["fromWei" in web3](https://web3js.readthedocs.io/en/v1.
|
|
11
|
+
* Similar to ["fromWei" in web3.js](https://web3js.readthedocs.io/en/v1.7.1/web3-utils.html#fromwei)
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
14
|
* ```javascript
|
|
15
|
-
*
|
|
16
|
-
* // '
|
|
17
|
-
*
|
|
18
|
-
* '
|
|
15
|
+
* weiToEther('1000000000000000000000').toString()
|
|
16
|
+
* // '1000'
|
|
17
|
+
* weiToEther(1000000000000000000000).toString()
|
|
18
|
+
* // '1000'
|
|
19
19
|
* ```
|
|
20
20
|
*
|
|
21
21
|
* @example
|
|
22
22
|
* ```javascript
|
|
23
|
-
*
|
|
24
|
-
* //
|
|
25
|
-
*
|
|
26
|
-
* //
|
|
23
|
+
* weiToEther('1000000000000000000000').toNumber()
|
|
24
|
+
* // 1000
|
|
25
|
+
* weiToEther(1000000000000000000000).toNumber()
|
|
26
|
+
* // 1000
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
29
|
function weiToEther(weiQuantity) {
|
|
@@ -40,17 +40,12 @@ export class BaseContract {
|
|
|
40
40
|
? estimateGas(data)
|
|
41
41
|
: null;
|
|
42
42
|
const req = () => __awaiter(this, void 0, void 0, function* () {
|
|
43
|
-
return yield post(this._provider._rpcUrl
|
|
44
|
-
Object.assign({ to: this._address.toLowerCase(), data
|
|
43
|
+
return yield post(this._provider._rpcUrl, buildRPCPostBody('eth_call', [
|
|
44
|
+
Object.assign({ to: this._address.toLowerCase(), data }, (decimalGas
|
|
45
45
|
? { gas: `0x${decimalGas.toString(16)}` }
|
|
46
46
|
: {})),
|
|
47
47
|
'latest',
|
|
48
|
-
]))
|
|
49
|
-
if (e.code === 'ENOTFOUND') {
|
|
50
|
-
this._provider._rpcUrlCounter++;
|
|
51
|
-
return req();
|
|
52
|
-
}
|
|
53
|
-
});
|
|
48
|
+
]));
|
|
54
49
|
});
|
|
55
50
|
const nodeResponse = yield req();
|
|
56
51
|
return decodeRPCResponse(jsonABIArgument, nodeResponse);
|
|
@@ -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';
|
|
3
|
-
export declare function buildRPCPostBody(method: RPCMethodName, params:
|
|
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:
|
|
7
|
+
params: unknown[];
|
|
8
8
|
};
|
|
9
9
|
export {};
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
1
10
|
import unfetch from 'isomorphic-unfetch';
|
|
2
11
|
export function post(url, body) {
|
|
3
12
|
return unfetch(url, {
|
|
@@ -7,10 +16,19 @@ export function post(url, body) {
|
|
|
7
16
|
},
|
|
8
17
|
body: JSON.stringify(body),
|
|
9
18
|
})
|
|
10
|
-
.then((r) =>
|
|
19
|
+
.then((r) => __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
const t = yield r.text();
|
|
21
|
+
try {
|
|
22
|
+
return JSON.parse(t);
|
|
23
|
+
}
|
|
24
|
+
catch (_a) {
|
|
25
|
+
throw new Error(`Invalid JSON RPC response: "${t}"`);
|
|
26
|
+
}
|
|
27
|
+
}))
|
|
11
28
|
.then((response) => {
|
|
12
|
-
|
|
13
|
-
|
|
29
|
+
const result = response === null || response === void 0 ? void 0 : response.result;
|
|
30
|
+
if (!result) {
|
|
31
|
+
throw new Error(`Invalid JSON RPC response: ${JSON.stringify(response)}`);
|
|
14
32
|
}
|
|
15
33
|
return response.result;
|
|
16
34
|
});
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -5,9 +5,10 @@ import { Block } from './types/Block.types';
|
|
|
5
5
|
import { ContractTypes, JSONABI, JSONABIArgument } from './types/Contract.types';
|
|
6
6
|
import { Network } from './types/Network.types';
|
|
7
7
|
import { Transaction } from './types/Transaction.types';
|
|
8
|
-
import
|
|
8
|
+
import { etherToGwei } from './utils/ether-to-gwei';
|
|
9
9
|
import { etherToWei } from './utils/ether-to-wei';
|
|
10
|
+
import { gweiToEther } from './utils/gwei-to-ether';
|
|
10
11
|
import { isAddress } from './utils/is-address';
|
|
11
12
|
import { toChecksumAddress } from './utils/to-checksum-address';
|
|
12
13
|
import { weiToEther } from './utils/wei-to-ether';
|
|
13
|
-
export {
|
|
14
|
+
export { etherToWei, etherToGwei, isAddress, jsonRpcProvider, JsonRpcProvider, tinyBig, toChecksumAddress, weiToEther, gweiToEther, Contract, TinyBig, Block, ContractTypes, JSONABI, JSONABIArgument, Network, Transaction, };
|
package/lib/esm/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Contract } from './classes/Contract';
|
|
2
2
|
import { JsonRpcProvider, jsonRpcProvider } from './providers/JsonRpcProvider';
|
|
3
3
|
import { tinyBig, TinyBig } from './shared/tiny-big/tiny-big';
|
|
4
|
-
import
|
|
4
|
+
import { etherToGwei } from './utils/ether-to-gwei';
|
|
5
5
|
import { etherToWei } from './utils/ether-to-wei';
|
|
6
|
+
import { gweiToEther } from './utils/gwei-to-ether';
|
|
6
7
|
import { isAddress } from './utils/is-address';
|
|
7
8
|
import { toChecksumAddress } from './utils/to-checksum-address';
|
|
8
9
|
import { weiToEther } from './utils/wei-to-ether';
|
|
9
|
-
export {
|
|
10
|
+
export { etherToWei, etherToGwei, isAddress, jsonRpcProvider, JsonRpcProvider, tinyBig, toChecksumAddress, weiToEther, gweiToEther, Contract, TinyBig, };
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
2
|
+
import { Block, BlockTag } from '../types/Block.types';
|
|
2
3
|
import { Network } from '../types/Network.types';
|
|
3
4
|
export declare class JsonRpcProvider {
|
|
4
|
-
readonly _rpcUrl:
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
getBlock(timeFrame: 'latest' | 'earliest' | 'pending' | number, returnTransactionObjects?: boolean): Promise<Block>;
|
|
5
|
+
readonly _rpcUrl: string;
|
|
6
|
+
constructor(rpcUrl?: string);
|
|
7
|
+
getBlock(timeFrame: BlockTag, returnTransactionObjects?: boolean): Promise<Block>;
|
|
8
8
|
getNetwork(): Promise<Network>;
|
|
9
|
+
getGasPrice(): Promise<TinyBig>;
|
|
10
|
+
getBalance(address: string, blockTag?: BlockTag): Promise<TinyBig>;
|
|
9
11
|
}
|
|
10
12
|
export declare function jsonRpcProvider(rpcUrl?: string): JsonRpcProvider;
|
|
@@ -10,19 +10,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
import { cleanBlock } from '../classes/utils/clean-block';
|
|
11
11
|
import { buildRPCPostBody, post } from '../classes/utils/fetchers';
|
|
12
12
|
import { hexToDecimal } from '../classes/utils/hex-to-decimal';
|
|
13
|
+
import { tinyBig } from '../shared/tiny-big/tiny-big';
|
|
13
14
|
import chainsInfo from './utils/chains-info';
|
|
14
15
|
export class JsonRpcProvider {
|
|
15
16
|
constructor(rpcUrl) {
|
|
16
|
-
this._rpcUrl =
|
|
17
|
-
if (!rpcUrl) {
|
|
18
|
-
return ['https://free-eth-node.com/api/eth'];
|
|
19
|
-
}
|
|
20
|
-
else if (!Array.isArray(rpcUrl)) {
|
|
21
|
-
return [rpcUrl];
|
|
22
|
-
}
|
|
23
|
-
return rpcUrl;
|
|
24
|
-
})();
|
|
25
|
-
this._rpcUrlCounter = 0;
|
|
17
|
+
this._rpcUrl = rpcUrl || 'https://free-eth-node.com/api/eth';
|
|
26
18
|
}
|
|
27
19
|
getBlock(timeFrame, returnTransactionObjects = false) {
|
|
28
20
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -33,31 +25,21 @@ export class JsonRpcProvider {
|
|
|
33
25
|
else {
|
|
34
26
|
rpcTimeFrame = timeFrame;
|
|
35
27
|
}
|
|
36
|
-
const req = () =>
|
|
37
|
-
return
|
|
28
|
+
const req = () => {
|
|
29
|
+
return post(this._rpcUrl, buildRPCPostBody('eth_getBlockByNumber', [
|
|
38
30
|
rpcTimeFrame,
|
|
39
31
|
returnTransactionObjects,
|
|
40
|
-
]))
|
|
41
|
-
|
|
42
|
-
this._rpcUrlCounter++;
|
|
43
|
-
return req();
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
});
|
|
32
|
+
]));
|
|
33
|
+
};
|
|
47
34
|
const nodeResponse = (yield req());
|
|
48
35
|
return cleanBlock(nodeResponse, returnTransactionObjects);
|
|
49
36
|
});
|
|
50
37
|
}
|
|
51
38
|
getNetwork() {
|
|
52
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
-
const req = () =>
|
|
54
|
-
return
|
|
55
|
-
|
|
56
|
-
this._rpcUrlCounter++;
|
|
57
|
-
return req();
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
});
|
|
40
|
+
const req = () => {
|
|
41
|
+
return post(this._rpcUrl, buildRPCPostBody('eth_chainId', []));
|
|
42
|
+
};
|
|
61
43
|
const nodeResponse = (yield req());
|
|
62
44
|
const chainId = hexToDecimal(nodeResponse);
|
|
63
45
|
const info = chainsInfo[chainId];
|
|
@@ -68,6 +50,24 @@ export class JsonRpcProvider {
|
|
|
68
50
|
};
|
|
69
51
|
});
|
|
70
52
|
}
|
|
53
|
+
getGasPrice() {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
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
|
+
};
|
|
67
|
+
const nodeResponse = (yield req());
|
|
68
|
+
return tinyBig(hexToDecimal(nodeResponse));
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
71
|
}
|
|
72
72
|
export function jsonRpcProvider(rpcUrl) {
|
|
73
73
|
return new JsonRpcProvider(rpcUrl);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const fakeUrls = {
|
|
2
|
+
notRPCButRealHttp: 'https://httpstat.us/200',
|
|
3
|
+
};
|
|
4
|
+
export const rpcUrls = {
|
|
5
|
+
mainnet: `${process.env.RPC_ORIGIN}/api/eth`,
|
|
6
|
+
gno: `${process.env.RPC_ORIGIN}/api/gno`,
|
|
7
|
+
bnb: `${process.env.RPC_ORIGIN}/api/bnb`,
|
|
8
|
+
arb1: `${process.env.RPC_ORIGIN}/api/arb1`,
|
|
9
|
+
gor: `${process.env.RPC_ORIGIN}/api/gor`,
|
|
10
|
+
};
|