essential-eth 0.5.1 → 0.5.5
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.d.ts +3 -2
- package/lib/cjs/classes/Contract.js +3 -2
- package/lib/cjs/classes/utils/clean-transaction-receipt.d.ts +5 -0
- package/lib/cjs/classes/utils/clean-transaction-receipt.js +55 -0
- package/lib/cjs/classes/utils/encode-decode-transaction.d.ts +1 -0
- package/lib/cjs/classes/utils/encode-decode-transaction.js +4 -4
- package/lib/cjs/classes/utils/fetchers.d.ts +1 -1
- package/lib/cjs/index.d.ts +12 -4
- package/lib/cjs/index.js +16 -1
- package/lib/cjs/logger/package-version.d.ts +1 -1
- package/lib/cjs/logger/package-version.js +1 -1
- package/lib/cjs/providers/BaseProvider.d.ts +284 -0
- package/lib/cjs/providers/BaseProvider.js +380 -0
- package/lib/cjs/providers/FallthroughProvider.d.ts +24 -0
- package/lib/cjs/providers/FallthroughProvider.js +65 -0
- package/lib/cjs/providers/JsonRpcProvider.d.ts +4 -200
- package/lib/cjs/providers/JsonRpcProvider.js +11 -263
- package/lib/cjs/providers/test/rpc-urls.d.ts +1 -0
- package/lib/cjs/providers/test/rpc-urls.js +1 -0
- package/lib/cjs/providers/utils/chains-info.d.ts +20 -0
- package/lib/cjs/providers/utils/chains-info.js +62 -2
- package/lib/cjs/shared/tiny-big/tiny-big.d.ts +2 -2
- package/lib/cjs/shared/tiny-big/tiny-big.js +2 -2
- package/lib/cjs/types/Transaction.types.d.ts +53 -0
- package/lib/cjs/utils/bytes.d.ts +86 -4
- package/lib/cjs/utils/bytes.js +83 -3
- package/lib/cjs/utils/compute-address.d.ts +8 -0
- package/lib/cjs/utils/compute-address.js +24 -0
- package/lib/cjs/utils/compute-public-key.d.ts +9 -0
- package/lib/cjs/utils/compute-public-key.js +17 -0
- package/lib/cjs/utils/hash-message.d.ts +12 -0
- package/lib/cjs/utils/hash-message.js +26 -0
- package/lib/cjs/utils/keccak256.d.ts +2 -0
- package/lib/cjs/utils/keccak256.js +17 -0
- package/lib/cjs/utils/solidity-keccak256.d.ts +0 -1
- package/lib/cjs/utils/solidity-keccak256.js +5 -12
- package/lib/cjs/utils/split-signature.d.ts +26 -0
- package/lib/cjs/utils/split-signature.js +165 -0
- package/lib/cjs/utils/to-utf8-bytes.d.ts +1 -0
- package/lib/cjs/utils/to-utf8-bytes.js +7 -0
- package/lib/esm/classes/Contract.js +1 -1
- package/lib/esm/classes/utils/clean-transaction-receipt.d.ts +2 -0
- package/lib/esm/classes/utils/clean-transaction-receipt.js +48 -0
- package/lib/esm/classes/utils/encode-decode-transaction.d.ts +1 -0
- package/lib/esm/classes/utils/encode-decode-transaction.js +2 -2
- package/lib/esm/classes/utils/fetchers.d.ts +1 -1
- package/lib/esm/index.d.ts +12 -4
- package/lib/esm/index.js +10 -2
- package/lib/esm/logger/package-version.d.ts +1 -1
- package/lib/esm/logger/package-version.js +1 -1
- package/lib/esm/providers/BaseProvider.d.ts +19 -0
- package/lib/esm/providers/BaseProvider.js +107 -0
- package/lib/esm/providers/FallthroughProvider.d.ts +11 -0
- package/lib/esm/providers/FallthroughProvider.js +41 -0
- package/lib/esm/providers/JsonRpcProvider.d.ts +4 -12
- package/lib/esm/providers/JsonRpcProvider.js +8 -69
- package/lib/esm/providers/test/rpc-urls.d.ts +1 -0
- package/lib/esm/providers/test/rpc-urls.js +1 -0
- package/lib/esm/providers/utils/chains-info.d.ts +20 -0
- package/lib/esm/providers/utils/chains-info.js +62 -2
- package/lib/esm/types/Transaction.types.d.ts +45 -0
- package/lib/esm/utils/bytes.d.ts +5 -4
- package/lib/esm/utils/bytes.js +2 -2
- package/lib/esm/utils/compute-address.d.ts +1 -0
- package/lib/esm/utils/compute-address.js +11 -0
- package/lib/esm/utils/compute-public-key.d.ts +2 -0
- package/lib/esm/utils/compute-public-key.js +6 -0
- package/lib/esm/utils/hash-message.d.ts +2 -0
- package/lib/esm/utils/hash-message.js +12 -0
- package/lib/esm/utils/keccak256.d.ts +2 -0
- package/lib/esm/utils/keccak256.js +13 -0
- package/lib/esm/utils/solidity-keccak256.d.ts +0 -1
- package/lib/esm/utils/solidity-keccak256.js +4 -10
- package/lib/esm/utils/split-signature.d.ts +2 -0
- package/lib/esm/utils/split-signature.js +126 -0
- package/lib/esm/utils/to-utf8-bytes.d.ts +1 -0
- package/lib/esm/utils/to-utf8-bytes.js +3 -0
- package/package.json +19 -14
- package/readme.md +648 -95
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { tinyBig, toChecksumAddress } from '../..';
|
|
2
|
+
import { cleanTransaction } from './clean-transaction';
|
|
3
|
+
import { hexToDecimal } from './hex-to-decimal';
|
|
4
|
+
export function cleanTransactionReceipt(transactionReceipt) {
|
|
5
|
+
const cleanedTransaction = cleanTransaction(transactionReceipt);
|
|
6
|
+
const cleanedTransactionReceipt = Object.assign({}, cleanedTransaction);
|
|
7
|
+
Object.keys(transactionReceipt).forEach((key) => {
|
|
8
|
+
if (!transactionReceipt[key])
|
|
9
|
+
return;
|
|
10
|
+
switch (key) {
|
|
11
|
+
case 'status':
|
|
12
|
+
cleanedTransactionReceipt[key] = Number(hexToDecimal(transactionReceipt[key]));
|
|
13
|
+
break;
|
|
14
|
+
case 'contractAddress':
|
|
15
|
+
if (transactionReceipt[key]) {
|
|
16
|
+
cleanedTransactionReceipt[key] = toChecksumAddress(transactionReceipt[key]);
|
|
17
|
+
}
|
|
18
|
+
break;
|
|
19
|
+
case 'cumulativeGasUsed':
|
|
20
|
+
case 'effectiveGasPrice':
|
|
21
|
+
case 'gasUsed':
|
|
22
|
+
cleanedTransactionReceipt[key] = tinyBig(hexToDecimal(transactionReceipt[key]));
|
|
23
|
+
break;
|
|
24
|
+
case 'logs':
|
|
25
|
+
transactionReceipt[key].forEach((log, index) => {
|
|
26
|
+
Object.keys(log).forEach((logKey) => {
|
|
27
|
+
switch (logKey) {
|
|
28
|
+
case 'address':
|
|
29
|
+
cleanedTransactionReceipt[key][index][logKey] =
|
|
30
|
+
toChecksumAddress(log[logKey]);
|
|
31
|
+
break;
|
|
32
|
+
case 'blockNumber':
|
|
33
|
+
case 'logIndex':
|
|
34
|
+
case 'transactionIndex':
|
|
35
|
+
cleanedTransactionReceipt[key][index][logKey] = Number(hexToDecimal(log[logKey]));
|
|
36
|
+
break;
|
|
37
|
+
case 'removed':
|
|
38
|
+
delete log[logKey];
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
cleanedTransactionReceipt.byzantium =
|
|
46
|
+
cleanedTransactionReceipt.blockNumber >= 4370000;
|
|
47
|
+
return cleanedTransactionReceipt;
|
|
48
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { JSONABIArgument } from '../../types/Contract.types';
|
|
2
|
+
export declare const hexFalse: string;
|
|
2
3
|
export declare function encodeData(jsonABIArgument: JSONABIArgument, args: any[]): string;
|
|
3
4
|
export declare function decodeRPCResponse(jsonABIArgument: JSONABIArgument, nodeResponse: string): string | number | boolean | import("../..").TinyBig | (string | number | boolean | import("../..").TinyBig)[];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Keccak } from 'sha3';
|
|
2
2
|
import { tinyBig, toChecksumAddress } from '../..';
|
|
3
3
|
import { hexToDecimal } from './hex-to-decimal';
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
export const hexFalse = '0'.repeat(64);
|
|
5
|
+
const hexTrue = '0'.repeat(63) + '1';
|
|
6
6
|
function expandType(type) {
|
|
7
7
|
if (type === 'uint[]') {
|
|
8
8
|
return 'uint256[]';
|
|
@@ -1,5 +1,5 @@
|
|
|
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' | 'eth_getBalance' | 'eth_getTransactionByHash';
|
|
2
|
+
declare type RPCMethodName = 'eth_getBlockByNumber' | 'eth_getBlockByHash' | 'eth_call' | 'eth_chainId' | 'eth_gasPrice' | 'eth_getBalance' | 'eth_getTransactionByHash' | 'eth_getTransactionReceipt' | 'eth_getTransactionCount' | 'eth_blockNumber';
|
|
3
3
|
export declare function buildRPCPostBody(method: RPCMethodName, params: unknown[]): {
|
|
4
4
|
jsonrpc: string;
|
|
5
5
|
id: number;
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
|
-
import { Contract } from './classes/Contract';
|
|
1
|
+
import { BaseContract, Contract } from './classes/Contract';
|
|
2
|
+
import { ConstructorOptions, FallthroughProvider } from './providers/FallthroughProvider';
|
|
2
3
|
import { JsonRpcProvider, jsonRpcProvider } from './providers/JsonRpcProvider';
|
|
3
4
|
import { tinyBig, TinyBig } from './shared/tiny-big/tiny-big';
|
|
4
|
-
import { BlockResponse } from './types/Block.types';
|
|
5
|
+
import { BlockResponse, BlockTag, RPCBlock } from './types/Block.types';
|
|
5
6
|
import { ContractTypes, JSONABI, JSONABIArgument } from './types/Contract.types';
|
|
6
7
|
import { Network } from './types/Network.types';
|
|
7
|
-
import { TransactionResponse } from './types/Transaction.types';
|
|
8
|
+
import { BlockTransactionResponse, Log, RPCLog, RPCTransaction, RPCTransactionReceipt, TransactionReceipt, TransactionResponse } from './types/Transaction.types';
|
|
9
|
+
import { computeAddress } from './utils/compute-address';
|
|
10
|
+
import { computePublicKey } from './utils/compute-public-key';
|
|
8
11
|
import { etherToGwei } from './utils/ether-to-gwei';
|
|
9
12
|
import { etherToWei } from './utils/ether-to-wei';
|
|
10
13
|
import { gweiToEther } from './utils/gwei-to-ether';
|
|
14
|
+
import { hashMessage } from './utils/hash-message';
|
|
11
15
|
import { isAddress } from './utils/is-address';
|
|
16
|
+
import { splitSignature } from './utils/split-signature';
|
|
12
17
|
import { toChecksumAddress } from './utils/to-checksum-address';
|
|
18
|
+
import { toUtf8Bytes } from './utils/to-utf8-bytes';
|
|
13
19
|
import { weiToEther } from './utils/wei-to-ether';
|
|
14
20
|
export * from './utils/bytes';
|
|
21
|
+
export * from './utils/hash-message';
|
|
22
|
+
export * from './utils/keccak256';
|
|
15
23
|
export * from './utils/solidity-keccak256';
|
|
16
|
-
export { etherToWei, etherToGwei, isAddress, jsonRpcProvider, JsonRpcProvider, tinyBig, toChecksumAddress, weiToEther, gweiToEther, Contract, TinyBig, BlockResponse, ContractTypes, JSONABI, JSONABIArgument, Network, TransactionResponse, };
|
|
24
|
+
export { etherToWei, etherToGwei, isAddress, jsonRpcProvider, JsonRpcProvider, FallthroughProvider, tinyBig, toChecksumAddress, weiToEther, gweiToEther, hashMessage, splitSignature, toUtf8Bytes, computeAddress, computePublicKey, Contract, TinyBig, BaseContract, BlockResponse, ContractTypes, JSONABI, JSONABIArgument, Network, TransactionResponse, RPCBlock, RPCTransaction, RPCTransactionReceipt, TransactionReceipt, BlockTag, RPCLog, Log, BlockTransactionResponse, ConstructorOptions, };
|
package/lib/esm/index.js
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
import { Contract } from './classes/Contract';
|
|
1
|
+
import { BaseContract, Contract } from './classes/Contract';
|
|
2
|
+
import { FallthroughProvider, } from './providers/FallthroughProvider';
|
|
2
3
|
import { JsonRpcProvider, jsonRpcProvider } from './providers/JsonRpcProvider';
|
|
3
4
|
import { tinyBig, TinyBig } from './shared/tiny-big/tiny-big';
|
|
5
|
+
import { computeAddress } from './utils/compute-address';
|
|
6
|
+
import { computePublicKey } from './utils/compute-public-key';
|
|
4
7
|
import { etherToGwei } from './utils/ether-to-gwei';
|
|
5
8
|
import { etherToWei } from './utils/ether-to-wei';
|
|
6
9
|
import { gweiToEther } from './utils/gwei-to-ether';
|
|
10
|
+
import { hashMessage } from './utils/hash-message';
|
|
7
11
|
import { isAddress } from './utils/is-address';
|
|
12
|
+
import { splitSignature } from './utils/split-signature';
|
|
8
13
|
import { toChecksumAddress } from './utils/to-checksum-address';
|
|
14
|
+
import { toUtf8Bytes } from './utils/to-utf8-bytes';
|
|
9
15
|
import { weiToEther } from './utils/wei-to-ether';
|
|
10
16
|
export * from './utils/bytes';
|
|
17
|
+
export * from './utils/hash-message';
|
|
18
|
+
export * from './utils/keccak256';
|
|
11
19
|
export * from './utils/solidity-keccak256';
|
|
12
|
-
export { etherToWei, etherToGwei, isAddress, jsonRpcProvider, JsonRpcProvider, tinyBig, toChecksumAddress, weiToEther, gweiToEther, Contract, TinyBig, };
|
|
20
|
+
export { etherToWei, etherToGwei, isAddress, jsonRpcProvider, JsonRpcProvider, FallthroughProvider, tinyBig, toChecksumAddress, weiToEther, gweiToEther, hashMessage, splitSignature, toUtf8Bytes, computeAddress, computePublicKey, Contract, TinyBig, BaseContract, };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "0.5.
|
|
1
|
+
export declare const version = "0.5.5";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '0.5.
|
|
1
|
+
export const version = '0.5.5';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TinyBig } from '../shared/tiny-big/tiny-big';
|
|
2
|
+
import { BlockResponse, BlockTag } from '../types/Block.types';
|
|
3
|
+
import { Network } from '../types/Network.types';
|
|
4
|
+
import { TransactionReceipt, TransactionResponse } from '../types/Transaction.types';
|
|
5
|
+
export declare abstract class BaseProvider {
|
|
6
|
+
abstract selectRpcUrl(): string;
|
|
7
|
+
abstract post(body: Record<string, unknown>): Promise<any>;
|
|
8
|
+
readonly _rpcUrls: string[];
|
|
9
|
+
protected _post: (body: Record<string, unknown>) => Promise<any>;
|
|
10
|
+
constructor(rpcUrls: string[]);
|
|
11
|
+
getNetwork(): Promise<Network>;
|
|
12
|
+
getBlockNumber(): Promise<number>;
|
|
13
|
+
getTransaction(transactionHash: string): Promise<TransactionResponse>;
|
|
14
|
+
getTransactionReceipt(transactionHash: string): Promise<TransactionReceipt>;
|
|
15
|
+
getTransactionCount(address: string, blockTag?: BlockTag): Promise<number>;
|
|
16
|
+
getBlock(timeFrame?: BlockTag, returnTransactionObjects?: boolean): Promise<BlockResponse>;
|
|
17
|
+
getGasPrice(): Promise<TinyBig>;
|
|
18
|
+
getBalance(address: string, blockTag?: BlockTag): Promise<TinyBig>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
+
};
|
|
10
|
+
import { cleanBlock } from '../classes/utils/clean-block';
|
|
11
|
+
import { cleanTransaction } from '../classes/utils/clean-transaction';
|
|
12
|
+
import { cleanTransactionReceipt } from '../classes/utils/clean-transaction-receipt';
|
|
13
|
+
import { buildRPCPostBody, post } from '../classes/utils/fetchers';
|
|
14
|
+
import { hexToDecimal } from '../classes/utils/hex-to-decimal';
|
|
15
|
+
import { tinyBig } from '../shared/tiny-big/tiny-big';
|
|
16
|
+
import chainsInfo from './utils/chains-info';
|
|
17
|
+
export class BaseProvider {
|
|
18
|
+
constructor(rpcUrls) {
|
|
19
|
+
this._rpcUrls = [];
|
|
20
|
+
this._post = (body) => post(this.selectRpcUrl(), body);
|
|
21
|
+
this._rpcUrls = rpcUrls;
|
|
22
|
+
}
|
|
23
|
+
getNetwork() {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const hexChainId = (yield this.post(buildRPCPostBody('eth_chainId', [])));
|
|
26
|
+
const chainId = hexToDecimal(hexChainId);
|
|
27
|
+
const info = chainsInfo[chainId];
|
|
28
|
+
return {
|
|
29
|
+
chainId: Number(chainId),
|
|
30
|
+
name: info[0] || 'unknown',
|
|
31
|
+
ensAddress: info[1] || null,
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
getBlockNumber() {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
const currentBlockNumber = (yield this.post(buildRPCPostBody('eth_blockNumber', [])));
|
|
38
|
+
return Number(hexToDecimal(currentBlockNumber));
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
getTransaction(transactionHash) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
const [rpcTransaction, blockNumber] = yield Promise.all([
|
|
44
|
+
this.post(buildRPCPostBody('eth_getTransactionByHash', [transactionHash])),
|
|
45
|
+
this.getBlock('latest'),
|
|
46
|
+
]);
|
|
47
|
+
const cleanedTransaction = cleanTransaction(rpcTransaction);
|
|
48
|
+
cleanedTransaction.confirmations =
|
|
49
|
+
blockNumber.number - cleanedTransaction.blockNumber + 1;
|
|
50
|
+
return cleanedTransaction;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
getTransactionReceipt(transactionHash) {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
const [rpcTransaction, blockNumber] = yield Promise.all([
|
|
56
|
+
this.post(buildRPCPostBody('eth_getTransactionReceipt', [transactionHash])),
|
|
57
|
+
this.getBlock('latest'),
|
|
58
|
+
]);
|
|
59
|
+
const cleanedTransactionReceipt = cleanTransactionReceipt(rpcTransaction);
|
|
60
|
+
cleanedTransactionReceipt.confirmations =
|
|
61
|
+
blockNumber.number - cleanedTransactionReceipt.blockNumber + 1;
|
|
62
|
+
return cleanedTransactionReceipt;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
getTransactionCount(address, blockTag = 'latest') {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
if (typeof blockTag === 'number') {
|
|
68
|
+
blockTag = `0x${blockTag.toString(16)}`;
|
|
69
|
+
}
|
|
70
|
+
const transactionCount = (yield this.post(buildRPCPostBody('eth_getTransactionCount', [address, blockTag])));
|
|
71
|
+
return Number(hexToDecimal(transactionCount));
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
getBlock(timeFrame = 'latest', returnTransactionObjects = false) {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
let rpcTimeFrame;
|
|
77
|
+
let type = 'Number';
|
|
78
|
+
if (typeof timeFrame === 'number') {
|
|
79
|
+
rpcTimeFrame = `0x${timeFrame.toString(16)}`;
|
|
80
|
+
}
|
|
81
|
+
else if (timeFrame.startsWith('0x')) {
|
|
82
|
+
rpcTimeFrame = timeFrame;
|
|
83
|
+
type = 'Hash';
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
rpcTimeFrame = timeFrame;
|
|
87
|
+
}
|
|
88
|
+
const rpcBlock = (yield this.post(buildRPCPostBody(`eth_getBlockBy${type}`, [
|
|
89
|
+
rpcTimeFrame,
|
|
90
|
+
returnTransactionObjects,
|
|
91
|
+
])));
|
|
92
|
+
return cleanBlock(rpcBlock, returnTransactionObjects);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
getGasPrice() {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
const hexGasPrice = (yield this.post(buildRPCPostBody('eth_gasPrice', [])));
|
|
98
|
+
return tinyBig(hexToDecimal(hexGasPrice));
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
getBalance(address, blockTag = 'latest') {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
const hexBalance = (yield this.post(buildRPCPostBody('eth_getBalance', [address, blockTag])));
|
|
104
|
+
return tinyBig(hexToDecimal(hexBalance));
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BaseProvider } from './BaseProvider';
|
|
2
|
+
export interface ConstructorOptions {
|
|
3
|
+
timeoutDuration?: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class FallthroughProvider extends BaseProvider {
|
|
6
|
+
private rpcUrlCounter;
|
|
7
|
+
private readonly timeoutDuration;
|
|
8
|
+
selectRpcUrl(): string;
|
|
9
|
+
constructor(rpcUrls: string[], options?: ConstructorOptions);
|
|
10
|
+
post: (body: Record<string, unknown>) => Promise<any>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { logger } from '../logger/logger';
|
|
2
|
+
import { BaseProvider } from './BaseProvider';
|
|
3
|
+
const promiseTimeout = (prom, time) => Promise.race([
|
|
4
|
+
prom,
|
|
5
|
+
new Promise((_r, reject) => setTimeout(() => reject('Promise timed out'), time)),
|
|
6
|
+
]);
|
|
7
|
+
const DEFAULT_TIMEOUT_DURATION = 8000;
|
|
8
|
+
export class FallthroughProvider extends BaseProvider {
|
|
9
|
+
constructor(rpcUrls, options = {}) {
|
|
10
|
+
if (!Array.isArray(rpcUrls)) {
|
|
11
|
+
logger.throwError('Array required', { rpcUrls });
|
|
12
|
+
}
|
|
13
|
+
if (rpcUrls.length <= 1) {
|
|
14
|
+
logger.throwError('More than one rpcUrl is required', { rpcUrls });
|
|
15
|
+
}
|
|
16
|
+
super(rpcUrls);
|
|
17
|
+
this.rpcUrlCounter = 0;
|
|
18
|
+
this.post = (body) => {
|
|
19
|
+
const genesisCount = this.rpcUrlCounter;
|
|
20
|
+
const recursivePostRetry = () => {
|
|
21
|
+
const genesisRpcUrl = this.selectRpcUrl();
|
|
22
|
+
const res = promiseTimeout(this._post(body), this.timeoutDuration).catch((e) => {
|
|
23
|
+
if (genesisRpcUrl === this.selectRpcUrl()) {
|
|
24
|
+
this.rpcUrlCounter =
|
|
25
|
+
(this.rpcUrlCounter + 1) % this._rpcUrls.length;
|
|
26
|
+
}
|
|
27
|
+
if (this.rpcUrlCounter === genesisCount) {
|
|
28
|
+
throw e;
|
|
29
|
+
}
|
|
30
|
+
return recursivePostRetry();
|
|
31
|
+
});
|
|
32
|
+
return res;
|
|
33
|
+
};
|
|
34
|
+
return recursivePostRetry();
|
|
35
|
+
};
|
|
36
|
+
this.timeoutDuration = options.timeoutDuration || DEFAULT_TIMEOUT_DURATION;
|
|
37
|
+
}
|
|
38
|
+
selectRpcUrl() {
|
|
39
|
+
return this._rpcUrls[this.rpcUrlCounter];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export declare class JsonRpcProvider {
|
|
6
|
-
readonly _rpcUrl: string;
|
|
7
|
-
private post;
|
|
1
|
+
import { BaseProvider } from './BaseProvider';
|
|
2
|
+
export declare class JsonRpcProvider extends BaseProvider {
|
|
3
|
+
selectRpcUrl(): string;
|
|
4
|
+
post(body: Record<string, unknown>): Promise<any>;
|
|
8
5
|
constructor(rpcUrl?: string);
|
|
9
|
-
getBlock(timeFrame?: BlockTag, returnTransactionObjects?: boolean): Promise<BlockResponse>;
|
|
10
|
-
getNetwork(): Promise<Network>;
|
|
11
|
-
getGasPrice(): Promise<TinyBig>;
|
|
12
|
-
getBalance(address: string, blockTag?: BlockTag): Promise<TinyBig>;
|
|
13
|
-
getTransaction(hash: string): Promise<TransactionResponse>;
|
|
14
6
|
}
|
|
15
7
|
export declare function jsonRpcProvider(rpcUrl?: string): JsonRpcProvider;
|
|
@@ -1,74 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
};
|
|
10
|
-
import { cleanBlock } from '../classes/utils/clean-block';
|
|
11
|
-
import { cleanTransaction } from '../classes/utils/clean-transaction';
|
|
12
|
-
import { buildRPCPostBody, post } from '../classes/utils/fetchers';
|
|
13
|
-
import { hexToDecimal } from '../classes/utils/hex-to-decimal';
|
|
14
|
-
import { tinyBig } from '../shared/tiny-big/tiny-big';
|
|
15
|
-
import chainsInfo from './utils/chains-info';
|
|
16
|
-
export class JsonRpcProvider {
|
|
17
|
-
constructor(rpcUrl) {
|
|
18
|
-
this.post = (body) => post(this._rpcUrl, body);
|
|
19
|
-
this._rpcUrl = rpcUrl || 'https://free-eth-node.com/api/eth';
|
|
1
|
+
import { BaseProvider } from './BaseProvider';
|
|
2
|
+
export class JsonRpcProvider extends BaseProvider {
|
|
3
|
+
selectRpcUrl() {
|
|
4
|
+
return this._rpcUrls[0];
|
|
20
5
|
}
|
|
21
|
-
|
|
22
|
-
return
|
|
23
|
-
let rpcTimeFrame;
|
|
24
|
-
if (typeof timeFrame === 'number') {
|
|
25
|
-
rpcTimeFrame = `0x${timeFrame.toString(16)}`;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
rpcTimeFrame = timeFrame;
|
|
29
|
-
}
|
|
30
|
-
const rpcBlock = (yield this.post(buildRPCPostBody('eth_getBlockByNumber', [
|
|
31
|
-
rpcTimeFrame,
|
|
32
|
-
returnTransactionObjects,
|
|
33
|
-
])));
|
|
34
|
-
return cleanBlock(rpcBlock, returnTransactionObjects);
|
|
35
|
-
});
|
|
6
|
+
post(body) {
|
|
7
|
+
return this._post(body);
|
|
36
8
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const hexChainId = (yield this.post(buildRPCPostBody('eth_chainId', [])));
|
|
40
|
-
const chainId = hexToDecimal(hexChainId);
|
|
41
|
-
const info = chainsInfo[chainId];
|
|
42
|
-
return {
|
|
43
|
-
chainId: Number(chainId),
|
|
44
|
-
name: info[0] || 'unknown',
|
|
45
|
-
ensAddress: info[1] || null,
|
|
46
|
-
};
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
getGasPrice() {
|
|
50
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
const hexGasPrice = (yield this.post(buildRPCPostBody('eth_gasPrice', [])));
|
|
52
|
-
return tinyBig(hexToDecimal(hexGasPrice));
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
getBalance(address, blockTag = 'latest') {
|
|
56
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
const hexBalance = (yield this.post(buildRPCPostBody('eth_getBalance', [address, blockTag])));
|
|
58
|
-
return tinyBig(hexToDecimal(hexBalance));
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
getTransaction(hash) {
|
|
62
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
-
const [rpcTransaction, blockNumber] = yield Promise.all([
|
|
64
|
-
this.post(buildRPCPostBody('eth_getTransactionByHash', [hash])),
|
|
65
|
-
this.getBlock('latest'),
|
|
66
|
-
]);
|
|
67
|
-
const cleanedTransaction = cleanTransaction(rpcTransaction);
|
|
68
|
-
cleanedTransaction.confirmations =
|
|
69
|
-
blockNumber.number - cleanedTransaction.blockNumber + 1;
|
|
70
|
-
return cleanedTransaction;
|
|
71
|
-
});
|
|
9
|
+
constructor(rpcUrl = 'https://free-eth-node.com/api/eth') {
|
|
10
|
+
super([rpcUrl]);
|
|
72
11
|
}
|
|
73
12
|
}
|
|
74
13
|
export function jsonRpcProvider(rpcUrl) {
|
|
@@ -3,6 +3,7 @@ export const fakeUrls = {
|
|
|
3
3
|
};
|
|
4
4
|
export const rpcUrls = {
|
|
5
5
|
mainnet: `${process.env.RPC_ORIGIN}/api/eth`,
|
|
6
|
+
matic: `${process.env.RPC_ORIGIN}/api/MATIC`,
|
|
6
7
|
gno: `${process.env.RPC_ORIGIN}/api/gno`,
|
|
7
8
|
bnb: `${process.env.RPC_ORIGIN}/api/bnb`,
|
|
8
9
|
arb1: `${process.env.RPC_ORIGIN}/api/arb1`,
|
|
@@ -61,6 +61,7 @@ declare const _default: {
|
|
|
61
61
|
"67": string[];
|
|
62
62
|
"68": string[];
|
|
63
63
|
"69": string[];
|
|
64
|
+
"70": string[];
|
|
64
65
|
"71": string[];
|
|
65
66
|
"74": string[];
|
|
66
67
|
"76": string[];
|
|
@@ -118,6 +119,7 @@ declare const _default: {
|
|
|
118
119
|
"222": string[];
|
|
119
120
|
"225": string[];
|
|
120
121
|
"226": string[];
|
|
122
|
+
"239": string[];
|
|
121
123
|
"246": string[];
|
|
122
124
|
"250": string[];
|
|
123
125
|
"256": string[];
|
|
@@ -142,10 +144,14 @@ declare const _default: {
|
|
|
142
144
|
"499": string[];
|
|
143
145
|
"512": string[];
|
|
144
146
|
"513": string[];
|
|
147
|
+
"534": string[];
|
|
145
148
|
"555": string[];
|
|
146
149
|
"558": string[];
|
|
147
150
|
"588": string[];
|
|
151
|
+
"592": string[];
|
|
148
152
|
"595": string[];
|
|
153
|
+
"596": string[];
|
|
154
|
+
"597": string[];
|
|
149
155
|
"600": string[];
|
|
150
156
|
"666": string[];
|
|
151
157
|
"686": string[];
|
|
@@ -153,6 +159,7 @@ declare const _default: {
|
|
|
153
159
|
"707": string[];
|
|
154
160
|
"708": string[];
|
|
155
161
|
"721": string[];
|
|
162
|
+
"776": string[];
|
|
156
163
|
"777": string[];
|
|
157
164
|
"787": string[];
|
|
158
165
|
"788": string[];
|
|
@@ -200,14 +207,18 @@ declare const _default: {
|
|
|
200
207
|
"1620": string[];
|
|
201
208
|
"1657": string[];
|
|
202
209
|
"1688": string[];
|
|
210
|
+
"1819": string[];
|
|
203
211
|
"1856": string[];
|
|
204
212
|
"1898": string[];
|
|
205
213
|
"1984": string[];
|
|
206
214
|
"1987": string[];
|
|
207
215
|
"2001": string[];
|
|
216
|
+
"2008": string[];
|
|
217
|
+
"2009": string[];
|
|
208
218
|
"2020": string[];
|
|
209
219
|
"2021": string[];
|
|
210
220
|
"2022": string[];
|
|
221
|
+
"2023": string[];
|
|
211
222
|
"2025": string[];
|
|
212
223
|
"2100": string[];
|
|
213
224
|
"2101": string[];
|
|
@@ -217,6 +228,9 @@ declare const _default: {
|
|
|
217
228
|
"2221": string[];
|
|
218
229
|
"2223": string[];
|
|
219
230
|
"2559": string[];
|
|
231
|
+
"2569": string[];
|
|
232
|
+
"2612": string[];
|
|
233
|
+
"2613": string[];
|
|
220
234
|
"3000": string[];
|
|
221
235
|
"3001": string[];
|
|
222
236
|
"3331": string[];
|
|
@@ -252,6 +266,7 @@ declare const _default: {
|
|
|
252
266
|
"8723": string[];
|
|
253
267
|
"8724": string[];
|
|
254
268
|
"8888": string[];
|
|
269
|
+
"8898": string[];
|
|
255
270
|
"8995": string[];
|
|
256
271
|
"9000": string[];
|
|
257
272
|
"9001": string[];
|
|
@@ -272,10 +287,13 @@ declare const _default: {
|
|
|
272
287
|
"19845": string[];
|
|
273
288
|
"21337": string[];
|
|
274
289
|
"21816": string[];
|
|
290
|
+
"22023": string[];
|
|
275
291
|
"24484": string[];
|
|
276
292
|
"24734": string[];
|
|
293
|
+
"30067": string[];
|
|
277
294
|
"31102": string[];
|
|
278
295
|
"31337": string[];
|
|
296
|
+
"32520": string[];
|
|
279
297
|
"32659": string[];
|
|
280
298
|
"39797": string[];
|
|
281
299
|
"42069": string[];
|
|
@@ -298,6 +316,7 @@ declare const _default: {
|
|
|
298
316
|
"60002": string[];
|
|
299
317
|
"60103": string[];
|
|
300
318
|
"62320": string[];
|
|
319
|
+
"62621": string[];
|
|
301
320
|
"63000": string[];
|
|
302
321
|
"63001": string[];
|
|
303
322
|
"69420": string[];
|
|
@@ -343,6 +362,7 @@ declare const _default: {
|
|
|
343
362
|
"333999": string[];
|
|
344
363
|
"421611": string[];
|
|
345
364
|
"444900": string[];
|
|
365
|
+
"512512": string[];
|
|
346
366
|
"666666": string[];
|
|
347
367
|
"888888": string[];
|
|
348
368
|
"955305": string[];
|