abi-util-lite
Version:
A light impletation to parse abi string array to abi json
90 lines • 3.78 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorFragment = void 0;
var Logger_1 = require("../Logger");
var ParamType_1 = require("../ParamType");
var Fragments_1 = require("./Fragments");
var Checkers_1 = require("../Checkers");
var Utils_1 = require("../Utils");
var Format_1 = require("../Format");
var ErrorFragment = /** @class */ (function (_super) {
__extends(ErrorFragment, _super);
function ErrorFragment() {
return _super !== null && _super.apply(this, arguments) || this;
}
ErrorFragment.prototype.format = function (format) {
if (!format) {
format = Format_1.FormatTypes.sighash;
}
if (!Format_1.FormatTypes[format]) {
Logger_1.logger.throwArgumentError("invalid format type", "format", format);
}
if (format === Format_1.FormatTypes.json) {
return JSON.stringify({
type: "error",
name: this.name,
inputs: this.inputs.map(function (input) { return JSON.parse(input.format(format)); }),
});
}
var result = "";
if (format !== Format_1.FormatTypes.sighash) {
result += "error ";
}
result += this.name + "(" + this.inputs.map(function (input) { return input.format(format); }).join((format === Format_1.FormatTypes.full) ? ", " : ",") + ") ";
return result.trim();
};
ErrorFragment.from = function (value) {
if (typeof (value) === "string") {
return ErrorFragment.fromString(value);
}
return ErrorFragment.fromObject(value);
};
ErrorFragment.fromObject = function (value) {
if (ErrorFragment.isErrorFragment(value)) {
return value;
}
if (value.type !== "error") {
Logger_1.logger.throwArgumentError("invalid error object", "value", value);
}
var params = {
type: value.type,
name: (0, Checkers_1.verifyIdentifier)(value.name),
inputs: (value.inputs ? value.inputs.map(ParamType_1.ParamType.fromObject) : [])
};
return new ErrorFragment(Utils_1._constructorGuard, params);
};
ErrorFragment.fromString = function (value) {
var params = { type: "error" };
var parens = value.match(Fragments_1.regexParen);
if (!parens) {
Logger_1.logger.throwArgumentError("invalid error signature", "value", value);
}
params.name = parens[1].trim();
if (params.name) {
(0, Checkers_1.verifyIdentifier)(params.name);
}
params.inputs = (0, ParamType_1.parseParams)(parens[2], false);
return ErrorFragment.fromObject(params);
};
ErrorFragment.isErrorFragment = function (value) {
return (value && value._isFragment && value.type === "error");
};
return ErrorFragment;
}(Fragments_1.Fragment));
exports.ErrorFragment = ErrorFragment;
//# sourceMappingURL=ErrorFragment.js.map