{"_id":"@ariesgun/ethers-decode-error","name":"@ariesgun/ethers-decode-error","dist-tags":{"latest":"1.1.0"},"versions":{"1.1.0":{"name":"@ariesgun/ethers-decode-error","description":"Decode ethers.js smart contract errors into human-readable messages","version":"1.1.0","author":{"name":"superical","url":"https://github.com/superical"},"repository":{"type":"git","url":"git+https://github.com/superical/ethers-decode-error.git"},"license":"Apache-2.0","type":"module","source":"src/index.ts","exports":{"require":"./dist/index.cjs","types":"./dist/index.d.ts","default":"./dist/index.mjs"},"types":"dist/index.d.ts","main":"dist/index.cjs","module":"dist/index.mjs","umd:main":"dist/index.umd.js","engines":{"node":">= 16"},"peerDependencies":{"ethers":"^5.0.0"},"devDependencies":{"@types/jest":"~29.5","@types/node":"~18","@typescript-eslint/eslint-plugin":"~5.59","@typescript-eslint/parser":"~5.59","ethers":"^5.7.2","eslint":"~8.38","eslint-config-prettier":"~8.8","eslint-plugin-jest":"~27.2","jest":"~29.5","microbundle":"^0.15.1","prettier":"~2.8","rimraf":"~5.0","ts-api-utils":"~0.0.44","ts-jest":"~29.1","typescript":"~5.0"},"scripts":{"start":"node build/src/main.js","clean":"rimraf coverage build tmp","prebuild":"npm run lint","build":"rimraf dist && microbundle src/*.ts","build:watch":"tsc -w -p tsconfig.json","build:release":"npm run clean && tsc -p tsconfig.release.json","lint":"eslint . --ext .ts --ext .mts","test":"jest --coverage","prettier":"prettier --config .prettierrc --write .","test:watch":"jest --watch"},"keywords":["blockchain","ethers","ethereum","smart-contracts","solidity","typescript","typechain","errors","decode-error","ethers-decode-error","ethers.js","ethersjs","rpc-error","json-rpc"],"_id":"@ariesgun/ethers-decode-error@1.1.0","gitHead":"6e1af869a6bc0b0599edb939ef40cfbd52a2dfa8","bugs":{"url":"https://github.com/superical/ethers-decode-error/issues"},"homepage":"https://github.com/superical/ethers-decode-error#readme","_nodeVersion":"20.10.0","_npmVersion":"10.2.3","dist":{"integrity":"sha512-psAOh66dlP/UYVl01CS3sI1+h2tUs6UbTASHTgX1zeKNaaGqlymLerZVty+9ICuZ+jctyqv19Ofwib980F9hRA==","shasum":"4f711ffb58002a80a1aa7890493188d4a6065767","tarball":"https://registry.npmjs.org/@ariesgun/ethers-decode-error/-/ethers-decode-error-1.1.0.tgz","fileCount":38,"unpackedSize":87343,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGGYwi9Cet2SBUXHuIS7RyiE+Db2Lhw3si3UcX3zlTWuAiAlGrST/81uyUYO1zdh048M3E+Kjfg8Q6QX+zCJ/XJTvg=="}]},"_npmUser":{"name":"ariesgun","email":"aries.thio@hotmail.com"},"directories":{},"maintainers":[{"name":"ariesgun","email":"aries.thio@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ethers-decode-error_1.1.0_1729934611967_0.6974578793105177"},"_hasShrinkwrap":false}},"time":{"created":"2024-10-26T09:23:31.864Z","1.1.0":"2024-10-26T09:23:32.170Z","modified":"2024-10-26T09:23:32.444Z"},"maintainers":[{"name":"ariesgun","email":"aries.thio@hotmail.com"}],"description":"Decode ethers.js smart contract errors into human-readable messages","homepage":"https://github.com/superical/ethers-decode-error#readme","keywords":["blockchain","ethers","ethereum","smart-contracts","solidity","typescript","typechain","errors","decode-error","ethers-decode-error","ethers.js","ethersjs","rpc-error","json-rpc"],"repository":{"type":"git","url":"git+https://github.com/superical/ethers-decode-error.git"},"author":{"name":"superical","url":"https://github.com/superical"},"bugs":{"url":"https://github.com/superical/ethers-decode-error/issues"},"license":"Apache-2.0","readme":"# ethers-decode-error\n\n[![Release][gha-badge]][gha-ci] [![TypeScript version][ts-badge]][typescript-5-0]\n[![License: Apache 2.0][license-badge]][license]\n\n[gha-ci]: https://github.com/superical/ethers-decode-error/actions/workflows/release.yml\n[gha-badge]: https://github.com/superical/ethers-decode-error/actions/workflows/release.yml/badge.svg\n[ts-badge]: https://img.shields.io/badge/TypeScript-5.0-blue.svg\n[typescript-5-0]: https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/\n[license-badge]: https://img.shields.io/badge/license-Apache_2.0-blue.svg\n[license]: https://github.com/superical/ethers-decode-error/blob/main/LICENSE\n\nFor those who've grappled with extracting the actual error message or reason from the JSON RPC when a transaction fails\nor a smart contract reverts, you'll certainly appreciate how cumbersome it could at times.\n\nThis utility library can help to simplify the process for you. You simply pass in the error object, the library will\nreturn the actual error message. It works with the regular revert errors, panic errors, Metamask errors and custom\nerrors.\n\n## Installation\n\n```bash\nnpm install ethers-decode-error --save\n```\n\nYou will need to install ethers.js in your project if you have not:\n\n```bash\nnpm install ethers@^5 --save\n```\n\n## Usage\n\nTo decode an error, pass it as the first argument to the `decodeError` function. This will provide you with the\ndecoded error, allowing you to decide the best course of action from there.\n\n### Revert/Require Errors\n\n```typescript\nimport { decodeError } from 'ethers-decode-error'\n\nconst WETH = new ethers.Contract('0xC02aa...756Cc2', abi, provider)\ntry {\n  const tx = await WETH.transfer('0x0', amount)\n  await tx.wait()\n} catch (err) {\n  const { error } = decodeError(err)\n  // Prints \"ERC20: transfer to the zero address\"\n  console.log('Revert reason:', error)\n}\n```\n\n### Panic Errors\n\n```typescript\nimport { decodeError } from 'ethers-decode-error'\n\nconst MyWeirdContract = new ethers.Contract('0x12345678', abi, provider)\ntry {\n  const tx = await MyWeirdContract.add(123)\n  await tx.wait()\n} catch (err) {\n  const { error } = decodeError(err)\n  // Prints \"Arithmetic operation underflowed or overflowed outside of an unchecked block\"\n  console.log('Panic message:', error)\n}\n```\n\n### Custom Errors\n\n```typescript\nimport { decodeError, DecodedError } from 'ethers-decode-error'\n\nconst abi = [\n  {\n    inputs: [\n      {\n        internalType: 'address',\n        name: 'token',\n        type: 'address',\n      },\n    ],\n    name: 'InvalidSwapToken',\n    type: 'error',\n  },\n]\n\nconst MyCustomErrorContract = new ethers.Contract('0x12345678', abi, provider)\ntry {\n  const tx = await MyCustomErrorContract.swap('0xabcd', 123)\n  await tx.wait()\n} catch (err) {\n  const decodedError = decodeError(err, abi)\n  const reason = customReasonMapper(decodedError)\n  // Prints \"Invalid swap with token contract address 0xabcd.\"\n  console.log('Custom error reason:', reason)\n}\n\nconst customReasonMapper = ({ error, args }: DecodedError): string => {\n  switch (error) {\n    case 'InvalidSwapToken':\n      return `Invalid swap with token contract address ${args[0]}.`\n    default:\n      return 'The transaction has reverted.'\n  }\n}\n```\n\nIf you're using TypeChain in your project, simply pass the contract's `Interface` to the `decodeError` function so that\nit can decode all custom errors that the contract could possibly revert with:\n\n```typescript\nconst decodedError = decodeError(err, MyCustomErrorContract__factory.createInterface())\n// Prints \"Invalid swap with token contract address 0xabcd.\"\nconsole.log('Custom error reason:', reason)\n```\n\n## Contributing\n\nFeel free to open an issue or PR for any bugs/improvements.\n","readmeFilename":"README.md"}