Created
September 11, 2020 22:38
-
-
Save spalladino/7bce4a27626fc3327731f3ddb9f7587f to your computer and use it in GitHub Desktop.
Get the revert reason before sending an Ethereum transaction if the transaction would fail
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
export async function tryGetRevertReason(to: string, from: string, data: string): Promise<string | undefined> { | |
const provider = ethers.getDefaultProvider(); | |
const tx = { to, from, data }; | |
try { | |
await provider.estimateGas(tx); | |
} catch { | |
const value = await provider.call(tx); | |
return hexDataLength(value) % 32 === 4 && hexDataSlice(value, 0, 4) === '0x08c379a0' | |
? defaultAbiCoder.decode(['string'], hexDataSlice(value, 4)) | |
: undefined; | |
} | |
return undefined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment