-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathethers-utils.ts
424 lines (370 loc) · 10.1 KB
/
ethers-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// these helpers functions were copied verbatim from ethers
import type {
TransactionRequest,
PreparedTransactionRequest,
BlockParams,
TransactionResponseParams,
TransactionReceiptParams,
LogParams,
JsonRpcTransactionRequest,
} from "ethers";
import {
accessListify,
assert,
assertArgument,
getAddress,
getBigInt,
getCreateAddress,
getNumber,
hexlify,
isHexString,
Signature,
toQuantity,
} from "ethers";
import { HardhatEthersError } from "./errors";
export type FormatFunc = (value: any) => any;
export function copyRequest(
req: TransactionRequest
): PreparedTransactionRequest {
const result: any = {};
// These could be addresses, ENS names or Addressables
if (req.to !== null && req.to !== undefined) {
result.to = req.to;
}
if (req.from !== null && req.from !== undefined) {
result.from = req.from;
}
if (req.data !== null && req.data !== undefined) {
result.data = hexlify(req.data);
}
const bigIntKeys =
"chainId,gasLimit,gasPrice,maxFeePerGas,maxPriorityFeePerGas,value".split(
/,/
);
for (const key of bigIntKeys) {
if (
!(key in req) ||
(req as any)[key] === null ||
(req as any)[key] === undefined
) {
continue;
}
result[key] = getBigInt((req as any)[key], `request.${key}`);
}
const numberKeys = "type,nonce".split(/,/);
for (const key of numberKeys) {
if (
!(key in req) ||
(req as any)[key] === null ||
(req as any)[key] === undefined
) {
continue;
}
result[key] = getNumber((req as any)[key], `request.${key}`);
}
if (req.accessList !== null && req.accessList !== undefined) {
result.accessList = accessListify(req.accessList);
}
if ("blockTag" in req) {
result.blockTag = req.blockTag;
}
if ("enableCcipRead" in req) {
result.enableCcipReadEnabled = Boolean(req.enableCcipRead);
}
if ("customData" in req) {
result.customData = req.customData;
}
return result;
}
export async function resolveProperties<T>(value: {
[P in keyof T]: T[P] | Promise<T[P]>;
}): Promise<T> {
const keys = Object.keys(value);
const results = await Promise.all(
keys.map((k) => Promise.resolve(value[k as keyof T]))
);
return results.reduce((accum: any, v, index) => {
accum[keys[index]] = v;
return accum;
}, {} as { [P in keyof T]: T[P] });
}
export function formatBlock(value: any): BlockParams {
const result = _formatBlock(value);
result.transactions = value.transactions.map(
(tx: string | TransactionResponseParams) => {
if (typeof tx === "string") {
return tx;
}
return formatTransactionResponse(tx);
}
);
return result;
}
const _formatBlock = object({
hash: allowNull(formatHash),
parentHash: formatHash,
number: getNumber,
timestamp: getNumber,
nonce: allowNull(formatData),
difficulty: getBigInt,
gasLimit: getBigInt,
gasUsed: getBigInt,
miner: allowNull(getAddress),
extraData: formatData,
baseFeePerGas: allowNull(getBigInt),
});
function object(
format: Record<string, FormatFunc>,
altNames?: Record<string, string[]>
): FormatFunc {
return (value: any) => {
const result: any = {};
// eslint-disable-next-line guard-for-in
for (const key in format) {
let srcKey = key;
if (altNames !== undefined && key in altNames && !(srcKey in value)) {
for (const altKey of altNames[key]) {
if (altKey in value) {
srcKey = altKey;
break;
}
}
}
try {
const nv = format[key](value[srcKey]);
if (nv !== undefined) {
result[key] = nv;
}
} catch (error) {
const message = error instanceof Error ? error.message : "not-an-error";
assert(
false,
`invalid value for value.${key} (${message})`,
"BAD_DATA",
{ value }
);
}
}
return result;
};
}
function allowNull(format: FormatFunc, nullValue?: any): FormatFunc {
return function (value: any) {
// eslint-disable-next-line eqeqeq
if (value === null || value === undefined) {
return nullValue;
}
return format(value);
};
}
function formatHash(value: any): string {
assertArgument(isHexString(value, 32), "invalid hash", "value", value);
return value;
}
function formatData(value: string): string {
assertArgument(isHexString(value, true), "invalid data", "value", value);
return value;
}
export function formatTransactionResponse(
value: any
): TransactionResponseParams {
// Some clients (TestRPC) do strange things like return 0x0 for the
// 0 address; correct this to be a real address
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (value.to && getBigInt(value.to) === 0n) {
value.to = "0x0000000000000000000000000000000000000000";
}
const result = object(
{
hash: formatHash,
type: (v: any) => {
// eslint-disable-next-line eqeqeq
if (v === "0x" || v == null) {
return 0;
}
return getNumber(v);
},
accessList: allowNull(accessListify, null),
blockHash: allowNull(formatHash, null),
blockNumber: allowNull(getNumber, null),
transactionIndex: allowNull(getNumber, null),
from: getAddress,
// either (gasPrice) or (maxPriorityFeePerGas + maxFeePerGas) must be set
gasPrice: allowNull(getBigInt),
maxPriorityFeePerGas: allowNull(getBigInt),
maxFeePerGas: allowNull(getBigInt),
gasLimit: getBigInt,
to: allowNull(getAddress, null),
value: getBigInt,
nonce: getNumber,
data: formatData,
creates: allowNull(getAddress, null),
chainId: allowNull(getBigInt, null),
},
{
data: ["input"],
gasLimit: ["gas"],
}
)(value);
// If to and creates are empty, populate the creates from the value
// eslint-disable-next-line eqeqeq
if (result.to == null && result.creates == null) {
result.creates = getCreateAddress(result);
}
// @TODO: Check fee data
// Add an access list to supported transaction types
// eslint-disable-next-line eqeqeq
if ((value.type === 1 || value.type === 2) && value.accessList == null) {
result.accessList = [];
}
// Compute the signature
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (value.signature) {
result.signature = Signature.from(value.signature);
} else {
result.signature = Signature.from(value);
}
// Some backends omit ChainId on legacy transactions, but we can compute it
// eslint-disable-next-line eqeqeq
if (result.chainId == null) {
const chainId = result.signature.legacyChainId;
// eslint-disable-next-line eqeqeq
if (chainId != null) {
result.chainId = chainId;
}
}
// 0x0000... should actually be null
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (result.blockHash && getBigInt(result.blockHash) === 0n) {
result.blockHash = null;
}
return result;
}
function arrayOf(format: FormatFunc): FormatFunc {
return (array: any) => {
if (!Array.isArray(array)) {
throw new HardhatEthersError("not an array");
}
return array.map((i) => format(i));
};
}
const _formatReceiptLog = object(
{
transactionIndex: getNumber,
blockNumber: getNumber,
transactionHash: formatHash,
address: getAddress,
topics: arrayOf(formatHash),
data: formatData,
index: getNumber,
blockHash: formatHash,
},
{
index: ["logIndex"],
}
);
const _formatTransactionReceipt = object(
{
to: allowNull(getAddress, null),
from: allowNull(getAddress, null),
contractAddress: allowNull(getAddress, null),
// should be allowNull(hash), but broken-EIP-658 support is handled in receipt
index: getNumber,
root: allowNull(hexlify),
gasUsed: getBigInt,
logsBloom: allowNull(formatData),
blockHash: formatHash,
hash: formatHash,
logs: arrayOf(formatReceiptLog),
blockNumber: getNumber,
cumulativeGasUsed: getBigInt,
effectiveGasPrice: allowNull(getBigInt),
status: allowNull(getNumber),
type: allowNull(getNumber, 0),
},
{
effectiveGasPrice: ["gasPrice"],
hash: ["transactionHash"],
index: ["transactionIndex"],
}
);
export function formatTransactionReceipt(value: any): TransactionReceiptParams {
return _formatTransactionReceipt(value);
}
export function formatReceiptLog(value: any): LogParams {
return _formatReceiptLog(value);
}
function formatBoolean(value: any): boolean {
switch (value) {
case true:
case "true":
return true;
case false:
case "false":
return false;
}
assertArgument(
false,
`invalid boolean; ${JSON.stringify(value)}`,
"value",
value
);
}
const _formatLog = object(
{
address: getAddress,
blockHash: formatHash,
blockNumber: getNumber,
data: formatData,
index: getNumber,
removed: formatBoolean,
topics: arrayOf(formatHash),
transactionHash: formatHash,
transactionIndex: getNumber,
},
{
index: ["logIndex"],
}
);
export function formatLog(value: any): LogParams {
return _formatLog(value);
}
export function getRpcTransaction(
tx: TransactionRequest
): JsonRpcTransactionRequest {
const result: JsonRpcTransactionRequest = {};
// JSON-RPC now requires numeric values to be "quantity" values
[
"chainId",
"gasLimit",
"gasPrice",
"type",
"maxFeePerGas",
"maxPriorityFeePerGas",
"nonce",
"value",
].forEach((key) => {
if ((tx as any)[key] === null || (tx as any)[key] === undefined) {
return;
}
let dstKey = key;
if (key === "gasLimit") {
dstKey = "gas";
}
(result as any)[dstKey] = toQuantity(
getBigInt((tx as any)[key], `tx.${key}`)
);
});
// Make sure addresses and data are lowercase
["from", "to", "data"].forEach((key) => {
if ((tx as any)[key] === null || (tx as any)[key] === undefined) {
return;
}
(result as any)[key] = hexlify((tx as any)[key]);
});
// Normalize the access list object
if (tx.accessList !== null && tx.accessList !== undefined) {
result.accessList = accessListify(tx.accessList);
}
return result;
}