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 - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

7

dist/common/enums.d.ts
export declare enum ErrorType {
UserRejectError = "UserRejectError",
EmptyError = "EmptyError",
RevertError = "RevertError",
PanicError = "PanicError",
UnknownError = "UnknownError",
CustomError = "CustomError"
CustomError = "CustomError",
UserRejectError = "UserRejectError",
RpcError = "RpcError",
UnknownError = "UnknownError"
}

@@ -8,4 +8,6 @@ import { Fragment, Interface, JsonFragment } from 'ethers';

private constructor();
private getContractOrTransactionError;
private getTransactionError;
private getDataFromError;
decode(error: unknown | Error): DecodedError;
decode(error: unknown | Error): Promise<DecodedError>;
static create(errorInterfaces?: ReadonlyArray<Fragment[] | JsonFragment[] | Interface>, opts?: {

@@ -12,0 +14,0 @@ additionalErrorHandlers?: ErrorHandler[];

@@ -1,2 +0,2 @@

var r=require("ethers");function e(){return e=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},e.apply(this,arguments)}function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,t=new Array(e);n<e;n++)t[n]=r[n];return t}var t,a="0x08c379a0",o="0x4e487b71";!function(r){r.UserRejectError="UserRejectError",r.EmptyError="EmptyError",r.RevertError="RevertError",r.PanicError="PanicError",r.UnknownError="UnknownError",r.CustomError="CustomError"}(t||(t={}));var i=function(n){var t=n.data,a=n.reason,o=n.fragment,i=n.args,s=n.selector,u=n.name,c={type:n.type,reason:null!=a?a:null,data:null!=t?t:null,fragment:null,args:null!=i?i:new r.Result,selector:null!=s?s:null,name:null!=u?u:null,signature:null};return o&&(c=e({},c,new r.ErrorDescription(o,o.selector,i))),c},s=function(r){var e=r.reason;return i({type:t.UnknownError,reason:null!=e?e:"Unknown error",data:r.data})},u=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r){return"0x"===r},e.handle=function(r){return i({type:t.EmptyError,data:{data:r}.data})},r}(),c=/*#__PURE__*/function(){function e(){}var n=e.prototype;return n.predicate=function(r){return r.startsWith(a)},n.handle=function(e){var n,a=e.slice(10),o=new r.AbiCoder;try{var u=r.ErrorFragment.from("Error(string)"),c=o.decode(u.inputs,"0x"+a);return i({type:t.RevertError,reason:(n={data:e,fragment:u,reason:c[0],args:c}).reason,data:n.data,fragment:n.fragment,args:n.args})}catch(r){return s({reason:"Unknown error returned",data:e})}},e}(),l=/*#__PURE__*/function(){function e(){}var n=e.prototype;return n.predicate=function(r){return r.startsWith(o)},n.handle=function(e){var n,a=e.slice(10),o=new r.AbiCoder;try{var u,c=r.ErrorFragment.from("Panic(uint256)"),l=o.decode(c.inputs,"0x"+a),d=null!=(u=function(r){switch(r){case 0x0n:return"Generic compiler inserted panic";case 0x1n:return"Assertion error";case 0x11n:return"Arithmetic operation underflowed or overflowed outside of an unchecked block";case 0x12n:return"Division or modulo division by zero";case 0x21n:return"Tried to convert a value into an enum, but the value was too big or negative";case 0x22n:return"Incorrectly encoded storage byte array";case 0x31n:return".pop() was called on an empty array";case 0x32n:return"Array accessed at an out-of-bounds or negative index";case 0x41n:return"Too much memory was allocated, or an array was created that is too large";case 0x51n:return"Called a zero-initialized variable of internal function type"}}(l[0]))?u:"Unknown panic code";return i({type:t.PanicError,reason:(n={data:e,fragment:c,reason:d,args:l}).reason,data:n.data,args:n.args})}catch(r){return s({reason:"Unknown panic error",data:e})}},e}(),d=/*#__PURE__*/function(){function r(){}var n=r.prototype;return n.predicate=function(r){return"0x"!==r&&!r.startsWith(a)&&!r.startsWith(o)},n.handle=function(r,n){var a={data:r};if(n){var o=n.parseError(r);o&&(a=e({},a,{fragment:o.fragment,reason:o.name,args:o.args}))}return function(r){var e=r.data,n=r.reason,a=r.fragment,o=r.args,s=e.slice(0,10);return i({type:t.CustomError,reason:null!=n?n:"No ABI for custom error "+s,data:e,fragment:a,args:o,selector:s,name:s})}(a)},r}();exports.ErrorDecoder=/*#__PURE__*/function(){function e(r,e){this.errorInterface=void 0,this.errorHandlers=[],this.errorInterface=e,this.errorHandlers=r.map(function(r){return{predicate:r.predicate,handler:r.handle}})}var a=e.prototype;return a.getDataFromError=function(r){var e,n,t=null!=(e=r.data)?e:null==(n=r.error)?void 0:n.data;if(void 0===t)throw r;var a="string"==typeof t?t:t.data;if("object"==typeof a&&a.data&&(a=a.data),void 0===a||"string"!=typeof a)throw r;return a},a.decode=function(r){var e,a,o,u,c,l;if(!(r instanceof Error))return s({data:void 0,reason:null!=(a=r.message)?a:"Unexpected error"});try{o=this.getDataFromError(r)}catch(e){return r.message?r.message.includes("rejected transaction")?i({type:t.UserRejectError,reason:null!=(l=(u={data:null,reason:r.message}).reason)?l:"User has rejected the transaction",data:void 0===(c=u.data)?null:c}):s({data:null,reason:r.message}):s({data:null})}for(var d,f=function(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return n(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(r,e):void 0}}(r))){t&&(r=t);var a=0;return function(){return a>=r.length?{done:!0}:{done:!1,value:r[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(this.errorHandlers);!(d=f()).done;){var p=d.value,m=p.handler;if((0,p.predicate)(o))return m(o,this.errorInterface)}return s({data:o,reason:null!=(e=r.message)?e:"Unrecognised error"})},e.create=function(n,t){void 0===t&&(t={});var a,o=t.additionalErrorHandlers;if(n){var i=n.flatMap(function(e){return e instanceof r.Interface?e.fragments.filter(function(e){return r.ErrorFragment.isFragment(e)}):e.filter(function(e){return"error"===e.type||r.ErrorFragment.isFragment(e)})});a=new r.Interface(i)}return new e([new u,new c,new l,new d].concat(null!=o?o:[]),a)},e}();
var r=require("ethers");function e(){return e=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},e.apply(this,arguments)}function n(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,t=new Array(e);n<e;n++)t[n]=r[n];return t}var t,a="0x08c379a0",o="0x4e487b71";!function(r){r.EmptyError="EmptyError",r.RevertError="RevertError",r.PanicError="PanicError",r.CustomError="CustomError",r.UserRejectError="UserRejectError",r.RpcError="RpcError",r.UnknownError="UnknownError"}(t||(t={}));var i=function(r,e){return r&&""!==r.trim()?r:e},u=function(n){var t=n.data,a=n.fragment,o=n.args,u=n.selector,c=n.name,s={type:n.type,reason:i(n.reason,null),data:null!=t?t:null,fragment:null,args:null!=o?o:new r.Result,selector:null!=u?u:null,name:null!=c?c:null,signature:null};return a&&(s=e({},s,new r.ErrorDescription(a,a.selector,o))),s},c=function(r){var e=r.data,n=r.name;return u({type:t.UnknownError,reason:i(r.reason,"Unknown error"),data:e,name:n})},s=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r){return"0x"===r},e.handle=function(r){return u({type:t.EmptyError,data:{data:r}.data})},r}(),l=/*#__PURE__*/function(){function e(){}var n=e.prototype;return n.predicate=function(r){return null==r?void 0:r.startsWith(a)},n.handle=function(e){var n,a=e.slice(10),o=new r.AbiCoder;try{var i=r.ErrorFragment.from("Error(string)"),s=o.decode(i.inputs,"0x"+a);return u({type:t.RevertError,reason:(n={data:e,fragment:i,reason:s[0],args:s}).reason,data:n.data,fragment:n.fragment,args:n.args})}catch(r){return c({reason:"Unknown error returned",data:e})}},e}(),d=/*#__PURE__*/function(){function e(){}var n=e.prototype;return n.predicate=function(r){return null==r?void 0:r.startsWith(o)},n.handle=function(e){var n,a=e.slice(10),o=new r.AbiCoder;try{var i,s=r.ErrorFragment.from("Panic(uint256)"),l=o.decode(s.inputs,"0x"+a),d=null!=(i=function(r){switch(r){case 0x0n:return"Generic compiler inserted panic";case 0x1n:return"Assertion error";case 0x11n:return"Arithmetic operation underflowed or overflowed outside of an unchecked block";case 0x12n:return"Division or modulo division by zero";case 0x21n:return"Tried to convert a value into an enum, but the value was too big or negative";case 0x22n:return"Incorrectly encoded storage byte array";case 0x31n:return".pop() was called on an empty array";case 0x32n:return"Array accessed at an out-of-bounds or negative index";case 0x41n:return"Too much memory was allocated, or an array was created that is too large";case 0x51n:return"Called a zero-initialized variable of internal function type"}}(l[0]))?i:"Unknown panic code";return u({type:t.PanicError,reason:(n={data:e,fragment:s,reason:d,args:l}).reason,data:n.data,args:n.args})}catch(r){return c({reason:"Unknown panic error",data:e})}},e}(),f=/*#__PURE__*/function(){function r(){}var n=r.prototype;return n.predicate=function(r){return r&&"0x"!==r&&!(null!=r&&r.startsWith(a))&&!(null!=r&&r.startsWith(o))},n.handle=function(r,n){var a=n.errorInterface,o={data:r};if(a){var c=a.parseError(r);c&&(o=e({},o,{fragment:c.fragment,reason:c.name,args:c.args}))}return function(r){var e=r.data,n=r.reason,a=r.fragment,o=r.args,c=e.slice(0,10);return u({type:t.CustomError,reason:i(n,"No ABI for custom error "+c),data:e,fragment:a,args:o,selector:c,name:c})}(o)},r}(),v=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r,e){var n;return!r&&(null==e||null==(n=e.message)?void 0:n.includes("rejected transaction"))},e.handle=function(r,e){var n,a,o,c;return c=void 0===(o=(a={data:null,reason:null!=(n=e.error.message)?n:"The transaction was rejected"}).data)?null:o,u({type:t.UserRejectError,reason:i(a.reason,"User has rejected the transaction"),data:c})},r}(),m=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r,e){var n;return!r&&e.message&&!(null!=e&&null!=(n=e.message)&&n.includes("rejected transaction"))&&void 0!==e.code},e.handle=function(r,e){var n,a,o,c=e.error;return o=(n={data:null,name:c.code,reason:c.message}).name,u({type:t.RpcError,reason:i(n.reason,"Error from JSON RPC provider"),data:null,name:null!=(a=null==o?void 0:o.toString())?a:null})},r}();exports.ErrorDecoder=/*#__PURE__*/function(){function t(r,e){this.errorInterface=void 0,this.errorHandlers=[],this.errorInterface=e,this.errorHandlers=r.map(function(r){return{predicate:r.predicate,handle:r.handle}})}var a=t.prototype;return a.getContractOrTransactionError=function(r){try{var e=r.receipt;return e?Promise.resolve(this.getTransactionError(e)).then(function(e){return e||r}):Promise.resolve(r)}catch(r){return Promise.reject(r)}},a.getTransactionError=function(r){try{if(!r||0!==r.status)return Promise.resolve(void 0);var n=r.provider;return Promise.resolve(n.getTransaction(r.hash)).then(function(r){return function(t,a){try{var o=Promise.resolve(n.call(e({},r,{maxFeePerGas:void 0,maxPriorityFeePerGas:void 0}))).then(function(){return null})}catch(r){return r}return o&&o.then?o.then(void 0,function(r){return r}):o}()})}catch(r){return Promise.reject(r)}},a.getDataFromError=function(r){var e,n,t=null!=(e=r.data)?e:null==(n=r.error)?void 0:n.data;if(void 0!==t){var a="string"==typeof t?t:t.data;if("object"==typeof a&&a.data&&(a=a.data),void 0!==a&&"string"==typeof a)return a}},a.decode=function(r){try{var e,t=this;return r instanceof Error?Promise.resolve(t.getContractOrTransactionError(r)).then(function(r){for(var e,a,o=t.getDataFromError(r),i=function(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return n(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(r,e):void 0}}(r))){t&&(r=t);var a=0;return function(){return a>=r.length?{done:!0}:{done:!1,value:r[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(t.errorHandlers);!(a=i()).done;){var u=a.value,s=u.handle;if((0,u.predicate)(o,r))return s(o,{errorInterface:t.errorInterface,error:r})}return c({data:o,reason:null!=(e=null==r?void 0:r.message)?e:"Unexpected error",name:null==r?void 0:r.name})}):Promise.resolve(c({data:void 0,reason:null!=(e=r.message)?e:"Invalid error"}))}catch(r){return Promise.reject(r)}},t.create=function(e,n){void 0===n&&(n={});var a,o=n.additionalErrorHandlers;if(e){var i=e.flatMap(function(e){return e instanceof r.Interface?e.fragments.filter(function(e){return r.ErrorFragment.isFragment(e)}):e.filter(function(e){return"error"===e.type||r.ErrorFragment.isFragment(e)})});a=new r.Interface(i)}return new t([new s,new l,new d,new f,new v,new m].concat(null!=o?o:[]),a)},t}();
//# sourceMappingURL=error-decoder.js.map

@@ -1,2 +0,2 @@

!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("ethers")):"function"==typeof define&&define.amd?define(["exports","ethers"],e):e((r||self).ethersDecodeError={},r.ethers)}(this,function(r,e){function n(){return n=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},n.apply(this,arguments)}function t(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,t=new Array(e);n<e;n++)t[n]=r[n];return t}var a,o="0x08c379a0",i="0x4e487b71";!function(r){r.UserRejectError="UserRejectError",r.EmptyError="EmptyError",r.RevertError="RevertError",r.PanicError="PanicError",r.UnknownError="UnknownError",r.CustomError="CustomError"}(a||(a={}));var s=function(r){var t=r.data,a=r.reason,o=r.fragment,i=r.args,s=r.selector,u=r.name,c={type:r.type,reason:null!=a?a:null,data:null!=t?t:null,fragment:null,args:null!=i?i:new e.Result,selector:null!=s?s:null,name:null!=u?u:null,signature:null};return o&&(c=n({},c,new e.ErrorDescription(o,o.selector,i))),c},u=function(r){var e=r.reason;return s({type:a.UnknownError,reason:null!=e?e:"Unknown error",data:r.data})},c=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r){return"0x"===r},e.handle=function(r){return s({type:a.EmptyError,data:{data:r}.data})},r}(),l=/*#__PURE__*/function(){function r(){}var n=r.prototype;return n.predicate=function(r){return r.startsWith(o)},n.handle=function(r){var n,t=r.slice(10),o=new e.AbiCoder;try{var i=e.ErrorFragment.from("Error(string)"),c=o.decode(i.inputs,"0x"+t);return s({type:a.RevertError,reason:(n={data:r,fragment:i,reason:c[0],args:c}).reason,data:n.data,fragment:n.fragment,args:n.args})}catch(e){return u({reason:"Unknown error returned",data:r})}},r}(),d=/*#__PURE__*/function(){function r(){}var n=r.prototype;return n.predicate=function(r){return r.startsWith(i)},n.handle=function(r){var n,t=r.slice(10),o=new e.AbiCoder;try{var i,c=e.ErrorFragment.from("Panic(uint256)"),l=o.decode(c.inputs,"0x"+t),d=null!=(i=function(r){switch(r){case 0x0n:return"Generic compiler inserted panic";case 0x1n:return"Assertion error";case 0x11n:return"Arithmetic operation underflowed or overflowed outside of an unchecked block";case 0x12n:return"Division or modulo division by zero";case 0x21n:return"Tried to convert a value into an enum, but the value was too big or negative";case 0x22n:return"Incorrectly encoded storage byte array";case 0x31n:return".pop() was called on an empty array";case 0x32n:return"Array accessed at an out-of-bounds or negative index";case 0x41n:return"Too much memory was allocated, or an array was created that is too large";case 0x51n:return"Called a zero-initialized variable of internal function type"}}(l[0]))?i:"Unknown panic code";return s({type:a.PanicError,reason:(n={data:r,fragment:c,reason:d,args:l}).reason,data:n.data,args:n.args})}catch(e){return u({reason:"Unknown panic error",data:r})}},r}(),f=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r){return"0x"!==r&&!r.startsWith(o)&&!r.startsWith(i)},e.handle=function(r,e){var t={data:r};if(e){var o=e.parseError(r);o&&(t=n({},t,{fragment:o.fragment,reason:o.name,args:o.args}))}return function(r){var e=r.data,n=r.reason,t=r.fragment,o=r.args,i=e.slice(0,10);return s({type:a.CustomError,reason:null!=n?n:"No ABI for custom error "+i,data:e,fragment:t,args:o,selector:i,name:i})}(t)},r}();r.ErrorDecoder=/*#__PURE__*/function(){function r(r,e){this.errorInterface=void 0,this.errorHandlers=[],this.errorInterface=e,this.errorHandlers=r.map(function(r){return{predicate:r.predicate,handler:r.handle}})}var n=r.prototype;return n.getDataFromError=function(r){var e,n,t=null!=(e=r.data)?e:null==(n=r.error)?void 0:n.data;if(void 0===t)throw r;var a="string"==typeof t?t:t.data;if("object"==typeof a&&a.data&&(a=a.data),void 0===a||"string"!=typeof a)throw r;return a},n.decode=function(r){var e,n,o,i,c,l;if(!(r instanceof Error))return u({data:void 0,reason:null!=(n=r.message)?n:"Unexpected error"});try{o=this.getDataFromError(r)}catch(e){return r.message?r.message.includes("rejected transaction")?s({type:a.UserRejectError,reason:null!=(l=(i={data:null,reason:r.message}).reason)?l:"User has rejected the transaction",data:void 0===(c=i.data)?null:c}):u({data:null,reason:r.message}):u({data:null})}for(var d,f=function(r,e){var n="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(n)return(n=n.call(r)).next.bind(n);if(Array.isArray(r)||(n=function(r,e){if(r){if("string"==typeof r)return t(r,e);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(r,e):void 0}}(r))){n&&(r=n);var a=0;return function(){return a>=r.length?{done:!0}:{done:!1,value:r[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(this.errorHandlers);!(d=f()).done;){var p=d.value,m=p.handler;if((0,p.predicate)(o))return m(o,this.errorInterface)}return u({data:o,reason:null!=(e=r.message)?e:"Unrecognised error"})},r.create=function(n,t){void 0===t&&(t={});var a,o=t.additionalErrorHandlers;if(n){var i=n.flatMap(function(r){return r instanceof e.Interface?r.fragments.filter(function(r){return e.ErrorFragment.isFragment(r)}):r.filter(function(r){return"error"===r.type||e.ErrorFragment.isFragment(r)})});a=new e.Interface(i)}return new r([new c,new l,new d,new f].concat(null!=o?o:[]),a)},r}()});
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("ethers")):"function"==typeof define&&define.amd?define(["exports","ethers"],e):e((r||self).ethersDecodeError={},r.ethers)}(this,function(r,e){function n(){return n=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},n.apply(this,arguments)}function t(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,t=new Array(e);n<e;n++)t[n]=r[n];return t}var a,o="0x08c379a0",i="0x4e487b71";!function(r){r.EmptyError="EmptyError",r.RevertError="RevertError",r.PanicError="PanicError",r.CustomError="CustomError",r.UserRejectError="UserRejectError",r.RpcError="RpcError",r.UnknownError="UnknownError"}(a||(a={}));var u=function(r,e){return r&&""!==r.trim()?r:e},c=function(r){var t=r.data,a=r.fragment,o=r.args,i=r.selector,c=r.name,s={type:r.type,reason:u(r.reason,null),data:null!=t?t:null,fragment:null,args:null!=o?o:new e.Result,selector:null!=i?i:null,name:null!=c?c:null,signature:null};return a&&(s=n({},s,new e.ErrorDescription(a,a.selector,o))),s},s=function(r){var e=r.data,n=r.name;return c({type:a.UnknownError,reason:u(r.reason,"Unknown error"),data:e,name:n})},l=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r){return"0x"===r},e.handle=function(r){return c({type:a.EmptyError,data:{data:r}.data})},r}(),d=/*#__PURE__*/function(){function r(){}var n=r.prototype;return n.predicate=function(r){return null==r?void 0:r.startsWith(o)},n.handle=function(r){var n,t=r.slice(10),o=new e.AbiCoder;try{var i=e.ErrorFragment.from("Error(string)"),u=o.decode(i.inputs,"0x"+t);return c({type:a.RevertError,reason:(n={data:r,fragment:i,reason:u[0],args:u}).reason,data:n.data,fragment:n.fragment,args:n.args})}catch(e){return s({reason:"Unknown error returned",data:r})}},r}(),f=/*#__PURE__*/function(){function r(){}var n=r.prototype;return n.predicate=function(r){return null==r?void 0:r.startsWith(i)},n.handle=function(r){var n,t=r.slice(10),o=new e.AbiCoder;try{var i,u=e.ErrorFragment.from("Panic(uint256)"),l=o.decode(u.inputs,"0x"+t),d=null!=(i=function(r){switch(r){case 0x0n:return"Generic compiler inserted panic";case 0x1n:return"Assertion error";case 0x11n:return"Arithmetic operation underflowed or overflowed outside of an unchecked block";case 0x12n:return"Division or modulo division by zero";case 0x21n:return"Tried to convert a value into an enum, but the value was too big or negative";case 0x22n:return"Incorrectly encoded storage byte array";case 0x31n:return".pop() was called on an empty array";case 0x32n:return"Array accessed at an out-of-bounds or negative index";case 0x41n:return"Too much memory was allocated, or an array was created that is too large";case 0x51n:return"Called a zero-initialized variable of internal function type"}}(l[0]))?i:"Unknown panic code";return c({type:a.PanicError,reason:(n={data:r,fragment:u,reason:d,args:l}).reason,data:n.data,args:n.args})}catch(e){return s({reason:"Unknown panic error",data:r})}},r}(),v=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r){return r&&"0x"!==r&&!(null!=r&&r.startsWith(o))&&!(null!=r&&r.startsWith(i))},e.handle=function(r,e){var t=e.errorInterface,o={data:r};if(t){var i=t.parseError(r);i&&(o=n({},o,{fragment:i.fragment,reason:i.name,args:i.args}))}return function(r){var e=r.data,n=r.reason,t=r.fragment,o=r.args,i=e.slice(0,10);return c({type:a.CustomError,reason:u(n,"No ABI for custom error "+i),data:e,fragment:t,args:o,selector:i,name:i})}(o)},r}(),m=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r,e){var n;return!r&&(null==e||null==(n=e.message)?void 0:n.includes("rejected transaction"))},e.handle=function(r,e){var n,t,o,i;return i=void 0===(o=(t={data:null,reason:null!=(n=e.error.message)?n:"The transaction was rejected"}).data)?null:o,c({type:a.UserRejectError,reason:u(t.reason,"User has rejected the transaction"),data:i})},r}(),p=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r,e){var n;return!r&&e.message&&!(null!=e&&null!=(n=e.message)&&n.includes("rejected transaction"))&&void 0!==e.code},e.handle=function(r,e){var n,t,o,i=e.error;return o=(n={data:null,name:i.code,reason:i.message}).name,c({type:a.RpcError,reason:u(n.reason,"Error from JSON RPC provider"),data:null,name:null!=(t=null==o?void 0:o.toString())?t:null})},r}();r.ErrorDecoder=/*#__PURE__*/function(){function r(r,e){this.errorInterface=void 0,this.errorHandlers=[],this.errorInterface=e,this.errorHandlers=r.map(function(r){return{predicate:r.predicate,handle:r.handle}})}var a=r.prototype;return a.getContractOrTransactionError=function(r){try{var e=r.receipt;return e?Promise.resolve(this.getTransactionError(e)).then(function(e){return e||r}):Promise.resolve(r)}catch(r){return Promise.reject(r)}},a.getTransactionError=function(r){try{if(!r||0!==r.status)return Promise.resolve(void 0);var e=r.provider;return Promise.resolve(e.getTransaction(r.hash)).then(function(r){return function(t,a){try{var o=Promise.resolve(e.call(n({},r,{maxFeePerGas:void 0,maxPriorityFeePerGas:void 0}))).then(function(){return null})}catch(r){return r}return o&&o.then?o.then(void 0,function(r){return r}):o}()})}catch(r){return Promise.reject(r)}},a.getDataFromError=function(r){var e,n,t=null!=(e=r.data)?e:null==(n=r.error)?void 0:n.data;if(void 0!==t){var a="string"==typeof t?t:t.data;if("object"==typeof a&&a.data&&(a=a.data),void 0!==a&&"string"==typeof a)return a}},a.decode=function(r){try{var e,n=this;return r instanceof Error?Promise.resolve(n.getContractOrTransactionError(r)).then(function(r){for(var e,a,o=n.getDataFromError(r),i=function(r,e){var n="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(n)return(n=n.call(r)).next.bind(n);if(Array.isArray(r)||(n=function(r,e){if(r){if("string"==typeof r)return t(r,e);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(r,e):void 0}}(r))){n&&(r=n);var a=0;return function(){return a>=r.length?{done:!0}:{done:!1,value:r[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(n.errorHandlers);!(a=i()).done;){var u=a.value,c=u.handle;if((0,u.predicate)(o,r))return c(o,{errorInterface:n.errorInterface,error:r})}return s({data:o,reason:null!=(e=null==r?void 0:r.message)?e:"Unexpected error",name:null==r?void 0:r.name})}):Promise.resolve(s({data:void 0,reason:null!=(e=r.message)?e:"Invalid error"}))}catch(r){return Promise.reject(r)}},r.create=function(n,t){void 0===t&&(t={});var a,o=t.additionalErrorHandlers;if(n){var i=n.flatMap(function(r){return r instanceof e.Interface?r.fragments.filter(function(r){return e.ErrorFragment.isFragment(r)}):r.filter(function(r){return"error"===r.type||e.ErrorFragment.isFragment(r)})});a=new e.Interface(i)}return new r([new l,new d,new f,new v,new m,new p].concat(null!=o?o:[]),a)},r}()});
//# sourceMappingURL=error-decoder.umd.js.map
import { Interface } from 'ethers';
import { DecodedError } from '../types';
type ErrorHandlerErrorInfo = {
errorInterface: Interface;
error: Error;
};
export interface ErrorHandler {
predicate: (data: string) => boolean;
handle: (data: string, errorInterface?: Interface) => DecodedError;
predicate: (data: string | undefined, error: Error) => boolean;
handle: (data: string | undefined, errorInfo: ErrorHandlerErrorInfo) => DecodedError;
}

@@ -21,3 +25,12 @@ export declare class EmptyErrorHandler implements ErrorHandler {

predicate(data: string): boolean;
handle(data: string, errorInterface?: Interface): DecodedError;
handle(data: string, { errorInterface }: ErrorHandlerErrorInfo): DecodedError;
}
export declare class UserRejectionHandler implements ErrorHandler {
predicate(data: string, error: Error): boolean;
handle(_data: string, { error }: ErrorHandlerErrorInfo): DecodedError;
}
export declare class RpcErrorHandler implements ErrorHandler {
predicate(data: string, error: Error): boolean;
handle(_data: string, { error }: ErrorHandlerErrorInfo): DecodedError;
}
export {};

@@ -18,2 +18,3 @@ import { ErrorFragment, Result } from 'ethers';

export declare const customErrorResult: ErrorResultFormatter;
export declare const rpcErrorResult: ErrorResultFormatter;
export {};

@@ -1,2 +0,2 @@

var r,e=require("ethers");function n(){return n=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},n.apply(this,arguments)}function t(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,t=new Array(e);n<e;n++)t[n]=r[n];return t}exports.ErrorType=void 0,(r=exports.ErrorType||(exports.ErrorType={})).UserRejectError="UserRejectError",r.EmptyError="EmptyError",r.RevertError="RevertError",r.PanicError="PanicError",r.UnknownError="UnknownError",r.CustomError="CustomError";var a="0x08c379a0",o="0x4e487b71",i=function(r){var t=r.data,a=r.reason,o=r.fragment,i=r.args,s=r.selector,u=r.name,c={type:r.type,reason:null!=a?a:null,data:null!=t?t:null,fragment:null,args:null!=i?i:new e.Result,selector:null!=s?s:null,name:null!=u?u:null,signature:null};return o&&(c=n({},c,new e.ErrorDescription(o,o.selector,i))),c},s=function(r){var e=r.reason;return i({type:exports.ErrorType.UnknownError,reason:null!=e?e:"Unknown error",data:r.data})},u=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r){return"0x"===r},e.handle=function(r){return i({type:exports.ErrorType.EmptyError,data:{data:r}.data})},r}(),c=/*#__PURE__*/function(){function r(){}var n=r.prototype;return n.predicate=function(r){return r.startsWith(a)},n.handle=function(r){var n,t=r.slice(10),a=new e.AbiCoder;try{var o=e.ErrorFragment.from("Error(string)"),u=a.decode(o.inputs,"0x"+t);return n={data:r,fragment:o,reason:u[0],args:u},i({type:exports.ErrorType.RevertError,reason:n.reason,data:n.data,fragment:n.fragment,args:n.args})}catch(e){return s({reason:"Unknown error returned",data:r})}},r}(),l=/*#__PURE__*/function(){function r(){}var n=r.prototype;return n.predicate=function(r){return r.startsWith(o)},n.handle=function(r){var n,t=r.slice(10),a=new e.AbiCoder;try{var o,u=e.ErrorFragment.from("Panic(uint256)"),c=a.decode(u.inputs,"0x"+t);return n={data:r,fragment:u,reason:null!=(o=function(r){switch(r){case 0x0n:return"Generic compiler inserted panic";case 0x1n:return"Assertion error";case 0x11n:return"Arithmetic operation underflowed or overflowed outside of an unchecked block";case 0x12n:return"Division or modulo division by zero";case 0x21n:return"Tried to convert a value into an enum, but the value was too big or negative";case 0x22n:return"Incorrectly encoded storage byte array";case 0x31n:return".pop() was called on an empty array";case 0x32n:return"Array accessed at an out-of-bounds or negative index";case 0x41n:return"Too much memory was allocated, or an array was created that is too large";case 0x51n:return"Called a zero-initialized variable of internal function type"}}(c[0]))?o:"Unknown panic code",args:c},i({type:exports.ErrorType.PanicError,reason:n.reason,data:n.data,args:n.args})}catch(e){return s({reason:"Unknown panic error",data:r})}},r}(),d=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r){return"0x"!==r&&!r.startsWith(a)&&!r.startsWith(o)},e.handle=function(r,e){var t={data:r};if(e){var a=e.parseError(r);a&&(t=n({},t,{fragment:a.fragment,reason:a.name,args:a.args}))}return function(r){var e=r.data,n=r.reason,t=r.fragment,a=r.args,o=e.slice(0,10);return i({type:exports.ErrorType.CustomError,reason:null!=n?n:"No ABI for custom error "+o,data:e,fragment:t,args:a,selector:o,name:o})}(t)},r}();exports.ErrorDecoder=/*#__PURE__*/function(){function r(r,e){this.errorInterface=void 0,this.errorHandlers=[],this.errorInterface=e,this.errorHandlers=r.map(function(r){return{predicate:r.predicate,handler:r.handle}})}var n=r.prototype;return n.getDataFromError=function(r){var e,n,t=null!=(e=r.data)?e:null==(n=r.error)?void 0:n.data;if(void 0===t)throw r;var a="string"==typeof t?t:t.data;if("object"==typeof a&&a.data&&(a=a.data),void 0===a||"string"!=typeof a)throw r;return a},n.decode=function(r){var e,n,a,o,u,c;if(!(r instanceof Error))return s({data:void 0,reason:null!=(n=r.message)?n:"Unexpected error"});try{a=this.getDataFromError(r)}catch(e){return r.message?r.message.includes("rejected transaction")?(u=(o={data:null,reason:r.message}).data,c=o.reason,i({type:exports.ErrorType.UserRejectError,reason:null!=c?c:"User has rejected the transaction",data:void 0===u?null:u})):s({data:null,reason:r.message}):s({data:null})}for(var l,d=function(r,e){var n="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(n)return(n=n.call(r)).next.bind(n);if(Array.isArray(r)||(n=function(r,e){if(r){if("string"==typeof r)return t(r,e);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(r,e):void 0}}(r))){n&&(r=n);var a=0;return function(){return a>=r.length?{done:!0}:{done:!1,value:r[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(this.errorHandlers);!(l=d()).done;){var f=l.value,p=f.handler;if((0,f.predicate)(a))return p(a,this.errorInterface)}return s({data:a,reason:null!=(e=r.message)?e:"Unrecognised error"})},r.create=function(n,t){void 0===t&&(t={});var a,o=t.additionalErrorHandlers;if(n){var i=n.flatMap(function(r){return r instanceof e.Interface?r.fragments.filter(function(r){return e.ErrorFragment.isFragment(r)}):r.filter(function(r){return"error"===r.type||e.ErrorFragment.isFragment(r)})});a=new e.Interface(i)}return new r([new u,new c,new l,new d].concat(null!=o?o:[]),a)},r}();
var r,e=require("ethers");function n(){return n=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},n.apply(this,arguments)}function t(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,t=new Array(e);n<e;n++)t[n]=r[n];return t}exports.ErrorType=void 0,(r=exports.ErrorType||(exports.ErrorType={})).EmptyError="EmptyError",r.RevertError="RevertError",r.PanicError="PanicError",r.CustomError="CustomError",r.UserRejectError="UserRejectError",r.RpcError="RpcError",r.UnknownError="UnknownError";var a="0x08c379a0",o="0x4e487b71",i=function(r,e){return r&&""!==r.trim()?r:e},s=function(r){var t=r.data,a=r.fragment,o=r.args,s=r.selector,u=r.name,c={type:r.type,reason:i(r.reason,null),data:null!=t?t:null,fragment:null,args:null!=o?o:new e.Result,selector:null!=s?s:null,name:null!=u?u:null,signature:null};return a&&(c=n({},c,new e.ErrorDescription(a,a.selector,o))),c},u=function(r){var e=r.data,n=r.name;return s({type:exports.ErrorType.UnknownError,reason:i(r.reason,"Unknown error"),data:e,name:n})},c=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r){return"0x"===r},e.handle=function(r){return s({type:exports.ErrorType.EmptyError,data:{data:r}.data})},r}(),l=/*#__PURE__*/function(){function r(){}var n=r.prototype;return n.predicate=function(r){return null==r?void 0:r.startsWith(a)},n.handle=function(r){var n,t=r.slice(10),a=new e.AbiCoder;try{var o=e.ErrorFragment.from("Error(string)"),i=a.decode(o.inputs,"0x"+t);return n={data:r,fragment:o,reason:i[0],args:i},s({type:exports.ErrorType.RevertError,reason:n.reason,data:n.data,fragment:n.fragment,args:n.args})}catch(e){return u({reason:"Unknown error returned",data:r})}},r}(),d=/*#__PURE__*/function(){function r(){}var n=r.prototype;return n.predicate=function(r){return null==r?void 0:r.startsWith(o)},n.handle=function(r){var n,t=r.slice(10),a=new e.AbiCoder;try{var o,i=e.ErrorFragment.from("Panic(uint256)"),c=a.decode(i.inputs,"0x"+t);return n={data:r,fragment:i,reason:null!=(o=function(r){switch(r){case 0x0n:return"Generic compiler inserted panic";case 0x1n:return"Assertion error";case 0x11n:return"Arithmetic operation underflowed or overflowed outside of an unchecked block";case 0x12n:return"Division or modulo division by zero";case 0x21n:return"Tried to convert a value into an enum, but the value was too big or negative";case 0x22n:return"Incorrectly encoded storage byte array";case 0x31n:return".pop() was called on an empty array";case 0x32n:return"Array accessed at an out-of-bounds or negative index";case 0x41n:return"Too much memory was allocated, or an array was created that is too large";case 0x51n:return"Called a zero-initialized variable of internal function type"}}(c[0]))?o:"Unknown panic code",args:c},s({type:exports.ErrorType.PanicError,reason:n.reason,data:n.data,args:n.args})}catch(e){return u({reason:"Unknown panic error",data:r})}},r}(),f=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r){return r&&"0x"!==r&&!(null!=r&&r.startsWith(a))&&!(null!=r&&r.startsWith(o))},e.handle=function(r,e){var t=e.errorInterface,a={data:r};if(t){var o=t.parseError(r);o&&(a=n({},a,{fragment:o.fragment,reason:o.name,args:o.args}))}return function(r){var e=r.data,n=r.reason,t=r.fragment,a=r.args,o=e.slice(0,10);return s({type:exports.ErrorType.CustomError,reason:i(n,"No ABI for custom error "+o),data:e,fragment:t,args:a,selector:o,name:o})}(a)},r}(),p=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r,e){var n;return!r&&(null==e||null==(n=e.message)?void 0:n.includes("rejected transaction"))},e.handle=function(r,e){var n,t,a,o;return o=void 0===(a=(t={data:null,reason:null!=(n=e.error.message)?n:"The transaction was rejected"}).data)?null:a,s({type:exports.ErrorType.UserRejectError,reason:i(t.reason,"User has rejected the transaction"),data:o})},r}(),v=/*#__PURE__*/function(){function r(){}var e=r.prototype;return e.predicate=function(r,e){var n;return!r&&e.message&&!(null!=e&&null!=(n=e.message)&&n.includes("rejected transaction"))&&void 0!==e.code},e.handle=function(r,e){var n,t,a,o=e.error;return a=(n={data:null,name:o.code,reason:o.message}).name,s({type:exports.ErrorType.RpcError,reason:i(n.reason,"Error from JSON RPC provider"),data:null,name:null!=(t=null==a?void 0:a.toString())?t:null})},r}();exports.ErrorDecoder=/*#__PURE__*/function(){function r(r,e){this.errorInterface=void 0,this.errorHandlers=[],this.errorInterface=e,this.errorHandlers=r.map(function(r){return{predicate:r.predicate,handle:r.handle}})}var a=r.prototype;return a.getContractOrTransactionError=function(r){try{var e=r.receipt;return e?Promise.resolve(this.getTransactionError(e)).then(function(e){return e||r}):Promise.resolve(r)}catch(r){return Promise.reject(r)}},a.getTransactionError=function(r){try{if(!r||0!==r.status)return Promise.resolve(void 0);var e=r.provider;return Promise.resolve(e.getTransaction(r.hash)).then(function(r){return function(t,a){try{var o=Promise.resolve(e.call(n({},r,{maxFeePerGas:void 0,maxPriorityFeePerGas:void 0}))).then(function(){return null})}catch(r){return r}return o&&o.then?o.then(void 0,function(r){return r}):o}()})}catch(r){return Promise.reject(r)}},a.getDataFromError=function(r){var e,n,t=null!=(e=r.data)?e:null==(n=r.error)?void 0:n.data;if(void 0!==t){var a="string"==typeof t?t:t.data;if("object"==typeof a&&a.data&&(a=a.data),void 0!==a&&"string"==typeof a)return a}},a.decode=function(r){try{var e,n=this;return r instanceof Error?Promise.resolve(n.getContractOrTransactionError(r)).then(function(r){for(var e,a,o=n.getDataFromError(r),i=function(r,e){var n="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(n)return(n=n.call(r)).next.bind(n);if(Array.isArray(r)||(n=function(r,e){if(r){if("string"==typeof r)return t(r,e);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(r,e):void 0}}(r))){n&&(r=n);var a=0;return function(){return a>=r.length?{done:!0}:{done:!1,value:r[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(n.errorHandlers);!(a=i()).done;){var s=a.value,c=s.handle;if((0,s.predicate)(o,r))return c(o,{errorInterface:n.errorInterface,error:r})}return u({data:o,reason:null!=(e=null==r?void 0:r.message)?e:"Unexpected error",name:null==r?void 0:r.name})}):Promise.resolve(u({data:void 0,reason:null!=(e=r.message)?e:"Invalid error"}))}catch(r){return Promise.reject(r)}},r.create=function(n,t){void 0===t&&(t={});var a,o=t.additionalErrorHandlers;if(n){var i=n.flatMap(function(r){return r instanceof e.Interface?r.fragments.filter(function(r){return e.ErrorFragment.isFragment(r)}):r.filter(function(r){return"error"===r.type||e.ErrorFragment.isFragment(r)})});a=new e.Interface(i)}return new r([new c,new l,new d,new f,new p,new v].concat(null!=o?o:[]),a)},r}();
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("ethers")):"function"==typeof define&&define.amd?define(["exports","ethers"],e):e((r||self).ethersDecodeError={},r.ethers)}(this,function(r,e){var n;function t(){return t=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},t.apply(this,arguments)}function a(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,t=new Array(e);n<e;n++)t[n]=r[n];return t}r.ErrorType=void 0,(n=r.ErrorType||(r.ErrorType={})).UserRejectError="UserRejectError",n.EmptyError="EmptyError",n.RevertError="RevertError",n.PanicError="PanicError",n.UnknownError="UnknownError",n.CustomError="CustomError";var o="0x08c379a0",i="0x4e487b71",s=function(r){var n=r.data,a=r.reason,o=r.fragment,i=r.args,s=r.selector,u=r.name,c={type:r.type,reason:null!=a?a:null,data:null!=n?n:null,fragment:null,args:null!=i?i:new e.Result,selector:null!=s?s:null,name:null!=u?u:null,signature:null};return o&&(c=t({},c,new e.ErrorDescription(o,o.selector,i))),c},u=function(e){var n=e.reason;return s({type:r.ErrorType.UnknownError,reason:null!=n?n:"Unknown error",data:e.data})},c=/*#__PURE__*/function(){function e(){}var n=e.prototype;return n.predicate=function(r){return"0x"===r},n.handle=function(e){return s({type:r.ErrorType.EmptyError,data:{data:e}.data})},e}(),l=/*#__PURE__*/function(){function n(){}var t=n.prototype;return t.predicate=function(r){return r.startsWith(o)},t.handle=function(n){var t,a=n.slice(10),o=new e.AbiCoder;try{var i=e.ErrorFragment.from("Error(string)"),c=o.decode(i.inputs,"0x"+a);return s({type:r.ErrorType.RevertError,reason:(t={data:n,fragment:i,reason:c[0],args:c}).reason,data:t.data,fragment:t.fragment,args:t.args})}catch(r){return u({reason:"Unknown error returned",data:n})}},n}(),d=/*#__PURE__*/function(){function n(){}var t=n.prototype;return t.predicate=function(r){return r.startsWith(i)},t.handle=function(n){var t,a=n.slice(10),o=new e.AbiCoder;try{var i,c=e.ErrorFragment.from("Panic(uint256)"),l=o.decode(c.inputs,"0x"+a),d=null!=(i=function(r){switch(r){case 0x0n:return"Generic compiler inserted panic";case 0x1n:return"Assertion error";case 0x11n:return"Arithmetic operation underflowed or overflowed outside of an unchecked block";case 0x12n:return"Division or modulo division by zero";case 0x21n:return"Tried to convert a value into an enum, but the value was too big or negative";case 0x22n:return"Incorrectly encoded storage byte array";case 0x31n:return".pop() was called on an empty array";case 0x32n:return"Array accessed at an out-of-bounds or negative index";case 0x41n:return"Too much memory was allocated, or an array was created that is too large";case 0x51n:return"Called a zero-initialized variable of internal function type"}}(l[0]))?i:"Unknown panic code";return s({type:r.ErrorType.PanicError,reason:(t={data:n,fragment:c,reason:d,args:l}).reason,data:t.data,args:t.args})}catch(r){return u({reason:"Unknown panic error",data:n})}},n}(),f=/*#__PURE__*/function(){function e(){}var n=e.prototype;return n.predicate=function(r){return"0x"!==r&&!r.startsWith(o)&&!r.startsWith(i)},n.handle=function(e,n){var a={data:e};if(n){var o=n.parseError(e);o&&(a=t({},a,{fragment:o.fragment,reason:o.name,args:o.args}))}return function(e){var n=e.data,t=e.reason,a=e.fragment,o=e.args,i=n.slice(0,10);return s({type:r.ErrorType.CustomError,reason:null!=t?t:"No ABI for custom error "+i,data:n,fragment:a,args:o,selector:i,name:i})}(a)},e}();r.ErrorDecoder=/*#__PURE__*/function(){function n(r,e){this.errorInterface=void 0,this.errorHandlers=[],this.errorInterface=e,this.errorHandlers=r.map(function(r){return{predicate:r.predicate,handler:r.handle}})}var t=n.prototype;return t.getDataFromError=function(r){var e,n,t=null!=(e=r.data)?e:null==(n=r.error)?void 0:n.data;if(void 0===t)throw r;var a="string"==typeof t?t:t.data;if("object"==typeof a&&a.data&&(a=a.data),void 0===a||"string"!=typeof a)throw r;return a},t.decode=function(e){var n,t,o,i,c,l;if(!(e instanceof Error))return u({data:void 0,reason:null!=(t=e.message)?t:"Unexpected error"});try{o=this.getDataFromError(e)}catch(n){return e.message?e.message.includes("rejected transaction")?s({type:r.ErrorType.UserRejectError,reason:null!=(l=(i={data:null,reason:e.message}).reason)?l:"User has rejected the transaction",data:void 0===(c=i.data)?null:c}):u({data:null,reason:e.message}):u({data:null})}for(var d,f=function(r,e){var n="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(n)return(n=n.call(r)).next.bind(n);if(Array.isArray(r)||(n=function(r,e){if(r){if("string"==typeof r)return a(r,e);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?a(r,e):void 0}}(r))){n&&(r=n);var t=0;return function(){return t>=r.length?{done:!0}:{done:!1,value:r[t++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(this.errorHandlers);!(d=f()).done;){var p=d.value,y=p.handler;if((0,p.predicate)(o))return y(o,this.errorInterface)}return u({data:o,reason:null!=(n=e.message)?n:"Unrecognised error"})},n.create=function(r,t){void 0===t&&(t={});var a,o=t.additionalErrorHandlers;if(r){var i=r.flatMap(function(r){return r instanceof e.Interface?r.fragments.filter(function(r){return e.ErrorFragment.isFragment(r)}):r.filter(function(r){return"error"===r.type||e.ErrorFragment.isFragment(r)})});a=new e.Interface(i)}return new n([new c,new l,new d,new f].concat(null!=o?o:[]),a)},n}()});
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("ethers")):"function"==typeof define&&define.amd?define(["exports","ethers"],e):e((r||self).ethersDecodeError={},r.ethers)}(this,function(r,e){var n;function t(){return t=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},t.apply(this,arguments)}function o(r,e){(null==e||e>r.length)&&(e=r.length);for(var n=0,t=new Array(e);n<e;n++)t[n]=r[n];return t}r.ErrorType=void 0,(n=r.ErrorType||(r.ErrorType={})).EmptyError="EmptyError",n.RevertError="RevertError",n.PanicError="PanicError",n.CustomError="CustomError",n.UserRejectError="UserRejectError",n.RpcError="RpcError",n.UnknownError="UnknownError";var a="0x08c379a0",i="0x4e487b71",u=function(r,e){return r&&""!==r.trim()?r:e},c=function(r){var n=r.data,o=r.fragment,a=r.args,i=r.selector,c=r.name,s={type:r.type,reason:u(r.reason,null),data:null!=n?n:null,fragment:null,args:null!=a?a:new e.Result,selector:null!=i?i:null,name:null!=c?c:null,signature:null};return o&&(s=t({},s,new e.ErrorDescription(o,o.selector,a))),s},s=function(e){var n=e.data,t=e.name;return c({type:r.ErrorType.UnknownError,reason:u(e.reason,"Unknown error"),data:n,name:t})},l=/*#__PURE__*/function(){function e(){}var n=e.prototype;return n.predicate=function(r){return"0x"===r},n.handle=function(e){return c({type:r.ErrorType.EmptyError,data:{data:e}.data})},e}(),d=/*#__PURE__*/function(){function n(){}var t=n.prototype;return t.predicate=function(r){return null==r?void 0:r.startsWith(a)},t.handle=function(n){var t,o=n.slice(10),a=new e.AbiCoder;try{var i=e.ErrorFragment.from("Error(string)"),u=a.decode(i.inputs,"0x"+o);return c({type:r.ErrorType.RevertError,reason:(t={data:n,fragment:i,reason:u[0],args:u}).reason,data:t.data,fragment:t.fragment,args:t.args})}catch(r){return s({reason:"Unknown error returned",data:n})}},n}(),f=/*#__PURE__*/function(){function n(){}var t=n.prototype;return t.predicate=function(r){return null==r?void 0:r.startsWith(i)},t.handle=function(n){var t,o=n.slice(10),a=new e.AbiCoder;try{var i,u=e.ErrorFragment.from("Panic(uint256)"),l=a.decode(u.inputs,"0x"+o),d=null!=(i=function(r){switch(r){case 0x0n:return"Generic compiler inserted panic";case 0x1n:return"Assertion error";case 0x11n:return"Arithmetic operation underflowed or overflowed outside of an unchecked block";case 0x12n:return"Division or modulo division by zero";case 0x21n:return"Tried to convert a value into an enum, but the value was too big or negative";case 0x22n:return"Incorrectly encoded storage byte array";case 0x31n:return".pop() was called on an empty array";case 0x32n:return"Array accessed at an out-of-bounds or negative index";case 0x41n:return"Too much memory was allocated, or an array was created that is too large";case 0x51n:return"Called a zero-initialized variable of internal function type"}}(l[0]))?i:"Unknown panic code";return c({type:r.ErrorType.PanicError,reason:(t={data:n,fragment:u,reason:d,args:l}).reason,data:t.data,args:t.args})}catch(r){return s({reason:"Unknown panic error",data:n})}},n}(),p=/*#__PURE__*/function(){function e(){}var n=e.prototype;return n.predicate=function(r){return r&&"0x"!==r&&!(null!=r&&r.startsWith(a))&&!(null!=r&&r.startsWith(i))},n.handle=function(e,n){var o=n.errorInterface,a={data:e};if(o){var i=o.parseError(e);i&&(a=t({},a,{fragment:i.fragment,reason:i.name,args:i.args}))}return function(e){var n=e.data,t=e.reason,o=e.fragment,a=e.args,i=n.slice(0,10);return c({type:r.ErrorType.CustomError,reason:u(t,"No ABI for custom error "+i),data:n,fragment:o,args:a,selector:i,name:i})}(a)},e}(),v=/*#__PURE__*/function(){function e(){}var n=e.prototype;return n.predicate=function(r,e){var n;return!r&&(null==e||null==(n=e.message)?void 0:n.includes("rejected transaction"))},n.handle=function(e,n){var t,o,a,i;return i=void 0===(a=(o={data:null,reason:null!=(t=n.error.message)?t:"The transaction was rejected"}).data)?null:a,c({type:r.ErrorType.UserRejectError,reason:u(o.reason,"User has rejected the transaction"),data:i})},e}(),m=/*#__PURE__*/function(){function e(){}var n=e.prototype;return n.predicate=function(r,e){var n;return!r&&e.message&&!(null!=e&&null!=(n=e.message)&&n.includes("rejected transaction"))&&void 0!==e.code},n.handle=function(e,n){var t,o,a,i=n.error;return a=(t={data:null,name:i.code,reason:i.message}).name,c({type:r.ErrorType.RpcError,reason:u(t.reason,"Error from JSON RPC provider"),data:null,name:null!=(o=null==a?void 0:a.toString())?o:null})},e}();r.ErrorDecoder=/*#__PURE__*/function(){function r(r,e){this.errorInterface=void 0,this.errorHandlers=[],this.errorInterface=e,this.errorHandlers=r.map(function(r){return{predicate:r.predicate,handle:r.handle}})}var n=r.prototype;return n.getContractOrTransactionError=function(r){try{var e=r.receipt;return e?Promise.resolve(this.getTransactionError(e)).then(function(e){return e||r}):Promise.resolve(r)}catch(r){return Promise.reject(r)}},n.getTransactionError=function(r){try{if(!r||0!==r.status)return Promise.resolve(void 0);var e=r.provider;return Promise.resolve(e.getTransaction(r.hash)).then(function(r){return function(n,o){try{var a=Promise.resolve(e.call(t({},r,{maxFeePerGas:void 0,maxPriorityFeePerGas:void 0}))).then(function(){return null})}catch(r){return r}return a&&a.then?a.then(void 0,function(r){return r}):a}()})}catch(r){return Promise.reject(r)}},n.getDataFromError=function(r){var e,n,t=null!=(e=r.data)?e:null==(n=r.error)?void 0:n.data;if(void 0!==t){var o="string"==typeof t?t:t.data;if("object"==typeof o&&o.data&&(o=o.data),void 0!==o&&"string"==typeof o)return o}},n.decode=function(r){try{var e,n=this;return r instanceof Error?Promise.resolve(n.getContractOrTransactionError(r)).then(function(r){for(var e,t,a=n.getDataFromError(r),i=function(r,e){var n="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(n)return(n=n.call(r)).next.bind(n);if(Array.isArray(r)||(n=function(r,e){if(r){if("string"==typeof r)return o(r,e);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?o(r,e):void 0}}(r))){n&&(r=n);var t=0;return function(){return t>=r.length?{done:!0}:{done:!1,value:r[t++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(n.errorHandlers);!(t=i()).done;){var u=t.value,c=u.handle;if((0,u.predicate)(a,r))return c(a,{errorInterface:n.errorInterface,error:r})}return s({data:a,reason:null!=(e=null==r?void 0:r.message)?e:"Unexpected error",name:null==r?void 0:r.name})}):Promise.resolve(s({data:void 0,reason:null!=(e=r.message)?e:"Invalid error"}))}catch(r){return Promise.reject(r)}},r.create=function(n,t){void 0===t&&(t={});var o,a=t.additionalErrorHandlers;if(n){var i=n.flatMap(function(r){return r instanceof e.Interface?r.fragments.filter(function(r){return e.ErrorFragment.isFragment(r)}):r.filter(function(r){return"error"===r.type||e.ErrorFragment.isFragment(r)})});o=new e.Interface(i)}return new r([new l,new d,new f,new p,new v,new m].concat(null!=a?a:[]),o)},r}()});
//# sourceMappingURL=index.umd.js.map
{
"name": "ethers-decode-error",
"description": "Decode ethers.js smart contract errors into human-readable messages",
"version": "2.0.0",
"version": "2.1.0",
"author": {

@@ -39,2 +39,3 @@ "name": "superical",

"@types/node": "^18.19.8",
"@types/sinon": "^17.0.3",
"@typescript-eslint/eslint-plugin": "~5.59",

@@ -51,2 +52,3 @@ "@typescript-eslint/parser": "~5.59",

"semantic-release": "^23.0.0",
"sinon": "^17.0.1",
"ts-api-utils": "~0.0.44",

@@ -53,0 +55,0 @@ "ts-node": "^10.9.2",

@@ -53,3 +53,3 @@ # ethers-decode-error

} catch (err) {
const decodedError: DecodedError = errorDecoder.decode(err)
const decodedError: decodedError = await errorDecoder.decode(err)
console.log(`Revert reason: ${decodedError.reason}`)

@@ -59,15 +59,31 @@ }

The `DecodedError` object is the result of the decoded error, which contains the following properties for handling errors:
### Decoded Error
| Property | Value Type | Remarks |
| ----------- | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `type` | `ErrorType` | The type of the error. For eg, a revert due to custom error will have `ErrorType.CustomError`. |
| `reason` | `string \| null` | The decoded error message, or `null` if error is unknown or has no message. |
| `data` | `string \| null` | The raw data bytes returned from the contract error, or `null` if error is unknown or empty. |
| `args` | `Array` | The 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. |
| `name` | `string \| null` | The name of the error. This can be used to identify the custom error emitted. For eg, `InvalidTokenSwap`. If no ABI is supplied for custom error, this will be the selector hex. `null` if error is `EmptyError`. |
| `selector` | `string \| null` | The hexidecimal value of the selector. `null` if error is `EmptyError`. |
| `signature` | `string \| null` | The signature of the error. `null` if error is `EmptyError` or no specified ABI for custom error. |
| `fragment` | `string \| null` | The ABI fragment of the error. `null` if error is `EmptyError` or no specified ABI for custom error. |
The `DecodedError` object is the result of the decoded error, which contains the following properties for handling the error occurred:
| Property | Value Type | Remarks |
| ----------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `type` | `ErrorType` | The type of the error. See [Error Types](#error-types). |
| `reason` | `string \| null` | The decoded error message, or `null` if error is unknown or has no message. |
| `data` | `string \| null` | The raw data bytes returned from the contract error, or `null` if error is unknown or empty. |
| `args` | `Array` | The 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. |
| `name` | `string \| null` | The 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`. |
| `selector` | `string \| null` | The hexidecimal value of the selector. `null` if error is `EmptyError`. |
| `signature` | `string \| null` | The signature of the error. `null` if error is `EmptyError` or no specified ABI for custom error. |
| `fragment` | `string \| null` | The 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:
| Type | Description |
| --------------------------- | ----------------------------------------- |
| `ErrorType.EmptyError` | Contract reverted without reason provided |
| `ErrorType.RevertError` | Contract reverted with reason provided |
| `ErrorType.PanicError` | Contract reverted due to a panic error |
| `ErrorType.CustomError` | Contract reverted due to a custom error |
| `ErrorType.UserRejectError` | User rejected the transaction |
| `ErrorType.RpcError` | An error from the JSON RPC |
| `ErrorType.UnknownError` | An unknown error was thrown |
## Examples

@@ -137,3 +153,3 @@

} catch (err) {
const decodedError = errorDecoder.decode(err)
const decodedError = await errorDecoder.decode(err)
const reason = customReasonMapper(decodedError)

@@ -174,3 +190,3 @@ // Prints "Invalid swap with token contract address 0xabcd."

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

@@ -177,0 +193,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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