-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c43f157
commit 42aa2b6
Showing
6 changed files
with
158 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Interface } from '@ethersproject/abi' | ||
import { | ||
EntryPoint__factory, | ||
EntryPointSimulations__factory, | ||
TokenPaymaster__factory | ||
} from '@account-abstraction/contracts' | ||
import { ethers } from 'ethers' | ||
|
||
const decodeRevertReasonContracts = new Interface([ | ||
...EntryPointSimulations__factory.createInterface().fragments, | ||
...TokenPaymaster__factory.createInterface().fragments, | ||
// ...TestERC20__factory.createInterface().fragments, // for OZ errors, | ||
'error ECDSAInvalidSignature()' | ||
]) // .filter(f => f.type === 'error')) | ||
|
||
/** | ||
* helper to decode revert data into its string representation | ||
* @param data revert data or an exception thrown by eth_call | ||
* @param nullIfNoMatch true to return null if not found. otherwise, return input data as-is | ||
*/ | ||
export function decodeRevertReason (data: string | Error, nullIfNoMatch = true): string | null { | ||
if (typeof data !== 'string') { | ||
const err = data as any | ||
data = (err.data?.data ?? err.data ?? err.error.data) as string | ||
} | ||
const methodSig = data.slice(0, 10) | ||
const dataParams = '0x' + data.slice(10) | ||
|
||
try { | ||
// would be nice to add these to above "decodeRevertReasonContracts", but we can't add Error(string) to xface... | ||
if (methodSig === '0x08c379a0') { | ||
const [err] = ethers.utils.defaultAbiCoder.decode(['string'], dataParams) | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
return `Error(${err})` | ||
} else if (methodSig === '0x4e487b71') { | ||
const [code] = ethers.utils.defaultAbiCoder.decode(['uint256'], dataParams) | ||
return `Panic(${panicCodes[code] ?? code} + ')` | ||
} | ||
const err = decodeRevertReasonContracts.parseError(data) | ||
// treat any error "bytes" argument as possible error to decode (e.g. FailedOpWithRevert, PostOpReverted) | ||
const args = err.args.map((arg: any, index) => { | ||
switch (err.errorFragment.inputs[index].type) { | ||
case 'bytes' : return decodeRevertReason(arg, false) | ||
case 'string': return `"${(arg as string)}"` | ||
default: return arg | ||
} | ||
}) | ||
return `${err.name}(${args.join(',')})` | ||
} catch (e) { | ||
// throw new Error('unsupported errorSig ' + data) | ||
if (!nullIfNoMatch) { | ||
return data | ||
} | ||
return null | ||
} | ||
} | ||
|
||
const panicCodes: { [key: number]: string } = { | ||
// from https://docs.soliditylang.org/en/v0.8.0/control-structures.html | ||
0x01: 'assert(false)', | ||
0x11: 'arithmetic overflow/underflow', | ||
0x12: 'divide by zero', | ||
0x21: 'invalid enum value', | ||
0x22: 'storage byte array that is incorrectly encoded', | ||
0x31: '.pop() on an empty array.', | ||
0x32: 'array sout-of-bounds or negative index', | ||
0x41: 'memory overflow', | ||
0x51: 'zero-initialized variable of internal function type' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters