Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ethers-decode-error

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ethers-decode-error

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

  • 2.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.1K
decreased by-2.37%
Maintainers
1
Weekly downloads
 
Created
Source

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 is a simple utility library to help simplify the process of determining the actual errors from smart contract. You simply pass in the error object and you will get the actual error message and a bunch of other information about the error. It works with the regular revert errors, panic errors, Metamask rejection error 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@^6 --save

💡 If you wish to use it with ethers v5 instead, please refer to the v1 release.

Usage

This library decodes an ethers error object reverted from a smart contract into results that lets you decide the best course of action from there.

Start by creating an instance of the ErrorDecoder:

import { ErrorDecoder } from 'ethers-decode-error'

const errorDecoder = ErrorDecoder.create()

The create method optionally accepts an array of ABI or contract interface objects as its first argument. Although the ABI is not required for normal reverts, it's recommended to supply the ABI or contract interfaces if you're expecting custom errors. See the examples in Custom Errors section for more details.

After creating the instance, you can repeatedly use the decode method throughout your code to decode error objects:

try {
  // Send a transaction that will revert
} catch (err) {
  const decodedError: decodedError = await errorDecoder.decode(err)
  console.log(`Revert reason: ${decodedError.reason}`)
}

Decoded Error

The DecodedError object is the result of the decoded error, which contains the following properties for handling the error occurred:

PropertyValue TypeRemarks
typeErrorTypeThe type of the error. See Error Types.
reasonstring | nullThe decoded error message, or null if error is unknown or has no message.
datastring | nullThe raw data bytes returned from the contract error, or null if error is unknown or empty.
argsArrayThe parameter values of the error if exists. For custom errors, the args will always be empty if no ABI or interface is supplied for decoding.
namestring | nullThe name of the error. This can be used to identify the custom error emitted. If no ABI is supplied for custom error, this will be the selector hex. If error is RpcError, this will be the error code. null if error is EmptyError.
selectorstring | nullThe hexidecimal value of the selector. null if error is EmptyError.
signaturestring | nullThe signature of the error. null if error is EmptyError or no specified ABI for custom error.
fragmentstring | nullThe ABI fragment of the error. null if error is EmptyError or no specified ABI for custom error.

Error Types

These are the possible ErrorType that could be returned for the type property in the DecodedError object:

TypeDescription
ErrorType.EmptyErrorContract reverted without reason provided
ErrorType.RevertErrorContract reverted with reason provided
ErrorType.PanicErrorContract reverted due to a panic error
ErrorType.CustomErrorContract reverted due to a custom error
ErrorType.UserRejectErrorUser rejected the transaction
ErrorType.RpcErrorAn error from the JSON RPC
ErrorType.UnknownErrorAn unknown error was thrown

Examples

Revert/Require Errors

import { ErrorDecoder } from 'ethers-decode-error'

const errorDecoder = ErrorDecoder.create()

const WETH = new ethers.Contract('0xC02aa...756Cc2', abi, provider)
try {
  const tx = await WETH.transfer('0x0', amount)
  await tx.wait()
} catch (err) {
  const { reason } = await errorDecoder.decode(err)
  // Prints "ERC20: transfer to the zero address"
  console.log('Revert reason:', reason)
}

Panic Errors

import { ErrorDecoder } from 'ethers-decode-error'

const errorDecoder = ErrorDecoder.create()

const OverflowContract = new ethers.Contract('0x12345678', abi, provider)
try {
  const tx = await OverflowContract.add(123)
  await tx.wait()
} catch (err) {
  const { reason } = await errorDecoder.decode(err)
  // Prints "Arithmetic operation underflowed or overflowed outside of an unchecked block"
  console.log('Panic message:', reason)
}

Custom Errors

import { ErrorDecoder } from 'ethers-decode-error'
import type { DecodedError } from 'ethers-decode-error'

const abi = [
  {
    inputs: [
      {
        internalType: 'address',
        name: 'token',
        type: 'address',
      },
    ],
    name: 'InvalidSwapToken',
    type: 'error',
  },
]
const errorDecoder = ErrorDecoder.create([abi])

const MyCustomErrorContract = new ethers.Contract('0x12345678', abi, provider)
try {
  const tx = await MyCustomErrorContract.swap('0xabcd', 123)
  await tx.wait()
} catch (err) {
  const decodedError = await errorDecoder.decode(err)
  const reason = customReasonMapper(decodedError)
  // Prints "Invalid swap with token contract address 0xabcd."
  console.log('Custom error reason:', reason)
}

const customReasonMapper = ({ name, args }: DecodedError): string => {
  switch (name) {
    case 'InvalidSwapToken':
      // You can access the error parameters using their index:
      return `Invalid swap with token contract address ${args[0]}.`
      // Or, you could also access the error parameters using their names:
      return `Invalid swap with token contract address ${args['token']}.`
    default:
      return 'The transaction has reverted.'
  }
}
Custom Errors ABI and Interfaces

Although the ABI or ethers Interface object of the contract is not required when decoding normal revert errors, it is recommended to provide it if you're expecting custom errors. This is because the ABI or Interface object is needed to decode the custom error name and parameters.

💡 You can provide ABIs and Interface objects of multiple smart contracts where you expect custom errors. By doing so, you have a "universal" ErrorDecoder within your codebase capable of decoding any contract errors thrown. This decoder can then be reused throughout your code to handle any errors.

If you're expecting custom errors from multiple contracts or from external contracts called within your contract, you can provide the ABIs or interfaces of those contracts:

const myContractAbi = [...]
const externalContractAbi = [...]

// From here on, the errorDecoder is aware of all the custom errors throw from these contracts.
const errorDecoder = ErrorDecoder.create([myContractAbi, externalContractAbi])

try {...} catch (err) {
  // It's aware of errors from MyContract, ExternalContract and ExternalContract errors emitted from MyContract.
  const decodedError = await errorDecoder.decode(err)
  // ...
}

If you are using TypeChain in your project, it may be more convenient to pass the contract Interface objects directly:

// If you have the contract instances, you can access their `interface` property
const errorDecoder = ErrorDecoder.create([MyContract.interface, MySecondContract.interface])

// Otherwise, you can use the `createInterface` method from the contract factory
const errorDecoder = ErrorDecoder.create([
  MyContract__factory.createInterface(),
  MySecondContract__factory.createInterface(),
])

You can also mix both ABIs and contract Interface objects, and the library will sort out the ABIs for you. This can be useful if you just want to append adhoc ABI of external contracts so that their errors can be recognised when decoding:

const externalContractFullAbi = [...]
const anotherExternalContractErrorOnlyAbi = [{
    name: 'ExternalContractCustomError1',
    type: 'error',
}]

const errorDecoder = ErrorDecoder.create([MyContract__factory.createInterface(), externalContractFullAbi, anotherExternalContractErrorOnlyAbi])

If the ABI of a custom error is not provided, the error name will be the selector of the custom error. In that case, you can check the selector of the error name in your reason mapper to handle the error accordingly:

const customReasonMapper = ({ name, args }: DecodedError): string => {
  switch (name) {
    // For custom errors with ABI, you can check the error name directly
    case 'InvalidSwapToken':
      return `Invalid swap with token contract address ${args[0]}.`

    // For custom errors without ABI, you'll have to check the error name against the selector
    // Note that when ABI is not provided, the `args` will always be empty even if the custom error has parameters.
    case '0xec7240f7':
      return 'This is a custom error caught without its ABI provided.'

    default:
      return 'The transaction has reverted.'
  }
}

Contributing

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

Keywords

FAQs

Package last updated on 24 Jan 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc