|
1 |
| -import { bytesToBigInt } from "@nomicfoundation/ethereumjs-util"; |
2 |
| -import { assertHardhatInvariant } from "../../core/errors"; |
3 |
| - |
4 |
| -const { rawDecode } = require("ethereumjs-abi"); |
5 |
| - |
6 |
| -// selector of Error(string) |
7 |
| -const ERROR_SELECTOR = "08c379a0"; |
8 |
| -// selector of Panic(uint256) |
9 |
| -const PANIC_SELECTOR = "4e487b71"; |
10 |
| - |
11 |
| -/** |
12 |
| - * Represents the returnData of a transaction, whose contents are unknown. |
13 |
| - */ |
14 |
| -export class ReturnData { |
15 |
| - private _selector: string | undefined; |
16 |
| - |
17 |
| - constructor(public value: Uint8Array) { |
18 |
| - if (value.length >= 4) { |
19 |
| - this._selector = Buffer.from(value.slice(0, 4)).toString("hex"); |
20 |
| - } |
21 |
| - } |
22 |
| - |
23 |
| - public isEmpty(): boolean { |
24 |
| - return this.value.length === 0; |
25 |
| - } |
26 |
| - |
27 |
| - public matchesSelector(selector: Uint8Array): boolean { |
28 |
| - if (this._selector === undefined) { |
29 |
| - return false; |
30 |
| - } |
31 |
| - |
32 |
| - return this._selector === Buffer.from(selector).toString("hex"); |
33 |
| - } |
34 |
| - |
35 |
| - public isErrorReturnData(): boolean { |
36 |
| - return this._selector === ERROR_SELECTOR; |
37 |
| - } |
38 |
| - |
39 |
| - public isPanicReturnData(): boolean { |
40 |
| - return this._selector === PANIC_SELECTOR; |
41 |
| - } |
42 |
| - |
43 |
| - public decodeError(): string { |
44 |
| - if (this.isEmpty()) { |
45 |
| - return ""; |
46 |
| - } |
47 |
| - |
48 |
| - assertHardhatInvariant( |
49 |
| - this._selector === ERROR_SELECTOR, |
50 |
| - "Expected return data to be a Error(string)" |
51 |
| - ); |
52 |
| - |
53 |
| - const [decodedError] = rawDecode(["string"], this.value.slice(4)) as [ |
54 |
| - string |
55 |
| - ]; |
56 |
| - |
57 |
| - return decodedError; |
58 |
| - } |
59 |
| - |
60 |
| - public decodePanic(): bigint { |
61 |
| - assertHardhatInvariant( |
62 |
| - this._selector === PANIC_SELECTOR, |
63 |
| - "Expected return data to be a Panic(uint256)" |
64 |
| - ); |
65 |
| - |
66 |
| - // we are assuming that panic codes are smaller than Number.MAX_SAFE_INTEGER |
67 |
| - const errorCode = bytesToBigInt(this.value.slice(4)); |
68 |
| - |
69 |
| - return errorCode; |
70 |
| - } |
71 |
| - |
72 |
| - public getSelector(): string | undefined { |
73 |
| - return this._selector; |
74 |
| - } |
75 |
| -} |
| 1 | +export { ReturnData } from "@nomicfoundation/edr"; |
0 commit comments