essential-eth 0.10.3 → 0.11.1
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/dist/index.cjs +2024 -0
- package/dist/index.d.cts +330 -0
- package/dist/index.d.ts +330 -0
- package/dist/index.js +1952 -0
- package/package.json +14 -15
- package/readme.md +2 -2
- package/dist/cjs/classes/Contract.d.ts +0 -81
- package/dist/cjs/classes/Contract.js +0 -139
- package/dist/cjs/classes/test/Contract/crv-abi.d.ts +0 -2
- package/dist/cjs/classes/test/Contract/crv-abi.js +0 -413
- package/dist/cjs/classes/test/Contract/ens-abi.d.ts +0 -2
- package/dist/cjs/classes/test/Contract/ens-abi.js +0 -453
- package/dist/cjs/classes/test/Contract/fei-abi.d.ts +0 -2
- package/dist/cjs/classes/test/Contract/fei-abi.js +0 -526
- package/dist/cjs/classes/test/Contract/foo-abi.d.ts +0 -2
- package/dist/cjs/classes/test/Contract/foo-abi.js +0 -42
- package/dist/cjs/classes/test/Contract/jokerrace-abi.d.ts +0 -2
- package/dist/cjs/classes/test/Contract/jokerrace-abi.js +0 -22
- package/dist/cjs/classes/test/Contract/uniswap-abi.d.ts +0 -2
- package/dist/cjs/classes/test/Contract/uniswap-abi.js +0 -121
- package/dist/cjs/classes/utils/clean-block.d.ts +0 -120
- package/dist/cjs/classes/utils/clean-block.js +0 -160
- package/dist/cjs/classes/utils/clean-log.d.ts +0 -9
- package/dist/cjs/classes/utils/clean-log.js +0 -39
- package/dist/cjs/classes/utils/clean-transaction-receipt.d.ts +0 -14
- package/dist/cjs/classes/utils/clean-transaction-receipt.js +0 -53
- package/dist/cjs/classes/utils/clean-transaction.d.ts +0 -14
- package/dist/cjs/classes/utils/clean-transaction.js +0 -54
- package/dist/cjs/classes/utils/encode-decode-transaction.d.ts +0 -16
- package/dist/cjs/classes/utils/encode-decode-transaction.js +0 -185
- package/dist/cjs/classes/utils/fetchers.d.ts +0 -53
- package/dist/cjs/classes/utils/fetchers.js +0 -85
- package/dist/cjs/classes/utils/hex-to-decimal.d.ts +0 -18
- package/dist/cjs/classes/utils/hex-to-decimal.js +0 -24
- package/dist/cjs/classes/utils/prepare-transaction.d.ts +0 -6
- package/dist/cjs/classes/utils/prepare-transaction.js +0 -47
- package/dist/cjs/index.d.ts +0 -24
- package/dist/cjs/index.js +0 -58
- package/dist/cjs/index.umd.js +0 -2
- package/dist/cjs/index.umd.js.map +0 -1
- package/dist/cjs/logger/logger.d.ts +0 -11
- package/dist/cjs/logger/logger.js +0 -37
- package/dist/cjs/logger/package-version.d.ts +0 -1
- package/dist/cjs/logger/package-version.js +0 -5
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/providers/AlchemyProvider.d.ts +0 -4
- package/dist/cjs/providers/AlchemyProvider.js +0 -11
- package/dist/cjs/providers/BaseProvider.d.ts +0 -374
- package/dist/cjs/providers/BaseProvider.js +0 -514
- package/dist/cjs/providers/FallthroughProvider.d.ts +0 -24
- package/dist/cjs/providers/FallthroughProvider.js +0 -74
- package/dist/cjs/providers/JsonRpcProvider.d.ts +0 -33
- package/dist/cjs/providers/JsonRpcProvider.js +0 -46
- package/dist/cjs/providers/test/json-rpc-provider/get-logs/mocks.d.ts +0 -14
- package/dist/cjs/providers/test/json-rpc-provider/get-logs/mocks.js +0 -152
- package/dist/cjs/providers/test/mock-of.d.ts +0 -8
- package/dist/cjs/providers/test/mock-of.js +0 -12
- package/dist/cjs/providers/test/rpc-urls.d.ts +0 -12
- package/dist/cjs/providers/test/rpc-urls.js +0 -27
- package/dist/cjs/providers/utils/chains-info.d.ts +0 -24
- package/dist/cjs/providers/utils/chains-info.js +0 -71
- package/dist/cjs/shared/tiny-big/helpers.d.ts +0 -17
- package/dist/cjs/shared/tiny-big/helpers.js +0 -97
- package/dist/cjs/shared/tiny-big/tiny-big.d.ts +0 -58
- package/dist/cjs/shared/tiny-big/tiny-big.js +0 -101
- package/dist/cjs/shared/validate-type.d.ts +0 -3
- package/dist/cjs/shared/validate-type.js +0 -9
- package/dist/cjs/types/Block.types.d.ts +0 -41
- package/dist/cjs/types/Block.types.js +0 -2
- package/dist/cjs/types/Contract.types.d.ts +0 -24
- package/dist/cjs/types/Contract.types.js +0 -2
- package/dist/cjs/types/FeeData.types.d.ts +0 -7
- package/dist/cjs/types/FeeData.types.js +0 -2
- package/dist/cjs/types/Filter.types.d.ts +0 -16
- package/dist/cjs/types/Filter.types.js +0 -2
- package/dist/cjs/types/Network.types.d.ts +0 -8
- package/dist/cjs/types/Network.types.js +0 -2
- package/dist/cjs/types/Transaction.types.d.ts +0 -123
- package/dist/cjs/types/Transaction.types.js +0 -2
- package/dist/cjs/utils/bytes.d.ts +0 -289
- package/dist/cjs/utils/bytes.js +0 -711
- package/dist/cjs/utils/compute-address.d.ts +0 -17
- package/dist/cjs/utils/compute-address.js +0 -34
- package/dist/cjs/utils/compute-public-key.d.ts +0 -18
- package/dist/cjs/utils/compute-public-key.js +0 -26
- package/dist/cjs/utils/ether-to-gwei.d.ts +0 -27
- package/dist/cjs/utils/ether-to-gwei.js +0 -35
- package/dist/cjs/utils/ether-to-wei.d.ts +0 -27
- package/dist/cjs/utils/ether-to-wei.js +0 -35
- package/dist/cjs/utils/gwei-to-ether.d.ts +0 -27
- package/dist/cjs/utils/gwei-to-ether.js +0 -35
- package/dist/cjs/utils/hash-message.d.ts +0 -14
- package/dist/cjs/utils/hash-message.js +0 -30
- package/dist/cjs/utils/is-address.d.ts +0 -24
- package/dist/cjs/utils/is-address.js +0 -39
- package/dist/cjs/utils/keccak256.d.ts +0 -16
- package/dist/cjs/utils/keccak256.js +0 -31
- package/dist/cjs/utils/solidity-keccak256.d.ts +0 -39
- package/dist/cjs/utils/solidity-keccak256.js +0 -138
- package/dist/cjs/utils/split-signature.d.ts +0 -24
- package/dist/cjs/utils/split-signature.js +0 -163
- package/dist/cjs/utils/to-checksum-address.d.ts +0 -17
- package/dist/cjs/utils/to-checksum-address.js +0 -46
- package/dist/cjs/utils/to-utf8-bytes.d.ts +0 -15
- package/dist/cjs/utils/to-utf8-bytes.js +0 -21
- package/dist/cjs/utils/wei-to-ether.d.ts +0 -27
- package/dist/cjs/utils/wei-to-ether.js +0 -45
- package/dist/esm/classes/Contract.d.ts +0 -11
- package/dist/esm/classes/Contract.js +0 -58
- package/dist/esm/classes/test/Contract/crv-abi.d.ts +0 -2
- package/dist/esm/classes/test/Contract/crv-abi.js +0 -395
- package/dist/esm/classes/test/Contract/ens-abi.d.ts +0 -2
- package/dist/esm/classes/test/Contract/ens-abi.js +0 -435
- package/dist/esm/classes/test/Contract/fei-abi.d.ts +0 -2
- package/dist/esm/classes/test/Contract/fei-abi.js +0 -521
- package/dist/esm/classes/test/Contract/foo-abi.d.ts +0 -2
- package/dist/esm/classes/test/Contract/foo-abi.js +0 -33
- package/dist/esm/classes/test/Contract/jokerrace-abi.d.ts +0 -2
- package/dist/esm/classes/test/Contract/jokerrace-abi.js +0 -18
- package/dist/esm/classes/test/Contract/uniswap-abi.d.ts +0 -2
- package/dist/esm/classes/test/Contract/uniswap-abi.js +0 -116
- package/dist/esm/classes/utils/clean-block.d.ts +0 -2
- package/dist/esm/classes/utils/clean-block.js +0 -35
- package/dist/esm/classes/utils/clean-log.d.ts +0 -2
- package/dist/esm/classes/utils/clean-log.js +0 -28
- package/dist/esm/classes/utils/clean-transaction-receipt.d.ts +0 -2
- package/dist/esm/classes/utils/clean-transaction-receipt.js +0 -37
- package/dist/esm/classes/utils/clean-transaction.d.ts +0 -2
- package/dist/esm/classes/utils/clean-transaction.js +0 -36
- package/dist/esm/classes/utils/encode-decode-transaction.d.ts +0 -4
- package/dist/esm/classes/utils/encode-decode-transaction.js +0 -143
- package/dist/esm/classes/utils/fetchers.d.ts +0 -15
- package/dist/esm/classes/utils/fetchers.js +0 -37
- package/dist/esm/classes/utils/hex-to-decimal.d.ts +0 -1
- package/dist/esm/classes/utils/hex-to-decimal.js +0 -3
- package/dist/esm/classes/utils/prepare-transaction.d.ts +0 -2
- package/dist/esm/classes/utils/prepare-transaction.js +0 -36
- package/dist/esm/index.d.ts +0 -24
- package/dist/esm/index.js +0 -19
- package/dist/esm/logger/logger.d.ts +0 -11
- package/dist/esm/logger/logger.js +0 -34
- package/dist/esm/logger/package-version.d.ts +0 -1
- package/dist/esm/logger/package-version.js +0 -1
- package/dist/esm/providers/AlchemyProvider.d.ts +0 -4
- package/dist/esm/providers/AlchemyProvider.js +0 -7
- package/dist/esm/providers/BaseProvider.d.ts +0 -26
- package/dist/esm/providers/BaseProvider.js +0 -137
- package/dist/esm/providers/FallthroughProvider.d.ts +0 -11
- package/dist/esm/providers/FallthroughProvider.js +0 -50
- package/dist/esm/providers/JsonRpcProvider.d.ts +0 -7
- package/dist/esm/providers/JsonRpcProvider.js +0 -15
- package/dist/esm/providers/test/json-rpc-provider/get-logs/mocks.d.ts +0 -14
- package/dist/esm/providers/test/json-rpc-provider/get-logs/mocks.js +0 -142
- package/dist/esm/providers/test/mock-of.d.ts +0 -1
- package/dist/esm/providers/test/mock-of.js +0 -1
- package/dist/esm/providers/test/rpc-urls.d.ts +0 -12
- package/dist/esm/providers/test/rpc-urls.js +0 -21
- package/dist/esm/providers/utils/chains-info.d.ts +0 -24
- package/dist/esm/providers/utils/chains-info.js +0 -67
- package/dist/esm/shared/tiny-big/helpers.d.ts +0 -1
- package/dist/esm/shared/tiny-big/helpers.js +0 -58
- package/dist/esm/shared/tiny-big/tiny-big.d.ts +0 -10
- package/dist/esm/shared/tiny-big/tiny-big.js +0 -45
- package/dist/esm/shared/validate-type.d.ts +0 -3
- package/dist/esm/shared/validate-type.js +0 -5
- package/dist/esm/types/Block.types.d.ts +0 -40
- package/dist/esm/types/Block.types.js +0 -1
- package/dist/esm/types/Contract.types.d.ts +0 -24
- package/dist/esm/types/Contract.types.js +0 -1
- package/dist/esm/types/FeeData.types.d.ts +0 -7
- package/dist/esm/types/FeeData.types.js +0 -1
- package/dist/esm/types/Filter.types.d.ts +0 -12
- package/dist/esm/types/Filter.types.js +0 -1
- package/dist/esm/types/Network.types.d.ts +0 -5
- package/dist/esm/types/Network.types.js +0 -1
- package/dist/esm/types/Transaction.types.d.ts +0 -113
- package/dist/esm/types/Transaction.types.js +0 -1
- package/dist/esm/utils/bytes.d.ts +0 -40
- package/dist/esm/utils/bytes.js +0 -245
- package/dist/esm/utils/compute-address.d.ts +0 -1
- package/dist/esm/utils/compute-address.js +0 -12
- package/dist/esm/utils/compute-public-key.d.ts +0 -2
- package/dist/esm/utils/compute-public-key.js +0 -6
- package/dist/esm/utils/ether-to-gwei.d.ts +0 -3
- package/dist/esm/utils/ether-to-gwei.js +0 -7
- package/dist/esm/utils/ether-to-wei.d.ts +0 -3
- package/dist/esm/utils/ether-to-wei.js +0 -7
- package/dist/esm/utils/gwei-to-ether.d.ts +0 -3
- package/dist/esm/utils/gwei-to-ether.js +0 -7
- package/dist/esm/utils/hash-message.d.ts +0 -2
- package/dist/esm/utils/hash-message.js +0 -14
- package/dist/esm/utils/is-address.d.ts +0 -1
- package/dist/esm/utils/is-address.js +0 -12
- package/dist/esm/utils/keccak256.d.ts +0 -2
- package/dist/esm/utils/keccak256.js +0 -13
- package/dist/esm/utils/solidity-keccak256.d.ts +0 -2
- package/dist/esm/utils/solidity-keccak256.js +0 -85
- package/dist/esm/utils/split-signature.d.ts +0 -2
- package/dist/esm/utils/split-signature.js +0 -126
- package/dist/esm/utils/to-checksum-address.d.ts +0 -1
- package/dist/esm/utils/to-checksum-address.js +0 -25
- package/dist/esm/utils/to-utf8-bytes.d.ts +0 -1
- package/dist/esm/utils/to-utf8-bytes.js +0 -3
- package/dist/esm/utils/wei-to-ether.d.ts +0 -3
- package/dist/esm/utils/wei-to-ether.js +0 -16
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2024 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
AlchemyProvider: () => AlchemyProvider,
|
|
34
|
+
BaseContract: () => BaseContract,
|
|
35
|
+
Contract: () => Contract,
|
|
36
|
+
FallthroughProvider: () => FallthroughProvider,
|
|
37
|
+
JsonRpcProvider: () => JsonRpcProvider,
|
|
38
|
+
TinyBig: () => TinyBig,
|
|
39
|
+
arrayify: () => arrayify,
|
|
40
|
+
computeAddress: () => computeAddress,
|
|
41
|
+
computePublicKey: () => computePublicKey,
|
|
42
|
+
concat: () => concat,
|
|
43
|
+
etherToGwei: () => etherToGwei,
|
|
44
|
+
etherToWei: () => etherToWei,
|
|
45
|
+
gweiToEther: () => gweiToEther,
|
|
46
|
+
hashMessage: () => hashMessage,
|
|
47
|
+
hexConcat: () => hexConcat,
|
|
48
|
+
hexDataLength: () => hexDataLength,
|
|
49
|
+
hexDataSlice: () => hexDataSlice,
|
|
50
|
+
hexStripZeros: () => hexStripZeros,
|
|
51
|
+
hexValue: () => hexValue,
|
|
52
|
+
hexZeroPad: () => hexZeroPad,
|
|
53
|
+
hexlify: () => hexlify,
|
|
54
|
+
isAddress: () => isAddress,
|
|
55
|
+
isBytes: () => isBytes,
|
|
56
|
+
isBytesLike: () => isBytesLike,
|
|
57
|
+
isHexString: () => isHexString,
|
|
58
|
+
jsonRpcProvider: () => jsonRpcProvider,
|
|
59
|
+
keccak256: () => keccak256,
|
|
60
|
+
pack: () => pack,
|
|
61
|
+
solidityKeccak256: () => solidityKeccak256,
|
|
62
|
+
splitSignature: () => splitSignature,
|
|
63
|
+
stripZeros: () => stripZeros,
|
|
64
|
+
tinyBig: () => tinyBig,
|
|
65
|
+
toChecksumAddress: () => toChecksumAddress,
|
|
66
|
+
toUtf8Bytes: () => toUtf8Bytes,
|
|
67
|
+
weiToEther: () => weiToEther,
|
|
68
|
+
zeroPad: () => zeroPad
|
|
69
|
+
});
|
|
70
|
+
module.exports = __toCommonJS(index_exports);
|
|
71
|
+
|
|
72
|
+
// src/classes/utils/encode-decode-transaction.ts
|
|
73
|
+
var import_sha32 = require("sha3");
|
|
74
|
+
|
|
75
|
+
// src/shared/tiny-big/tiny-big.ts
|
|
76
|
+
var import_big = __toESM(require("big.js"), 1);
|
|
77
|
+
|
|
78
|
+
// src/classes/utils/hex-to-decimal.ts
|
|
79
|
+
function hexToDecimal(hex) {
|
|
80
|
+
return BigInt(hex).toString();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/shared/tiny-big/helpers.ts
|
|
84
|
+
function stripTrailingZeroes(numberString) {
|
|
85
|
+
const isNegative = numberString.startsWith("-");
|
|
86
|
+
numberString = numberString.replace("-", "");
|
|
87
|
+
numberString = numberString.replace(
|
|
88
|
+
/\.0*$/g,
|
|
89
|
+
""
|
|
90
|
+
);
|
|
91
|
+
numberString = numberString.replace(/^0+/, "");
|
|
92
|
+
if (numberString.includes(".")) {
|
|
93
|
+
numberString = numberString.replace(/0+$/, "");
|
|
94
|
+
}
|
|
95
|
+
if (numberString.startsWith(".")) {
|
|
96
|
+
numberString = `0${numberString}`;
|
|
97
|
+
}
|
|
98
|
+
return `${isNegative ? "-" : ""}${numberString}`;
|
|
99
|
+
}
|
|
100
|
+
function scientificStrToDecimalStr(scientificString) {
|
|
101
|
+
if (!scientificString.match(
|
|
102
|
+
/e/i
|
|
103
|
+
/* lowercase and uppercase E */
|
|
104
|
+
)) {
|
|
105
|
+
return stripTrailingZeroes(scientificString);
|
|
106
|
+
}
|
|
107
|
+
let [base, power] = scientificString.split(
|
|
108
|
+
/e/i
|
|
109
|
+
);
|
|
110
|
+
const isNegative = Number(base) < 0;
|
|
111
|
+
base = base.replace("-", "");
|
|
112
|
+
base = stripTrailingZeroes(base);
|
|
113
|
+
const [wholeNumber, fraction = ""] = base.split(".");
|
|
114
|
+
if (Number(power) === 0) {
|
|
115
|
+
return `${isNegative ? "-" : ""}${stripTrailingZeroes(base)}`;
|
|
116
|
+
} else {
|
|
117
|
+
const includesDecimal = base.includes(".");
|
|
118
|
+
if (!includesDecimal) {
|
|
119
|
+
base = `${base}.`;
|
|
120
|
+
}
|
|
121
|
+
base = base.replace(".", "");
|
|
122
|
+
const baseLength = base.length;
|
|
123
|
+
let splitPaddedNumber;
|
|
124
|
+
if (Number(power) < 0) {
|
|
125
|
+
if (wholeNumber.length < Math.abs(Number(power))) {
|
|
126
|
+
base = base.padStart(
|
|
127
|
+
baseLength + Math.abs(Number(power)) - wholeNumber.length,
|
|
128
|
+
"0"
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
splitPaddedNumber = base.split("");
|
|
132
|
+
if (wholeNumber.length < Math.abs(Number(power))) {
|
|
133
|
+
splitPaddedNumber = [".", ...splitPaddedNumber];
|
|
134
|
+
} else {
|
|
135
|
+
splitPaddedNumber.splice(
|
|
136
|
+
splitPaddedNumber.length - Math.abs(Number(power)),
|
|
137
|
+
0,
|
|
138
|
+
"."
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
if (fraction.length < Math.abs(Number(power))) {
|
|
143
|
+
base = base.padEnd(
|
|
144
|
+
baseLength + Math.abs(Number(power)) - fraction.length,
|
|
145
|
+
"0"
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
splitPaddedNumber = base.split("");
|
|
149
|
+
if (fraction.length > Math.abs(Number(power))) {
|
|
150
|
+
splitPaddedNumber.splice(
|
|
151
|
+
splitPaddedNumber.length - Math.abs(Number(power)),
|
|
152
|
+
0,
|
|
153
|
+
"."
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const toReturn = stripTrailingZeroes(splitPaddedNumber.join(""));
|
|
158
|
+
return `${isNegative ? "-" : ""}${toReturn}`;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/shared/tiny-big/tiny-big.ts
|
|
163
|
+
var TinyBig = class extends import_big.default {
|
|
164
|
+
constructor(value) {
|
|
165
|
+
if (typeof value === "string" && value.startsWith("0x")) {
|
|
166
|
+
value = hexToDecimal(value);
|
|
167
|
+
}
|
|
168
|
+
super(value);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Used anytime you're passing in "value" to ethers or web3
|
|
172
|
+
* For now, TypeScript will complain that `TinyBig` is not a `BigNumberish`. You can // @ts-ignore or call this
|
|
173
|
+
*
|
|
174
|
+
* @returns the TinyBig represented as a hex string
|
|
175
|
+
* @example
|
|
176
|
+
* ```javascript
|
|
177
|
+
* tinyBig(293).toHexString();
|
|
178
|
+
* // '0x125'
|
|
179
|
+
* ```
|
|
180
|
+
* @example
|
|
181
|
+
* ```javascript
|
|
182
|
+
* tinyBig(681365874).toHexString();
|
|
183
|
+
* // '0x289cd172'
|
|
184
|
+
*/
|
|
185
|
+
toHexString() {
|
|
186
|
+
return `0x${BigInt(this.toString()).toString(16)}`;
|
|
187
|
+
}
|
|
188
|
+
toNumber() {
|
|
189
|
+
return Number(scientificStrToDecimalStr(super.toString()));
|
|
190
|
+
}
|
|
191
|
+
toString() {
|
|
192
|
+
if (this.toNumber() === 0) {
|
|
193
|
+
return "0";
|
|
194
|
+
}
|
|
195
|
+
return scientificStrToDecimalStr(super.toString());
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Eithers pads or shortens a string to a specified length
|
|
199
|
+
*
|
|
200
|
+
* @param str the string to pad or chop
|
|
201
|
+
* @param padChar the character to pad the string with
|
|
202
|
+
* @param length the desired length of the given string
|
|
203
|
+
* @returns a string of the desired length, either padded with the specified padChar or with the beginning of the string chopped off
|
|
204
|
+
* @example
|
|
205
|
+
* ```javascript
|
|
206
|
+
* padAndChop('essential-eth', 'a', 8);
|
|
207
|
+
* // 'tial-eth'
|
|
208
|
+
* ```
|
|
209
|
+
* @example
|
|
210
|
+
* ```javascript
|
|
211
|
+
* padAndChop('essential-eth', 'A', 20);
|
|
212
|
+
* // 'AAAAAAAessential-eth'
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
padAndChop = (str, padChar, length) => {
|
|
216
|
+
return (Array(length).fill(padChar).join("") + str).slice(length * -1);
|
|
217
|
+
};
|
|
218
|
+
toTwos(bitCount) {
|
|
219
|
+
let binaryStr;
|
|
220
|
+
if (this.gte(0)) {
|
|
221
|
+
const twosComp = this.toNumber().toString(2);
|
|
222
|
+
binaryStr = this.padAndChop(twosComp, "0", bitCount || twosComp.length);
|
|
223
|
+
} else {
|
|
224
|
+
binaryStr = this.plus(Math.pow(2, bitCount)).toNumber().toString(2);
|
|
225
|
+
if (Number(binaryStr) < 0) {
|
|
226
|
+
throw new Error("Cannot calculate twos complement");
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const binary = `0b${binaryStr}`;
|
|
230
|
+
const decimal = Number(binary);
|
|
231
|
+
return tinyBig(decimal);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
function tinyBig(value) {
|
|
235
|
+
return new TinyBig(value);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/utils/to-checksum-address.ts
|
|
239
|
+
var import_sha3 = require("sha3");
|
|
240
|
+
|
|
241
|
+
// src/shared/validate-type.ts
|
|
242
|
+
var validateType = (value, allowedTypes) => {
|
|
243
|
+
if (!allowedTypes.includes(typeof value)) {
|
|
244
|
+
throw new Error(
|
|
245
|
+
`${allowedTypes.join(" or ")} required. Received ${typeof value}`
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
// src/utils/to-checksum-address.ts
|
|
251
|
+
function toChecksumAddress(address) {
|
|
252
|
+
validateType(address, ["string"]);
|
|
253
|
+
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
|
|
254
|
+
throw new Error(`Invalid Ethereum address "${address}"`);
|
|
255
|
+
}
|
|
256
|
+
const _address = address.toLowerCase().replace(/^0x/i, "");
|
|
257
|
+
const keccak = new import_sha3.Keccak(256);
|
|
258
|
+
const addressHash = keccak.update(_address).digest("hex").replace(/^0x/i, "");
|
|
259
|
+
let checksumAddress = "0x";
|
|
260
|
+
for (let i = 0; i < _address.length; i++) {
|
|
261
|
+
if (parseInt(addressHash[i], 16) > 7) {
|
|
262
|
+
checksumAddress += _address[i].toUpperCase();
|
|
263
|
+
} else {
|
|
264
|
+
checksumAddress += _address[i];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (address.match(/([A-F].*[a-f])|([a-f].*[A-F])/) && checksumAddress !== address) {
|
|
268
|
+
throw new Error(`Invalid Checksum address for "${address}"`);
|
|
269
|
+
}
|
|
270
|
+
return checksumAddress;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// src/classes/utils/encode-decode-transaction.ts
|
|
274
|
+
var hexFalse = "0".repeat(64);
|
|
275
|
+
var hexTrue = "0".repeat(63) + "1";
|
|
276
|
+
function hexToUtf8(hex) {
|
|
277
|
+
let str = "";
|
|
278
|
+
let i = 0;
|
|
279
|
+
const l = hex.length;
|
|
280
|
+
if (hex.substring(0, 2) === "0x") {
|
|
281
|
+
i = 2;
|
|
282
|
+
}
|
|
283
|
+
for (; i < l; i += 2) {
|
|
284
|
+
const code = parseInt(hex.substr(i, 2), 16);
|
|
285
|
+
if (code === 0) continue;
|
|
286
|
+
str += String.fromCharCode(code);
|
|
287
|
+
}
|
|
288
|
+
try {
|
|
289
|
+
return decodeURIComponent(escape(str));
|
|
290
|
+
} catch (e) {
|
|
291
|
+
return str;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
function expandType(type) {
|
|
295
|
+
if (type === "uint[]") {
|
|
296
|
+
return "uint256[]";
|
|
297
|
+
}
|
|
298
|
+
if (type === "int[]") {
|
|
299
|
+
return "int256[]";
|
|
300
|
+
}
|
|
301
|
+
return type;
|
|
302
|
+
}
|
|
303
|
+
function encodeData(jsonABIArgument, args) {
|
|
304
|
+
const hash = new import_sha32.Keccak(256);
|
|
305
|
+
const functionString = `${jsonABIArgument.name}(${jsonABIArgument.inputs.map(
|
|
306
|
+
(input) => expandType(input.type)
|
|
307
|
+
)})`;
|
|
308
|
+
const functionHash = hash.update(functionString).digest("hex");
|
|
309
|
+
const jsonABIInputsLength = jsonABIArgument.inputs.length;
|
|
310
|
+
let shouldValidateInputLength = true;
|
|
311
|
+
if (jsonABIArgument.inputs.find((input) => input.type.includes("["))) {
|
|
312
|
+
shouldValidateInputLength = false;
|
|
313
|
+
}
|
|
314
|
+
if (shouldValidateInputLength && args.length !== jsonABIInputsLength) {
|
|
315
|
+
throw new Error(
|
|
316
|
+
`args inputs of "${args.length}" does not match expected length of "${jsonABIArgument.inputs.length}"`
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
const argsWithTypes = (jsonABIArgument.inputs || []).reduce(
|
|
320
|
+
(acc, input, i) => {
|
|
321
|
+
if (input.type.includes("[")) {
|
|
322
|
+
const basicType = /([^[]*)\[.*$/g.exec(input.type)?.[1];
|
|
323
|
+
args.forEach((arg) => {
|
|
324
|
+
acc = acc.concat([[arg, basicType]]);
|
|
325
|
+
});
|
|
326
|
+
return acc;
|
|
327
|
+
} else {
|
|
328
|
+
return acc.concat([[args[i], input.type]]);
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
[]
|
|
332
|
+
);
|
|
333
|
+
const encodedArgs = argsWithTypes.map(([arg, inputType]) => {
|
|
334
|
+
let rawArg = arg;
|
|
335
|
+
switch (inputType) {
|
|
336
|
+
case "bool":
|
|
337
|
+
return arg ? hexTrue : hexFalse;
|
|
338
|
+
case "address":
|
|
339
|
+
rawArg = arg.replace(/^0x/g, "").toLowerCase();
|
|
340
|
+
break;
|
|
341
|
+
default:
|
|
342
|
+
if (inputType.startsWith("bytes")) {
|
|
343
|
+
if (Array.isArray(arg)) {
|
|
344
|
+
throw new Error(
|
|
345
|
+
`essential-eth does not yet support "${inputType}[]" inputs. Make a PR today!"`
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
const argEncoded2 = BigInt(arg).toString(16);
|
|
349
|
+
const paddedEncodedArg2 = argEncoded2.padStart(64, "0");
|
|
350
|
+
return paddedEncodedArg2;
|
|
351
|
+
} else if (inputType === "uint256") {
|
|
352
|
+
const argEncoded2 = BigInt(arg).toString(16);
|
|
353
|
+
const paddedEncodedArg2 = argEncoded2.padStart(64, "0");
|
|
354
|
+
return paddedEncodedArg2;
|
|
355
|
+
} else if (inputType.startsWith("uint")) {
|
|
356
|
+
break;
|
|
357
|
+
} else {
|
|
358
|
+
throw new Error(
|
|
359
|
+
`essential-eth does not yet support "${inputType}" inputs. Make a PR today!"`
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
const argEncoded = rawArg.toString(16);
|
|
364
|
+
const paddedEncodedArg = argEncoded.padStart(64, "0");
|
|
365
|
+
return paddedEncodedArg;
|
|
366
|
+
});
|
|
367
|
+
const functionEncoded = functionHash.slice(0, 8);
|
|
368
|
+
const data = `0x${functionEncoded}${encodedArgs.join("")}`;
|
|
369
|
+
return data;
|
|
370
|
+
}
|
|
371
|
+
function decodeRPCResponse(jsonABIArgument, nodeResponse) {
|
|
372
|
+
const rawOutputs = jsonABIArgument.outputs || [];
|
|
373
|
+
const slicedResponse = nodeResponse.slice(2);
|
|
374
|
+
if (rawOutputs.length === 1 && rawOutputs[0].type === "string") {
|
|
375
|
+
const [hexOffset, responseData] = [
|
|
376
|
+
slicedResponse.slice(0, 64),
|
|
377
|
+
slicedResponse.slice(64)
|
|
378
|
+
];
|
|
379
|
+
const decimalOffset = Number(hexToDecimal(`0x${hexOffset}`));
|
|
380
|
+
const hexLength = responseData.slice(0, decimalOffset * 2);
|
|
381
|
+
const decimalLength = Number(hexToDecimal(`0x${hexLength}`));
|
|
382
|
+
const hexToDecode = responseData.slice(
|
|
383
|
+
decimalOffset * 2,
|
|
384
|
+
decimalOffset * 2 + decimalLength * 2
|
|
385
|
+
);
|
|
386
|
+
return hexToUtf8(hexToDecode);
|
|
387
|
+
}
|
|
388
|
+
const encodedOutputs = slicedResponse.match(/.{1,64}/g) || [];
|
|
389
|
+
if (rawOutputs.length === 1 && rawOutputs[0].type === "address[]") {
|
|
390
|
+
const unformattedAddresses = encodedOutputs.slice(2);
|
|
391
|
+
return unformattedAddresses.map((unformattedAddress) => {
|
|
392
|
+
return toChecksumAddress(`0x${unformattedAddress.slice(24)}`);
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
if (rawOutputs?.length === 1 && rawOutputs[0].type === "uint256[]") {
|
|
396
|
+
const outputs2 = encodedOutputs.slice(2);
|
|
397
|
+
return outputs2.map((output) => {
|
|
398
|
+
return tinyBig(hexToDecimal(`0x${output}`));
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
const outputs = encodedOutputs.map((output, i) => {
|
|
402
|
+
const outputType = rawOutputs[i].type;
|
|
403
|
+
switch (outputType) {
|
|
404
|
+
case "bool":
|
|
405
|
+
return output === hexTrue;
|
|
406
|
+
case "address":
|
|
407
|
+
return toChecksumAddress(`0x${output.slice(24)}`);
|
|
408
|
+
case "uint256":
|
|
409
|
+
case "uint120":
|
|
410
|
+
return tinyBig(hexToDecimal(`0x${output}`));
|
|
411
|
+
case "bytes32":
|
|
412
|
+
return `0x${output}`;
|
|
413
|
+
case "uint8":
|
|
414
|
+
return Number(hexToDecimal(`0x${output}`));
|
|
415
|
+
default:
|
|
416
|
+
throw new Error(
|
|
417
|
+
`essential-eth does not yet support "${outputType}" outputs. Make a PR today!"`
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
return outputs.length === 1 ? outputs[0] : outputs;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// src/classes/Contract.ts
|
|
425
|
+
function estimateGas(txnData) {
|
|
426
|
+
txnData.split("").reduce((previousValue, currentValue) => {
|
|
427
|
+
const characterCost = currentValue === "0" ? 4 : 68;
|
|
428
|
+
return previousValue + characterCost;
|
|
429
|
+
}, 0);
|
|
430
|
+
}
|
|
431
|
+
var BaseContract = class {
|
|
432
|
+
/**
|
|
433
|
+
* The URL to your Eth node. Consider POKT or Infura
|
|
434
|
+
*/
|
|
435
|
+
_address;
|
|
436
|
+
_provider;
|
|
437
|
+
/**
|
|
438
|
+
* @param addressOrName The ethereum address of the smart-contract
|
|
439
|
+
* @param contractInterface The JSON ABI of the smart-contract (like http://api.etherscan.io/api?module=contract&action=getabi&address=0x090d4613473dee047c3f2706764f49e0821d256e&format=raw)
|
|
440
|
+
* @param signerOrProvider An instantiated essential-eth provider
|
|
441
|
+
* @example
|
|
442
|
+
*/
|
|
443
|
+
constructor(addressOrName, contractInterface, signerOrProvider) {
|
|
444
|
+
this._address = addressOrName;
|
|
445
|
+
this._provider = signerOrProvider;
|
|
446
|
+
contractInterface.filter((jsonABIArgument) => jsonABIArgument.type === "function").forEach((jsonABIArgument) => {
|
|
447
|
+
if ("name" in jsonABIArgument && typeof jsonABIArgument.name === "string") {
|
|
448
|
+
defineReadOnly(
|
|
449
|
+
this,
|
|
450
|
+
jsonABIArgument.name,
|
|
451
|
+
async (..._args) => {
|
|
452
|
+
let functionArguments = _args;
|
|
453
|
+
let options = {};
|
|
454
|
+
const lastArg = _args[_args.length - 1];
|
|
455
|
+
if (!Array.isArray(lastArg) && typeof lastArg === "object") {
|
|
456
|
+
options = lastArg;
|
|
457
|
+
functionArguments = _args.slice(0, _args.length - 1);
|
|
458
|
+
}
|
|
459
|
+
const data = encodeData(jsonABIArgument, functionArguments);
|
|
460
|
+
const decimalGas = typeof options.gasLimit === "number" ? options.gasLimit : typeof jsonABIArgument?.gas === "number" ? estimateGas(data) : null;
|
|
461
|
+
const req = async () => {
|
|
462
|
+
return await this._provider.call(
|
|
463
|
+
{
|
|
464
|
+
to: this._address.toLowerCase(),
|
|
465
|
+
data,
|
|
466
|
+
// sometimes gas is defined in the ABI
|
|
467
|
+
...decimalGas ? { gas: `0x${decimalGas.toString(16)}` } : {}
|
|
468
|
+
},
|
|
469
|
+
"latest"
|
|
470
|
+
);
|
|
471
|
+
};
|
|
472
|
+
const nodeResponse = await req();
|
|
473
|
+
return decodeRPCResponse(jsonABIArgument, nodeResponse);
|
|
474
|
+
}
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
function defineReadOnly(object, name, value) {
|
|
481
|
+
Object.defineProperty(object, name, {
|
|
482
|
+
enumerable: true,
|
|
483
|
+
value,
|
|
484
|
+
writable: false
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
var Contract = class extends BaseContract {
|
|
488
|
+
// readonly [key: string]: ContractFunction | any;
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
// src/classes/utils/clean-transaction.ts
|
|
492
|
+
function cleanTransaction(transaction) {
|
|
493
|
+
const cleanedTransaction = {
|
|
494
|
+
...transaction
|
|
495
|
+
};
|
|
496
|
+
Object.keys(transaction).forEach((key) => {
|
|
497
|
+
if (!transaction[key]) return;
|
|
498
|
+
switch (key) {
|
|
499
|
+
case "blockNumber":
|
|
500
|
+
case "chainId":
|
|
501
|
+
case "transactionIndex":
|
|
502
|
+
case "type":
|
|
503
|
+
case "v":
|
|
504
|
+
cleanedTransaction[key] = Number(hexToDecimal(transaction[key]));
|
|
505
|
+
break;
|
|
506
|
+
case "from":
|
|
507
|
+
case "to":
|
|
508
|
+
if (transaction[key]) {
|
|
509
|
+
cleanedTransaction[key] = toChecksumAddress(transaction[key]);
|
|
510
|
+
}
|
|
511
|
+
break;
|
|
512
|
+
case "value":
|
|
513
|
+
case "gas":
|
|
514
|
+
case "gasPrice":
|
|
515
|
+
case "maxFeePerGas":
|
|
516
|
+
case "maxPriorityFeePerGas":
|
|
517
|
+
case "nonce":
|
|
518
|
+
cleanedTransaction[key] = tinyBig(hexToDecimal(transaction[key]));
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
return cleanedTransaction;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// src/classes/utils/clean-block.ts
|
|
526
|
+
function cleanBlock(block, returnTransactionObjects) {
|
|
527
|
+
const cleanedBlock = { ...block };
|
|
528
|
+
Object.keys(block).forEach((key) => {
|
|
529
|
+
if (!block[key]) return;
|
|
530
|
+
switch (key) {
|
|
531
|
+
case "difficulty":
|
|
532
|
+
case "totalDifficulty":
|
|
533
|
+
case "gasLimit":
|
|
534
|
+
case "gasUsed":
|
|
535
|
+
case "size":
|
|
536
|
+
case "timestamp":
|
|
537
|
+
case "baseFeePerGas":
|
|
538
|
+
cleanedBlock[key] = tinyBig(hexToDecimal(block[key]));
|
|
539
|
+
break;
|
|
540
|
+
case "number":
|
|
541
|
+
cleanedBlock[key] = Number(hexToDecimal(block[key]));
|
|
542
|
+
break;
|
|
543
|
+
case "miner":
|
|
544
|
+
cleanedBlock[key] = toChecksumAddress(block[key]);
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
if (returnTransactionObjects) {
|
|
549
|
+
const txns = block.transactions;
|
|
550
|
+
txns.forEach((transaction, index) => {
|
|
551
|
+
cleanedBlock.transactions[index] = cleanTransaction(transaction);
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
return cleanedBlock;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// src/classes/utils/clean-log.ts
|
|
558
|
+
function cleanLog(log, receiptLog) {
|
|
559
|
+
const cleanedLog = {
|
|
560
|
+
...log
|
|
561
|
+
};
|
|
562
|
+
Object.keys(log).forEach((key) => {
|
|
563
|
+
switch (key) {
|
|
564
|
+
case "address":
|
|
565
|
+
cleanedLog[key] = toChecksumAddress(log[key]);
|
|
566
|
+
break;
|
|
567
|
+
case "blockNumber":
|
|
568
|
+
case "logIndex":
|
|
569
|
+
case "transactionIndex":
|
|
570
|
+
cleanedLog[key] = Number(hexToDecimal(log[key]));
|
|
571
|
+
break;
|
|
572
|
+
case "removed":
|
|
573
|
+
if (receiptLog) {
|
|
574
|
+
delete cleanedLog[key];
|
|
575
|
+
} else if (log[key] == null) {
|
|
576
|
+
cleanedLog[key] === false;
|
|
577
|
+
}
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
return cleanedLog;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// src/classes/utils/clean-transaction-receipt.ts
|
|
585
|
+
function cleanTransactionReceipt(transactionReceipt) {
|
|
586
|
+
const cleanedTransaction = cleanTransaction(transactionReceipt);
|
|
587
|
+
const cleanedTransactionReceipt = {
|
|
588
|
+
...cleanedTransaction
|
|
589
|
+
};
|
|
590
|
+
Object.keys(transactionReceipt).forEach((key) => {
|
|
591
|
+
if (!transactionReceipt[key]) return;
|
|
592
|
+
switch (key) {
|
|
593
|
+
case "status":
|
|
594
|
+
cleanedTransactionReceipt[key] = Number(
|
|
595
|
+
hexToDecimal(transactionReceipt[key])
|
|
596
|
+
);
|
|
597
|
+
break;
|
|
598
|
+
case "contractAddress":
|
|
599
|
+
if (transactionReceipt[key]) {
|
|
600
|
+
cleanedTransactionReceipt[key] = toChecksumAddress(
|
|
601
|
+
transactionReceipt[key]
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
break;
|
|
605
|
+
case "cumulativeGasUsed":
|
|
606
|
+
case "effectiveGasPrice":
|
|
607
|
+
case "gasUsed":
|
|
608
|
+
cleanedTransactionReceipt[key] = tinyBig(
|
|
609
|
+
hexToDecimal(transactionReceipt[key])
|
|
610
|
+
);
|
|
611
|
+
break;
|
|
612
|
+
case "logs":
|
|
613
|
+
transactionReceipt[key].forEach((log, index) => {
|
|
614
|
+
cleanedTransactionReceipt[key][index] = cleanLog(log, true);
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
cleanedTransactionReceipt.byzantium = cleanedTransactionReceipt.blockNumber >= 437e4;
|
|
619
|
+
return cleanedTransactionReceipt;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// src/classes/utils/fetchers.ts
|
|
623
|
+
var import_isomorphic_unfetch = __toESM(require("isomorphic-unfetch"), 1);
|
|
624
|
+
function buildFetchInit(body) {
|
|
625
|
+
return {
|
|
626
|
+
method: "POST",
|
|
627
|
+
headers: {
|
|
628
|
+
"Content-Type": "application/json"
|
|
629
|
+
},
|
|
630
|
+
body: JSON.stringify(body)
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
function post(url, body) {
|
|
634
|
+
return (0, import_isomorphic_unfetch.default)(url, buildFetchInit(body)).then(async (r) => {
|
|
635
|
+
const t = await r.text();
|
|
636
|
+
try {
|
|
637
|
+
return JSON.parse(t);
|
|
638
|
+
} catch {
|
|
639
|
+
throw new Error(`Invalid JSON RPC response: "${t}"`);
|
|
640
|
+
}
|
|
641
|
+
}).then((response) => {
|
|
642
|
+
const result = response?.result;
|
|
643
|
+
if (!result) {
|
|
644
|
+
throw new Error(
|
|
645
|
+
`Invalid JSON RPC response: ${JSON.stringify(response)}`
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
return response.result;
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
function buildRPCPostBody(method, params) {
|
|
652
|
+
return {
|
|
653
|
+
jsonrpc: "2.0",
|
|
654
|
+
// TODO: Increment ID will be needed when websocket support is added
|
|
655
|
+
id: 1,
|
|
656
|
+
method,
|
|
657
|
+
params
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// src/classes/utils/prepare-transaction.ts
|
|
662
|
+
var import_big2 = __toESM(require("big.js"), 1);
|
|
663
|
+
|
|
664
|
+
// src/logger/package-version.ts
|
|
665
|
+
var version = "0.11.1";
|
|
666
|
+
|
|
667
|
+
// src/logger/logger.ts
|
|
668
|
+
var Logger = class {
|
|
669
|
+
packageVersion;
|
|
670
|
+
constructor() {
|
|
671
|
+
this.packageVersion = version;
|
|
672
|
+
}
|
|
673
|
+
throwError(message, args) {
|
|
674
|
+
const argsLength = Object.keys(args).length;
|
|
675
|
+
throw new Error(
|
|
676
|
+
`${message} (${Object.entries(args).map(
|
|
677
|
+
([key, value], index) => `${key}=${value}${index < argsLength - 1 && ", "}`
|
|
678
|
+
)}, version=essential-eth@${this.packageVersion})`
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
throwArgumentError(message, arg, value) {
|
|
682
|
+
throw new Error(
|
|
683
|
+
`${message} (argument="${arg}" value=${value}, version=essential-eth@${this.packageVersion})`
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
checkSafeUint53(value, message = "value not safe") {
|
|
687
|
+
if (typeof value !== "number") {
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
if (value < 0 || value >= 9007199254740991) {
|
|
691
|
+
this.throwError(message, {
|
|
692
|
+
operation: "checkSafeInteger",
|
|
693
|
+
fault: "out-of-safe-range",
|
|
694
|
+
value
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
if (value % 1) {
|
|
698
|
+
this.throwError(message, {
|
|
699
|
+
operation: "checkSafeInteger",
|
|
700
|
+
fault: "non-integer",
|
|
701
|
+
value
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
var logger = new Logger();
|
|
707
|
+
|
|
708
|
+
// src/utils/bytes.ts
|
|
709
|
+
function isHexable(value) {
|
|
710
|
+
return !!value.toHexString;
|
|
711
|
+
}
|
|
712
|
+
function isBytesLike(value) {
|
|
713
|
+
return isHexString(value) && !(value.length % 2) || isBytes(value);
|
|
714
|
+
}
|
|
715
|
+
function isInteger(value) {
|
|
716
|
+
return typeof value === "number" && value == value && value % 1 === 0;
|
|
717
|
+
}
|
|
718
|
+
function isBytes(value) {
|
|
719
|
+
if (value == null) {
|
|
720
|
+
return false;
|
|
721
|
+
}
|
|
722
|
+
if (value.constructor === Uint8Array) {
|
|
723
|
+
return true;
|
|
724
|
+
}
|
|
725
|
+
if (typeof value === "string") {
|
|
726
|
+
return false;
|
|
727
|
+
}
|
|
728
|
+
if (!isInteger(value.length) || value.length < 0) {
|
|
729
|
+
return false;
|
|
730
|
+
}
|
|
731
|
+
for (let i = 0; i < value.length; i++) {
|
|
732
|
+
const v = value[i];
|
|
733
|
+
if (!isInteger(v) || v < 0 || v >= 256) {
|
|
734
|
+
return false;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
return true;
|
|
738
|
+
}
|
|
739
|
+
function arrayify(value, options) {
|
|
740
|
+
if (!options) {
|
|
741
|
+
options = {};
|
|
742
|
+
}
|
|
743
|
+
if (typeof value === "number") {
|
|
744
|
+
logger.checkSafeUint53(value, "invalid arrayify value");
|
|
745
|
+
const result = [];
|
|
746
|
+
while (value) {
|
|
747
|
+
result.unshift(value & 255);
|
|
748
|
+
value = parseInt(String(value / 256));
|
|
749
|
+
}
|
|
750
|
+
if (result.length === 0) {
|
|
751
|
+
result.push(0);
|
|
752
|
+
}
|
|
753
|
+
return new Uint8Array(result);
|
|
754
|
+
}
|
|
755
|
+
if (options.allowMissingPrefix && typeof value === "string" && value.substring(0, 2) !== "0x") {
|
|
756
|
+
value = "0x" + value;
|
|
757
|
+
}
|
|
758
|
+
if (isHexable(value)) {
|
|
759
|
+
value = value.toHexString();
|
|
760
|
+
}
|
|
761
|
+
if (isHexString(value)) {
|
|
762
|
+
let hex = value.substring(2);
|
|
763
|
+
if (hex.length % 2) {
|
|
764
|
+
if (options.hexPad === "left") {
|
|
765
|
+
hex = "0" + hex;
|
|
766
|
+
} else if (options.hexPad === "right") {
|
|
767
|
+
hex += "0";
|
|
768
|
+
} else {
|
|
769
|
+
logger.throwArgumentError("hex data is odd-length", "value", value);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
const result = [];
|
|
773
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
774
|
+
result.push(parseInt(hex.substring(i, i + 2), 16));
|
|
775
|
+
}
|
|
776
|
+
return new Uint8Array(result);
|
|
777
|
+
}
|
|
778
|
+
if (isBytes(value)) {
|
|
779
|
+
return new Uint8Array(value);
|
|
780
|
+
}
|
|
781
|
+
return logger.throwArgumentError("invalid arrayify value", "value", value);
|
|
782
|
+
}
|
|
783
|
+
function concat(arrayOfBytesLike) {
|
|
784
|
+
const objects = arrayOfBytesLike.map((item) => arrayify(item));
|
|
785
|
+
const length = objects.reduce((accum, item) => accum + item.length, 0);
|
|
786
|
+
const result = new Uint8Array(length);
|
|
787
|
+
objects.reduce((offset, object) => {
|
|
788
|
+
result.set(object, offset);
|
|
789
|
+
return offset + object.length;
|
|
790
|
+
}, 0);
|
|
791
|
+
return result;
|
|
792
|
+
}
|
|
793
|
+
function stripZeros(value) {
|
|
794
|
+
let result = arrayify(value);
|
|
795
|
+
if (result.length === 0) {
|
|
796
|
+
return result;
|
|
797
|
+
}
|
|
798
|
+
let start = 0;
|
|
799
|
+
while (start < result.length && result[start] === 0) {
|
|
800
|
+
start++;
|
|
801
|
+
}
|
|
802
|
+
if (start) {
|
|
803
|
+
result = result.slice(start);
|
|
804
|
+
}
|
|
805
|
+
return result;
|
|
806
|
+
}
|
|
807
|
+
function zeroPad(value, length) {
|
|
808
|
+
value = arrayify(value);
|
|
809
|
+
if (value.length > length) {
|
|
810
|
+
logger.throwArgumentError("value out of range", "value", value);
|
|
811
|
+
}
|
|
812
|
+
const result = new Uint8Array(length);
|
|
813
|
+
result.set(value, length - value.length);
|
|
814
|
+
return result;
|
|
815
|
+
}
|
|
816
|
+
function isHexString(value, length) {
|
|
817
|
+
if (typeof value !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
|
|
818
|
+
return false;
|
|
819
|
+
}
|
|
820
|
+
if (length && value.length !== 2 + 2 * length) {
|
|
821
|
+
return false;
|
|
822
|
+
}
|
|
823
|
+
return true;
|
|
824
|
+
}
|
|
825
|
+
var HexCharacters = "0123456789abcdef";
|
|
826
|
+
function hexlify(value, options) {
|
|
827
|
+
if (!options) {
|
|
828
|
+
options = {};
|
|
829
|
+
}
|
|
830
|
+
if (typeof value === "number") {
|
|
831
|
+
logger.checkSafeUint53(value, "invalid hexlify value");
|
|
832
|
+
let hex = "";
|
|
833
|
+
while (value) {
|
|
834
|
+
hex = HexCharacters[value & 15] + hex;
|
|
835
|
+
value = Math.floor(value / 16);
|
|
836
|
+
}
|
|
837
|
+
if (hex.length) {
|
|
838
|
+
if (hex.length % 2) {
|
|
839
|
+
hex = "0" + hex;
|
|
840
|
+
}
|
|
841
|
+
return "0x" + hex;
|
|
842
|
+
}
|
|
843
|
+
return "0x00";
|
|
844
|
+
}
|
|
845
|
+
if (typeof value === "bigint") {
|
|
846
|
+
value = value.toString(16);
|
|
847
|
+
if (value.length % 2) {
|
|
848
|
+
return "0x0" + value;
|
|
849
|
+
}
|
|
850
|
+
return "0x" + value;
|
|
851
|
+
}
|
|
852
|
+
if (options.allowMissingPrefix && typeof value === "string" && value.substring(0, 2) !== "0x") {
|
|
853
|
+
value = "0x" + value;
|
|
854
|
+
}
|
|
855
|
+
if (isHexable(value)) {
|
|
856
|
+
return value.toHexString();
|
|
857
|
+
}
|
|
858
|
+
if (isHexString(value)) {
|
|
859
|
+
if (value.length % 2) {
|
|
860
|
+
if (options.hexPad === "left") {
|
|
861
|
+
value = "0x0" + value.substring(2);
|
|
862
|
+
} else if (options.hexPad === "right") {
|
|
863
|
+
value += "0";
|
|
864
|
+
} else {
|
|
865
|
+
logger.throwArgumentError("hex data is odd-length", "value", value);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
return value.toLowerCase();
|
|
869
|
+
}
|
|
870
|
+
if (isBytes(value)) {
|
|
871
|
+
let result = "0x";
|
|
872
|
+
for (let i = 0; i < value.length; i++) {
|
|
873
|
+
const v = value[i];
|
|
874
|
+
result += HexCharacters[(v & 240) >> 4] + HexCharacters[v & 15];
|
|
875
|
+
}
|
|
876
|
+
return result;
|
|
877
|
+
}
|
|
878
|
+
return logger.throwArgumentError("invalid hexlify value", "value", value);
|
|
879
|
+
}
|
|
880
|
+
function hexDataLength(data) {
|
|
881
|
+
if (typeof data !== "string") {
|
|
882
|
+
data = hexlify(data);
|
|
883
|
+
} else if (!isHexString(data) || data.length % 2) {
|
|
884
|
+
return null;
|
|
885
|
+
}
|
|
886
|
+
return (data.length - 2) / 2;
|
|
887
|
+
}
|
|
888
|
+
function hexDataSlice(data, offset, endOffset) {
|
|
889
|
+
if (typeof data !== "string") {
|
|
890
|
+
data = hexlify(data);
|
|
891
|
+
} else if (!isHexString(data) || data.length % 2) {
|
|
892
|
+
logger.throwArgumentError("invalid hexData", "value", data);
|
|
893
|
+
}
|
|
894
|
+
offset = 2 + 2 * offset;
|
|
895
|
+
if (endOffset != null) {
|
|
896
|
+
return "0x" + data.substring(offset, 2 + 2 * endOffset);
|
|
897
|
+
}
|
|
898
|
+
return "0x" + data.substring(offset);
|
|
899
|
+
}
|
|
900
|
+
function hexConcat(items) {
|
|
901
|
+
let result = "0x";
|
|
902
|
+
items.forEach((item) => {
|
|
903
|
+
result += hexlify(item).substring(2);
|
|
904
|
+
});
|
|
905
|
+
return result;
|
|
906
|
+
}
|
|
907
|
+
function hexValue(value) {
|
|
908
|
+
const trimmed = hexStripZeros(hexlify(value, { hexPad: "left" }));
|
|
909
|
+
if (trimmed === "0x") {
|
|
910
|
+
return "0x0";
|
|
911
|
+
}
|
|
912
|
+
return trimmed;
|
|
913
|
+
}
|
|
914
|
+
function hexStripZeros(value) {
|
|
915
|
+
if (typeof value !== "string") {
|
|
916
|
+
value = hexlify(value);
|
|
917
|
+
}
|
|
918
|
+
if (!isHexString(value)) {
|
|
919
|
+
logger.throwArgumentError("invalid hex string", "value", value);
|
|
920
|
+
}
|
|
921
|
+
value = value.substring(2);
|
|
922
|
+
let offset = 0;
|
|
923
|
+
while (offset < value.length && value[offset] === "0") {
|
|
924
|
+
offset++;
|
|
925
|
+
}
|
|
926
|
+
return "0x" + value.substring(offset);
|
|
927
|
+
}
|
|
928
|
+
function hexZeroPad(value, length) {
|
|
929
|
+
if (typeof value !== "string") {
|
|
930
|
+
value = hexlify(value);
|
|
931
|
+
} else if (!isHexString(value)) {
|
|
932
|
+
logger.throwArgumentError("invalid hex string", "value", value);
|
|
933
|
+
}
|
|
934
|
+
if (value.length > 2 * length + 2) {
|
|
935
|
+
logger.throwError("value out of range", { value, length });
|
|
936
|
+
}
|
|
937
|
+
while (value.length < 2 * length + 2) {
|
|
938
|
+
value = "0x0" + value.substring(2);
|
|
939
|
+
}
|
|
940
|
+
return value;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
// src/classes/utils/prepare-transaction.ts
|
|
944
|
+
function prepareTransaction(transaction) {
|
|
945
|
+
const preparedTransaction = {
|
|
946
|
+
...transaction
|
|
947
|
+
};
|
|
948
|
+
Object.keys(transaction).forEach(
|
|
949
|
+
(key) => {
|
|
950
|
+
switch (key) {
|
|
951
|
+
case "gas":
|
|
952
|
+
case "gasPrice":
|
|
953
|
+
case "nonce":
|
|
954
|
+
case "maxFeePerGas":
|
|
955
|
+
case "maxPriorityFeePerGas":
|
|
956
|
+
case "value": {
|
|
957
|
+
const value = transaction[key];
|
|
958
|
+
if (value instanceof TinyBig) {
|
|
959
|
+
preparedTransaction[key] = value.toHexString();
|
|
960
|
+
} else if (value instanceof import_big2.default) {
|
|
961
|
+
preparedTransaction[key] = `0x${BigInt(value.toString()).toString(
|
|
962
|
+
16
|
|
963
|
+
)}`;
|
|
964
|
+
} else if (typeof transaction[key] === "number")
|
|
965
|
+
preparedTransaction[key] = "0x" + transaction[key].toString(16);
|
|
966
|
+
else preparedTransaction[key] = transaction[key].toString();
|
|
967
|
+
break;
|
|
968
|
+
}
|
|
969
|
+
case "data":
|
|
970
|
+
preparedTransaction[key] = hexlify(transaction[key]);
|
|
971
|
+
break;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
);
|
|
975
|
+
return preparedTransaction;
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
// src/providers/utils/chains-info.ts
|
|
979
|
+
var chains_info_default = {
|
|
980
|
+
"1": [
|
|
981
|
+
"eth",
|
|
982
|
+
"0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"
|
|
983
|
+
],
|
|
984
|
+
"5": [
|
|
985
|
+
"gor",
|
|
986
|
+
"0x112234455c3a32fd11230c42e7bccd4a84e02010"
|
|
987
|
+
],
|
|
988
|
+
"10": [
|
|
989
|
+
"oeth"
|
|
990
|
+
],
|
|
991
|
+
"50": [
|
|
992
|
+
"xdc"
|
|
993
|
+
],
|
|
994
|
+
"56": [
|
|
995
|
+
"bnb"
|
|
996
|
+
],
|
|
997
|
+
"100": [
|
|
998
|
+
"gno"
|
|
999
|
+
],
|
|
1000
|
+
"137": [
|
|
1001
|
+
"pol"
|
|
1002
|
+
],
|
|
1003
|
+
"250": [
|
|
1004
|
+
"ftm"
|
|
1005
|
+
],
|
|
1006
|
+
"324": [
|
|
1007
|
+
"zksync"
|
|
1008
|
+
],
|
|
1009
|
+
"1101": [
|
|
1010
|
+
"zkevm"
|
|
1011
|
+
],
|
|
1012
|
+
"1337": [
|
|
1013
|
+
"geth"
|
|
1014
|
+
],
|
|
1015
|
+
"7700": [
|
|
1016
|
+
"canto"
|
|
1017
|
+
],
|
|
1018
|
+
"9001": [
|
|
1019
|
+
"evmos"
|
|
1020
|
+
],
|
|
1021
|
+
"42161": [
|
|
1022
|
+
"arb1"
|
|
1023
|
+
],
|
|
1024
|
+
"42220": [
|
|
1025
|
+
"celo"
|
|
1026
|
+
],
|
|
1027
|
+
"43114": [
|
|
1028
|
+
"avax"
|
|
1029
|
+
],
|
|
1030
|
+
"59140": [
|
|
1031
|
+
"linea-goerli"
|
|
1032
|
+
],
|
|
1033
|
+
"80001": [
|
|
1034
|
+
"maticmum"
|
|
1035
|
+
],
|
|
1036
|
+
"421613": [
|
|
1037
|
+
"arb-goerli"
|
|
1038
|
+
],
|
|
1039
|
+
"534352": [
|
|
1040
|
+
"scr"
|
|
1041
|
+
],
|
|
1042
|
+
"11155111": [
|
|
1043
|
+
"sep"
|
|
1044
|
+
]
|
|
1045
|
+
};
|
|
1046
|
+
|
|
1047
|
+
// src/providers/BaseProvider.ts
|
|
1048
|
+
function prepBlockTag(blockTag) {
|
|
1049
|
+
return typeof blockTag === "number" ? tinyBig(blockTag).toHexString() : blockTag;
|
|
1050
|
+
}
|
|
1051
|
+
var BaseProvider = class {
|
|
1052
|
+
/**
|
|
1053
|
+
* @ignore
|
|
1054
|
+
*/
|
|
1055
|
+
_rpcUrls = [];
|
|
1056
|
+
/**
|
|
1057
|
+
* @ignore
|
|
1058
|
+
*/
|
|
1059
|
+
_post = (body) => post(this.selectRpcUrl(), body);
|
|
1060
|
+
/**
|
|
1061
|
+
* @param rpcUrls The URL(s) to your Eth node(s). Consider POKT or Infura
|
|
1062
|
+
* @example
|
|
1063
|
+
* `https://free-eth-node.com/api/eth`
|
|
1064
|
+
* @example
|
|
1065
|
+
* `https://mainnet.infura.io/v3/YOUR-PROJECT-ID`
|
|
1066
|
+
*/
|
|
1067
|
+
constructor(rpcUrls) {
|
|
1068
|
+
this._rpcUrls = rpcUrls;
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Gets information (name, chainId, and ensAddress when applicable) about the network the provider is connected to.
|
|
1072
|
+
*
|
|
1073
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.getNetwork`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getNetwork) in ethers.js
|
|
1074
|
+
* * [Similar](/docs/api#isd) to [`web3.eth.getChainId`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getchainid) in web3.js, returns more than just the `chainId`
|
|
1075
|
+
*
|
|
1076
|
+
* @returns information about the network this provider is currently connected to
|
|
1077
|
+
* @example
|
|
1078
|
+
* ```javascript
|
|
1079
|
+
* jsonRpcProvider('https://free-eth-node.com/api/eth').getNetwork();
|
|
1080
|
+
* // { chainId: 1, name: 'eth', ensAddress: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e' }
|
|
1081
|
+
* ```
|
|
1082
|
+
* @example
|
|
1083
|
+
* ```javascript
|
|
1084
|
+
* jsonRpcProvider('https://free-eth-node.com/api/MATIC').getNetwork();
|
|
1085
|
+
* // { chainId: 137, name: 'MATIC', ensAddress: null }
|
|
1086
|
+
* ```
|
|
1087
|
+
*/
|
|
1088
|
+
async getNetwork() {
|
|
1089
|
+
const hexChainId = await this.post(
|
|
1090
|
+
buildRPCPostBody("eth_chainId", [])
|
|
1091
|
+
);
|
|
1092
|
+
const chainId = hexToDecimal(hexChainId);
|
|
1093
|
+
const info = chains_info_default[chainId];
|
|
1094
|
+
return {
|
|
1095
|
+
chainId: Number(chainId),
|
|
1096
|
+
name: info[0] || "unknown",
|
|
1097
|
+
ensAddress: info[1] || null
|
|
1098
|
+
// only send ensAddress if it exists
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Gets the number of the most recently mined block on the network the provider is connected to.
|
|
1103
|
+
*
|
|
1104
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.getBlockNumber`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getBlockNumber) in ethers.js
|
|
1105
|
+
* * [Identical](/docs/api#isd) to [`web3.eth.getBlockNumber`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getblocknumber) in web3.js
|
|
1106
|
+
*
|
|
1107
|
+
* @returns the number of the most recently mined block
|
|
1108
|
+
* @example
|
|
1109
|
+
* ```javascript
|
|
1110
|
+
* await provider.getBlockNumber();
|
|
1111
|
+
* // 1053312
|
|
1112
|
+
* ```
|
|
1113
|
+
*/
|
|
1114
|
+
async getBlockNumber() {
|
|
1115
|
+
const currentBlockNumber = await this.post(
|
|
1116
|
+
buildRPCPostBody("eth_blockNumber", [])
|
|
1117
|
+
);
|
|
1118
|
+
return Number(hexToDecimal(currentBlockNumber));
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Gets information about a specified transaction, even if it hasn't been mined yet.
|
|
1122
|
+
*
|
|
1123
|
+
* * [Similar](/docs/api#isd) to [`ethers.provider.getTransaction`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getTransaction) in ethers.js, does not have `wait` method that waits until the transaction has been mined
|
|
1124
|
+
* * [Similar](/docs/api#isd) to [`web3.eth.getTransaction`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#gettransaction) in web3.js, some information returned using different types
|
|
1125
|
+
*
|
|
1126
|
+
* @param transactionHash the hash of the transaction to get information about
|
|
1127
|
+
* @returns information about the specified transaction
|
|
1128
|
+
* @example
|
|
1129
|
+
* ```javascript
|
|
1130
|
+
* await provider.getTransaction('0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789');
|
|
1131
|
+
* // {
|
|
1132
|
+
* // accessList: [],
|
|
1133
|
+
* // blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
|
|
1134
|
+
* // blockNumber: 14578286,
|
|
1135
|
+
* // chainId: 1,
|
|
1136
|
+
* // from: "0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4",
|
|
1137
|
+
* // gas: { TinyBig: 112163 },
|
|
1138
|
+
* // gasPrice: { TinyBig: 48592426858 },
|
|
1139
|
+
* // hash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
|
|
1140
|
+
* // input: "0x83259f17000000000000000000000000000000000000000000...",
|
|
1141
|
+
* // maxFeePerGas: { TinyBig: 67681261618 },
|
|
1142
|
+
* // maxPriorityFeePerGas: { TinyBig: 1500000000 },
|
|
1143
|
+
* // nonce: { TinyBig: 129 },
|
|
1144
|
+
* // r: "0x59a7c15b12c18cd68d6c440963d959bff3e73831ffc938e75ecad07f7ee43fbc",
|
|
1145
|
+
* // s: "0x1ebaf05f0d9273b16c2a7748b150a79d22533a8cd74552611cbe620fee3dcf1c",
|
|
1146
|
+
* // to: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
|
|
1147
|
+
* // transactionIndex: 29,
|
|
1148
|
+
* // type: 2,
|
|
1149
|
+
* // v: 0,
|
|
1150
|
+
* // value: { TinyBig: 0 },
|
|
1151
|
+
* // confirmations: 298140,
|
|
1152
|
+
* // }
|
|
1153
|
+
* ```
|
|
1154
|
+
*/
|
|
1155
|
+
async getTransaction(transactionHash) {
|
|
1156
|
+
const [rpcTransaction, blockNumber] = await Promise.all([
|
|
1157
|
+
this.post(
|
|
1158
|
+
buildRPCPostBody("eth_getTransactionByHash", [transactionHash])
|
|
1159
|
+
),
|
|
1160
|
+
this.getBlock("latest")
|
|
1161
|
+
]);
|
|
1162
|
+
const cleanedTransaction = cleanTransaction(rpcTransaction);
|
|
1163
|
+
cleanedTransaction.confirmations = blockNumber.number - cleanedTransaction.blockNumber + 1;
|
|
1164
|
+
return cleanedTransaction;
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* Gives information about a transaction that has already been mined. Includes additional information beyond what's provided by [`getTransaction`](/docs/api/modules#gettransaction).
|
|
1168
|
+
*
|
|
1169
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.getTransactionReceipt`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getTransactionReceipt) in ethers.js
|
|
1170
|
+
* * [Similar](/docs/api#isd) to [`web3.eth.getTransactionReceipt`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#gettransactionreceipt) in web3.js, some information returned using different types
|
|
1171
|
+
*
|
|
1172
|
+
* @param transactionHash the hash of the transaction to get information about
|
|
1173
|
+
* @returns information about the specified transaction that has already been mined
|
|
1174
|
+
* @example
|
|
1175
|
+
* ```javascript
|
|
1176
|
+
* await provider.getTransactionReceipt('0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789');
|
|
1177
|
+
* // {
|
|
1178
|
+
* // blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
|
|
1179
|
+
* // blockNumber: 14578286,
|
|
1180
|
+
* // contractAddress: null,
|
|
1181
|
+
* // cumulativeGasUsed: { TinyBig: 3067973 },
|
|
1182
|
+
* // effectiveGasPrice: { TinyBig: 48592426858 },
|
|
1183
|
+
* // from: "0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4",
|
|
1184
|
+
* // gasUsed: { TinyBig: 112163 },
|
|
1185
|
+
* // logs: [
|
|
1186
|
+
* // {
|
|
1187
|
+
* // address: "0x0eDF9bc41Bbc1354c70e2107F80C42caE7FBBcA8",
|
|
1188
|
+
* // blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
|
|
1189
|
+
* // blockNumber: 14578286,
|
|
1190
|
+
* // data: "0x0000000000000000000000000000000000000000000003a12ec797b5484968c1",
|
|
1191
|
+
* // logIndex: 42,
|
|
1192
|
+
* // topics: [
|
|
1193
|
+
* // "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
|
|
1194
|
+
* // "0x00000000000000000000000039b72d136ba3e4cef35f48cd09587ffab754dd8b",
|
|
1195
|
+
* // "0x000000000000000000000000dfd9de5f6fa60bd70636c0900752e93a6144aed4",
|
|
1196
|
+
* // ],
|
|
1197
|
+
* // transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
|
|
1198
|
+
* // transactionIndex: 29,
|
|
1199
|
+
* // },
|
|
1200
|
+
* // {
|
|
1201
|
+
* // address: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
|
|
1202
|
+
* // blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
|
|
1203
|
+
* // blockNumber: 14578286,
|
|
1204
|
+
* // data: "0x0000000000000000000000000000000000000000000003a12ec797b5484968c1",
|
|
1205
|
+
* // logIndex: 43,
|
|
1206
|
+
* // topics: [
|
|
1207
|
+
* // "0x34fcbac0073d7c3d388e51312faf357774904998eeb8fca628b9e6f65ee1cbf7",
|
|
1208
|
+
* // "0x000000000000000000000000dfd9de5f6fa60bd70636c0900752e93a6144aed4",
|
|
1209
|
+
* // "0x0000000000000000000000000000000000000000000000000000000000000003",
|
|
1210
|
+
* // ],
|
|
1211
|
+
* // transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
|
|
1212
|
+
* // transactionIndex: 29,
|
|
1213
|
+
* // },
|
|
1214
|
+
* // ],
|
|
1215
|
+
* // logsBloom: "0x00000000000000000000000000000...",
|
|
1216
|
+
* // status: 1,
|
|
1217
|
+
* // to: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
|
|
1218
|
+
* // transactionHash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
|
|
1219
|
+
* // transactionIndex: 29,
|
|
1220
|
+
* // type: 2,
|
|
1221
|
+
* // byzantium: true,
|
|
1222
|
+
* // confirmations: 298171,
|
|
1223
|
+
* // }
|
|
1224
|
+
* ```
|
|
1225
|
+
*/
|
|
1226
|
+
async getTransactionReceipt(transactionHash) {
|
|
1227
|
+
const [rpcTransaction, blockNumber] = await Promise.all([
|
|
1228
|
+
this.post(
|
|
1229
|
+
buildRPCPostBody("eth_getTransactionReceipt", [transactionHash])
|
|
1230
|
+
),
|
|
1231
|
+
this.getBlock("latest")
|
|
1232
|
+
]);
|
|
1233
|
+
const cleanedTransactionReceipt = cleanTransactionReceipt(rpcTransaction);
|
|
1234
|
+
cleanedTransactionReceipt.confirmations = blockNumber.number - cleanedTransactionReceipt.blockNumber + 1;
|
|
1235
|
+
return cleanedTransactionReceipt;
|
|
1236
|
+
}
|
|
1237
|
+
/**
|
|
1238
|
+
* Returns the number of sent transactions by an address, from genesis (or as far back as a provider looks) up to specified block.
|
|
1239
|
+
*
|
|
1240
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.getTransactionCount`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getTransactionCount) in ethers.js
|
|
1241
|
+
* * [Identical](/docs/api#isd) to [`web3.eth.getTransactionCount`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#gettransactioncount) in web3.js
|
|
1242
|
+
*
|
|
1243
|
+
* @param address the address to count number of sent transactions
|
|
1244
|
+
* @param blockTag the block to count transactions up to, inclusive
|
|
1245
|
+
* @returns the number of transactions sent by the specified address
|
|
1246
|
+
* @example
|
|
1247
|
+
* ```javascript
|
|
1248
|
+
* await provider.getTransactionCount('0x71660c4005ba85c37ccec55d0c4493e66fe775d3');
|
|
1249
|
+
* // 1060000
|
|
1250
|
+
* ```
|
|
1251
|
+
* @example
|
|
1252
|
+
* ```javascript
|
|
1253
|
+
* await provider.getTransactionCount('0x71660c4005ba85c37ccec55d0c4493e66fe775d3', 'latest');
|
|
1254
|
+
* // 1060000
|
|
1255
|
+
* ```
|
|
1256
|
+
* @example
|
|
1257
|
+
* ```javascript
|
|
1258
|
+
* await provider.getTransactionCount('0x71660c4005ba85c37ccec55d0c4493e66fe775d3', 14649390);
|
|
1259
|
+
* // 1053312
|
|
1260
|
+
* ```
|
|
1261
|
+
*/
|
|
1262
|
+
async getTransactionCount(address, blockTag = "latest") {
|
|
1263
|
+
blockTag = prepBlockTag(blockTag);
|
|
1264
|
+
const transactionCount = await this.post(
|
|
1265
|
+
buildRPCPostBody("eth_getTransactionCount", [address, blockTag])
|
|
1266
|
+
);
|
|
1267
|
+
return Number(hexToDecimal(transactionCount));
|
|
1268
|
+
}
|
|
1269
|
+
/**
|
|
1270
|
+
* Gets information about a certain block, optionally with full transaction objects.
|
|
1271
|
+
*
|
|
1272
|
+
* * [Similar](/docs/api#isd) to [`ethers.provider.getBlock`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getLogs) in ethers.js, includes some additional information. Can also return block with full transaction objects, similar to [`ethers.providers.getBlockWithTransactions`]
|
|
1273
|
+
* * [Identical](/docs/api#isd) to [`web3.eth.getBlock`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getpastlogs) in web3.js
|
|
1274
|
+
*
|
|
1275
|
+
* @param timeFrame The number, hash, or text-based description ('latest', 'earliest', or 'pending') of the block to collect information on.
|
|
1276
|
+
* @param returnTransactionObjects Whether to also return data about the transactions on the block.
|
|
1277
|
+
* @returns A BlockResponse object with information about the specified block
|
|
1278
|
+
* @example
|
|
1279
|
+
* ```javascript
|
|
1280
|
+
* await provider.getBlock(14879862);
|
|
1281
|
+
* // {
|
|
1282
|
+
* // baseFeePerGas: { TinyBig: 39095728776 },
|
|
1283
|
+
* // difficulty: { TinyBig: 14321294455359973 },
|
|
1284
|
+
* // extraData: "0x486976656f6e2073672d6865617679",
|
|
1285
|
+
* // gasLimit: { TinyBig: 29970620 },
|
|
1286
|
+
* // gasUsed: { TinyBig: 20951384 },
|
|
1287
|
+
* // hash: "0x563b458ec3c4f87393b53f70bdddc0058497109b784d8cacd9247ddf267049ab",
|
|
1288
|
+
* // logsBloom:
|
|
1289
|
+
* // "0x9f38794fe80b521794df6efad8b0d2e9582f9ec3959a3f9384bda0fa371cfa5fac5af9d515c6bdf1ec325f5b5f7ebdd6a3a9fae17b38a86d4dc4b0971afc68d8086640550f4c156e6f923f4a1bb94fb0bed6cdcc474c5c64bfeff7a4a906f72b9a7b94004ee58efc53d63ac66961acd3a431b2d896cc9fd75f6072960bced45f770587caf130f57504decfcb63c6ca8fbc5bdbd749edd5a99a7375d2b81872289adb775fb3c928259f4be39c6d3f4d5b6217822979bb88c1f1fb62429b1b6d41cf4e3f77f9e1db3f5723108f1e5b1255dd734ad8cdb11e7ea22487c788e67c83777b6f395e504ca59c64f52245ee6de3804cf809e5caa4f0ea6a9aa9eb6ed801",
|
|
1290
|
+
* // miner: "0x1aD91ee08f21bE3dE0BA2ba6918E714dA6B45836",
|
|
1291
|
+
* // mixHash: "0x73cc9419bfb89c9d41c3a8c34ce56b5ebe468bdcf870258d2e77262275d580ec",
|
|
1292
|
+
* // nonce: "0x976f3f5d596ffb08",
|
|
1293
|
+
* // number: 14879862,
|
|
1294
|
+
* // parentHash: "0x95986ae14a71face8d9a6a379edd875b2e8bc73e4de0d9d460e7752bddb0f579",
|
|
1295
|
+
* // receiptsRoot: "0x8e6ba2fd9bee602b653dae6e3132f16538c2c5df24f1df8c000392053f73defa",
|
|
1296
|
+
* // sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
|
|
1297
|
+
* // size: { TinyBig: 134483 },
|
|
1298
|
+
* // stateRoot: "0xbf2bb67bd1c741f3d00904b8451d7c2cf4e3a2726f5a5884792ede2074747b85",
|
|
1299
|
+
* // timestamp: { TinyBig: 1654016186 },
|
|
1300
|
+
* // totalDifficulty: { TinyBig: 50478104614257705213748 },
|
|
1301
|
+
* // transactions: [
|
|
1302
|
+
* // "0xb3326a9149809603a2c28545e50e4f7d16e194bf5ee9764e0544603854c4a8d2",
|
|
1303
|
+
* // "0x8b42095f8d335404a4896b2817b8e5e3d86a5a87cb434a8eec295d5280a7f48e",
|
|
1304
|
+
* // "0x882f78fcb73f0f7ad0700bb0424a8b4beb366aaa93b88a3562c49a8d0ce4dcff",
|
|
1305
|
+
* // ...
|
|
1306
|
+
* // ],
|
|
1307
|
+
* // transactionsRoot: "0x5934902f3dcc263ec34f24318179bf6301f53f4834685792066026f3a4849d72",
|
|
1308
|
+
* // uncles: [],
|
|
1309
|
+
* // }
|
|
1310
|
+
* ```
|
|
1311
|
+
*/
|
|
1312
|
+
async getBlock(timeFrame = "latest", returnTransactionObjects = false) {
|
|
1313
|
+
let type = "Number";
|
|
1314
|
+
if (typeof timeFrame === "string" && timeFrame.length === 66) {
|
|
1315
|
+
type = "Hash";
|
|
1316
|
+
} else {
|
|
1317
|
+
timeFrame = prepBlockTag(timeFrame);
|
|
1318
|
+
}
|
|
1319
|
+
const rpcBlock = await this.post(
|
|
1320
|
+
buildRPCPostBody(`eth_getBlockBy${type}`, [
|
|
1321
|
+
timeFrame,
|
|
1322
|
+
returnTransactionObjects
|
|
1323
|
+
])
|
|
1324
|
+
);
|
|
1325
|
+
return cleanBlock(rpcBlock, returnTransactionObjects);
|
|
1326
|
+
}
|
|
1327
|
+
/**
|
|
1328
|
+
* Gives an estimate of the current gas price in wei.
|
|
1329
|
+
*
|
|
1330
|
+
* * [Similar](/docs/api#isd) to [`ethers.provider.getGasPrice`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getGasPrice) in ethers.js, does not have a parameter specifying what unit you'd like to return. See also [`weiToEther`](/docs/api/modules#weitoether) and [`etherToGwei`](/docs/api/modules#ethertogwei)
|
|
1331
|
+
* * [Identical](/docs/api#isd) to [`web3.eth.getGasPrice`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getgasprice) in web3.js, returns a number (TinyBig) instead of a string
|
|
1332
|
+
*
|
|
1333
|
+
* @returns an estimate of the current gas price in wei
|
|
1334
|
+
* @example
|
|
1335
|
+
* ```javascript
|
|
1336
|
+
* await provider.getGasPrice();
|
|
1337
|
+
* // 52493941856
|
|
1338
|
+
* ```
|
|
1339
|
+
*/
|
|
1340
|
+
async getGasPrice() {
|
|
1341
|
+
const hexGasPrice = await this.post(
|
|
1342
|
+
buildRPCPostBody("eth_gasPrice", [])
|
|
1343
|
+
);
|
|
1344
|
+
return tinyBig(hexToDecimal(hexGasPrice));
|
|
1345
|
+
}
|
|
1346
|
+
/**
|
|
1347
|
+
* Returns the balance of the account in wei.
|
|
1348
|
+
*
|
|
1349
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.getBalance`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getBalance) in ethers.js
|
|
1350
|
+
* * [Identical](/docs/api#isd) to [`web3.eth.getBalance`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getbalance) in web3.js, returns a number (TinyBig) instead of a string
|
|
1351
|
+
*
|
|
1352
|
+
* @param address the address to check the balance of
|
|
1353
|
+
* @param blockTag the block to check the specified address' balance on
|
|
1354
|
+
* @returns the balance of the network's native token for the specified address on the specified block
|
|
1355
|
+
* @example
|
|
1356
|
+
* ```javascript
|
|
1357
|
+
* await provider.getBalance('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8');
|
|
1358
|
+
* // 28798127851528138
|
|
1359
|
+
* ```
|
|
1360
|
+
*/
|
|
1361
|
+
async getBalance(address, blockTag = "latest") {
|
|
1362
|
+
blockTag = prepBlockTag(blockTag);
|
|
1363
|
+
const hexBalance = await this.post(
|
|
1364
|
+
buildRPCPostBody("eth_getBalance", [address, blockTag])
|
|
1365
|
+
);
|
|
1366
|
+
return tinyBig(hexToDecimal(hexBalance));
|
|
1367
|
+
}
|
|
1368
|
+
/**
|
|
1369
|
+
* Gets the code of a contract on a specified block.
|
|
1370
|
+
*
|
|
1371
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.getCode`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getCode) in ethers.js
|
|
1372
|
+
* * [Identical](/docs/api#isd) to [`web3.eth.getCode`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getcode) in web3.js
|
|
1373
|
+
*
|
|
1374
|
+
* @param address the contract address to get the contract code from
|
|
1375
|
+
* @param blockTag the block height to search for the contract code from. Contract code can change, so this allows for checking a specific block
|
|
1376
|
+
* @returns the contract creation code for the specified address at the specified block height
|
|
1377
|
+
* @example
|
|
1378
|
+
* ```javascript
|
|
1379
|
+
* await jsonRpcProvider().getCode('0xaC6095720221C79C6E7C638d260A2eFBC5D8d880', 'latest');
|
|
1380
|
+
* // '0x608060405234801561001057600080fd5b506004361061...'
|
|
1381
|
+
* ```
|
|
1382
|
+
*/
|
|
1383
|
+
async getCode(address, blockTag = "latest") {
|
|
1384
|
+
blockTag = prepBlockTag(blockTag);
|
|
1385
|
+
const contractCode = await this.post(
|
|
1386
|
+
buildRPCPostBody("eth_getCode", [address, blockTag])
|
|
1387
|
+
);
|
|
1388
|
+
return contractCode;
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* Returns an estimate of the amount of gas that would be required to submit transaction to the network.
|
|
1392
|
+
* An estimate may not be accurate since there could be another transaction on the network that was not accounted for.
|
|
1393
|
+
*
|
|
1394
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.estimateGas`](https://docs.ethers.io/v5/api/providers/provider/#Provider-estimateGas) in ethers.js
|
|
1395
|
+
* * [Identical](/docs/api#isd) to [`web3.eth.estimateGas`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#estimateGas) in web3.js
|
|
1396
|
+
*
|
|
1397
|
+
* @param transaction the transaction to check the estimated gas cost for
|
|
1398
|
+
* @returns the estimated amount of gas charged for submitting the specified transaction to the blockchain
|
|
1399
|
+
* @example
|
|
1400
|
+
* ```javascript
|
|
1401
|
+
* await provider.estimateGas({
|
|
1402
|
+
* // Wrapped ETH address
|
|
1403
|
+
* to: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
|
|
1404
|
+
* data: "0xd0e30db0",
|
|
1405
|
+
* value: etherToWei('1.0').toHexString(),
|
|
1406
|
+
* });
|
|
1407
|
+
* // { TinyBig: "27938" }
|
|
1408
|
+
* ```
|
|
1409
|
+
*/
|
|
1410
|
+
async estimateGas(transaction) {
|
|
1411
|
+
const rpcTransaction = prepareTransaction(transaction);
|
|
1412
|
+
const gasUsed = await this.post(
|
|
1413
|
+
buildRPCPostBody("eth_estimateGas", [rpcTransaction])
|
|
1414
|
+
);
|
|
1415
|
+
return tinyBig(hexToDecimal(gasUsed));
|
|
1416
|
+
}
|
|
1417
|
+
/**
|
|
1418
|
+
* Returns the current recommended FeeData to use in a transaction.
|
|
1419
|
+
* For an EIP-1559 transaction, the maxFeePerGas and maxPriorityFeePerGas should be used.
|
|
1420
|
+
* For legacy transactions and networks which do not support EIP-1559, the gasPrice should be used.Returns an estimate of the amount of gas that would be required to submit transaction to the network.
|
|
1421
|
+
*
|
|
1422
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.getFeeData`](https://docs.ethers.org/v5/api/providers/provider/#Provider-getFeeData) in ethers.js
|
|
1423
|
+
*
|
|
1424
|
+
* @returns an object with gas estimates for the network currently
|
|
1425
|
+
* @example
|
|
1426
|
+
* ```javascript
|
|
1427
|
+
* await provider.getFeeData();
|
|
1428
|
+
* // {
|
|
1429
|
+
* // gasPrice: { TinyBig: "14184772639" },
|
|
1430
|
+
* // lastBaseFeePerGas: { TinyBig: "14038523098" },
|
|
1431
|
+
* // maxFeePerGas: { TinyBig: "29577046196" },
|
|
1432
|
+
* // maxPriorityFeePerGas: { TinyBig: "1500000000" }
|
|
1433
|
+
* // }
|
|
1434
|
+
* ```
|
|
1435
|
+
*/
|
|
1436
|
+
async getFeeData() {
|
|
1437
|
+
const [block, gasPrice] = await Promise.all([
|
|
1438
|
+
this.getBlock("latest"),
|
|
1439
|
+
this.getGasPrice()
|
|
1440
|
+
]);
|
|
1441
|
+
let lastBaseFeePerGas = null, maxFeePerGas = null, maxPriorityFeePerGas = null;
|
|
1442
|
+
if (block && block.baseFeePerGas) {
|
|
1443
|
+
lastBaseFeePerGas = block.baseFeePerGas;
|
|
1444
|
+
maxPriorityFeePerGas = tinyBig("1500000000");
|
|
1445
|
+
maxFeePerGas = tinyBig(
|
|
1446
|
+
block.baseFeePerGas.mul(2).add(maxPriorityFeePerGas)
|
|
1447
|
+
);
|
|
1448
|
+
}
|
|
1449
|
+
return { lastBaseFeePerGas, maxFeePerGas, maxPriorityFeePerGas, gasPrice };
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* Returns transaction receipt event logs that match a specified filter.
|
|
1453
|
+
* May return `[]` if parameters are too broad, even if logs exist.
|
|
1454
|
+
*
|
|
1455
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.getLogs`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getLogs) in ethers.js
|
|
1456
|
+
* * [Identical](/docs/api#isd) to [`web3.eth.getPastLogs`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getpastlogs) in web3.js
|
|
1457
|
+
*
|
|
1458
|
+
* @param filter parameters to filter the logs by
|
|
1459
|
+
* @returns an array of logs matching the specified filter
|
|
1460
|
+
* @example
|
|
1461
|
+
* ```javascript
|
|
1462
|
+
* provider.getLogs({
|
|
1463
|
+
* address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
|
|
1464
|
+
* topics: [
|
|
1465
|
+
* "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
|
|
1466
|
+
* "0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e",
|
|
1467
|
+
* ],
|
|
1468
|
+
* fromBlock: 14825027,
|
|
1469
|
+
* toBlock: 14825039,
|
|
1470
|
+
* });
|
|
1471
|
+
*
|
|
1472
|
+
* [
|
|
1473
|
+
* {
|
|
1474
|
+
* address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
|
|
1475
|
+
* blockHash: '0x8e0dfac2f704851960f866c8708b3bef2f66c0fee0329cf25ff0261b264ca6bc',
|
|
1476
|
+
* blockNumber: 14825029,
|
|
1477
|
+
* data: '0x000000000000000000000000000000000000000000000000005f862ee352a38a',
|
|
1478
|
+
* logIndex: 384,
|
|
1479
|
+
* removed: false,
|
|
1480
|
+
* topics: [
|
|
1481
|
+
* '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
|
|
1482
|
+
* '0x00000000000000000000000021b8065d10f73ee2e260e5b47d3344d3ced7596e',
|
|
1483
|
+
* '0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45'
|
|
1484
|
+
* ],
|
|
1485
|
+
* transactionHash: '0xbd49031be16f8fd1775f4e0fe79b408ffd8ae9c65b2827ee47e3238e3f51f4c0',
|
|
1486
|
+
* transactionIndex: 226
|
|
1487
|
+
* }
|
|
1488
|
+
* ]
|
|
1489
|
+
* ```
|
|
1490
|
+
*/
|
|
1491
|
+
async getLogs(filter) {
|
|
1492
|
+
const filterByRange = filter;
|
|
1493
|
+
if (filterByRange.fromBlock)
|
|
1494
|
+
filterByRange.fromBlock = prepBlockTag(filterByRange.fromBlock);
|
|
1495
|
+
if (filterByRange.toBlock)
|
|
1496
|
+
filterByRange.toBlock = prepBlockTag(filterByRange.toBlock);
|
|
1497
|
+
const rpcLogs = await this.post(
|
|
1498
|
+
buildRPCPostBody("eth_getLogs", [filter])
|
|
1499
|
+
);
|
|
1500
|
+
const logs = rpcLogs.map((log) => cleanLog(log, false));
|
|
1501
|
+
return logs;
|
|
1502
|
+
}
|
|
1503
|
+
/**
|
|
1504
|
+
* Returns the result of adding a transaction to the blockchain without actually adding that transaction to the blockchain.
|
|
1505
|
+
* Does not require any ether as gas.
|
|
1506
|
+
*
|
|
1507
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.call`](https://docs.ethers.io/v5/api/providers/provider/#Provider-call) in ethers.js
|
|
1508
|
+
* * [Identical](/docs/api#isd) to [`web3.eth.call`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#call) in web3.js
|
|
1509
|
+
*
|
|
1510
|
+
* @param transaction the transaction object to, in theory, execute. Doesn't actually get added to the blockchain.
|
|
1511
|
+
* @param blockTag the block to execute this transaction on
|
|
1512
|
+
* @returns the result of executing the transaction on the specified block
|
|
1513
|
+
* @example
|
|
1514
|
+
* ```javascript
|
|
1515
|
+
* await provider.call({ to: "0x6b175474e89094c44da98b954eedeac495271d0f", data: "0x70a082310000000000000000000000006E0d01A76C3Cf4288372a29124A26D4353EE51BE" });
|
|
1516
|
+
* // '0x0000000000000000000000000000000000000000000000000858898f93629000'
|
|
1517
|
+
* ```
|
|
1518
|
+
*/
|
|
1519
|
+
async call(transaction, blockTag = "latest") {
|
|
1520
|
+
if (transaction.gasPrice && (transaction.maxPriorityFeePerGas || transaction.maxFeePerGas)) {
|
|
1521
|
+
logger.throwError(
|
|
1522
|
+
'Cannot specify both "gasPrice" and ("maxPriorityFeePerGas" or "maxFeePerGas")',
|
|
1523
|
+
{
|
|
1524
|
+
gasPrice: transaction.gasPrice,
|
|
1525
|
+
maxFeePerGas: transaction.maxFeePerGas,
|
|
1526
|
+
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas
|
|
1527
|
+
}
|
|
1528
|
+
);
|
|
1529
|
+
}
|
|
1530
|
+
if (transaction.maxFeePerGas && transaction.maxPriorityFeePerGas) {
|
|
1531
|
+
logger.throwError(
|
|
1532
|
+
'Cannot specify both "maxFeePerGas" and "maxPriorityFeePerGas"',
|
|
1533
|
+
{
|
|
1534
|
+
maxFeePerGas: transaction.maxFeePerGas,
|
|
1535
|
+
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas
|
|
1536
|
+
}
|
|
1537
|
+
);
|
|
1538
|
+
}
|
|
1539
|
+
blockTag = prepBlockTag(blockTag);
|
|
1540
|
+
const rpcTransaction = prepareTransaction(transaction);
|
|
1541
|
+
const transactionRes = await this.post(
|
|
1542
|
+
buildRPCPostBody("eth_call", [rpcTransaction, blockTag])
|
|
1543
|
+
);
|
|
1544
|
+
return transactionRes;
|
|
1545
|
+
}
|
|
1546
|
+
};
|
|
1547
|
+
|
|
1548
|
+
// src/providers/JsonRpcProvider.ts
|
|
1549
|
+
var JsonRpcProvider = class extends BaseProvider {
|
|
1550
|
+
/**
|
|
1551
|
+
* @ignore
|
|
1552
|
+
*/
|
|
1553
|
+
selectRpcUrl() {
|
|
1554
|
+
return this._rpcUrls[0];
|
|
1555
|
+
}
|
|
1556
|
+
/**
|
|
1557
|
+
* @ignore
|
|
1558
|
+
*/
|
|
1559
|
+
post(body) {
|
|
1560
|
+
return this._post(body);
|
|
1561
|
+
}
|
|
1562
|
+
/**
|
|
1563
|
+
* @param rpcUrl The URL to your Eth node. Consider POKT or Infura
|
|
1564
|
+
* @example
|
|
1565
|
+
* `https://free-eth-node.com/api/eth`
|
|
1566
|
+
* @example
|
|
1567
|
+
* `https://mainnet.infura.io/v3/YOUR-PROJECT-ID`
|
|
1568
|
+
*/
|
|
1569
|
+
constructor(rpcUrl = "https://free-eth-node.com/api/eth") {
|
|
1570
|
+
super([rpcUrl]);
|
|
1571
|
+
}
|
|
1572
|
+
};
|
|
1573
|
+
function jsonRpcProvider(rpcUrl) {
|
|
1574
|
+
return new JsonRpcProvider(rpcUrl);
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
// src/providers/AlchemyProvider.ts
|
|
1578
|
+
var AlchemyProvider = class extends JsonRpcProvider {
|
|
1579
|
+
constructor(apiKey, network = "mainnet") {
|
|
1580
|
+
const alchemyUrl = `https://eth-${network}.alchemyapi.io/v2/${apiKey}`;
|
|
1581
|
+
super(alchemyUrl);
|
|
1582
|
+
}
|
|
1583
|
+
};
|
|
1584
|
+
|
|
1585
|
+
// src/providers/FallthroughProvider.ts
|
|
1586
|
+
var promiseTimeout = (prom, time) => new Promise((resolve, reject) => {
|
|
1587
|
+
const timeout = setTimeout(
|
|
1588
|
+
() => reject(new Error("Promise timed out")),
|
|
1589
|
+
time
|
|
1590
|
+
);
|
|
1591
|
+
prom.then((result) => {
|
|
1592
|
+
clearTimeout(timeout);
|
|
1593
|
+
resolve(result);
|
|
1594
|
+
}).catch((error) => {
|
|
1595
|
+
clearTimeout(timeout);
|
|
1596
|
+
reject(error);
|
|
1597
|
+
});
|
|
1598
|
+
});
|
|
1599
|
+
var DEFAULT_TIMEOUT_DURATION = 8e3;
|
|
1600
|
+
var FallthroughProvider = class extends BaseProvider {
|
|
1601
|
+
// index of current trusted rpc url
|
|
1602
|
+
/**
|
|
1603
|
+
* @ignore
|
|
1604
|
+
*/
|
|
1605
|
+
rpcUrlCounter = 0;
|
|
1606
|
+
timeoutDuration;
|
|
1607
|
+
/**
|
|
1608
|
+
* @ignore
|
|
1609
|
+
*/
|
|
1610
|
+
selectRpcUrl() {
|
|
1611
|
+
return this._rpcUrls[this.rpcUrlCounter];
|
|
1612
|
+
}
|
|
1613
|
+
constructor(rpcUrls, options = {}) {
|
|
1614
|
+
if (!Array.isArray(rpcUrls)) {
|
|
1615
|
+
logger.throwError("Array required", { rpcUrls });
|
|
1616
|
+
}
|
|
1617
|
+
if (rpcUrls.length <= 1) {
|
|
1618
|
+
logger.throwError("More than one rpcUrl is required", { rpcUrls });
|
|
1619
|
+
}
|
|
1620
|
+
super(rpcUrls);
|
|
1621
|
+
this.timeoutDuration = options.timeoutDuration || DEFAULT_TIMEOUT_DURATION;
|
|
1622
|
+
}
|
|
1623
|
+
/**
|
|
1624
|
+
* @ignore
|
|
1625
|
+
*/
|
|
1626
|
+
post = (body) => {
|
|
1627
|
+
const genesisCount = this.rpcUrlCounter;
|
|
1628
|
+
const recursivePostRetry = () => {
|
|
1629
|
+
const genesisRpcUrl = this.selectRpcUrl();
|
|
1630
|
+
const res = promiseTimeout(this._post(body), this.timeoutDuration).catch(
|
|
1631
|
+
(e) => {
|
|
1632
|
+
if (genesisRpcUrl === this.selectRpcUrl()) {
|
|
1633
|
+
this.rpcUrlCounter = (this.rpcUrlCounter + 1) % this._rpcUrls.length;
|
|
1634
|
+
}
|
|
1635
|
+
if (this.rpcUrlCounter === genesisCount) {
|
|
1636
|
+
throw e;
|
|
1637
|
+
}
|
|
1638
|
+
return recursivePostRetry();
|
|
1639
|
+
}
|
|
1640
|
+
);
|
|
1641
|
+
return res;
|
|
1642
|
+
};
|
|
1643
|
+
return recursivePostRetry();
|
|
1644
|
+
};
|
|
1645
|
+
};
|
|
1646
|
+
|
|
1647
|
+
// src/utils/compute-public-key.ts
|
|
1648
|
+
var import_secp256k1 = require("@noble/secp256k1");
|
|
1649
|
+
function computePublicKey(privKey) {
|
|
1650
|
+
privKey = hexlify(privKey).slice(2);
|
|
1651
|
+
return "0x" + import_secp256k1.Point.fromPrivateKey(privKey).toHex();
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
// src/utils/keccak256.ts
|
|
1655
|
+
var import_sha33 = require("sha3");
|
|
1656
|
+
function keccak256(data) {
|
|
1657
|
+
let bufferableData;
|
|
1658
|
+
if (typeof data === "string") {
|
|
1659
|
+
bufferableData = Buffer.from(data.replace(/^0x/, ""), "hex");
|
|
1660
|
+
} else {
|
|
1661
|
+
bufferableData = Buffer.from(data);
|
|
1662
|
+
}
|
|
1663
|
+
const keccak = new import_sha33.Keccak(256);
|
|
1664
|
+
const addressHash = "0x" + keccak.update(bufferableData).digest("hex");
|
|
1665
|
+
return addressHash;
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
// src/utils/compute-address.ts
|
|
1669
|
+
function computeAddress(key) {
|
|
1670
|
+
if (!key.startsWith("0x04") && !key.startsWith("0x03") && !key.startsWith("0x02")) {
|
|
1671
|
+
key = computePublicKey(key);
|
|
1672
|
+
}
|
|
1673
|
+
return toChecksumAddress(hexDataSlice(keccak256(hexDataSlice(key, 1)), 12));
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
// src/utils/ether-to-gwei.ts
|
|
1677
|
+
function etherToGwei(etherQuantity) {
|
|
1678
|
+
validateType(etherQuantity, ["string", "number", "object"]);
|
|
1679
|
+
const result = tinyBig(etherQuantity).times("1000000000");
|
|
1680
|
+
return tinyBig(result);
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
// src/utils/ether-to-wei.ts
|
|
1684
|
+
function etherToWei(etherQuantity) {
|
|
1685
|
+
validateType(etherQuantity, ["string", "number", "object"]);
|
|
1686
|
+
const result = tinyBig(etherQuantity).times("1000000000000000000");
|
|
1687
|
+
return tinyBig(result);
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
// src/utils/gwei-to-ether.ts
|
|
1691
|
+
function gweiToEther(gweiQuantity) {
|
|
1692
|
+
validateType(gweiQuantity, ["string", "number", "object"]);
|
|
1693
|
+
const result = tinyBig(gweiQuantity).div("1000000000");
|
|
1694
|
+
return tinyBig(result);
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
// src/utils/to-utf8-bytes.ts
|
|
1698
|
+
function toUtf8Bytes(data) {
|
|
1699
|
+
return new Uint8Array(Buffer.from(data));
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1702
|
+
// src/utils/hash-message.ts
|
|
1703
|
+
var messagePrefix = "Ethereum Signed Message:\n";
|
|
1704
|
+
function hashMessage(message) {
|
|
1705
|
+
if (typeof message === "string") {
|
|
1706
|
+
message = toUtf8Bytes(message);
|
|
1707
|
+
}
|
|
1708
|
+
return keccak256(
|
|
1709
|
+
concat([
|
|
1710
|
+
toUtf8Bytes(messagePrefix),
|
|
1711
|
+
toUtf8Bytes(String(message.length)),
|
|
1712
|
+
message
|
|
1713
|
+
])
|
|
1714
|
+
);
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
// src/utils/is-address.ts
|
|
1718
|
+
function isAddress(address) {
|
|
1719
|
+
validateType(address, ["string"]);
|
|
1720
|
+
try {
|
|
1721
|
+
toChecksumAddress(address);
|
|
1722
|
+
return true;
|
|
1723
|
+
} catch (error) {
|
|
1724
|
+
return false;
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
// src/utils/solidity-keccak256.ts
|
|
1729
|
+
var import_buffer = require("buffer");
|
|
1730
|
+
var regexBytes = new RegExp("^bytes([0-9]+)$");
|
|
1731
|
+
var regexNumber = new RegExp("^(u?int)([0-9]*)$");
|
|
1732
|
+
var regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$");
|
|
1733
|
+
function _pack(type, value, isArray) {
|
|
1734
|
+
switch (type) {
|
|
1735
|
+
case "address":
|
|
1736
|
+
if (isArray) {
|
|
1737
|
+
return zeroPad(value, 32);
|
|
1738
|
+
}
|
|
1739
|
+
return arrayify(value);
|
|
1740
|
+
case "string":
|
|
1741
|
+
return import_buffer.Buffer.from(value);
|
|
1742
|
+
case "bytes":
|
|
1743
|
+
return arrayify(value);
|
|
1744
|
+
case "bool":
|
|
1745
|
+
value = value ? "0x01" : "0x00";
|
|
1746
|
+
if (isArray) {
|
|
1747
|
+
return zeroPad(value, 32);
|
|
1748
|
+
}
|
|
1749
|
+
return arrayify(value);
|
|
1750
|
+
}
|
|
1751
|
+
let match = type.match(regexNumber);
|
|
1752
|
+
if (match) {
|
|
1753
|
+
let size = parseInt(match[2] || "256");
|
|
1754
|
+
if (match[2] && String(size) !== match[2] || size % 8 !== 0 || size === 0 || size > 256) {
|
|
1755
|
+
logger.throwArgumentError("invalid number type", "type", type);
|
|
1756
|
+
}
|
|
1757
|
+
if (isArray) {
|
|
1758
|
+
size = 256;
|
|
1759
|
+
}
|
|
1760
|
+
value = tinyBig(value).toTwos(size).toNumber();
|
|
1761
|
+
const hexValue2 = hexlify(value);
|
|
1762
|
+
return zeroPad(hexValue2, size / 8);
|
|
1763
|
+
}
|
|
1764
|
+
match = type.match(regexBytes);
|
|
1765
|
+
if (match) {
|
|
1766
|
+
const size = parseInt(match[1]);
|
|
1767
|
+
if (String(size) !== match[1] || size === 0 || size > 32) {
|
|
1768
|
+
logger.throwArgumentError("invalid bytes type", "type", type);
|
|
1769
|
+
}
|
|
1770
|
+
if (arrayify(value).byteLength !== size) {
|
|
1771
|
+
logger.throwArgumentError(`invalid value for ${type}`, "value", value);
|
|
1772
|
+
}
|
|
1773
|
+
if (isArray) {
|
|
1774
|
+
return arrayify((value + hexFalse).substring(0, 66));
|
|
1775
|
+
}
|
|
1776
|
+
return value;
|
|
1777
|
+
}
|
|
1778
|
+
match = type.match(regexArray);
|
|
1779
|
+
if (match && Array.isArray(value)) {
|
|
1780
|
+
const baseType = match[1];
|
|
1781
|
+
const count = parseInt(match[2] || String(value.length));
|
|
1782
|
+
if (count != value.length) {
|
|
1783
|
+
logger.throwArgumentError(
|
|
1784
|
+
`invalid array length for ${type}`,
|
|
1785
|
+
"value",
|
|
1786
|
+
value
|
|
1787
|
+
);
|
|
1788
|
+
}
|
|
1789
|
+
const result = [];
|
|
1790
|
+
value.forEach(function(value2) {
|
|
1791
|
+
result.push(_pack(baseType, value2, true));
|
|
1792
|
+
});
|
|
1793
|
+
return concat(result);
|
|
1794
|
+
}
|
|
1795
|
+
return logger.throwArgumentError("invalid type", "type", type);
|
|
1796
|
+
}
|
|
1797
|
+
function pack(types, values) {
|
|
1798
|
+
if (types.length != values.length) {
|
|
1799
|
+
logger.throwArgumentError(
|
|
1800
|
+
"wrong number of values; expected ${ types.length }",
|
|
1801
|
+
"values",
|
|
1802
|
+
values
|
|
1803
|
+
);
|
|
1804
|
+
}
|
|
1805
|
+
const tight = [];
|
|
1806
|
+
types.forEach(function(type, index) {
|
|
1807
|
+
tight.push(_pack(type, values[index]));
|
|
1808
|
+
});
|
|
1809
|
+
return hexlify(concat(tight));
|
|
1810
|
+
}
|
|
1811
|
+
function solidityKeccak256(types, values) {
|
|
1812
|
+
return keccak256(pack(types, values));
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
// src/utils/split-signature.ts
|
|
1816
|
+
function splitSignature(signature) {
|
|
1817
|
+
const result = {
|
|
1818
|
+
r: "0x",
|
|
1819
|
+
s: "0x",
|
|
1820
|
+
_vs: "0x",
|
|
1821
|
+
recoveryParam: 0,
|
|
1822
|
+
v: 0,
|
|
1823
|
+
yParityAndS: "0x",
|
|
1824
|
+
compact: "0x"
|
|
1825
|
+
};
|
|
1826
|
+
if (isBytesLike(signature)) {
|
|
1827
|
+
const bytes = arrayify(signature);
|
|
1828
|
+
if (bytes.length === 64) {
|
|
1829
|
+
result.v = 27 + (bytes[32] >> 7);
|
|
1830
|
+
bytes[32] &= 127;
|
|
1831
|
+
result.r = hexlify(bytes.slice(0, 32));
|
|
1832
|
+
result.s = hexlify(bytes.slice(32, 64));
|
|
1833
|
+
} else if (bytes.length === 65) {
|
|
1834
|
+
result.r = hexlify(bytes.slice(0, 32));
|
|
1835
|
+
result.s = hexlify(bytes.slice(32, 64));
|
|
1836
|
+
result.v = bytes[64];
|
|
1837
|
+
} else {
|
|
1838
|
+
logger.throwArgumentError(
|
|
1839
|
+
"invalid signature string",
|
|
1840
|
+
"signature",
|
|
1841
|
+
signature
|
|
1842
|
+
);
|
|
1843
|
+
}
|
|
1844
|
+
if (result.v < 27) {
|
|
1845
|
+
if (result.v === 0 || result.v === 1) {
|
|
1846
|
+
result.v += 27;
|
|
1847
|
+
} else {
|
|
1848
|
+
logger.throwArgumentError(
|
|
1849
|
+
"signature invalid v byte",
|
|
1850
|
+
"signature",
|
|
1851
|
+
signature
|
|
1852
|
+
);
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
result.recoveryParam = 1 - result.v % 2;
|
|
1856
|
+
if (result.recoveryParam) {
|
|
1857
|
+
bytes[32] |= 128;
|
|
1858
|
+
}
|
|
1859
|
+
result._vs = hexlify(bytes.slice(32, 64));
|
|
1860
|
+
} else {
|
|
1861
|
+
result.r = signature.r;
|
|
1862
|
+
result.s = signature.s;
|
|
1863
|
+
result.v = signature.v;
|
|
1864
|
+
result.recoveryParam = signature.recoveryParam;
|
|
1865
|
+
result._vs = signature._vs;
|
|
1866
|
+
if (result._vs != null) {
|
|
1867
|
+
const vs_1 = zeroPad(arrayify(result._vs), 32);
|
|
1868
|
+
result._vs = hexlify(vs_1);
|
|
1869
|
+
const recoveryParam = vs_1[0] >= 128 ? 1 : 0;
|
|
1870
|
+
if (result.recoveryParam == null) {
|
|
1871
|
+
result.recoveryParam = recoveryParam;
|
|
1872
|
+
} else if (result.recoveryParam !== recoveryParam) {
|
|
1873
|
+
logger.throwArgumentError(
|
|
1874
|
+
"signature recoveryParam mismatch _vs",
|
|
1875
|
+
"signature",
|
|
1876
|
+
signature
|
|
1877
|
+
);
|
|
1878
|
+
}
|
|
1879
|
+
vs_1[0] &= 127;
|
|
1880
|
+
const s = hexlify(vs_1);
|
|
1881
|
+
if (result.s == null) {
|
|
1882
|
+
result.s = s;
|
|
1883
|
+
} else if (result.s !== s) {
|
|
1884
|
+
logger.throwArgumentError(
|
|
1885
|
+
"signature v mismatch _vs",
|
|
1886
|
+
"signature",
|
|
1887
|
+
signature
|
|
1888
|
+
);
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
if (result.recoveryParam == null) {
|
|
1892
|
+
if (result.v == null) {
|
|
1893
|
+
logger.throwArgumentError(
|
|
1894
|
+
"signature missing v and recoveryParam",
|
|
1895
|
+
"signature",
|
|
1896
|
+
signature
|
|
1897
|
+
);
|
|
1898
|
+
} else if (result.v === 0 || result.v === 1) {
|
|
1899
|
+
result.recoveryParam = result.v;
|
|
1900
|
+
} else {
|
|
1901
|
+
result.recoveryParam = 1 - result.v % 2;
|
|
1902
|
+
}
|
|
1903
|
+
} else {
|
|
1904
|
+
if (result.v == null) {
|
|
1905
|
+
result.v = 27 + result.recoveryParam;
|
|
1906
|
+
} else {
|
|
1907
|
+
const recId = result.v === 0 || result.v === 1 ? result.v : 1 - result.v % 2;
|
|
1908
|
+
if (result.recoveryParam !== recId) {
|
|
1909
|
+
logger.throwArgumentError(
|
|
1910
|
+
"signature recoveryParam mismatch v",
|
|
1911
|
+
"signature",
|
|
1912
|
+
signature
|
|
1913
|
+
);
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
if (result.r == null || !isHexString(result.r)) {
|
|
1918
|
+
logger.throwArgumentError(
|
|
1919
|
+
"signature missing or invalid r",
|
|
1920
|
+
"signature",
|
|
1921
|
+
signature
|
|
1922
|
+
);
|
|
1923
|
+
} else {
|
|
1924
|
+
result.r = hexZeroPad(result.r, 32);
|
|
1925
|
+
}
|
|
1926
|
+
if (result.s == null || !isHexString(result.s)) {
|
|
1927
|
+
logger.throwArgumentError(
|
|
1928
|
+
"signature missing or invalid s",
|
|
1929
|
+
"signature",
|
|
1930
|
+
signature
|
|
1931
|
+
);
|
|
1932
|
+
} else {
|
|
1933
|
+
result.s = hexZeroPad(result.s, 32);
|
|
1934
|
+
}
|
|
1935
|
+
const vs = arrayify(result.s);
|
|
1936
|
+
if (vs[0] >= 128) {
|
|
1937
|
+
logger.throwArgumentError(
|
|
1938
|
+
"signature s out of range",
|
|
1939
|
+
"signature",
|
|
1940
|
+
signature
|
|
1941
|
+
);
|
|
1942
|
+
}
|
|
1943
|
+
if (result.recoveryParam) {
|
|
1944
|
+
vs[0] |= 128;
|
|
1945
|
+
}
|
|
1946
|
+
const _vs = hexlify(vs);
|
|
1947
|
+
if (result._vs) {
|
|
1948
|
+
if (!isHexString(result._vs)) {
|
|
1949
|
+
logger.throwArgumentError(
|
|
1950
|
+
"signature invalid _vs",
|
|
1951
|
+
"signature",
|
|
1952
|
+
signature
|
|
1953
|
+
);
|
|
1954
|
+
}
|
|
1955
|
+
result._vs = hexZeroPad(result._vs, 32);
|
|
1956
|
+
}
|
|
1957
|
+
if (result._vs == null) {
|
|
1958
|
+
result._vs = _vs;
|
|
1959
|
+
} else if (result._vs !== _vs) {
|
|
1960
|
+
logger.throwArgumentError(
|
|
1961
|
+
"signature _vs mismatch v and s",
|
|
1962
|
+
"signature",
|
|
1963
|
+
signature
|
|
1964
|
+
);
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
result.yParityAndS = result._vs;
|
|
1968
|
+
result.compact = result.r + result.yParityAndS.substring(2);
|
|
1969
|
+
return result;
|
|
1970
|
+
}
|
|
1971
|
+
|
|
1972
|
+
// src/utils/wei-to-ether.ts
|
|
1973
|
+
function weiToEther(weiQuantity) {
|
|
1974
|
+
validateType(weiQuantity, ["string", "number", "object"]);
|
|
1975
|
+
try {
|
|
1976
|
+
let _weiQuantity = weiQuantity;
|
|
1977
|
+
if (typeof weiQuantity === "string" && weiQuantity.slice(0, 2) === "0x") {
|
|
1978
|
+
_weiQuantity = BigInt(weiQuantity).toString();
|
|
1979
|
+
}
|
|
1980
|
+
const result = tinyBig(_weiQuantity).div("1000000000000000000");
|
|
1981
|
+
return tinyBig(result);
|
|
1982
|
+
} catch (error) {
|
|
1983
|
+
throw error;
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1987
|
+
0 && (module.exports = {
|
|
1988
|
+
AlchemyProvider,
|
|
1989
|
+
BaseContract,
|
|
1990
|
+
Contract,
|
|
1991
|
+
FallthroughProvider,
|
|
1992
|
+
JsonRpcProvider,
|
|
1993
|
+
TinyBig,
|
|
1994
|
+
arrayify,
|
|
1995
|
+
computeAddress,
|
|
1996
|
+
computePublicKey,
|
|
1997
|
+
concat,
|
|
1998
|
+
etherToGwei,
|
|
1999
|
+
etherToWei,
|
|
2000
|
+
gweiToEther,
|
|
2001
|
+
hashMessage,
|
|
2002
|
+
hexConcat,
|
|
2003
|
+
hexDataLength,
|
|
2004
|
+
hexDataSlice,
|
|
2005
|
+
hexStripZeros,
|
|
2006
|
+
hexValue,
|
|
2007
|
+
hexZeroPad,
|
|
2008
|
+
hexlify,
|
|
2009
|
+
isAddress,
|
|
2010
|
+
isBytes,
|
|
2011
|
+
isBytesLike,
|
|
2012
|
+
isHexString,
|
|
2013
|
+
jsonRpcProvider,
|
|
2014
|
+
keccak256,
|
|
2015
|
+
pack,
|
|
2016
|
+
solidityKeccak256,
|
|
2017
|
+
splitSignature,
|
|
2018
|
+
stripZeros,
|
|
2019
|
+
tinyBig,
|
|
2020
|
+
toChecksumAddress,
|
|
2021
|
+
toUtf8Bytes,
|
|
2022
|
+
weiToEther,
|
|
2023
|
+
zeroPad
|
|
2024
|
+
});
|