abi-util-lite
Version:
A light impletation to parse abi string array to abi json
108 lines • 4.33 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.EventFragment = 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 EventFragment = /** @class */ (function (_super) {
__extends(EventFragment, _super);
function EventFragment() {
return _super !== null && _super.apply(this, arguments) || this;
}
EventFragment.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: "event",
anonymous: this.anonymous,
name: this.name,
inputs: this.inputs.map(function (input) { return JSON.parse(input.format(format)); })
});
}
var result = "";
if (format !== Format_1.FormatTypes.sighash) {
result += "event ";
}
result += this.name + "(" + this.inputs.map(function (input) { return input.format(format); }).join((format === Format_1.FormatTypes.full) ? ", " : ",") + ") ";
if (format !== Format_1.FormatTypes.sighash) {
if (this.anonymous) {
result += "anonymous ";
}
}
return result.trim();
};
EventFragment.from = function (value) {
if (typeof (value) === "string") {
return EventFragment.fromString(value);
}
return EventFragment.fromObject(value);
};
EventFragment.fromObject = function (value) {
if (EventFragment.isEventFragment(value)) {
return value;
}
if (value.type !== "event") {
Logger_1.logger.throwArgumentError("invalid event object", "value", value);
}
var params = {
name: (0, Checkers_1.verifyIdentifier)(value.name),
anonymous: value.anonymous,
inputs: (value.inputs ? value.inputs.map(ParamType_1.ParamType.fromObject) : []),
type: "event"
};
return new EventFragment(Utils_1._constructorGuard, params);
};
EventFragment.fromString = function (value) {
var match = value.match(Fragments_1.regexParen);
if (!match) {
Logger_1.logger.throwArgumentError("invalid event string", "value", value);
}
var anonymous = false;
match[3].split(" ").forEach(function (modifier) {
switch (modifier.trim()) {
case "anonymous":
anonymous = true;
break;
case "":
break;
default:
Logger_1.logger.warn("unknown modifier: " + modifier);
}
});
return EventFragment.fromObject({
name: match[1].trim(),
anonymous: anonymous,
inputs: (0, ParamType_1.parseParams)(match[2], true),
type: "event"
});
};
EventFragment.isEventFragment = function (value) {
return (value && value._isFragment && value.type === "event");
};
return EventFragment;
}(Fragments_1.Fragment));
exports.EventFragment = EventFragment;
//# sourceMappingURL=EventFragment.js.map