| 26 | |
| 27 | |
| 28 | function stringify(value: any, seen?: Set<any>): any { |
| 29 | if (value == null) { return "null"; } |
| 30 | |
| 31 | if (seen == null) { seen = new Set(); } |
| 32 | if (typeof(value) === "object") { |
| 33 | if (seen.has(value)) { return "[Circular]"; } |
| 34 | seen.add(value); |
| 35 | } |
| 36 | |
| 37 | if (Array.isArray(value)) { |
| 38 | return "[ " + (value.map((v) => stringify(v, seen))).join(", ") + " ]"; |
| 39 | } |
| 40 | |
| 41 | if (value instanceof Uint8Array) { |
| 42 | const HEX = "0123456789abcdef"; |
| 43 | let result = "0x"; |
| 44 | for (let i = 0; i < value.length; i++) { |
| 45 | result += HEX[value[i] >> 4]; |
| 46 | result += HEX[value[i] & 0xf]; |
| 47 | } |
| 48 | return result; |
| 49 | } |
| 50 | |
| 51 | if (typeof(value) === "object" && typeof(value.toJSON) === "function") { |
| 52 | return stringify(value.toJSON(), seen); |
| 53 | } |
| 54 | |
| 55 | switch (typeof(value)) { |
| 56 | case "boolean": case "number": case "symbol": |
| 57 | return value.toString(); |
| 58 | case "bigint": |
| 59 | return BigInt(value).toString(); |
| 60 | case "string": |
| 61 | return JSON.stringify(value); |
| 62 | case "object": { |
| 63 | const keys = Object.keys(value); |
| 64 | keys.sort(); |
| 65 | return "{ " + keys.map((k) => `${ stringify(k, seen) }: ${ stringify(value[k], seen) }`).join(", ") + " }"; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return `[ COULD NOT SERIALIZE ]`; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * All errors emitted by ethers have an **ErrorCode** to help |