npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ariesgun/ethers-decode-error

v1.1.0

Published

Decode ethers.js smart contract errors into human-readable messages

Downloads

97

Readme

ethers-decode-error

Release TypeScript version License: Apache 2.0

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.

Installation

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

Usage

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.

Revert/Require Errors

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)
}

Panic Errors

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)
}

Custom Errors

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)

Contributing

Feel free to open an issue or PR for any bugs/improvements.