2019-05-15 01:48:48 +03:00
|
|
|
"use strict";
|
2019-08-25 09:39:20 +03:00
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
|
|
|
import { defineReadOnly } from "@ethersproject/properties";
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-05-15 01:48:48 +03:00
|
|
|
;
|
2019-08-25 09:39:20 +03:00
|
|
|
const _constructorGuard = {};
|
|
|
|
let ModifiersBytes = { calldata: true, memory: true, storage: true };
|
2019-07-21 02:13:00 +03:00
|
|
|
function checkModifier(type, name) {
|
|
|
|
if (type === "bytes" || type === "string") {
|
|
|
|
if (ModifiersBytes[name]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type === "address") {
|
|
|
|
if (name === "payable") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ModifiersBytes[name] || name === "payable") {
|
2019-08-02 09:10:58 +03:00
|
|
|
logger.throwArgumentError("invalid modifier", "name", name);
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-15 01:48:48 +03:00
|
|
|
// @TODO: Make sure that children of an indexed tuple are marked with a null indexed
|
|
|
|
function parseParamType(param, allowIndexed) {
|
2019-08-25 09:39:20 +03:00
|
|
|
let originalParam = param;
|
2019-05-15 01:48:48 +03:00
|
|
|
function throwError(i) {
|
|
|
|
throw new Error("unexpected character '" + originalParam[i] + "' at position " + i + " in '" + originalParam + "'");
|
|
|
|
}
|
|
|
|
param = param.replace(/\s/g, " ");
|
|
|
|
function newNode(parent) {
|
2019-08-25 09:39:20 +03:00
|
|
|
let node = { type: "", name: "", parent: parent, state: { allowType: true } };
|
2019-05-15 01:48:48 +03:00
|
|
|
if (allowIndexed) {
|
|
|
|
node.indexed = false;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let parent = { type: "", name: "", state: { allowType: true } };
|
|
|
|
let node = parent;
|
|
|
|
for (let i = 0; i < param.length; i++) {
|
|
|
|
let c = param[i];
|
2019-05-15 01:48:48 +03:00
|
|
|
switch (c) {
|
|
|
|
case "(":
|
|
|
|
if (!node.state.allowParams) {
|
|
|
|
throwError(i);
|
|
|
|
}
|
|
|
|
node.state.allowType = false;
|
|
|
|
node.type = verifyType(node.type);
|
|
|
|
node.components = [newNode(node)];
|
|
|
|
node = node.components[0];
|
|
|
|
break;
|
|
|
|
case ")":
|
|
|
|
delete node.state;
|
2019-05-25 01:15:42 +03:00
|
|
|
if (node.name === "indexed") {
|
|
|
|
if (!allowIndexed) {
|
|
|
|
throwError(i);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
2019-05-25 01:15:42 +03:00
|
|
|
node.indexed = true;
|
|
|
|
node.name = "";
|
|
|
|
}
|
2019-07-21 02:13:00 +03:00
|
|
|
if (checkModifier(node.type, node.name)) {
|
2019-05-25 01:15:42 +03:00
|
|
|
node.name = "";
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
node.type = verifyType(node.type);
|
2019-08-25 09:39:20 +03:00
|
|
|
let child = node;
|
2019-05-15 01:48:48 +03:00
|
|
|
node = node.parent;
|
|
|
|
if (!node) {
|
|
|
|
throwError(i);
|
|
|
|
}
|
|
|
|
delete child.parent;
|
|
|
|
node.state.allowParams = false;
|
|
|
|
node.state.allowName = true;
|
|
|
|
node.state.allowArray = true;
|
|
|
|
break;
|
|
|
|
case ",":
|
|
|
|
delete node.state;
|
2019-05-25 01:15:42 +03:00
|
|
|
if (node.name === "indexed") {
|
|
|
|
if (!allowIndexed) {
|
|
|
|
throwError(i);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
2019-05-25 01:15:42 +03:00
|
|
|
node.indexed = true;
|
|
|
|
node.name = "";
|
|
|
|
}
|
2019-07-21 02:13:00 +03:00
|
|
|
if (checkModifier(node.type, node.name)) {
|
2019-05-25 01:15:42 +03:00
|
|
|
node.name = "";
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
node.type = verifyType(node.type);
|
2019-08-25 09:39:20 +03:00
|
|
|
let sibling = newNode(node.parent);
|
2019-05-15 01:48:48 +03:00
|
|
|
//{ type: "", name: "", parent: node.parent, state: { allowType: true } };
|
|
|
|
node.parent.components.push(sibling);
|
|
|
|
delete node.parent;
|
|
|
|
node = sibling;
|
|
|
|
break;
|
|
|
|
// Hit a space...
|
|
|
|
case " ":
|
|
|
|
// If reading type, the type is done and may read a param or name
|
|
|
|
if (node.state.allowType) {
|
|
|
|
if (node.type !== "") {
|
|
|
|
node.type = verifyType(node.type);
|
|
|
|
delete node.state.allowType;
|
|
|
|
node.state.allowName = true;
|
|
|
|
node.state.allowParams = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If reading name, the name is done
|
|
|
|
if (node.state.allowName) {
|
|
|
|
if (node.name !== "") {
|
2019-05-25 01:15:42 +03:00
|
|
|
if (node.name === "indexed") {
|
|
|
|
if (!allowIndexed) {
|
|
|
|
throwError(i);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
2019-05-25 01:15:42 +03:00
|
|
|
if (node.indexed) {
|
|
|
|
throwError(i);
|
|
|
|
}
|
|
|
|
node.indexed = true;
|
|
|
|
node.name = "";
|
|
|
|
}
|
2019-07-21 02:13:00 +03:00
|
|
|
else if (checkModifier(node.type, node.name)) {
|
2019-05-25 01:15:42 +03:00
|
|
|
node.name = "";
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.state.allowName = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "[":
|
|
|
|
if (!node.state.allowArray) {
|
|
|
|
throwError(i);
|
|
|
|
}
|
|
|
|
node.type += c;
|
|
|
|
node.state.allowArray = false;
|
|
|
|
node.state.allowName = false;
|
|
|
|
node.state.readArray = true;
|
|
|
|
break;
|
|
|
|
case "]":
|
|
|
|
if (!node.state.readArray) {
|
|
|
|
throwError(i);
|
|
|
|
}
|
|
|
|
node.type += c;
|
|
|
|
node.state.readArray = false;
|
|
|
|
node.state.allowArray = true;
|
|
|
|
node.state.allowName = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (node.state.allowType) {
|
|
|
|
node.type += c;
|
|
|
|
node.state.allowParams = true;
|
|
|
|
node.state.allowArray = true;
|
|
|
|
}
|
|
|
|
else if (node.state.allowName) {
|
|
|
|
node.name += c;
|
|
|
|
delete node.state.allowArray;
|
|
|
|
}
|
|
|
|
else if (node.state.readArray) {
|
|
|
|
node.type += c;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throwError(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.parent) {
|
|
|
|
throw new Error("unexpected eof");
|
|
|
|
}
|
|
|
|
delete parent.state;
|
2019-05-25 01:15:42 +03:00
|
|
|
if (node.name === "indexed") {
|
|
|
|
if (!allowIndexed) {
|
|
|
|
throwError(originalParam.length - 7);
|
|
|
|
}
|
|
|
|
if (node.indexed) {
|
|
|
|
throwError(originalParam.length - 7);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
2019-05-25 01:15:42 +03:00
|
|
|
node.indexed = true;
|
|
|
|
node.name = "";
|
|
|
|
}
|
2019-07-21 02:13:00 +03:00
|
|
|
else if (checkModifier(node.type, node.name)) {
|
2019-05-25 01:15:42 +03:00
|
|
|
node.name = "";
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
parent.type = verifyType(parent.type);
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
function populate(object, params) {
|
2019-08-25 09:39:20 +03:00
|
|
|
for (let key in params) {
|
|
|
|
defineReadOnly(object, key, params[key]);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
export const FormatTypes = Object.freeze({
|
2019-07-21 02:13:00 +03:00
|
|
|
// Bare formatting, as is needed for computing a sighash of an event or function
|
|
|
|
sighash: "sighash",
|
|
|
|
// Human-Readable with Minimal spacing and without names (compact human-readable)
|
|
|
|
minimal: "minimal",
|
|
|
|
// Human-Readble with nice spacing, including all names
|
|
|
|
full: "full",
|
|
|
|
// JSON-format a la Solidity
|
|
|
|
json: "json"
|
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
const paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/);
|
|
|
|
export class ParamType {
|
|
|
|
constructor(constructorGuard, params) {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (constructorGuard !== _constructorGuard) {
|
|
|
|
throw new Error("use fromString");
|
|
|
|
}
|
|
|
|
populate(this, params);
|
2019-08-25 09:39:20 +03:00
|
|
|
let match = this.type.match(paramTypeArray);
|
2019-05-15 01:48:48 +03:00
|
|
|
if (match) {
|
|
|
|
populate(this, {
|
|
|
|
arrayLength: parseInt(match[2] || "-1"),
|
|
|
|
arrayChildren: ParamType.fromObject({
|
|
|
|
type: match[1],
|
|
|
|
components: this.components
|
|
|
|
}),
|
|
|
|
baseType: "array"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
populate(this, {
|
|
|
|
arrayLength: null,
|
|
|
|
arrayChildren: null,
|
|
|
|
baseType: ((this.components != null) ? "tuple" : this.type)
|
|
|
|
});
|
|
|
|
}
|
2019-06-12 00:57:04 +03:00
|
|
|
this._isParamType = true;
|
|
|
|
Object.freeze(this);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
// Format the parameter fragment
|
2019-07-21 02:13:00 +03:00
|
|
|
// - sighash: "(uint256,address)"
|
|
|
|
// - minimal: "tuple(uint256,address) indexed"
|
|
|
|
// - full: "tuple(uint256 foo, addres bar) indexed baz"
|
2019-08-25 09:39:20 +03:00
|
|
|
format(format) {
|
2019-07-21 02:13:00 +03:00
|
|
|
if (!format) {
|
2019-08-25 09:39:20 +03:00
|
|
|
format = FormatTypes.sighash;
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (!FormatTypes[format]) {
|
2019-08-02 09:10:58 +03:00
|
|
|
logger.throwArgumentError("invalid format type", "format", format);
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (format === FormatTypes.json) {
|
|
|
|
let result = {
|
2019-07-21 02:13:00 +03:00
|
|
|
type: ((this.baseType === "tuple") ? "tuple" : this.type),
|
|
|
|
name: (this.name || undefined)
|
|
|
|
};
|
|
|
|
if (typeof (this.indexed) === "boolean") {
|
2019-08-25 09:39:20 +03:00
|
|
|
result.indexed = this.indexed;
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
|
|
|
if (this.components) {
|
2019-08-25 09:39:20 +03:00
|
|
|
result.components = this.components.map((comp) => JSON.parse(comp.format(format)));
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
return JSON.stringify(result);
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let result = "";
|
2019-05-15 01:48:48 +03:00
|
|
|
// Array
|
|
|
|
if (this.baseType === "array") {
|
2019-07-21 02:13:00 +03:00
|
|
|
result += this.arrayChildren.format(format);
|
2019-05-15 01:48:48 +03:00
|
|
|
result += "[" + (this.arrayLength < 0 ? "" : String(this.arrayLength)) + "]";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (this.baseType === "tuple") {
|
2019-08-25 09:39:20 +03:00
|
|
|
if (format !== FormatTypes.sighash) {
|
2019-05-15 01:48:48 +03:00
|
|
|
result += this.type;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
result += "(" + this.components.map((comp) => comp.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ")";
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result += this.type;
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (format !== FormatTypes.sighash) {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (this.indexed === true) {
|
|
|
|
result += " indexed";
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (format === FormatTypes.full && this.name) {
|
2019-05-15 01:48:48 +03:00
|
|
|
result += " " + this.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static from(value, allowIndexed) {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (typeof (value) === "string") {
|
|
|
|
return ParamType.fromString(value, allowIndexed);
|
|
|
|
}
|
|
|
|
return ParamType.fromObject(value);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static fromObject(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
if (ParamType.isParamType(value)) {
|
2019-05-15 01:48:48 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return new ParamType(_constructorGuard, {
|
|
|
|
name: (value.name || null),
|
|
|
|
type: verifyType(value.type),
|
|
|
|
indexed: ((value.indexed == null) ? null : !!value.indexed),
|
|
|
|
components: (value.components ? value.components.map(ParamType.fromObject) : null)
|
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static fromString(value, allowIndexed) {
|
2019-05-15 01:48:48 +03:00
|
|
|
function ParamTypify(node) {
|
|
|
|
return ParamType.fromObject({
|
|
|
|
name: node.name,
|
|
|
|
type: node.type,
|
|
|
|
indexed: node.indexed,
|
|
|
|
components: node.components
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return ParamTypify(parseParamType(value, !!allowIndexed));
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static isParamType(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
return !!(value != null && value._isParamType);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 01:48:48 +03:00
|
|
|
;
|
|
|
|
function parseParams(value, allowIndex) {
|
2019-08-25 09:39:20 +03:00
|
|
|
return splitNesting(value).map((param) => ParamType.fromString(param, allowIndex));
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
export class Fragment {
|
|
|
|
constructor(constructorGuard, params) {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (constructorGuard !== _constructorGuard) {
|
|
|
|
throw new Error("use a static from method");
|
|
|
|
}
|
|
|
|
populate(this, params);
|
2019-06-12 00:57:04 +03:00
|
|
|
this._isFragment = true;
|
|
|
|
Object.freeze(this);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
static from(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
if (Fragment.isFragment(value)) {
|
|
|
|
return value;
|
|
|
|
}
|
2019-05-15 01:48:48 +03:00
|
|
|
if (typeof (value) === "string") {
|
|
|
|
return Fragment.fromString(value);
|
|
|
|
}
|
|
|
|
return Fragment.fromObject(value);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static fromObject(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
if (Fragment.isFragment(value)) {
|
2019-05-15 01:48:48 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value.type === "function") {
|
|
|
|
return FunctionFragment.fromObject(value);
|
|
|
|
}
|
|
|
|
else if (value.type === "event") {
|
|
|
|
return EventFragment.fromObject(value);
|
|
|
|
}
|
|
|
|
else if (value.type === "constructor") {
|
|
|
|
return ConstructorFragment.fromObject(value);
|
|
|
|
}
|
|
|
|
else if (value.type === "fallback") {
|
|
|
|
// @TODO:
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-02 09:10:58 +03:00
|
|
|
return logger.throwArgumentError("invalid fragment object", "value", value);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static fromString(value) {
|
2019-05-15 01:48:48 +03:00
|
|
|
// Make sure the "returns" is surrounded by a space and all whitespace is exactly one space
|
|
|
|
value = value.replace(/\s/g, " ");
|
|
|
|
value = value.replace(/\(/g, " (").replace(/\)/g, ") ").replace(/\s+/g, " ");
|
|
|
|
value = value.trim();
|
|
|
|
if (value.split(" ")[0] === "event") {
|
|
|
|
return EventFragment.fromString(value.substring(5).trim());
|
|
|
|
}
|
|
|
|
else if (value.split(" ")[0] === "function") {
|
|
|
|
return FunctionFragment.fromString(value.substring(8).trim());
|
|
|
|
}
|
|
|
|
else if (value.split("(")[0].trim() === "constructor") {
|
|
|
|
return ConstructorFragment.fromString(value.trim());
|
|
|
|
}
|
|
|
|
throw new Error("unknown fragment");
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static isFragment(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
return !!(value && value._isFragment);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
export class EventFragment extends Fragment {
|
|
|
|
format(format) {
|
2019-07-21 02:13:00 +03:00
|
|
|
if (!format) {
|
2019-08-25 09:39:20 +03:00
|
|
|
format = FormatTypes.sighash;
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (!FormatTypes[format]) {
|
2019-08-02 09:10:58 +03:00
|
|
|
logger.throwArgumentError("invalid format type", "format", format);
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (format === FormatTypes.json) {
|
2019-07-21 02:13:00 +03:00
|
|
|
return JSON.stringify({
|
|
|
|
type: "event",
|
|
|
|
anonymous: this.anonymous,
|
|
|
|
name: this.name,
|
2019-08-25 09:39:20 +03:00
|
|
|
inputs: this.inputs.map((input) => JSON.parse(input.format(format)))
|
2019-07-21 02:13:00 +03:00
|
|
|
});
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let result = "";
|
|
|
|
if (format !== FormatTypes.sighash) {
|
2019-07-21 02:13:00 +03:00
|
|
|
result += "event ";
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
result += this.name + "(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") ";
|
|
|
|
if (format !== FormatTypes.sighash) {
|
2019-07-21 02:13:00 +03:00
|
|
|
if (this.anonymous) {
|
|
|
|
result += "anonymous ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result.trim();
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static from(value) {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (typeof (value) === "string") {
|
|
|
|
return EventFragment.fromString(value);
|
|
|
|
}
|
|
|
|
return EventFragment.fromObject(value);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static fromObject(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
if (EventFragment.isEventFragment(value)) {
|
2019-05-15 01:48:48 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value.type !== "event") {
|
|
|
|
throw new Error("invalid event object - " + value.type);
|
|
|
|
}
|
|
|
|
return new EventFragment(_constructorGuard, {
|
|
|
|
name: verifyIdentifier(value.name),
|
|
|
|
anonymous: value.anonymous,
|
|
|
|
inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []),
|
|
|
|
type: "event"
|
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static fromString(value) {
|
|
|
|
let match = value.match(regexParen);
|
2019-05-15 01:48:48 +03:00
|
|
|
if (!match) {
|
|
|
|
throw new Error("invalid event: " + value);
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let anonymous = false;
|
|
|
|
match[3].split(" ").forEach((modifier) => {
|
2019-05-15 01:48:48 +03:00
|
|
|
switch (modifier.trim()) {
|
|
|
|
case "anonymous":
|
|
|
|
anonymous = true;
|
|
|
|
break;
|
|
|
|
case "":
|
|
|
|
break;
|
|
|
|
default:
|
2019-08-02 09:10:58 +03:00
|
|
|
logger.warn("unknown modifier: " + modifier);
|
2019-05-15 01:48:48 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return EventFragment.fromObject({
|
|
|
|
name: match[1].trim(),
|
|
|
|
anonymous: anonymous,
|
|
|
|
inputs: parseParams(match[2], true),
|
|
|
|
type: "event"
|
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static isEventFragment(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
return (value && value._isFragment && value.type === "event");
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 01:48:48 +03:00
|
|
|
function parseGas(value, params) {
|
|
|
|
params.gas = null;
|
2019-08-25 09:39:20 +03:00
|
|
|
let comps = value.split("@");
|
2019-05-15 01:48:48 +03:00
|
|
|
if (comps.length !== 1) {
|
|
|
|
if (comps.length > 2) {
|
|
|
|
throw new Error("invalid signature");
|
|
|
|
}
|
|
|
|
if (!comps[1].match(/^[0-9]+$/)) {
|
|
|
|
throw new Error("invalid signature gas");
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
params.gas = BigNumber.from(comps[1]);
|
2019-05-15 01:48:48 +03:00
|
|
|
return comps[0];
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
function parseModifiers(value, params) {
|
|
|
|
params.constant = false;
|
|
|
|
params.payable = false;
|
2019-05-25 01:15:42 +03:00
|
|
|
params.stateMutability = "nonpayable";
|
2019-08-25 09:39:20 +03:00
|
|
|
value.split(" ").forEach((modifier) => {
|
2019-05-15 01:48:48 +03:00
|
|
|
switch (modifier.trim()) {
|
|
|
|
case "constant":
|
|
|
|
params.constant = true;
|
|
|
|
break;
|
|
|
|
case "payable":
|
|
|
|
params.payable = true;
|
|
|
|
params.stateMutability = "payable";
|
|
|
|
break;
|
|
|
|
case "pure":
|
|
|
|
params.constant = true;
|
|
|
|
params.stateMutability = "pure";
|
|
|
|
break;
|
|
|
|
case "view":
|
|
|
|
params.constant = true;
|
|
|
|
params.stateMutability = "view";
|
|
|
|
break;
|
|
|
|
case "external":
|
|
|
|
case "public":
|
|
|
|
case "":
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("unknown modifier: " + modifier);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-07-21 02:13:00 +03:00
|
|
|
function verifyState(value) {
|
2019-08-25 09:39:20 +03:00
|
|
|
let result = {
|
2019-07-21 02:13:00 +03:00
|
|
|
constant: false,
|
|
|
|
payable: true,
|
|
|
|
stateMutability: "payable"
|
|
|
|
};
|
|
|
|
if (value.stateMutability != null) {
|
|
|
|
result.stateMutability = value.stateMutability;
|
|
|
|
result.constant = (result.stateMutability === "view" || result.stateMutability === "pure");
|
|
|
|
if (value.constant != null) {
|
|
|
|
if ((!!value.constant) !== result.constant) {
|
|
|
|
throw new Error("cannot have constant function with mutability " + result.stateMutability);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.payable = (result.stateMutability === "payable");
|
|
|
|
if (value.payable != null) {
|
|
|
|
if ((!!value.payable) !== result.payable) {
|
|
|
|
throw new Error("cannot have payable function with mutability " + result.stateMutability);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (value.payable != null) {
|
|
|
|
result.payable = !!value.payable;
|
|
|
|
result.stateMutability = (result.payable ? "payable" : "nonpayable");
|
|
|
|
result.constant = !result.payable;
|
|
|
|
if (value.constant != null && (value.constant !== result.constant)) {
|
|
|
|
throw new Error("cannot have constant payable function");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (value.constant != null) {
|
|
|
|
result.constant = !!value.constant;
|
|
|
|
result.payable = !result.constant;
|
|
|
|
result.stateMutability = (result.constant ? "view" : "payable");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
export class ConstructorFragment extends Fragment {
|
|
|
|
format(format) {
|
2019-07-21 02:13:00 +03:00
|
|
|
if (!format) {
|
2019-08-25 09:39:20 +03:00
|
|
|
format = FormatTypes.sighash;
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (!FormatTypes[format]) {
|
2019-08-02 09:10:58 +03:00
|
|
|
logger.throwArgumentError("invalid format type", "format", format);
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (format === FormatTypes.json) {
|
2019-07-21 02:13:00 +03:00
|
|
|
return JSON.stringify({
|
|
|
|
type: "constructor",
|
|
|
|
stateMutability: ((this.stateMutability !== "nonpayable") ? this.stateMutability : undefined),
|
|
|
|
payble: this.payable,
|
|
|
|
gas: (this.gas ? this.gas.toNumber() : undefined),
|
2019-08-25 09:39:20 +03:00
|
|
|
inputs: this.inputs.map((input) => JSON.parse(input.format(format)))
|
2019-07-21 02:13:00 +03:00
|
|
|
});
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (format === FormatTypes.sighash) {
|
|
|
|
logger.throwError("cannot format a constructor for sighash", Logger.errors.UNSUPPORTED_OPERATION, {
|
2019-07-21 02:13:00 +03:00
|
|
|
operation: "format(sighash)"
|
|
|
|
});
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let result = "constructor(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") ";
|
2019-07-21 02:13:00 +03:00
|
|
|
if (this.stateMutability && this.stateMutability !== "nonpayable") {
|
|
|
|
result += this.stateMutability + " ";
|
|
|
|
}
|
|
|
|
return result.trim();
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static from(value) {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (typeof (value) === "string") {
|
|
|
|
return ConstructorFragment.fromString(value);
|
|
|
|
}
|
|
|
|
return ConstructorFragment.fromObject(value);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static fromObject(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
if (ConstructorFragment.isConstructorFragment(value)) {
|
2019-05-15 01:48:48 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value.type !== "constructor") {
|
|
|
|
throw new Error("invalid constructor object - " + value.type);
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let state = verifyState(value);
|
2019-07-21 02:13:00 +03:00
|
|
|
if (state.constant) {
|
|
|
|
throw new Error("constructor cannot be constant");
|
|
|
|
}
|
2019-05-15 01:48:48 +03:00
|
|
|
return new ConstructorFragment(_constructorGuard, {
|
|
|
|
type: value.type,
|
|
|
|
inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []),
|
2019-07-21 02:13:00 +03:00
|
|
|
payable: state.payable,
|
2019-08-25 09:39:20 +03:00
|
|
|
gas: (value.gas ? BigNumber.from(value.gas) : null)
|
2019-05-15 01:48:48 +03:00
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static fromString(value) {
|
|
|
|
let params = { type: "constructor" };
|
2019-05-15 01:48:48 +03:00
|
|
|
value = parseGas(value, params);
|
2019-08-25 09:39:20 +03:00
|
|
|
let parens = value.match(regexParen);
|
2019-05-15 01:48:48 +03:00
|
|
|
if (!parens) {
|
|
|
|
throw new Error("invalid constructor: " + value);
|
|
|
|
}
|
|
|
|
if (parens[1].trim() !== "constructor") {
|
|
|
|
throw new Error("invalid constructor");
|
|
|
|
}
|
|
|
|
params.inputs = parseParams(parens[2].trim(), false);
|
|
|
|
parseModifiers(parens[3].trim(), params);
|
|
|
|
return ConstructorFragment.fromObject(params);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static isConstructorFragment(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
return (value && value._isFragment && value.type === "constructor");
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
export class FunctionFragment extends ConstructorFragment {
|
|
|
|
format(format) {
|
2019-07-21 02:13:00 +03:00
|
|
|
if (!format) {
|
2019-08-25 09:39:20 +03:00
|
|
|
format = FormatTypes.sighash;
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (!FormatTypes[format]) {
|
2019-08-02 09:10:58 +03:00
|
|
|
logger.throwArgumentError("invalid format type", "format", format);
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
if (format === FormatTypes.json) {
|
2019-07-21 02:13:00 +03:00
|
|
|
return JSON.stringify({
|
|
|
|
type: "function",
|
|
|
|
name: this.name,
|
|
|
|
constant: this.constant,
|
|
|
|
stateMutability: ((this.stateMutability !== "nonpayable") ? this.stateMutability : undefined),
|
|
|
|
payble: this.payable,
|
|
|
|
gas: (this.gas ? this.gas.toNumber() : undefined),
|
2019-08-25 09:39:20 +03:00
|
|
|
inputs: this.inputs.map((input) => JSON.parse(input.format(format))),
|
|
|
|
ouputs: this.outputs.map((output) => JSON.parse(output.format(format))),
|
2019-07-21 02:13:00 +03:00
|
|
|
});
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let result = "";
|
|
|
|
if (format !== FormatTypes.sighash) {
|
2019-07-21 02:13:00 +03:00
|
|
|
result += "function ";
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
result += this.name + "(" + this.inputs.map((input) => input.format(format)).join((format === FormatTypes.full) ? ", " : ",") + ") ";
|
|
|
|
if (format !== FormatTypes.sighash) {
|
2019-07-21 02:13:00 +03:00
|
|
|
if (this.stateMutability) {
|
|
|
|
if (this.stateMutability !== "nonpayable") {
|
|
|
|
result += (this.stateMutability + " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (this.constant) {
|
|
|
|
result += "view ";
|
|
|
|
}
|
|
|
|
if (this.outputs && this.outputs.length) {
|
2019-08-25 09:39:20 +03:00
|
|
|
result += "returns (" + this.outputs.map((output) => output.format(format)).join(", ") + ") ";
|
2019-07-21 02:13:00 +03:00
|
|
|
}
|
|
|
|
if (this.gas != null) {
|
|
|
|
result += "@" + this.gas.toString() + " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result.trim();
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static from(value) {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (typeof (value) === "string") {
|
|
|
|
return FunctionFragment.fromString(value);
|
|
|
|
}
|
|
|
|
return FunctionFragment.fromObject(value);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static fromObject(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
if (FunctionFragment.isFunctionFragment(value)) {
|
2019-05-15 01:48:48 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value.type !== "function") {
|
|
|
|
throw new Error("invalid function object - " + value.type);
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let state = verifyState(value);
|
2019-05-15 01:48:48 +03:00
|
|
|
return new FunctionFragment(_constructorGuard, {
|
|
|
|
type: value.type,
|
|
|
|
name: verifyIdentifier(value.name),
|
2019-07-21 02:13:00 +03:00
|
|
|
constant: state.constant,
|
2019-05-15 01:48:48 +03:00
|
|
|
inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []),
|
|
|
|
outputs: (value.outputs ? value.outputs.map(ParamType.fromObject) : []),
|
2019-07-21 02:13:00 +03:00
|
|
|
payable: state.payable,
|
|
|
|
stateMutability: state.stateMutability,
|
2019-08-25 09:39:20 +03:00
|
|
|
gas: (value.gas ? BigNumber.from(value.gas) : null)
|
2019-05-15 01:48:48 +03:00
|
|
|
});
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static fromString(value) {
|
|
|
|
let params = { type: "function" };
|
2019-05-15 01:48:48 +03:00
|
|
|
value = parseGas(value, params);
|
2019-08-25 09:39:20 +03:00
|
|
|
let comps = value.split(" returns ");
|
2019-05-15 01:48:48 +03:00
|
|
|
if (comps.length > 2) {
|
|
|
|
throw new Error("invalid function");
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let parens = comps[0].match(regexParen);
|
2019-05-15 01:48:48 +03:00
|
|
|
if (!parens) {
|
|
|
|
throw new Error("invalid signature");
|
|
|
|
}
|
|
|
|
params.name = parens[1].trim();
|
|
|
|
if (!params.name.match(regexIdentifier)) {
|
|
|
|
throw new Error("invalid identifier: '" + params.name + "'");
|
|
|
|
}
|
|
|
|
params.inputs = parseParams(parens[2], false);
|
|
|
|
parseModifiers(parens[3].trim(), params);
|
|
|
|
// We have outputs
|
|
|
|
if (comps.length > 1) {
|
2019-08-25 09:39:20 +03:00
|
|
|
let returns = comps[1].match(regexParen);
|
2019-05-15 01:48:48 +03:00
|
|
|
if (returns[1].trim() != "" || returns[3].trim() != "") {
|
|
|
|
throw new Error("unexpected tokens");
|
|
|
|
}
|
|
|
|
params.outputs = parseParams(returns[2], false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
params.outputs = [];
|
|
|
|
}
|
|
|
|
return FunctionFragment.fromObject(params);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
static isFunctionFragment(value) {
|
2019-06-12 00:57:04 +03:00
|
|
|
return (value && value._isFragment && value.type === "function");
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 01:48:48 +03:00
|
|
|
//export class ErrorFragment extends Fragment {
|
|
|
|
//}
|
|
|
|
//export class StructFragment extends Fragment {
|
|
|
|
//}
|
|
|
|
function verifyType(type) {
|
|
|
|
// These need to be transformed to their full description
|
|
|
|
if (type.match(/^uint($|[^1-9])/)) {
|
|
|
|
type = "uint256" + type.substring(4);
|
|
|
|
}
|
|
|
|
else if (type.match(/^int($|[^1-9])/)) {
|
|
|
|
type = "int256" + type.substring(3);
|
|
|
|
}
|
|
|
|
// @TODO: more verification
|
|
|
|
return type;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
const regexIdentifier = new RegExp("^[A-Za-z_][A-Za-z0-9_]*$");
|
2019-05-15 01:48:48 +03:00
|
|
|
function verifyIdentifier(value) {
|
|
|
|
if (!value || !value.match(regexIdentifier)) {
|
|
|
|
throw new Error("invalid identifier: '" + value + "'");
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
const regexParen = new RegExp("^([^)(]*)\\((.*)\\)([^)(]*)$");
|
2019-05-15 01:48:48 +03:00
|
|
|
function splitNesting(value) {
|
|
|
|
value = value.trim();
|
2019-08-25 09:39:20 +03:00
|
|
|
let result = [];
|
|
|
|
let accum = "";
|
|
|
|
let depth = 0;
|
|
|
|
for (let offset = 0; offset < value.length; offset++) {
|
|
|
|
let c = value[offset];
|
2019-05-15 01:48:48 +03:00
|
|
|
if (c === "," && depth === 0) {
|
|
|
|
result.push(accum);
|
|
|
|
accum = "";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
accum += c;
|
|
|
|
if (c === "(") {
|
|
|
|
depth++;
|
|
|
|
}
|
|
|
|
else if (c === ")") {
|
|
|
|
depth--;
|
|
|
|
if (depth === -1) {
|
|
|
|
throw new Error("unbalanced parenthsis");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (accum) {
|
|
|
|
result.push(accum);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|