essential-eth 0.11.2 → 1.0.0
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/chunk-CLIQ4S3P.js +780 -0
- package/dist/chunk-GFWRB7PT.js +96 -0
- package/dist/conversions.d.ts +9 -0
- package/dist/conversions.js +1 -0
- package/dist/index-utils-e8a8adc8.d.ts +63 -0
- package/dist/index.cjs +419 -286
- package/dist/index.d.ts +60 -101
- package/dist/index.js +239 -1046
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +2 -0
- package/package.json +14 -7
- package/readme.md +750 -449
- package/dist/index.d.cts +0 -330
package/dist/index.js
CHANGED
|
@@ -1,354 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
// src/classes/utils/hex-to-decimal.ts
|
|
8
|
-
function hexToDecimal(hex) {
|
|
9
|
-
return BigInt(hex).toString();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// src/shared/tiny-big/helpers.ts
|
|
13
|
-
function stripTrailingZeroes(numberString) {
|
|
14
|
-
const isNegative = numberString.startsWith("-");
|
|
15
|
-
numberString = numberString.replace("-", "");
|
|
16
|
-
numberString = numberString.replace(
|
|
17
|
-
/\.0*$/g,
|
|
18
|
-
""
|
|
19
|
-
);
|
|
20
|
-
numberString = numberString.replace(/^0+/, "");
|
|
21
|
-
if (numberString.includes(".")) {
|
|
22
|
-
numberString = numberString.replace(/0+$/, "");
|
|
23
|
-
}
|
|
24
|
-
if (numberString.startsWith(".")) {
|
|
25
|
-
numberString = `0${numberString}`;
|
|
26
|
-
}
|
|
27
|
-
return `${isNegative ? "-" : ""}${numberString}`;
|
|
28
|
-
}
|
|
29
|
-
function scientificStrToDecimalStr(scientificString) {
|
|
30
|
-
if (!scientificString.match(
|
|
31
|
-
/e/i
|
|
32
|
-
/* lowercase and uppercase E */
|
|
33
|
-
)) {
|
|
34
|
-
return stripTrailingZeroes(scientificString);
|
|
35
|
-
}
|
|
36
|
-
let [base, power] = scientificString.split(
|
|
37
|
-
/e/i
|
|
38
|
-
);
|
|
39
|
-
const isNegative = Number(base) < 0;
|
|
40
|
-
base = base.replace("-", "");
|
|
41
|
-
base = stripTrailingZeroes(base);
|
|
42
|
-
const [wholeNumber, fraction = ""] = base.split(".");
|
|
43
|
-
if (Number(power) === 0) {
|
|
44
|
-
return `${isNegative ? "-" : ""}${stripTrailingZeroes(base)}`;
|
|
45
|
-
} else {
|
|
46
|
-
const includesDecimal = base.includes(".");
|
|
47
|
-
if (!includesDecimal) {
|
|
48
|
-
base = `${base}.`;
|
|
49
|
-
}
|
|
50
|
-
base = base.replace(".", "");
|
|
51
|
-
const baseLength = base.length;
|
|
52
|
-
let splitPaddedNumber;
|
|
53
|
-
if (Number(power) < 0) {
|
|
54
|
-
if (wholeNumber.length < Math.abs(Number(power))) {
|
|
55
|
-
base = base.padStart(
|
|
56
|
-
baseLength + Math.abs(Number(power)) - wholeNumber.length,
|
|
57
|
-
"0"
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
splitPaddedNumber = base.split("");
|
|
61
|
-
if (wholeNumber.length < Math.abs(Number(power))) {
|
|
62
|
-
splitPaddedNumber = [".", ...splitPaddedNumber];
|
|
63
|
-
} else {
|
|
64
|
-
splitPaddedNumber.splice(
|
|
65
|
-
splitPaddedNumber.length - Math.abs(Number(power)),
|
|
66
|
-
0,
|
|
67
|
-
"."
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
} else {
|
|
71
|
-
if (fraction.length < Math.abs(Number(power))) {
|
|
72
|
-
base = base.padEnd(
|
|
73
|
-
baseLength + Math.abs(Number(power)) - fraction.length,
|
|
74
|
-
"0"
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
splitPaddedNumber = base.split("");
|
|
78
|
-
if (fraction.length > Math.abs(Number(power))) {
|
|
79
|
-
splitPaddedNumber.splice(
|
|
80
|
-
splitPaddedNumber.length - Math.abs(Number(power)),
|
|
81
|
-
0,
|
|
82
|
-
"."
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
const toReturn = stripTrailingZeroes(splitPaddedNumber.join(""));
|
|
87
|
-
return `${isNegative ? "-" : ""}${toReturn}`;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// src/shared/tiny-big/tiny-big.ts
|
|
92
|
-
var TinyBig = class extends Big {
|
|
93
|
-
constructor(value) {
|
|
94
|
-
if (typeof value === "string" && value.startsWith("0x")) {
|
|
95
|
-
value = hexToDecimal(value);
|
|
96
|
-
}
|
|
97
|
-
super(value);
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Used anytime you're passing in "value" to ethers or web3
|
|
101
|
-
* For now, TypeScript will complain that `TinyBig` is not a `BigNumberish`. You can // @ts-ignore or call this
|
|
102
|
-
*
|
|
103
|
-
* @returns the TinyBig represented as a hex string
|
|
104
|
-
* @example
|
|
105
|
-
* ```javascript
|
|
106
|
-
* tinyBig(293).toHexString();
|
|
107
|
-
* // '0x125'
|
|
108
|
-
* ```
|
|
109
|
-
* @example
|
|
110
|
-
* ```javascript
|
|
111
|
-
* tinyBig(681365874).toHexString();
|
|
112
|
-
* // '0x289cd172'
|
|
113
|
-
*/
|
|
114
|
-
toHexString() {
|
|
115
|
-
return `0x${BigInt(this.toString()).toString(16)}`;
|
|
116
|
-
}
|
|
117
|
-
toNumber() {
|
|
118
|
-
return Number(scientificStrToDecimalStr(super.toString()));
|
|
119
|
-
}
|
|
120
|
-
toString() {
|
|
121
|
-
if (this.toNumber() === 0) {
|
|
122
|
-
return "0";
|
|
123
|
-
}
|
|
124
|
-
return scientificStrToDecimalStr(super.toString());
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Eithers pads or shortens a string to a specified length
|
|
128
|
-
*
|
|
129
|
-
* @param str the string to pad or chop
|
|
130
|
-
* @param padChar the character to pad the string with
|
|
131
|
-
* @param length the desired length of the given string
|
|
132
|
-
* @returns a string of the desired length, either padded with the specified padChar or with the beginning of the string chopped off
|
|
133
|
-
* @example
|
|
134
|
-
* ```javascript
|
|
135
|
-
* padAndChop('essential-eth', 'a', 8);
|
|
136
|
-
* // 'tial-eth'
|
|
137
|
-
* ```
|
|
138
|
-
* @example
|
|
139
|
-
* ```javascript
|
|
140
|
-
* padAndChop('essential-eth', 'A', 20);
|
|
141
|
-
* // 'AAAAAAAessential-eth'
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
padAndChop = (str, padChar, length) => {
|
|
145
|
-
return (Array(length).fill(padChar).join("") + str).slice(length * -1);
|
|
146
|
-
};
|
|
147
|
-
toTwos(bitCount) {
|
|
148
|
-
let binaryStr;
|
|
149
|
-
if (this.gte(0)) {
|
|
150
|
-
const twosComp = this.toNumber().toString(2);
|
|
151
|
-
binaryStr = this.padAndChop(twosComp, "0", bitCount || twosComp.length);
|
|
152
|
-
} else {
|
|
153
|
-
binaryStr = this.plus(Math.pow(2, bitCount)).toNumber().toString(2);
|
|
154
|
-
if (Number(binaryStr) < 0) {
|
|
155
|
-
throw new Error("Cannot calculate twos complement");
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
const binary = `0b${binaryStr}`;
|
|
159
|
-
const decimal = Number(binary);
|
|
160
|
-
return tinyBig(decimal);
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
function tinyBig(value) {
|
|
164
|
-
return new TinyBig(value);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// src/utils/to-checksum-address.ts
|
|
168
|
-
import { Keccak } from "sha3";
|
|
169
|
-
|
|
170
|
-
// src/shared/validate-type.ts
|
|
171
|
-
var validateType = (value, allowedTypes) => {
|
|
172
|
-
if (!allowedTypes.includes(typeof value)) {
|
|
173
|
-
throw new Error(
|
|
174
|
-
`${allowedTypes.join(" or ")} required. Received ${typeof value}`
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
// src/utils/to-checksum-address.ts
|
|
180
|
-
function toChecksumAddress(address) {
|
|
181
|
-
validateType(address, ["string"]);
|
|
182
|
-
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
|
|
183
|
-
throw new Error(`Invalid Ethereum address "${address}"`);
|
|
184
|
-
}
|
|
185
|
-
const _address = address.toLowerCase().replace(/^0x/i, "");
|
|
186
|
-
const keccak = new Keccak(256);
|
|
187
|
-
const addressHash = keccak.update(_address).digest("hex").replace(/^0x/i, "");
|
|
188
|
-
let checksumAddress = "0x";
|
|
189
|
-
for (let i = 0; i < _address.length; i++) {
|
|
190
|
-
if (parseInt(addressHash[i], 16) > 7) {
|
|
191
|
-
checksumAddress += _address[i].toUpperCase();
|
|
192
|
-
} else {
|
|
193
|
-
checksumAddress += _address[i];
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
if (address.match(/([A-F].*[a-f])|([a-f].*[A-F])/) && checksumAddress !== address) {
|
|
197
|
-
throw new Error(`Invalid Checksum address for "${address}"`);
|
|
198
|
-
}
|
|
199
|
-
return checksumAddress;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// src/classes/utils/encode-decode-transaction.ts
|
|
203
|
-
var hexFalse = "0".repeat(64);
|
|
204
|
-
var hexTrue = "0".repeat(63) + "1";
|
|
205
|
-
function hexToUtf8(hex) {
|
|
206
|
-
let str = "";
|
|
207
|
-
let i = 0;
|
|
208
|
-
const l = hex.length;
|
|
209
|
-
if (hex.substring(0, 2) === "0x") {
|
|
210
|
-
i = 2;
|
|
211
|
-
}
|
|
212
|
-
for (; i < l; i += 2) {
|
|
213
|
-
const code = parseInt(hex.substr(i, 2), 16);
|
|
214
|
-
if (code === 0) continue;
|
|
215
|
-
str += String.fromCharCode(code);
|
|
216
|
-
}
|
|
217
|
-
try {
|
|
218
|
-
return decodeURIComponent(escape(str));
|
|
219
|
-
} catch (e) {
|
|
220
|
-
return str;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
function expandType(type) {
|
|
224
|
-
if (type === "uint[]") {
|
|
225
|
-
return "uint256[]";
|
|
226
|
-
}
|
|
227
|
-
if (type === "int[]") {
|
|
228
|
-
return "int256[]";
|
|
229
|
-
}
|
|
230
|
-
return type;
|
|
231
|
-
}
|
|
232
|
-
function encodeData(jsonABIArgument, args) {
|
|
233
|
-
const hash = new Keccak2(256);
|
|
234
|
-
const functionString = `${jsonABIArgument.name}(${jsonABIArgument.inputs.map(
|
|
235
|
-
(input) => expandType(input.type)
|
|
236
|
-
)})`;
|
|
237
|
-
const functionHash = hash.update(functionString).digest("hex");
|
|
238
|
-
const jsonABIInputsLength = jsonABIArgument.inputs.length;
|
|
239
|
-
let shouldValidateInputLength = true;
|
|
240
|
-
if (jsonABIArgument.inputs.find((input) => input.type.includes("["))) {
|
|
241
|
-
shouldValidateInputLength = false;
|
|
242
|
-
}
|
|
243
|
-
if (shouldValidateInputLength && args.length !== jsonABIInputsLength) {
|
|
244
|
-
throw new Error(
|
|
245
|
-
`args inputs of "${args.length}" does not match expected length of "${jsonABIArgument.inputs.length}"`
|
|
246
|
-
);
|
|
247
|
-
}
|
|
248
|
-
const argsWithTypes = (jsonABIArgument.inputs || []).reduce(
|
|
249
|
-
(acc, input, i) => {
|
|
250
|
-
if (input.type.includes("[")) {
|
|
251
|
-
const basicType = /([^[]*)\[.*$/g.exec(input.type)?.[1];
|
|
252
|
-
args.forEach((arg) => {
|
|
253
|
-
acc = acc.concat([[arg, basicType]]);
|
|
254
|
-
});
|
|
255
|
-
return acc;
|
|
256
|
-
} else {
|
|
257
|
-
return acc.concat([[args[i], input.type]]);
|
|
258
|
-
}
|
|
259
|
-
},
|
|
260
|
-
[]
|
|
261
|
-
);
|
|
262
|
-
const encodedArgs = argsWithTypes.map(([arg, inputType]) => {
|
|
263
|
-
let rawArg = arg;
|
|
264
|
-
switch (inputType) {
|
|
265
|
-
case "bool":
|
|
266
|
-
return arg ? hexTrue : hexFalse;
|
|
267
|
-
case "address":
|
|
268
|
-
rawArg = arg.replace(/^0x/g, "").toLowerCase();
|
|
269
|
-
break;
|
|
270
|
-
default:
|
|
271
|
-
if (inputType.startsWith("bytes")) {
|
|
272
|
-
if (Array.isArray(arg)) {
|
|
273
|
-
throw new Error(
|
|
274
|
-
`essential-eth does not yet support "${inputType}[]" inputs. Make a PR today!"`
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
const argEncoded2 = BigInt(arg).toString(16);
|
|
278
|
-
const paddedEncodedArg2 = argEncoded2.padStart(64, "0");
|
|
279
|
-
return paddedEncodedArg2;
|
|
280
|
-
} else if (inputType === "uint256") {
|
|
281
|
-
const argEncoded2 = BigInt(arg).toString(16);
|
|
282
|
-
const paddedEncodedArg2 = argEncoded2.padStart(64, "0");
|
|
283
|
-
return paddedEncodedArg2;
|
|
284
|
-
} else if (inputType.startsWith("uint")) {
|
|
285
|
-
break;
|
|
286
|
-
} else {
|
|
287
|
-
throw new Error(
|
|
288
|
-
`essential-eth does not yet support "${inputType}" inputs. Make a PR today!"`
|
|
289
|
-
);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
const argEncoded = rawArg.toString(16);
|
|
293
|
-
const paddedEncodedArg = argEncoded.padStart(64, "0");
|
|
294
|
-
return paddedEncodedArg;
|
|
295
|
-
});
|
|
296
|
-
const functionEncoded = functionHash.slice(0, 8);
|
|
297
|
-
const data = `0x${functionEncoded}${encodedArgs.join("")}`;
|
|
298
|
-
return data;
|
|
299
|
-
}
|
|
300
|
-
function decodeRPCResponse(jsonABIArgument, nodeResponse) {
|
|
301
|
-
const rawOutputs = jsonABIArgument.outputs || [];
|
|
302
|
-
const slicedResponse = nodeResponse.slice(2);
|
|
303
|
-
if (rawOutputs.length === 1 && rawOutputs[0].type === "string") {
|
|
304
|
-
const [hexOffset, responseData] = [
|
|
305
|
-
slicedResponse.slice(0, 64),
|
|
306
|
-
slicedResponse.slice(64)
|
|
307
|
-
];
|
|
308
|
-
const decimalOffset = Number(hexToDecimal(`0x${hexOffset}`));
|
|
309
|
-
const hexLength = responseData.slice(0, decimalOffset * 2);
|
|
310
|
-
const decimalLength = Number(hexToDecimal(`0x${hexLength}`));
|
|
311
|
-
const hexToDecode = responseData.slice(
|
|
312
|
-
decimalOffset * 2,
|
|
313
|
-
decimalOffset * 2 + decimalLength * 2
|
|
314
|
-
);
|
|
315
|
-
return hexToUtf8(hexToDecode);
|
|
316
|
-
}
|
|
317
|
-
const encodedOutputs = slicedResponse.match(/.{1,64}/g) || [];
|
|
318
|
-
if (rawOutputs.length === 1 && rawOutputs[0].type === "address[]") {
|
|
319
|
-
const unformattedAddresses = encodedOutputs.slice(2);
|
|
320
|
-
return unformattedAddresses.map((unformattedAddress) => {
|
|
321
|
-
return toChecksumAddress(`0x${unformattedAddress.slice(24)}`);
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
if (rawOutputs?.length === 1 && rawOutputs[0].type === "uint256[]") {
|
|
325
|
-
const outputs2 = encodedOutputs.slice(2);
|
|
326
|
-
return outputs2.map((output) => {
|
|
327
|
-
return tinyBig(hexToDecimal(`0x${output}`));
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
const outputs = encodedOutputs.map((output, i) => {
|
|
331
|
-
const outputType = rawOutputs[i].type;
|
|
332
|
-
switch (outputType) {
|
|
333
|
-
case "bool":
|
|
334
|
-
return output === hexTrue;
|
|
335
|
-
case "address":
|
|
336
|
-
return toChecksumAddress(`0x${output.slice(24)}`);
|
|
337
|
-
case "uint256":
|
|
338
|
-
case "uint120":
|
|
339
|
-
return tinyBig(hexToDecimal(`0x${output}`));
|
|
340
|
-
case "bytes32":
|
|
341
|
-
return `0x${output}`;
|
|
342
|
-
case "uint8":
|
|
343
|
-
return Number(hexToDecimal(`0x${output}`));
|
|
344
|
-
default:
|
|
345
|
-
throw new Error(
|
|
346
|
-
`essential-eth does not yet support "${outputType}" outputs. Make a PR today!"`
|
|
347
|
-
);
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
return outputs.length === 1 ? outputs[0] : outputs;
|
|
351
|
-
}
|
|
1
|
+
import { keccak256, toUtf8Bytes, logger, encodeData, decodeRPCResponse, arrayify, hexlify, toChecksumAddress, hexToDecimal } from './chunk-CLIQ4S3P.js';
|
|
2
|
+
export { arrayify, computeAddress, computePublicKey, concat, hashMessage, hexConcat, hexDataLength, hexDataSlice, hexStripZeros, hexValue, hexZeroPad, hexlify, isAddress, isBytes, isBytesLike, isHexString, keccak256, pack, solidityKeccak256, splitSignature, stripZeros, toChecksumAddress, toUtf8Bytes, zeroPad } from './chunk-CLIQ4S3P.js';
|
|
3
|
+
import { formatFixed, toBigInt, parseFixed } from './chunk-GFWRB7PT.js';
|
|
4
|
+
export { etherToGwei, etherToWei, gweiToEther, weiToEther } from './chunk-GFWRB7PT.js';
|
|
5
|
+
import unfetch from 'isomorphic-unfetch';
|
|
352
6
|
|
|
353
7
|
// src/classes/Contract.ts
|
|
354
8
|
function estimateGas(txnData) {
|
|
@@ -423,7 +77,8 @@ function cleanTransaction(transaction) {
|
|
|
423
77
|
...transaction
|
|
424
78
|
};
|
|
425
79
|
Object.keys(transaction).forEach((key) => {
|
|
426
|
-
if (!transaction[key])
|
|
80
|
+
if (!transaction[key])
|
|
81
|
+
return;
|
|
427
82
|
switch (key) {
|
|
428
83
|
case "blockNumber":
|
|
429
84
|
case "chainId":
|
|
@@ -444,7 +99,7 @@ function cleanTransaction(transaction) {
|
|
|
444
99
|
case "maxFeePerGas":
|
|
445
100
|
case "maxPriorityFeePerGas":
|
|
446
101
|
case "nonce":
|
|
447
|
-
cleanedTransaction[key] =
|
|
102
|
+
cleanedTransaction[key] = BigInt(hexToDecimal(transaction[key]));
|
|
448
103
|
break;
|
|
449
104
|
}
|
|
450
105
|
});
|
|
@@ -455,7 +110,8 @@ function cleanTransaction(transaction) {
|
|
|
455
110
|
function cleanBlock(block, returnTransactionObjects) {
|
|
456
111
|
const cleanedBlock = { ...block };
|
|
457
112
|
Object.keys(block).forEach((key) => {
|
|
458
|
-
if (!block[key])
|
|
113
|
+
if (!block[key])
|
|
114
|
+
return;
|
|
459
115
|
switch (key) {
|
|
460
116
|
case "difficulty":
|
|
461
117
|
case "totalDifficulty":
|
|
@@ -464,7 +120,7 @@ function cleanBlock(block, returnTransactionObjects) {
|
|
|
464
120
|
case "size":
|
|
465
121
|
case "timestamp":
|
|
466
122
|
case "baseFeePerGas":
|
|
467
|
-
cleanedBlock[key] =
|
|
123
|
+
cleanedBlock[key] = BigInt(hexToDecimal(block[key]));
|
|
468
124
|
break;
|
|
469
125
|
case "number":
|
|
470
126
|
cleanedBlock[key] = Number(hexToDecimal(block[key]));
|
|
@@ -517,7 +173,8 @@ function cleanTransactionReceipt(transactionReceipt) {
|
|
|
517
173
|
...cleanedTransaction
|
|
518
174
|
};
|
|
519
175
|
Object.keys(transactionReceipt).forEach((key) => {
|
|
520
|
-
if (!transactionReceipt[key])
|
|
176
|
+
if (!transactionReceipt[key])
|
|
177
|
+
return;
|
|
521
178
|
switch (key) {
|
|
522
179
|
case "status":
|
|
523
180
|
cleanedTransactionReceipt[key] = Number(
|
|
@@ -534,7 +191,7 @@ function cleanTransactionReceipt(transactionReceipt) {
|
|
|
534
191
|
case "cumulativeGasUsed":
|
|
535
192
|
case "effectiveGasPrice":
|
|
536
193
|
case "gasUsed":
|
|
537
|
-
cleanedTransactionReceipt[key] =
|
|
194
|
+
cleanedTransactionReceipt[key] = BigInt(
|
|
538
195
|
hexToDecimal(transactionReceipt[key])
|
|
539
196
|
);
|
|
540
197
|
break;
|
|
@@ -547,9 +204,6 @@ function cleanTransactionReceipt(transactionReceipt) {
|
|
|
547
204
|
cleanedTransactionReceipt.byzantium = cleanedTransactionReceipt.blockNumber >= 437e4;
|
|
548
205
|
return cleanedTransactionReceipt;
|
|
549
206
|
}
|
|
550
|
-
|
|
551
|
-
// src/classes/utils/fetchers.ts
|
|
552
|
-
import unfetch from "isomorphic-unfetch";
|
|
553
207
|
function buildFetchInit(body) {
|
|
554
208
|
return {
|
|
555
209
|
method: "POST",
|
|
@@ -587,288 +241,6 @@ function buildRPCPostBody(method, params) {
|
|
|
587
241
|
};
|
|
588
242
|
}
|
|
589
243
|
|
|
590
|
-
// src/classes/utils/prepare-transaction.ts
|
|
591
|
-
import Big2 from "big.js";
|
|
592
|
-
|
|
593
|
-
// src/logger/package-version.ts
|
|
594
|
-
var version = "0.11.2";
|
|
595
|
-
|
|
596
|
-
// src/logger/logger.ts
|
|
597
|
-
var Logger = class {
|
|
598
|
-
packageVersion;
|
|
599
|
-
constructor() {
|
|
600
|
-
this.packageVersion = version;
|
|
601
|
-
}
|
|
602
|
-
throwError(message, args) {
|
|
603
|
-
const argsLength = Object.keys(args).length;
|
|
604
|
-
throw new Error(
|
|
605
|
-
`${message} (${Object.entries(args).map(
|
|
606
|
-
([key, value], index) => `${key}=${value}${index < argsLength - 1 && ", "}`
|
|
607
|
-
)}, version=essential-eth@${this.packageVersion})`
|
|
608
|
-
);
|
|
609
|
-
}
|
|
610
|
-
throwArgumentError(message, arg, value) {
|
|
611
|
-
throw new Error(
|
|
612
|
-
`${message} (argument="${arg}" value=${value}, version=essential-eth@${this.packageVersion})`
|
|
613
|
-
);
|
|
614
|
-
}
|
|
615
|
-
checkSafeUint53(value, message = "value not safe") {
|
|
616
|
-
if (typeof value !== "number") {
|
|
617
|
-
return;
|
|
618
|
-
}
|
|
619
|
-
if (value < 0 || value >= 9007199254740991) {
|
|
620
|
-
this.throwError(message, {
|
|
621
|
-
operation: "checkSafeInteger",
|
|
622
|
-
fault: "out-of-safe-range",
|
|
623
|
-
value
|
|
624
|
-
});
|
|
625
|
-
}
|
|
626
|
-
if (value % 1) {
|
|
627
|
-
this.throwError(message, {
|
|
628
|
-
operation: "checkSafeInteger",
|
|
629
|
-
fault: "non-integer",
|
|
630
|
-
value
|
|
631
|
-
});
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
};
|
|
635
|
-
var logger = new Logger();
|
|
636
|
-
|
|
637
|
-
// src/utils/bytes.ts
|
|
638
|
-
function isHexable(value) {
|
|
639
|
-
return !!value.toHexString;
|
|
640
|
-
}
|
|
641
|
-
function isBytesLike(value) {
|
|
642
|
-
return isHexString(value) && !(value.length % 2) || isBytes(value);
|
|
643
|
-
}
|
|
644
|
-
function isInteger(value) {
|
|
645
|
-
return typeof value === "number" && value == value && value % 1 === 0;
|
|
646
|
-
}
|
|
647
|
-
function isBytes(value) {
|
|
648
|
-
if (value == null) {
|
|
649
|
-
return false;
|
|
650
|
-
}
|
|
651
|
-
if (value.constructor === Uint8Array) {
|
|
652
|
-
return true;
|
|
653
|
-
}
|
|
654
|
-
if (typeof value === "string") {
|
|
655
|
-
return false;
|
|
656
|
-
}
|
|
657
|
-
if (!isInteger(value.length) || value.length < 0) {
|
|
658
|
-
return false;
|
|
659
|
-
}
|
|
660
|
-
for (let i = 0; i < value.length; i++) {
|
|
661
|
-
const v = value[i];
|
|
662
|
-
if (!isInteger(v) || v < 0 || v >= 256) {
|
|
663
|
-
return false;
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
return true;
|
|
667
|
-
}
|
|
668
|
-
function arrayify(value, options) {
|
|
669
|
-
if (!options) {
|
|
670
|
-
options = {};
|
|
671
|
-
}
|
|
672
|
-
if (typeof value === "number") {
|
|
673
|
-
logger.checkSafeUint53(value, "invalid arrayify value");
|
|
674
|
-
const result = [];
|
|
675
|
-
while (value) {
|
|
676
|
-
result.unshift(value & 255);
|
|
677
|
-
value = parseInt(String(value / 256));
|
|
678
|
-
}
|
|
679
|
-
if (result.length === 0) {
|
|
680
|
-
result.push(0);
|
|
681
|
-
}
|
|
682
|
-
return new Uint8Array(result);
|
|
683
|
-
}
|
|
684
|
-
if (options.allowMissingPrefix && typeof value === "string" && value.substring(0, 2) !== "0x") {
|
|
685
|
-
value = "0x" + value;
|
|
686
|
-
}
|
|
687
|
-
if (isHexable(value)) {
|
|
688
|
-
value = value.toHexString();
|
|
689
|
-
}
|
|
690
|
-
if (isHexString(value)) {
|
|
691
|
-
let hex = value.substring(2);
|
|
692
|
-
if (hex.length % 2) {
|
|
693
|
-
if (options.hexPad === "left") {
|
|
694
|
-
hex = "0" + hex;
|
|
695
|
-
} else if (options.hexPad === "right") {
|
|
696
|
-
hex += "0";
|
|
697
|
-
} else {
|
|
698
|
-
logger.throwArgumentError("hex data is odd-length", "value", value);
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
const result = [];
|
|
702
|
-
for (let i = 0; i < hex.length; i += 2) {
|
|
703
|
-
result.push(parseInt(hex.substring(i, i + 2), 16));
|
|
704
|
-
}
|
|
705
|
-
return new Uint8Array(result);
|
|
706
|
-
}
|
|
707
|
-
if (isBytes(value)) {
|
|
708
|
-
return new Uint8Array(value);
|
|
709
|
-
}
|
|
710
|
-
return logger.throwArgumentError("invalid arrayify value", "value", value);
|
|
711
|
-
}
|
|
712
|
-
function concat(arrayOfBytesLike) {
|
|
713
|
-
const objects = arrayOfBytesLike.map((item) => arrayify(item));
|
|
714
|
-
const length = objects.reduce((accum, item) => accum + item.length, 0);
|
|
715
|
-
const result = new Uint8Array(length);
|
|
716
|
-
objects.reduce((offset, object) => {
|
|
717
|
-
result.set(object, offset);
|
|
718
|
-
return offset + object.length;
|
|
719
|
-
}, 0);
|
|
720
|
-
return result;
|
|
721
|
-
}
|
|
722
|
-
function stripZeros(value) {
|
|
723
|
-
let result = arrayify(value);
|
|
724
|
-
if (result.length === 0) {
|
|
725
|
-
return result;
|
|
726
|
-
}
|
|
727
|
-
let start = 0;
|
|
728
|
-
while (start < result.length && result[start] === 0) {
|
|
729
|
-
start++;
|
|
730
|
-
}
|
|
731
|
-
if (start) {
|
|
732
|
-
result = result.slice(start);
|
|
733
|
-
}
|
|
734
|
-
return result;
|
|
735
|
-
}
|
|
736
|
-
function zeroPad(value, length) {
|
|
737
|
-
value = arrayify(value);
|
|
738
|
-
if (value.length > length) {
|
|
739
|
-
logger.throwArgumentError("value out of range", "value", value);
|
|
740
|
-
}
|
|
741
|
-
const result = new Uint8Array(length);
|
|
742
|
-
result.set(value, length - value.length);
|
|
743
|
-
return result;
|
|
744
|
-
}
|
|
745
|
-
function isHexString(value, length) {
|
|
746
|
-
if (typeof value !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
|
|
747
|
-
return false;
|
|
748
|
-
}
|
|
749
|
-
if (length && value.length !== 2 + 2 * length) {
|
|
750
|
-
return false;
|
|
751
|
-
}
|
|
752
|
-
return true;
|
|
753
|
-
}
|
|
754
|
-
var HexCharacters = "0123456789abcdef";
|
|
755
|
-
function hexlify(value, options) {
|
|
756
|
-
if (!options) {
|
|
757
|
-
options = {};
|
|
758
|
-
}
|
|
759
|
-
if (typeof value === "number") {
|
|
760
|
-
logger.checkSafeUint53(value, "invalid hexlify value");
|
|
761
|
-
let hex = "";
|
|
762
|
-
while (value) {
|
|
763
|
-
hex = HexCharacters[value & 15] + hex;
|
|
764
|
-
value = Math.floor(value / 16);
|
|
765
|
-
}
|
|
766
|
-
if (hex.length) {
|
|
767
|
-
if (hex.length % 2) {
|
|
768
|
-
hex = "0" + hex;
|
|
769
|
-
}
|
|
770
|
-
return "0x" + hex;
|
|
771
|
-
}
|
|
772
|
-
return "0x00";
|
|
773
|
-
}
|
|
774
|
-
if (typeof value === "bigint") {
|
|
775
|
-
value = value.toString(16);
|
|
776
|
-
if (value.length % 2) {
|
|
777
|
-
return "0x0" + value;
|
|
778
|
-
}
|
|
779
|
-
return "0x" + value;
|
|
780
|
-
}
|
|
781
|
-
if (options.allowMissingPrefix && typeof value === "string" && value.substring(0, 2) !== "0x") {
|
|
782
|
-
value = "0x" + value;
|
|
783
|
-
}
|
|
784
|
-
if (isHexable(value)) {
|
|
785
|
-
return value.toHexString();
|
|
786
|
-
}
|
|
787
|
-
if (isHexString(value)) {
|
|
788
|
-
if (value.length % 2) {
|
|
789
|
-
if (options.hexPad === "left") {
|
|
790
|
-
value = "0x0" + value.substring(2);
|
|
791
|
-
} else if (options.hexPad === "right") {
|
|
792
|
-
value += "0";
|
|
793
|
-
} else {
|
|
794
|
-
logger.throwArgumentError("hex data is odd-length", "value", value);
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
return value.toLowerCase();
|
|
798
|
-
}
|
|
799
|
-
if (isBytes(value)) {
|
|
800
|
-
let result = "0x";
|
|
801
|
-
for (let i = 0; i < value.length; i++) {
|
|
802
|
-
const v = value[i];
|
|
803
|
-
result += HexCharacters[(v & 240) >> 4] + HexCharacters[v & 15];
|
|
804
|
-
}
|
|
805
|
-
return result;
|
|
806
|
-
}
|
|
807
|
-
return logger.throwArgumentError("invalid hexlify value", "value", value);
|
|
808
|
-
}
|
|
809
|
-
function hexDataLength(data) {
|
|
810
|
-
if (typeof data !== "string") {
|
|
811
|
-
data = hexlify(data);
|
|
812
|
-
} else if (!isHexString(data) || data.length % 2) {
|
|
813
|
-
return null;
|
|
814
|
-
}
|
|
815
|
-
return (data.length - 2) / 2;
|
|
816
|
-
}
|
|
817
|
-
function hexDataSlice(data, offset, endOffset) {
|
|
818
|
-
if (typeof data !== "string") {
|
|
819
|
-
data = hexlify(data);
|
|
820
|
-
} else if (!isHexString(data) || data.length % 2) {
|
|
821
|
-
logger.throwArgumentError("invalid hexData", "value", data);
|
|
822
|
-
}
|
|
823
|
-
offset = 2 + 2 * offset;
|
|
824
|
-
if (endOffset != null) {
|
|
825
|
-
return "0x" + data.substring(offset, 2 + 2 * endOffset);
|
|
826
|
-
}
|
|
827
|
-
return "0x" + data.substring(offset);
|
|
828
|
-
}
|
|
829
|
-
function hexConcat(items) {
|
|
830
|
-
let result = "0x";
|
|
831
|
-
items.forEach((item) => {
|
|
832
|
-
result += hexlify(item).substring(2);
|
|
833
|
-
});
|
|
834
|
-
return result;
|
|
835
|
-
}
|
|
836
|
-
function hexValue(value) {
|
|
837
|
-
const trimmed = hexStripZeros(hexlify(value, { hexPad: "left" }));
|
|
838
|
-
if (trimmed === "0x") {
|
|
839
|
-
return "0x0";
|
|
840
|
-
}
|
|
841
|
-
return trimmed;
|
|
842
|
-
}
|
|
843
|
-
function hexStripZeros(value) {
|
|
844
|
-
if (typeof value !== "string") {
|
|
845
|
-
value = hexlify(value);
|
|
846
|
-
}
|
|
847
|
-
if (!isHexString(value)) {
|
|
848
|
-
logger.throwArgumentError("invalid hex string", "value", value);
|
|
849
|
-
}
|
|
850
|
-
value = value.substring(2);
|
|
851
|
-
let offset = 0;
|
|
852
|
-
while (offset < value.length && value[offset] === "0") {
|
|
853
|
-
offset++;
|
|
854
|
-
}
|
|
855
|
-
return "0x" + value.substring(offset);
|
|
856
|
-
}
|
|
857
|
-
function hexZeroPad(value, length) {
|
|
858
|
-
if (typeof value !== "string") {
|
|
859
|
-
value = hexlify(value);
|
|
860
|
-
} else if (!isHexString(value)) {
|
|
861
|
-
logger.throwArgumentError("invalid hex string", "value", value);
|
|
862
|
-
}
|
|
863
|
-
if (value.length > 2 * length + 2) {
|
|
864
|
-
logger.throwError("value out of range", { value, length });
|
|
865
|
-
}
|
|
866
|
-
while (value.length < 2 * length + 2) {
|
|
867
|
-
value = "0x0" + value.substring(2);
|
|
868
|
-
}
|
|
869
|
-
return value;
|
|
870
|
-
}
|
|
871
|
-
|
|
872
244
|
// src/classes/utils/prepare-transaction.ts
|
|
873
245
|
function prepareTransaction(transaction) {
|
|
874
246
|
const preparedTransaction = {
|
|
@@ -884,15 +256,13 @@ function prepareTransaction(transaction) {
|
|
|
884
256
|
case "maxPriorityFeePerGas":
|
|
885
257
|
case "value": {
|
|
886
258
|
const value = transaction[key];
|
|
887
|
-
if (value
|
|
888
|
-
preparedTransaction[key] = value.
|
|
889
|
-
} else if (value
|
|
890
|
-
preparedTransaction[key] =
|
|
891
|
-
|
|
892
|
-
)
|
|
893
|
-
}
|
|
894
|
-
preparedTransaction[key] = "0x" + transaction[key].toString(16);
|
|
895
|
-
else preparedTransaction[key] = transaction[key].toString();
|
|
259
|
+
if (typeof value === "bigint") {
|
|
260
|
+
preparedTransaction[key] = "0x" + value.toString(16);
|
|
261
|
+
} else if (typeof value === "number") {
|
|
262
|
+
preparedTransaction[key] = "0x" + value.toString(16);
|
|
263
|
+
} else {
|
|
264
|
+
preparedTransaction[key] = value.toString();
|
|
265
|
+
}
|
|
896
266
|
break;
|
|
897
267
|
}
|
|
898
268
|
case "data":
|
|
@@ -904,6 +274,20 @@ function prepareTransaction(transaction) {
|
|
|
904
274
|
return preparedTransaction;
|
|
905
275
|
}
|
|
906
276
|
|
|
277
|
+
// src/utils/namehash.ts
|
|
278
|
+
function namehash(name) {
|
|
279
|
+
let node = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
280
|
+
if (name === "") {
|
|
281
|
+
return node;
|
|
282
|
+
}
|
|
283
|
+
const labels = name.split(".");
|
|
284
|
+
for (let i = labels.length - 1; i >= 0; i--) {
|
|
285
|
+
const labelHash = keccak256(toUtf8Bytes(labels[i]));
|
|
286
|
+
node = keccak256("0x" + node.slice(2) + labelHash.slice(2));
|
|
287
|
+
}
|
|
288
|
+
return node;
|
|
289
|
+
}
|
|
290
|
+
|
|
907
291
|
// src/providers/utils/chains-info.ts
|
|
908
292
|
var chains_info_default = {
|
|
909
293
|
"1": [
|
|
@@ -975,7 +359,7 @@ var chains_info_default = {
|
|
|
975
359
|
|
|
976
360
|
// src/providers/BaseProvider.ts
|
|
977
361
|
function prepBlockTag(blockTag) {
|
|
978
|
-
return typeof blockTag === "number" ?
|
|
362
|
+
return typeof blockTag === "number" ? "0x" + blockTag.toString(16) : blockTag;
|
|
979
363
|
}
|
|
980
364
|
var BaseProvider = class {
|
|
981
365
|
/**
|
|
@@ -1001,7 +385,6 @@ var BaseProvider = class {
|
|
|
1001
385
|
*
|
|
1002
386
|
* * [Identical](/docs/api#isd) to [`ethers.provider.getNetwork`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getNetwork) in ethers.js
|
|
1003
387
|
* * [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`
|
|
1004
|
-
*
|
|
1005
388
|
* @returns information about the network this provider is currently connected to
|
|
1006
389
|
* @example
|
|
1007
390
|
* ```javascript
|
|
@@ -1032,7 +415,6 @@ var BaseProvider = class {
|
|
|
1032
415
|
*
|
|
1033
416
|
* * [Identical](/docs/api#isd) to [`ethers.provider.getBlockNumber`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getBlockNumber) in ethers.js
|
|
1034
417
|
* * [Identical](/docs/api#isd) to [`web3.eth.getBlockNumber`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getblocknumber) in web3.js
|
|
1035
|
-
*
|
|
1036
418
|
* @returns the number of the most recently mined block
|
|
1037
419
|
* @example
|
|
1038
420
|
* ```javascript
|
|
@@ -1051,7 +433,6 @@ var BaseProvider = class {
|
|
|
1051
433
|
*
|
|
1052
434
|
* * [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
|
|
1053
435
|
* * [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
|
|
1054
|
-
*
|
|
1055
436
|
* @param transactionHash the hash of the transaction to get information about
|
|
1056
437
|
* @returns information about the specified transaction
|
|
1057
438
|
* @example
|
|
@@ -1063,20 +444,20 @@ var BaseProvider = class {
|
|
|
1063
444
|
* // blockNumber: 14578286,
|
|
1064
445
|
* // chainId: 1,
|
|
1065
446
|
* // from: "0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4",
|
|
1066
|
-
* // gas:
|
|
1067
|
-
* // gasPrice:
|
|
447
|
+
* // gas: 112163n,
|
|
448
|
+
* // gasPrice: 48592426858n,
|
|
1068
449
|
* // hash: "0x9014ae6ef92464338355a79e5150e542ff9a83e2323318b21f40d6a3e65b4789",
|
|
1069
450
|
* // input: "0x83259f17000000000000000000000000000000000000000000...",
|
|
1070
|
-
* // maxFeePerGas:
|
|
1071
|
-
* // maxPriorityFeePerGas:
|
|
1072
|
-
* // nonce:
|
|
451
|
+
* // maxFeePerGas: 67681261618n,
|
|
452
|
+
* // maxPriorityFeePerGas: 1500000000n,
|
|
453
|
+
* // nonce: 129n,
|
|
1073
454
|
* // r: "0x59a7c15b12c18cd68d6c440963d959bff3e73831ffc938e75ecad07f7ee43fbc",
|
|
1074
455
|
* // s: "0x1ebaf05f0d9273b16c2a7748b150a79d22533a8cd74552611cbe620fee3dcf1c",
|
|
1075
456
|
* // to: "0x39B72d136ba3e4ceF35F48CD09587ffaB754DD8B",
|
|
1076
457
|
* // transactionIndex: 29,
|
|
1077
458
|
* // type: 2,
|
|
1078
459
|
* // v: 0,
|
|
1079
|
-
* // value:
|
|
460
|
+
* // value: 0n,
|
|
1080
461
|
* // confirmations: 298140,
|
|
1081
462
|
* // }
|
|
1082
463
|
* ```
|
|
@@ -1097,7 +478,6 @@ var BaseProvider = class {
|
|
|
1097
478
|
*
|
|
1098
479
|
* * [Identical](/docs/api#isd) to [`ethers.provider.getTransactionReceipt`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getTransactionReceipt) in ethers.js
|
|
1099
480
|
* * [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
|
|
1100
|
-
*
|
|
1101
481
|
* @param transactionHash the hash of the transaction to get information about
|
|
1102
482
|
* @returns information about the specified transaction that has already been mined
|
|
1103
483
|
* @example
|
|
@@ -1107,10 +487,10 @@ var BaseProvider = class {
|
|
|
1107
487
|
* // blockHash: "0x876810a013dbcd140f6fd6048c1dc33abbb901f1f96b394c2fa63aef3cb40b5d",
|
|
1108
488
|
* // blockNumber: 14578286,
|
|
1109
489
|
* // contractAddress: null,
|
|
1110
|
-
* // cumulativeGasUsed:
|
|
1111
|
-
* // effectiveGasPrice:
|
|
490
|
+
* // cumulativeGasUsed: 3067973n,
|
|
491
|
+
* // effectiveGasPrice: 48592426858n,
|
|
1112
492
|
* // from: "0xdfD9dE5f6FA60BD70636c0900752E93a6144AEd4",
|
|
1113
|
-
* // gasUsed:
|
|
493
|
+
* // gasUsed: 112163n,
|
|
1114
494
|
* // logs: [
|
|
1115
495
|
* // {
|
|
1116
496
|
* // address: "0x0eDF9bc41Bbc1354c70e2107F80C42caE7FBBcA8",
|
|
@@ -1168,7 +548,6 @@ var BaseProvider = class {
|
|
|
1168
548
|
*
|
|
1169
549
|
* * [Identical](/docs/api#isd) to [`ethers.provider.getTransactionCount`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getTransactionCount) in ethers.js
|
|
1170
550
|
* * [Identical](/docs/api#isd) to [`web3.eth.getTransactionCount`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#gettransactioncount) in web3.js
|
|
1171
|
-
*
|
|
1172
551
|
* @param address the address to count number of sent transactions
|
|
1173
552
|
* @param blockTag the block to count transactions up to, inclusive
|
|
1174
553
|
* @returns the number of transactions sent by the specified address
|
|
@@ -1200,7 +579,6 @@ var BaseProvider = class {
|
|
|
1200
579
|
*
|
|
1201
580
|
* * [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`]
|
|
1202
581
|
* * [Identical](/docs/api#isd) to [`web3.eth.getBlock`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getpastlogs) in web3.js
|
|
1203
|
-
*
|
|
1204
582
|
* @param timeFrame The number, hash, or text-based description ('latest', 'earliest', or 'pending') of the block to collect information on.
|
|
1205
583
|
* @param returnTransactionObjects Whether to also return data about the transactions on the block.
|
|
1206
584
|
* @returns A BlockResponse object with information about the specified block
|
|
@@ -1208,11 +586,11 @@ var BaseProvider = class {
|
|
|
1208
586
|
* ```javascript
|
|
1209
587
|
* await provider.getBlock(14879862);
|
|
1210
588
|
* // {
|
|
1211
|
-
* // baseFeePerGas:
|
|
1212
|
-
* // difficulty:
|
|
589
|
+
* // baseFeePerGas: 39095728776n,
|
|
590
|
+
* // difficulty: 14321294455359973n,
|
|
1213
591
|
* // extraData: "0x486976656f6e2073672d6865617679",
|
|
1214
|
-
* // gasLimit:
|
|
1215
|
-
* // gasUsed:
|
|
592
|
+
* // gasLimit: 29970620n,
|
|
593
|
+
* // gasUsed: 20951384n,
|
|
1216
594
|
* // hash: "0x563b458ec3c4f87393b53f70bdddc0058497109b784d8cacd9247ddf267049ab",
|
|
1217
595
|
* // logsBloom:
|
|
1218
596
|
* // "0x9f38794fe80b521794df6efad8b0d2e9582f9ec3959a3f9384bda0fa371cfa5fac5af9d515c6bdf1ec325f5b5f7ebdd6a3a9fae17b38a86d4dc4b0971afc68d8086640550f4c156e6f923f4a1bb94fb0bed6cdcc474c5c64bfeff7a4a906f72b9a7b94004ee58efc53d63ac66961acd3a431b2d896cc9fd75f6072960bced45f770587caf130f57504decfcb63c6ca8fbc5bdbd749edd5a99a7375d2b81872289adb775fb3c928259f4be39c6d3f4d5b6217822979bb88c1f1fb62429b1b6d41cf4e3f77f9e1db3f5723108f1e5b1255dd734ad8cdb11e7ea22487c788e67c83777b6f395e504ca59c64f52245ee6de3804cf809e5caa4f0ea6a9aa9eb6ed801",
|
|
@@ -1223,10 +601,10 @@ var BaseProvider = class {
|
|
|
1223
601
|
* // parentHash: "0x95986ae14a71face8d9a6a379edd875b2e8bc73e4de0d9d460e7752bddb0f579",
|
|
1224
602
|
* // receiptsRoot: "0x8e6ba2fd9bee602b653dae6e3132f16538c2c5df24f1df8c000392053f73defa",
|
|
1225
603
|
* // sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
|
|
1226
|
-
* // size:
|
|
604
|
+
* // size: 134483n,
|
|
1227
605
|
* // stateRoot: "0xbf2bb67bd1c741f3d00904b8451d7c2cf4e3a2726f5a5884792ede2074747b85",
|
|
1228
|
-
* // timestamp:
|
|
1229
|
-
* // totalDifficulty:
|
|
606
|
+
* // timestamp: 1654016186n,
|
|
607
|
+
* // totalDifficulty: 50478104614257705213748n,
|
|
1230
608
|
* // transactions: [
|
|
1231
609
|
* // "0xb3326a9149809603a2c28545e50e4f7d16e194bf5ee9764e0544603854c4a8d2",
|
|
1232
610
|
* // "0x8b42095f8d335404a4896b2817b8e5e3d86a5a87cb434a8eec295d5280a7f48e",
|
|
@@ -1257,8 +635,7 @@ var BaseProvider = class {
|
|
|
1257
635
|
* Gives an estimate of the current gas price in wei.
|
|
1258
636
|
*
|
|
1259
637
|
* * [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)
|
|
1260
|
-
* * [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 (
|
|
1261
|
-
*
|
|
638
|
+
* * [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 (bigint) instead of a string
|
|
1262
639
|
* @returns an estimate of the current gas price in wei
|
|
1263
640
|
* @example
|
|
1264
641
|
* ```javascript
|
|
@@ -1270,14 +647,13 @@ var BaseProvider = class {
|
|
|
1270
647
|
const hexGasPrice = await this.post(
|
|
1271
648
|
buildRPCPostBody("eth_gasPrice", [])
|
|
1272
649
|
);
|
|
1273
|
-
return
|
|
650
|
+
return BigInt(hexToDecimal(hexGasPrice));
|
|
1274
651
|
}
|
|
1275
652
|
/**
|
|
1276
653
|
* Returns the balance of the account in wei.
|
|
1277
654
|
*
|
|
1278
655
|
* * [Identical](/docs/api#isd) to [`ethers.provider.getBalance`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getBalance) in ethers.js
|
|
1279
|
-
* * [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 (
|
|
1280
|
-
*
|
|
656
|
+
* * [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 (bigint) instead of a string
|
|
1281
657
|
* @param address the address to check the balance of
|
|
1282
658
|
* @param blockTag the block to check the specified address' balance on
|
|
1283
659
|
* @returns the balance of the network's native token for the specified address on the specified block
|
|
@@ -1292,14 +668,13 @@ var BaseProvider = class {
|
|
|
1292
668
|
const hexBalance = await this.post(
|
|
1293
669
|
buildRPCPostBody("eth_getBalance", [address, blockTag])
|
|
1294
670
|
);
|
|
1295
|
-
return
|
|
671
|
+
return BigInt(hexToDecimal(hexBalance));
|
|
1296
672
|
}
|
|
1297
673
|
/**
|
|
1298
674
|
* Gets the code of a contract on a specified block.
|
|
1299
675
|
*
|
|
1300
676
|
* * [Identical](/docs/api#isd) to [`ethers.provider.getCode`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getCode) in ethers.js
|
|
1301
677
|
* * [Identical](/docs/api#isd) to [`web3.eth.getCode`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getcode) in web3.js
|
|
1302
|
-
*
|
|
1303
678
|
* @param address the contract address to get the contract code from
|
|
1304
679
|
* @param blockTag the block height to search for the contract code from. Contract code can change, so this allows for checking a specific block
|
|
1305
680
|
* @returns the contract creation code for the specified address at the specified block height
|
|
@@ -1322,7 +697,6 @@ var BaseProvider = class {
|
|
|
1322
697
|
*
|
|
1323
698
|
* * [Identical](/docs/api#isd) to [`ethers.provider.estimateGas`](https://docs.ethers.io/v5/api/providers/provider/#Provider-estimateGas) in ethers.js
|
|
1324
699
|
* * [Identical](/docs/api#isd) to [`web3.eth.estimateGas`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#estimateGas) in web3.js
|
|
1325
|
-
*
|
|
1326
700
|
* @param transaction the transaction to check the estimated gas cost for
|
|
1327
701
|
* @returns the estimated amount of gas charged for submitting the specified transaction to the blockchain
|
|
1328
702
|
* @example
|
|
@@ -1333,7 +707,7 @@ var BaseProvider = class {
|
|
|
1333
707
|
* data: "0xd0e30db0",
|
|
1334
708
|
* value: etherToWei('1.0').toHexString(),
|
|
1335
709
|
* });
|
|
1336
|
-
* //
|
|
710
|
+
* // 27938n
|
|
1337
711
|
* ```
|
|
1338
712
|
*/
|
|
1339
713
|
async estimateGas(transaction) {
|
|
@@ -1341,7 +715,7 @@ var BaseProvider = class {
|
|
|
1341
715
|
const gasUsed = await this.post(
|
|
1342
716
|
buildRPCPostBody("eth_estimateGas", [rpcTransaction])
|
|
1343
717
|
);
|
|
1344
|
-
return
|
|
718
|
+
return BigInt(hexToDecimal(gasUsed));
|
|
1345
719
|
}
|
|
1346
720
|
/**
|
|
1347
721
|
* Returns the current recommended FeeData to use in a transaction.
|
|
@@ -1349,16 +723,15 @@ var BaseProvider = class {
|
|
|
1349
723
|
* 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.
|
|
1350
724
|
*
|
|
1351
725
|
* * [Identical](/docs/api#isd) to [`ethers.provider.getFeeData`](https://docs.ethers.org/v5/api/providers/provider/#Provider-getFeeData) in ethers.js
|
|
1352
|
-
*
|
|
1353
726
|
* @returns an object with gas estimates for the network currently
|
|
1354
727
|
* @example
|
|
1355
728
|
* ```javascript
|
|
1356
729
|
* await provider.getFeeData();
|
|
1357
730
|
* // {
|
|
1358
|
-
* // gasPrice:
|
|
1359
|
-
* // lastBaseFeePerGas:
|
|
1360
|
-
* // maxFeePerGas:
|
|
1361
|
-
* // maxPriorityFeePerGas:
|
|
731
|
+
* // gasPrice: 14184772639n,
|
|
732
|
+
* // lastBaseFeePerGas: 14038523098n,
|
|
733
|
+
* // maxFeePerGas: 29577046196n,
|
|
734
|
+
* // maxPriorityFeePerGas: 1500000000n
|
|
1362
735
|
* // }
|
|
1363
736
|
* ```
|
|
1364
737
|
*/
|
|
@@ -1370,10 +743,8 @@ var BaseProvider = class {
|
|
|
1370
743
|
let lastBaseFeePerGas = null, maxFeePerGas = null, maxPriorityFeePerGas = null;
|
|
1371
744
|
if (block && block.baseFeePerGas) {
|
|
1372
745
|
lastBaseFeePerGas = block.baseFeePerGas;
|
|
1373
|
-
maxPriorityFeePerGas =
|
|
1374
|
-
maxFeePerGas =
|
|
1375
|
-
block.baseFeePerGas.mul(2).add(maxPriorityFeePerGas)
|
|
1376
|
-
);
|
|
746
|
+
maxPriorityFeePerGas = BigInt("1500000000");
|
|
747
|
+
maxFeePerGas = block.baseFeePerGas * 2n + maxPriorityFeePerGas;
|
|
1377
748
|
}
|
|
1378
749
|
return { lastBaseFeePerGas, maxFeePerGas, maxPriorityFeePerGas, gasPrice };
|
|
1379
750
|
}
|
|
@@ -1383,7 +754,6 @@ var BaseProvider = class {
|
|
|
1383
754
|
*
|
|
1384
755
|
* * [Identical](/docs/api#isd) to [`ethers.provider.getLogs`](https://docs.ethers.io/v5/api/providers/provider/#Provider-getLogs) in ethers.js
|
|
1385
756
|
* * [Identical](/docs/api#isd) to [`web3.eth.getPastLogs`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#getpastlogs) in web3.js
|
|
1386
|
-
*
|
|
1387
757
|
* @param filter parameters to filter the logs by
|
|
1388
758
|
* @returns an array of logs matching the specified filter
|
|
1389
759
|
* @example
|
|
@@ -1435,7 +805,6 @@ var BaseProvider = class {
|
|
|
1435
805
|
*
|
|
1436
806
|
* * [Identical](/docs/api#isd) to [`ethers.provider.call`](https://docs.ethers.io/v5/api/providers/provider/#Provider-call) in ethers.js
|
|
1437
807
|
* * [Identical](/docs/api#isd) to [`web3.eth.call`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth.html#call) in web3.js
|
|
1438
|
-
*
|
|
1439
808
|
* @param transaction the transaction object to, in theory, execute. Doesn't actually get added to the blockchain.
|
|
1440
809
|
* @param blockTag the block to execute this transaction on
|
|
1441
810
|
* @returns the result of executing the transaction on the specified block
|
|
@@ -1472,6 +841,61 @@ var BaseProvider = class {
|
|
|
1472
841
|
);
|
|
1473
842
|
return transactionRes;
|
|
1474
843
|
}
|
|
844
|
+
/**
|
|
845
|
+
* Resolves an ENS name to an Ethereum address.
|
|
846
|
+
*
|
|
847
|
+
* Performs the full ENS resolution process:
|
|
848
|
+
* 1. Computes the namehash of the ENS name
|
|
849
|
+
* 2. Queries the ENS Registry for the resolver contract
|
|
850
|
+
* 3. Queries the resolver for the address
|
|
851
|
+
*
|
|
852
|
+
* * [Identical](/docs/api#isd) to [`ethers.provider.resolveName`](https://docs.ethers.io/v5/api/providers/provider/#Provider-resolveName) in ethers.js
|
|
853
|
+
* @param name the ENS name to resolve (e.g. 'vitalik.eth')
|
|
854
|
+
* @returns the Ethereum address the name resolves to, or null if not found
|
|
855
|
+
* @example
|
|
856
|
+
* ```javascript
|
|
857
|
+
* await provider.resolveName('vitalik.eth');
|
|
858
|
+
* // '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
|
|
859
|
+
* ```
|
|
860
|
+
* @example
|
|
861
|
+
* ```javascript
|
|
862
|
+
* await provider.resolveName('thisshouldnotexist12345.eth');
|
|
863
|
+
* // null
|
|
864
|
+
* ```
|
|
865
|
+
*/
|
|
866
|
+
async resolveName(name) {
|
|
867
|
+
const ENS_REGISTRY = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
|
|
868
|
+
const RESOLVER_SELECTOR = "0x0178b8bf";
|
|
869
|
+
const ADDR_SELECTOR = "0x3b3b57de";
|
|
870
|
+
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
871
|
+
const node = namehash(name);
|
|
872
|
+
const nodeWithoutPrefix = node.slice(2);
|
|
873
|
+
const resolverData = RESOLVER_SELECTOR + nodeWithoutPrefix;
|
|
874
|
+
const resolverResult = await this.call({
|
|
875
|
+
to: ENS_REGISTRY,
|
|
876
|
+
data: resolverData
|
|
877
|
+
});
|
|
878
|
+
if (!resolverResult || resolverResult === ZERO_ADDRESS) {
|
|
879
|
+
return null;
|
|
880
|
+
}
|
|
881
|
+
const resolverAddress = "0x" + resolverResult.slice(26);
|
|
882
|
+
if (resolverAddress === "0x0000000000000000000000000000000000000000" || resolverAddress === "0x" + "0".repeat(resolverResult.length - 2)) {
|
|
883
|
+
return null;
|
|
884
|
+
}
|
|
885
|
+
const addrData = ADDR_SELECTOR + nodeWithoutPrefix;
|
|
886
|
+
const addrResult = await this.call({
|
|
887
|
+
to: resolverAddress,
|
|
888
|
+
data: addrData
|
|
889
|
+
});
|
|
890
|
+
if (!addrResult || addrResult === ZERO_ADDRESS) {
|
|
891
|
+
return null;
|
|
892
|
+
}
|
|
893
|
+
const rawAddress = "0x" + addrResult.slice(26);
|
|
894
|
+
if (rawAddress === "0x0000000000000000000000000000000000000000") {
|
|
895
|
+
return null;
|
|
896
|
+
}
|
|
897
|
+
return toChecksumAddress(rawAddress);
|
|
898
|
+
}
|
|
1475
899
|
};
|
|
1476
900
|
|
|
1477
901
|
// src/providers/JsonRpcProvider.ts
|
|
@@ -1573,380 +997,149 @@ var FallthroughProvider = class extends BaseProvider {
|
|
|
1573
997
|
};
|
|
1574
998
|
};
|
|
1575
999
|
|
|
1576
|
-
// src/utils/
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
// src/utils/keccak256.ts
|
|
1584
|
-
import { Keccak as Keccak3 } from "sha3";
|
|
1585
|
-
function keccak256(data) {
|
|
1586
|
-
let bufferableData;
|
|
1587
|
-
if (typeof data === "string") {
|
|
1588
|
-
bufferableData = Buffer.from(data.replace(/^0x/, ""), "hex");
|
|
1589
|
-
} else {
|
|
1590
|
-
bufferableData = Buffer.from(data);
|
|
1000
|
+
// src/utils/abi-encode-decode.ts
|
|
1001
|
+
function findFunctionABI(abi, functionName) {
|
|
1002
|
+
const entry = abi.find(
|
|
1003
|
+
(item) => item.type === "function" && item.name === functionName
|
|
1004
|
+
);
|
|
1005
|
+
if (!entry) {
|
|
1006
|
+
throw new Error(`Function "${functionName}" not found in ABI`);
|
|
1591
1007
|
}
|
|
1592
|
-
|
|
1593
|
-
const addressHash = "0x" + keccak.update(bufferableData).digest("hex");
|
|
1594
|
-
return addressHash;
|
|
1008
|
+
return entry;
|
|
1595
1009
|
}
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
if (!key.startsWith("0x04") && !key.startsWith("0x03") && !key.startsWith("0x02")) {
|
|
1600
|
-
key = computePublicKey(key);
|
|
1601
|
-
}
|
|
1602
|
-
return toChecksumAddress(hexDataSlice(keccak256(hexDataSlice(key, 1)), 12));
|
|
1010
|
+
function encodeFunctionData(abi, functionName, args = []) {
|
|
1011
|
+
const abiEntry = findFunctionABI(abi, functionName);
|
|
1012
|
+
return encodeData(abiEntry, args);
|
|
1603
1013
|
}
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
validateType(etherQuantity, ["string", "number", "object"]);
|
|
1608
|
-
const result = tinyBig(etherQuantity).times("1000000000");
|
|
1609
|
-
return tinyBig(result);
|
|
1014
|
+
function decodeFunctionResult(abi, functionName, data) {
|
|
1015
|
+
const abiEntry = findFunctionABI(abi, functionName);
|
|
1016
|
+
return decodeRPCResponse(abiEntry, data);
|
|
1610
1017
|
}
|
|
1611
1018
|
|
|
1612
|
-
// src/utils/
|
|
1613
|
-
function
|
|
1614
|
-
|
|
1615
|
-
const result = tinyBig(etherQuantity).times("1000000000000000000");
|
|
1616
|
-
return tinyBig(result);
|
|
1019
|
+
// src/utils/to-utf8-string.ts
|
|
1020
|
+
function toUtf8String(bytes) {
|
|
1021
|
+
return new TextDecoder().decode(arrayify(bytes));
|
|
1617
1022
|
}
|
|
1618
1023
|
|
|
1619
|
-
// src/utils/
|
|
1620
|
-
function
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1024
|
+
// src/utils/bytes32-string.ts
|
|
1025
|
+
function encodeBytes32String(text) {
|
|
1026
|
+
const bytes = toUtf8Bytes(text);
|
|
1027
|
+
if (bytes.length > 31) {
|
|
1028
|
+
throw new Error("bytes32 string must be less than 32 bytes");
|
|
1029
|
+
}
|
|
1030
|
+
const padded = new Uint8Array(32);
|
|
1031
|
+
padded.set(bytes);
|
|
1032
|
+
return hexlify(padded);
|
|
1624
1033
|
}
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1034
|
+
function decodeBytes32String(bytes32) {
|
|
1035
|
+
let hex = bytes32;
|
|
1036
|
+
if (hex.startsWith("0x") || hex.startsWith("0X")) {
|
|
1037
|
+
hex = hex.slice(2);
|
|
1038
|
+
}
|
|
1039
|
+
hex = hex.replace(/(00)+$/, "");
|
|
1040
|
+
if (hex.length === 0)
|
|
1041
|
+
return "";
|
|
1042
|
+
return toUtf8String("0x" + hex);
|
|
1629
1043
|
}
|
|
1630
1044
|
|
|
1631
|
-
// src/utils/
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
}
|
|
1637
|
-
return keccak256(
|
|
1638
|
-
concat([
|
|
1639
|
-
toUtf8Bytes(messagePrefix),
|
|
1640
|
-
toUtf8Bytes(String(message.length)),
|
|
1641
|
-
message
|
|
1642
|
-
])
|
|
1643
|
-
);
|
|
1045
|
+
// src/utils/decode-event-log.ts
|
|
1046
|
+
function computeEventTopic(event) {
|
|
1047
|
+
const types = event.inputs.map((input) => input.type);
|
|
1048
|
+
const signature = `${event.name}(${types.join(",")})`;
|
|
1049
|
+
return keccak256(toUtf8Bytes(signature));
|
|
1644
1050
|
}
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
validateType(address, ["string"]);
|
|
1649
|
-
try {
|
|
1650
|
-
toChecksumAddress(address);
|
|
1651
|
-
return true;
|
|
1652
|
-
} catch (error) {
|
|
1653
|
-
return false;
|
|
1051
|
+
function decodeValue(hexChunk, type) {
|
|
1052
|
+
if (type === "bool") {
|
|
1053
|
+
return hexChunk[hexChunk.length - 1] === "1";
|
|
1654
1054
|
}
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
// src/utils/solidity-keccak256.ts
|
|
1658
|
-
import { Buffer as Buffer2 } from "buffer";
|
|
1659
|
-
var regexBytes = new RegExp("^bytes([0-9]+)$");
|
|
1660
|
-
var regexNumber = new RegExp("^(u?int)([0-9]*)$");
|
|
1661
|
-
var regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$");
|
|
1662
|
-
function _pack(type, value, isArray) {
|
|
1663
|
-
switch (type) {
|
|
1664
|
-
case "address":
|
|
1665
|
-
if (isArray) {
|
|
1666
|
-
return zeroPad(value, 32);
|
|
1667
|
-
}
|
|
1668
|
-
return arrayify(value);
|
|
1669
|
-
case "string":
|
|
1670
|
-
return Buffer2.from(value);
|
|
1671
|
-
case "bytes":
|
|
1672
|
-
return arrayify(value);
|
|
1673
|
-
case "bool":
|
|
1674
|
-
value = value ? "0x01" : "0x00";
|
|
1675
|
-
if (isArray) {
|
|
1676
|
-
return zeroPad(value, 32);
|
|
1677
|
-
}
|
|
1678
|
-
return arrayify(value);
|
|
1055
|
+
if (type === "address") {
|
|
1056
|
+
return toChecksumAddress(`0x${hexChunk.slice(24)}`);
|
|
1679
1057
|
}
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
let size = parseInt(match[2] || "256");
|
|
1683
|
-
if (match[2] && String(size) !== match[2] || size % 8 !== 0 || size === 0 || size > 256) {
|
|
1684
|
-
logger.throwArgumentError("invalid number type", "type", type);
|
|
1685
|
-
}
|
|
1686
|
-
if (isArray) {
|
|
1687
|
-
size = 256;
|
|
1688
|
-
}
|
|
1689
|
-
value = tinyBig(value).toTwos(size).toNumber();
|
|
1690
|
-
const hexValue2 = hexlify(value);
|
|
1691
|
-
return zeroPad(hexValue2, size / 8);
|
|
1058
|
+
if (type === "bytes32") {
|
|
1059
|
+
return `0x${hexChunk}`;
|
|
1692
1060
|
}
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
const size = parseInt(match[1]);
|
|
1696
|
-
if (String(size) !== match[1] || size === 0 || size > 32) {
|
|
1697
|
-
logger.throwArgumentError("invalid bytes type", "type", type);
|
|
1698
|
-
}
|
|
1699
|
-
if (arrayify(value).byteLength !== size) {
|
|
1700
|
-
logger.throwArgumentError(`invalid value for ${type}`, "value", value);
|
|
1701
|
-
}
|
|
1702
|
-
if (isArray) {
|
|
1703
|
-
return arrayify((value + hexFalse).substring(0, 66));
|
|
1704
|
-
}
|
|
1705
|
-
return value;
|
|
1061
|
+
if (type === "uint8") {
|
|
1062
|
+
return Number(BigInt(`0x${hexChunk}`));
|
|
1706
1063
|
}
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
const baseType = match[1];
|
|
1710
|
-
const count = parseInt(match[2] || String(value.length));
|
|
1711
|
-
if (count != value.length) {
|
|
1712
|
-
logger.throwArgumentError(
|
|
1713
|
-
`invalid array length for ${type}`,
|
|
1714
|
-
"value",
|
|
1715
|
-
value
|
|
1716
|
-
);
|
|
1717
|
-
}
|
|
1718
|
-
const result = [];
|
|
1719
|
-
value.forEach(function(value2) {
|
|
1720
|
-
result.push(_pack(baseType, value2, true));
|
|
1721
|
-
});
|
|
1722
|
-
return concat(result);
|
|
1064
|
+
if (type.startsWith("uint")) {
|
|
1065
|
+
return BigInt(`0x${hexChunk}`);
|
|
1723
1066
|
}
|
|
1724
|
-
|
|
1725
|
-
}
|
|
1726
|
-
function pack(types, values) {
|
|
1727
|
-
if (types.length != values.length) {
|
|
1728
|
-
logger.throwArgumentError(
|
|
1729
|
-
"wrong number of values; expected ${ types.length }",
|
|
1730
|
-
"values",
|
|
1731
|
-
values
|
|
1732
|
-
);
|
|
1067
|
+
if (type.startsWith("int")) {
|
|
1068
|
+
return BigInt(`0x${hexChunk}`);
|
|
1733
1069
|
}
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
});
|
|
1738
|
-
return hexlify(concat(tight));
|
|
1739
|
-
}
|
|
1740
|
-
function solidityKeccak256(types, values) {
|
|
1741
|
-
return keccak256(pack(types, values));
|
|
1070
|
+
throw new Error(
|
|
1071
|
+
`essential-eth does not yet support decoding "${type}" in event logs. Make a PR today!`
|
|
1072
|
+
);
|
|
1742
1073
|
}
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
yParityAndS: "0x",
|
|
1753
|
-
compact: "0x"
|
|
1754
|
-
};
|
|
1755
|
-
if (isBytesLike(signature)) {
|
|
1756
|
-
const bytes = arrayify(signature);
|
|
1757
|
-
if (bytes.length === 64) {
|
|
1758
|
-
result.v = 27 + (bytes[32] >> 7);
|
|
1759
|
-
bytes[32] &= 127;
|
|
1760
|
-
result.r = hexlify(bytes.slice(0, 32));
|
|
1761
|
-
result.s = hexlify(bytes.slice(32, 64));
|
|
1762
|
-
} else if (bytes.length === 65) {
|
|
1763
|
-
result.r = hexlify(bytes.slice(0, 32));
|
|
1764
|
-
result.s = hexlify(bytes.slice(32, 64));
|
|
1765
|
-
result.v = bytes[64];
|
|
1766
|
-
} else {
|
|
1767
|
-
logger.throwArgumentError(
|
|
1768
|
-
"invalid signature string",
|
|
1769
|
-
"signature",
|
|
1770
|
-
signature
|
|
1771
|
-
);
|
|
1772
|
-
}
|
|
1773
|
-
if (result.v < 27) {
|
|
1774
|
-
if (result.v === 0 || result.v === 1) {
|
|
1775
|
-
result.v += 27;
|
|
1776
|
-
} else {
|
|
1777
|
-
logger.throwArgumentError(
|
|
1778
|
-
"signature invalid v byte",
|
|
1779
|
-
"signature",
|
|
1780
|
-
signature
|
|
1781
|
-
);
|
|
1782
|
-
}
|
|
1783
|
-
}
|
|
1784
|
-
result.recoveryParam = 1 - result.v % 2;
|
|
1785
|
-
if (result.recoveryParam) {
|
|
1786
|
-
bytes[32] |= 128;
|
|
1074
|
+
function decodeEventLog(abi, log) {
|
|
1075
|
+
const topic0 = log.topics[0];
|
|
1076
|
+
const events = abi.filter((entry) => entry.type === "event");
|
|
1077
|
+
let matchedEvent;
|
|
1078
|
+
for (const event of events) {
|
|
1079
|
+
const hash = computeEventTopic(event);
|
|
1080
|
+
if (hash === topic0) {
|
|
1081
|
+
matchedEvent = event;
|
|
1082
|
+
break;
|
|
1787
1083
|
}
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
const
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
logger.throwArgumentError(
|
|
1803
|
-
"signature recoveryParam mismatch _vs",
|
|
1804
|
-
"signature",
|
|
1805
|
-
signature
|
|
1806
|
-
);
|
|
1807
|
-
}
|
|
1808
|
-
vs_1[0] &= 127;
|
|
1809
|
-
const s = hexlify(vs_1);
|
|
1810
|
-
if (result.s == null) {
|
|
1811
|
-
result.s = s;
|
|
1812
|
-
} else if (result.s !== s) {
|
|
1813
|
-
logger.throwArgumentError(
|
|
1814
|
-
"signature v mismatch _vs",
|
|
1815
|
-
"signature",
|
|
1816
|
-
signature
|
|
1817
|
-
);
|
|
1818
|
-
}
|
|
1819
|
-
}
|
|
1820
|
-
if (result.recoveryParam == null) {
|
|
1821
|
-
if (result.v == null) {
|
|
1822
|
-
logger.throwArgumentError(
|
|
1823
|
-
"signature missing v and recoveryParam",
|
|
1824
|
-
"signature",
|
|
1825
|
-
signature
|
|
1826
|
-
);
|
|
1827
|
-
} else if (result.v === 0 || result.v === 1) {
|
|
1828
|
-
result.recoveryParam = result.v;
|
|
1829
|
-
} else {
|
|
1830
|
-
result.recoveryParam = 1 - result.v % 2;
|
|
1831
|
-
}
|
|
1832
|
-
} else {
|
|
1833
|
-
if (result.v == null) {
|
|
1834
|
-
result.v = 27 + result.recoveryParam;
|
|
1835
|
-
} else {
|
|
1836
|
-
const recId = result.v === 0 || result.v === 1 ? result.v : 1 - result.v % 2;
|
|
1837
|
-
if (result.recoveryParam !== recId) {
|
|
1838
|
-
logger.throwArgumentError(
|
|
1839
|
-
"signature recoveryParam mismatch v",
|
|
1840
|
-
"signature",
|
|
1841
|
-
signature
|
|
1842
|
-
);
|
|
1843
|
-
}
|
|
1844
|
-
}
|
|
1845
|
-
}
|
|
1846
|
-
if (result.r == null || !isHexString(result.r)) {
|
|
1847
|
-
logger.throwArgumentError(
|
|
1848
|
-
"signature missing or invalid r",
|
|
1849
|
-
"signature",
|
|
1850
|
-
signature
|
|
1851
|
-
);
|
|
1852
|
-
} else {
|
|
1853
|
-
result.r = hexZeroPad(result.r, 32);
|
|
1854
|
-
}
|
|
1855
|
-
if (result.s == null || !isHexString(result.s)) {
|
|
1856
|
-
logger.throwArgumentError(
|
|
1857
|
-
"signature missing or invalid s",
|
|
1858
|
-
"signature",
|
|
1859
|
-
signature
|
|
1860
|
-
);
|
|
1084
|
+
}
|
|
1085
|
+
if (!matchedEvent) {
|
|
1086
|
+
throw new Error(`No matching event found in ABI for topic0: ${topic0}`);
|
|
1087
|
+
}
|
|
1088
|
+
const args = {};
|
|
1089
|
+
let topicIndex = 1;
|
|
1090
|
+
let dataOffset = 0;
|
|
1091
|
+
const rawData = log.data.startsWith("0x") ? log.data.slice(2) : log.data;
|
|
1092
|
+
for (const input of matchedEvent.inputs) {
|
|
1093
|
+
if (input.indexed) {
|
|
1094
|
+
const topicHex = log.topics[topicIndex];
|
|
1095
|
+
const hexChunk = topicHex.startsWith("0x") ? topicHex.slice(2) : topicHex;
|
|
1096
|
+
args[input.name] = decodeValue(hexChunk, input.type);
|
|
1097
|
+
topicIndex++;
|
|
1861
1098
|
} else {
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
if (vs[0] >= 128) {
|
|
1866
|
-
logger.throwArgumentError(
|
|
1867
|
-
"signature s out of range",
|
|
1868
|
-
"signature",
|
|
1869
|
-
signature
|
|
1870
|
-
);
|
|
1871
|
-
}
|
|
1872
|
-
if (result.recoveryParam) {
|
|
1873
|
-
vs[0] |= 128;
|
|
1874
|
-
}
|
|
1875
|
-
const _vs = hexlify(vs);
|
|
1876
|
-
if (result._vs) {
|
|
1877
|
-
if (!isHexString(result._vs)) {
|
|
1878
|
-
logger.throwArgumentError(
|
|
1879
|
-
"signature invalid _vs",
|
|
1880
|
-
"signature",
|
|
1881
|
-
signature
|
|
1882
|
-
);
|
|
1883
|
-
}
|
|
1884
|
-
result._vs = hexZeroPad(result._vs, 32);
|
|
1885
|
-
}
|
|
1886
|
-
if (result._vs == null) {
|
|
1887
|
-
result._vs = _vs;
|
|
1888
|
-
} else if (result._vs !== _vs) {
|
|
1889
|
-
logger.throwArgumentError(
|
|
1890
|
-
"signature _vs mismatch v and s",
|
|
1891
|
-
"signature",
|
|
1892
|
-
signature
|
|
1893
|
-
);
|
|
1099
|
+
const hexChunk = rawData.slice(dataOffset, dataOffset + 64);
|
|
1100
|
+
args[input.name] = decodeValue(hexChunk, input.type);
|
|
1101
|
+
dataOffset += 64;
|
|
1894
1102
|
}
|
|
1895
1103
|
}
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1104
|
+
return {
|
|
1105
|
+
eventName: matchedEvent.name,
|
|
1106
|
+
args
|
|
1107
|
+
};
|
|
1899
1108
|
}
|
|
1900
1109
|
|
|
1901
|
-
// src/utils/
|
|
1902
|
-
function
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
} catch (error) {
|
|
1912
|
-
throw error;
|
|
1110
|
+
// src/utils/event-topic.ts
|
|
1111
|
+
function getEventTopic(eventSignature) {
|
|
1112
|
+
return keccak256(toUtf8Bytes(eventSignature));
|
|
1113
|
+
}
|
|
1114
|
+
function getEventSignature(abi, eventName) {
|
|
1115
|
+
const event = abi.find(
|
|
1116
|
+
(entry) => entry.type === "event" && entry.name === eventName
|
|
1117
|
+
);
|
|
1118
|
+
if (!event) {
|
|
1119
|
+
throw new Error(`Event "${eventName}" not found in ABI`);
|
|
1913
1120
|
}
|
|
1121
|
+
const signature = `${event.name}(${event.inputs.map((input) => input.type).join(",")})`;
|
|
1122
|
+
return getEventTopic(signature);
|
|
1914
1123
|
}
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
isAddress,
|
|
1938
|
-
isBytes,
|
|
1939
|
-
isBytesLike,
|
|
1940
|
-
isHexString,
|
|
1941
|
-
jsonRpcProvider,
|
|
1942
|
-
keccak256,
|
|
1943
|
-
pack,
|
|
1944
|
-
solidityKeccak256,
|
|
1945
|
-
splitSignature,
|
|
1946
|
-
stripZeros,
|
|
1947
|
-
tinyBig,
|
|
1948
|
-
toChecksumAddress,
|
|
1949
|
-
toUtf8Bytes,
|
|
1950
|
-
weiToEther,
|
|
1951
|
-
zeroPad
|
|
1952
|
-
};
|
|
1124
|
+
|
|
1125
|
+
// src/utils/format-units.ts
|
|
1126
|
+
function formatUnits(value, decimals = 18) {
|
|
1127
|
+
return formatFixed(toBigInt(value), decimals);
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
// src/utils/get-address.ts
|
|
1131
|
+
function getAddress(address) {
|
|
1132
|
+
return toChecksumAddress(address);
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
// src/utils/id.ts
|
|
1136
|
+
function id(text) {
|
|
1137
|
+
return keccak256(toUtf8Bytes(text));
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
// src/utils/parse-units.ts
|
|
1141
|
+
function parseUnits(value, decimals = 18) {
|
|
1142
|
+
return parseFixed(value, decimals);
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
export { AlchemyProvider, BaseContract, Contract, FallthroughProvider, JsonRpcProvider, decodeBytes32String, decodeEventLog, decodeFunctionResult, encodeBytes32String, encodeFunctionData, formatUnits, getAddress, getEventSignature, getEventTopic, id, jsonRpcProvider, namehash, parseUnits, toUtf8String };
|