
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
ethers-decode-error
Advanced tools
Decode ethers.js smart contract errors into human-readable messages
For those who've grappled with extracting the actual error message or reason from the JSON RPC when a transaction fails or a smart contract reverts, you'll certainly appreciate how cumbersome it could at times.
This utility library can help to simplify the process for you. You simply pass in the error object, the library will return the actual error message. It works with the regular revert errors, panic errors, Metamask errors and custom errors.
npm install ethers-decode-error --save
You will need to install ethers.js in your project if you have not:
npm install ethers@^5 --save
To decode an error, pass it as the first argument to the decodeError
function. This will provide you with the
decoded error, allowing you to decide the best course of action from there.
import { decodeError } from 'ethers-decode-error'
const WETH = new ethers.Contract('0xC02aa...756Cc2', abi, provider)
try {
const tx = await WETH.transfer('0x0', amount)
await tx.wait()
} catch (err) {
const { error } = decodeError(err)
// Prints "ERC20: transfer to the zero address"
console.log('Revert reason:', error)
}
import { decodeError } from 'ethers-decode-error'
const MyWeirdContract = new ethers.Contract('0x12345678', abi, provider)
try {
const tx = await MyWeirdContract.add(123)
await tx.wait()
} catch (err) {
const { error } = decodeError(err)
// Prints "Arithmetic operation underflowed or overflowed outside of an unchecked block"
console.log('Panic message:', error)
}
import { decodeError, DecodedError } from 'ethers-decode-error'
const abi = [
{
inputs: [
{
internalType: 'address',
name: 'token',
type: 'address',
},
],
name: 'InvalidSwapToken',
type: 'error',
},
]
const MyCustomErrorContract = new ethers.Contract('0x12345678', abi, provider)
try {
const tx = await MyCustomErrorContract.swap('0xabcd', 123)
await tx.wait()
} catch (err) {
const decodedError = decodeError(err, abi)
const reason = customReasonMapper(decodedError)
// Prints "Invalid swap with token contract address 0xabcd."
console.log('Custom error reason:', reason)
}
const customReasonMapper = ({ error, args }: DecodedError): string => {
switch (error) {
case 'InvalidSwapToken':
return `Invalid swap with token contract address ${args[0]}.`
default:
return 'The transaction has reverted.'
}
}
If you're using TypeChain in your project, simply pass the contract's Interface
to the decodeError
function so that
it can decode all custom errors that the contract could possibly revert with:
const decodedError = decodeError(err, MyCustomErrorContract__factory.createInterface())
// Prints "Invalid swap with token contract address 0xabcd."
console.log('Custom error reason:', reason)
Feel free to open an issue or PR for any bugs/improvements.
FAQs
Decode ethers.js smart contract errors into human-readable messages
The npm package ethers-decode-error receives a total of 2,620 weekly downloads. As such, ethers-decode-error popularity was classified as popular.
We found that ethers-decode-error demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.