2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
2019-06-10 22:25:46 -04:00
|
|
|
import { defineReadOnly } from "@ethersproject/properties";
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
export interface JsonFragmentType {
|
2021-03-30 14:28:43 -04:00
|
|
|
readonly name?: string;
|
|
|
|
readonly indexed?: boolean;
|
|
|
|
readonly type?: string;
|
2021-06-21 21:12:25 -04:00
|
|
|
readonly internalType?: any; // @TODO: in v6 reduce type
|
2021-03-30 14:28:43 -04:00
|
|
|
readonly components?: ReadonlyArray<JsonFragmentType>;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface JsonFragment {
|
2021-03-30 14:28:43 -04:00
|
|
|
readonly name?: string;
|
|
|
|
readonly type?: string;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2021-03-30 14:28:43 -04:00
|
|
|
readonly anonymous?: boolean;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2021-03-30 14:28:43 -04:00
|
|
|
readonly payable?: boolean;
|
|
|
|
readonly constant?: boolean;
|
|
|
|
readonly stateMutability?: string;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2021-03-30 14:28:43 -04:00
|
|
|
readonly inputs?: ReadonlyArray<JsonFragmentType>;
|
|
|
|
readonly outputs?: ReadonlyArray<JsonFragmentType>;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2021-03-30 14:28:43 -04:00
|
|
|
readonly gas?: string;
|
2019-05-14 18:25:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const _constructorGuard = { };
|
|
|
|
|
|
|
|
// AST Node parser state
|
|
|
|
type ParseState = {
|
|
|
|
allowArray?: boolean,
|
|
|
|
allowName?: boolean,
|
|
|
|
allowParams?: boolean,
|
|
|
|
allowType?: boolean,
|
|
|
|
readArray?: boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
// AST Node
|
|
|
|
type ParseNode = {
|
|
|
|
parent?: any,
|
|
|
|
type?: string,
|
|
|
|
name?: string,
|
|
|
|
state?: ParseState,
|
|
|
|
indexed?: boolean,
|
|
|
|
components?: Array<ParseNode>
|
|
|
|
};
|
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
let ModifiersBytes: { [ name: string ]: boolean } = { calldata: true, memory: true, storage: true };
|
2020-05-01 16:24:59 -04:00
|
|
|
let ModifiersNest: { [ name: string ]: boolean } = { calldata: true, memory: true };
|
2019-07-20 19:53:07 -03:00
|
|
|
function checkModifier(type: string, name: string): boolean {
|
|
|
|
if (type === "bytes" || type === "string") {
|
|
|
|
if (ModifiersBytes[name]) { return true; }
|
|
|
|
} else if (type === "address") {
|
|
|
|
if (name === "payable") { return true; }
|
2020-05-01 16:24:59 -04:00
|
|
|
} else if (type.indexOf("[") >= 0 || type === "tuple") {
|
|
|
|
if (ModifiersNest[name]) { return true; }
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
if (ModifiersBytes[name] || name === "payable") {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid modifier", "name", name);
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
// @TODO: Make sure that children of an indexed tuple are marked with a null indexed
|
|
|
|
function parseParamType(param: string, allowIndexed: boolean): ParseNode {
|
|
|
|
|
|
|
|
let originalParam = param;
|
|
|
|
function throwError(i: number) {
|
2020-04-15 16:49:03 -04:00
|
|
|
logger.throwArgumentError(`unexpected character at position ${ i }`, "param", param);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
param = param.replace(/\s/g, " ");
|
|
|
|
|
|
|
|
function newNode(parent: ParseNode): ParseNode {
|
|
|
|
let node: ParseNode = { type: "", name: "", parent: parent, state: { allowType: true } };
|
|
|
|
if (allowIndexed) { node.indexed = false; }
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
let parent: ParseNode = { type: "", name: "", state: { allowType: true } };
|
|
|
|
let node = parent;
|
|
|
|
|
|
|
|
for (let i = 0; i < param.length; i++) {
|
|
|
|
let c = param[i];
|
|
|
|
switch (c) {
|
|
|
|
case "(":
|
2019-11-20 18:26:59 +09:00
|
|
|
if (node.state.allowType && node.type === "") {
|
|
|
|
node.type = "tuple";
|
|
|
|
} else if (!node.state.allowParams) {
|
|
|
|
throwError(i);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
node.state.allowType = false;
|
|
|
|
node.type = verifyType(node.type);
|
|
|
|
node.components = [ newNode(node) ];
|
|
|
|
node = node.components[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ")":
|
|
|
|
delete node.state;
|
2019-05-24 15:51:45 -04:00
|
|
|
|
|
|
|
if (node.name === "indexed") {
|
|
|
|
if (!allowIndexed) { throwError(i); }
|
|
|
|
node.indexed = true;
|
|
|
|
node.name = "";
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2019-05-24 15:51:45 -04:00
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
if (checkModifier(node.type, node.name)) { node.name = ""; }
|
2019-05-24 15:51:45 -04:00
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
node.type = verifyType(node.type);
|
|
|
|
|
|
|
|
let child = node;
|
|
|
|
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-24 15:51:45 -04:00
|
|
|
|
|
|
|
if (node.name === "indexed") {
|
|
|
|
if (!allowIndexed) { throwError(i); }
|
|
|
|
node.indexed = true;
|
|
|
|
node.name = "";
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2019-05-24 15:51:45 -04:00
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
if (checkModifier(node.type, node.name)) { node.name = ""; }
|
2019-05-24 15:51:45 -04:00
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
node.type = verifyType(node.type);
|
|
|
|
|
|
|
|
let sibling: ParseNode = newNode(node.parent);
|
|
|
|
//{ 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-24 15:51:45 -04:00
|
|
|
if (node.name === "indexed") {
|
|
|
|
if (!allowIndexed) { throwError(i); }
|
|
|
|
if (node.indexed) { throwError(i); }
|
|
|
|
node.indexed = true;
|
|
|
|
node.name = "";
|
2019-07-20 19:53:07 -03:00
|
|
|
} else if (checkModifier(node.type, node.name)) {
|
2019-05-24 15:51:45 -04:00
|
|
|
node.name = "";
|
2019-05-14 18:25:46 -04: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 16:49:03 -04:00
|
|
|
if (node.parent) { logger.throwArgumentError("unexpected eof", "param", param); }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
delete parent.state;
|
|
|
|
|
2019-05-24 15:51:45 -04:00
|
|
|
if (node.name === "indexed") {
|
|
|
|
if (!allowIndexed) { throwError(originalParam.length - 7); }
|
|
|
|
if (node.indexed) { throwError(originalParam.length - 7); }
|
|
|
|
node.indexed = true;
|
|
|
|
node.name = "";
|
2019-07-20 19:53:07 -03:00
|
|
|
} else if (checkModifier(node.type, node.name)) {
|
2019-05-24 15:51:45 -04:00
|
|
|
node.name = "";
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2019-05-24 15:51:45 -04:00
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
parent.type = verifyType(parent.type);
|
|
|
|
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
function populate(object: any, params: any) {
|
|
|
|
for (let key in params) { defineReadOnly(object, key, params[key]); }
|
|
|
|
}
|
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
export const FormatTypes: { [ name: string ]: string } = Object.freeze({
|
|
|
|
// 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-05-14 18:25:46 -04:00
|
|
|
const paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/);
|
|
|
|
|
|
|
|
export class ParamType {
|
|
|
|
|
|
|
|
// The local name of the parameter (of null if unbound)
|
|
|
|
readonly name: string;
|
|
|
|
|
|
|
|
// The fully qualified type (e.g. "address", "tuple(address)", "uint256[3][]"
|
|
|
|
readonly type: string;
|
|
|
|
|
|
|
|
// The base type (e.g. "address", "tuple", "array")
|
|
|
|
readonly baseType: string;
|
|
|
|
|
|
|
|
// Indexable Paramters ONLY (otherwise null)
|
|
|
|
readonly indexed: boolean;
|
|
|
|
|
|
|
|
// Tuples ONLY: (otherwise null)
|
|
|
|
// - sub-components
|
|
|
|
readonly components: Array<ParamType>;
|
|
|
|
|
|
|
|
// Arrays ONLY: (otherwise null)
|
|
|
|
// - length of the array (-1 for dynamic length)
|
|
|
|
// - child type
|
|
|
|
readonly arrayLength: number;
|
|
|
|
readonly arrayChildren: ParamType;
|
|
|
|
|
2019-06-10 22:25:46 -04:00
|
|
|
readonly _isParamType: boolean;
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
constructor(constructorGuard: any, params: any) {
|
2020-04-15 16:49:03 -04:00
|
|
|
if (constructorGuard !== _constructorGuard) { logger.throwError("use fromString", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "new ParamType()"
|
|
|
|
}); }
|
2019-05-14 18:25:46 -04:00
|
|
|
populate(this, params);
|
|
|
|
|
|
|
|
let match = this.type.match(paramTypeArray);
|
|
|
|
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-10 22:25:46 -04:00
|
|
|
|
|
|
|
this._isParamType = true;
|
|
|
|
|
|
|
|
Object.freeze(this);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format the parameter fragment
|
2019-07-20 19:53:07 -03:00
|
|
|
// - sighash: "(uint256,address)"
|
|
|
|
// - minimal: "tuple(uint256,address) indexed"
|
|
|
|
// - full: "tuple(uint256 foo, addres bar) indexed baz"
|
|
|
|
format(format?: string): string {
|
|
|
|
if (!format) { format = FormatTypes.sighash; }
|
|
|
|
if (!FormatTypes[format]) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid format type", "format", format);
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format === FormatTypes.json) {
|
|
|
|
let result: any = {
|
|
|
|
type: ((this.baseType === "tuple") ? "tuple": this.type),
|
|
|
|
name: (this.name || undefined)
|
|
|
|
};
|
|
|
|
if (typeof(this.indexed) === "boolean") { result.indexed = this.indexed; }
|
|
|
|
if (this.components) {
|
|
|
|
result.components = this.components.map((comp) => JSON.parse(comp.format(format)));
|
|
|
|
}
|
|
|
|
return JSON.stringify(result);
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
let result = "";
|
|
|
|
|
|
|
|
// Array
|
|
|
|
if (this.baseType === "array") {
|
2019-07-20 19:53:07 -03:00
|
|
|
result += this.arrayChildren.format(format);
|
2019-05-14 18:25:46 -04:00
|
|
|
result += "[" + (this.arrayLength < 0 ? "": String(this.arrayLength)) + "]";
|
|
|
|
} else {
|
|
|
|
if (this.baseType === "tuple") {
|
2019-07-20 19:53:07 -03:00
|
|
|
if (format !== FormatTypes.sighash) {
|
2019-05-14 18:25:46 -04:00
|
|
|
result += this.type;
|
|
|
|
}
|
2019-07-20 19:53:07 -03:00
|
|
|
result += "(" + this.components.map(
|
|
|
|
(comp) => comp.format(format)
|
|
|
|
).join((format === FormatTypes.full) ? ", ": ",") + ")";
|
2019-05-14 18:25:46 -04:00
|
|
|
} else {
|
|
|
|
result += this.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
if (format !== FormatTypes.sighash) {
|
2019-05-14 18:25:46 -04:00
|
|
|
if (this.indexed === true) { result += " indexed"; }
|
2019-07-20 19:53:07 -03:00
|
|
|
if (format === FormatTypes.full && this.name) {
|
|
|
|
result += " " + this.name;
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static from(value: string | JsonFragmentType | ParamType, allowIndexed?: boolean): ParamType {
|
|
|
|
if (typeof(value) === "string") {
|
|
|
|
return ParamType.fromString(value, allowIndexed);
|
|
|
|
}
|
|
|
|
return ParamType.fromObject(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromObject(value: JsonFragmentType | ParamType): ParamType {
|
2019-06-10 22:25:46 -04:00
|
|
|
if (ParamType.isParamType(value)) { return value; }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromString(value: string, allowIndexed?: boolean): ParamType {
|
|
|
|
function ParamTypify(node: ParseNode): ParamType {
|
|
|
|
return ParamType.fromObject({
|
|
|
|
name: node.name,
|
|
|
|
type: node.type,
|
|
|
|
indexed: node.indexed,
|
|
|
|
components: node.components
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParamTypify(parseParamType(value, !!allowIndexed));
|
|
|
|
}
|
2019-06-10 22:25:46 -04:00
|
|
|
|
|
|
|
static isParamType(value: any): value is ParamType {
|
|
|
|
return !!(value != null && value._isParamType);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
function parseParams(value: string, allowIndex: boolean): Array<ParamType> {
|
|
|
|
return splitNesting(value).map((param) => ParamType.fromString(param, allowIndex));
|
|
|
|
}
|
|
|
|
|
2020-04-24 03:02:57 -04:00
|
|
|
type TypeCheck<T> = { -readonly [ K in keyof T ]: T[K] };
|
|
|
|
|
|
|
|
interface _Fragment {
|
|
|
|
readonly type: string;
|
|
|
|
readonly name: string;
|
2021-03-30 14:28:43 -04:00
|
|
|
readonly inputs: ReadonlyArray<ParamType>;
|
2020-04-24 03:02:57 -04:00
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
export abstract class Fragment {
|
|
|
|
|
|
|
|
readonly type: string;
|
|
|
|
readonly name: string;
|
|
|
|
readonly inputs: Array<ParamType>;
|
|
|
|
|
2019-06-10 22:25:46 -04:00
|
|
|
readonly _isFragment: boolean;
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
constructor(constructorGuard: any, params: any) {
|
2020-04-15 16:49:03 -04:00
|
|
|
if (constructorGuard !== _constructorGuard) {
|
|
|
|
logger.throwError("use a static from method", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "new Fragment()"
|
|
|
|
});
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
populate(this, params);
|
2019-06-10 22:25:46 -04:00
|
|
|
|
|
|
|
this._isFragment = true;
|
|
|
|
|
|
|
|
Object.freeze(this);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
abstract format(format?: string): string;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
static from(value: Fragment | JsonFragment | string): Fragment {
|
2019-06-10 22:25:46 -04:00
|
|
|
if (Fragment.isFragment(value)) { return value; }
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
if (typeof(value) === "string") {
|
|
|
|
return Fragment.fromString(value);
|
|
|
|
}
|
2019-06-10 22:25:46 -04:00
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
return Fragment.fromObject(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromObject(value: Fragment | JsonFragment): Fragment {
|
2019-06-10 22:25:46 -04:00
|
|
|
if (Fragment.isFragment(value)) { return value; }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-03-21 12:48:22 -04:00
|
|
|
switch (value.type) {
|
|
|
|
case "function":
|
|
|
|
return FunctionFragment.fromObject(value);
|
|
|
|
case "event":
|
|
|
|
return EventFragment.fromObject(value);
|
|
|
|
case "constructor":
|
|
|
|
return ConstructorFragment.fromObject(value);
|
2021-04-22 06:26:56 -04:00
|
|
|
case "error":
|
2021-04-22 22:29:30 -04:00
|
|
|
return ErrorFragment.fromObject(value);
|
2020-03-21 12:48:22 -04:00
|
|
|
case "fallback":
|
|
|
|
case "receive":
|
|
|
|
// @TODO: Something? Maybe return a FunctionFragment? A custom DefaultFunctionFragment?
|
|
|
|
return null;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
return logger.throwArgumentError("invalid fragment object", "value", value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static fromString(value: string): Fragment {
|
|
|
|
// 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());
|
2021-04-22 22:29:30 -04:00
|
|
|
} else if (value.split(" ")[0] === "error") {
|
|
|
|
return ErrorFragment.fromString(value.substring(5).trim());
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-04-15 16:49:03 -04:00
|
|
|
return logger.throwArgumentError("unsupported fragment", "value", value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2019-06-10 22:25:46 -04:00
|
|
|
|
|
|
|
static isFragment(value: any): value is Fragment {
|
|
|
|
return !!(value && value._isFragment);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-04-24 03:02:57 -04:00
|
|
|
interface _EventFragment extends _Fragment {
|
|
|
|
readonly anonymous: boolean;
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
export class EventFragment extends Fragment {
|
|
|
|
readonly anonymous: boolean;
|
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
format(format?: string): string {
|
|
|
|
if (!format) { format = FormatTypes.sighash; }
|
|
|
|
if (!FormatTypes[format]) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid format type", "format", format);
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format === FormatTypes.json) {
|
|
|
|
return JSON.stringify({
|
|
|
|
type: "event",
|
|
|
|
anonymous: this.anonymous,
|
|
|
|
name: this.name,
|
|
|
|
inputs: this.inputs.map((input) => JSON.parse(input.format(format)))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = "";
|
|
|
|
|
|
|
|
if (format !== FormatTypes.sighash) {
|
|
|
|
result += "event ";
|
|
|
|
}
|
|
|
|
|
|
|
|
result += this.name + "(" + this.inputs.map(
|
|
|
|
(input) => input.format(format)
|
|
|
|
).join((format === FormatTypes.full) ? ", ": ",") + ") ";
|
|
|
|
|
|
|
|
if (format !== FormatTypes.sighash) {
|
|
|
|
if (this.anonymous) {
|
|
|
|
result += "anonymous ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.trim();
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
static from(value: EventFragment | JsonFragment | string): EventFragment {
|
|
|
|
if (typeof(value) === "string") {
|
|
|
|
return EventFragment.fromString(value);
|
|
|
|
}
|
|
|
|
return EventFragment.fromObject(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromObject(value: JsonFragment | EventFragment): EventFragment {
|
2019-06-10 22:25:46 -04:00
|
|
|
if (EventFragment.isEventFragment(value)) { return value; }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-15 16:49:03 -04:00
|
|
|
if (value.type !== "event") {
|
|
|
|
logger.throwArgumentError("invalid event object", "value", value);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-24 03:02:57 -04:00
|
|
|
const params: TypeCheck<_EventFragment> = {
|
2019-05-14 18:25:46 -04:00
|
|
|
name: verifyIdentifier(value.name),
|
|
|
|
anonymous: value.anonymous,
|
|
|
|
inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []),
|
|
|
|
type: "event"
|
2020-04-24 03:02:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return new EventFragment(_constructorGuard, params);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static fromString(value: string): EventFragment {
|
|
|
|
|
|
|
|
let match = value.match(regexParen);
|
2020-04-15 16:49:03 -04:00
|
|
|
if (!match) {
|
|
|
|
logger.throwArgumentError("invalid event string", "value", value);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
let anonymous = false;
|
|
|
|
match[3].split(" ").forEach((modifier) => {
|
|
|
|
switch(modifier.trim()) {
|
|
|
|
case "anonymous":
|
|
|
|
anonymous = true;
|
|
|
|
break;
|
|
|
|
case "":
|
|
|
|
break;
|
|
|
|
default:
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.warn("unknown modifier: " + modifier);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return EventFragment.fromObject({
|
|
|
|
name: match[1].trim(),
|
|
|
|
anonymous: anonymous,
|
|
|
|
inputs: parseParams(match[2], true),
|
|
|
|
type: "event"
|
|
|
|
});
|
|
|
|
}
|
2019-06-10 22:25:46 -04:00
|
|
|
|
|
|
|
static isEventFragment(value: any): value is EventFragment {
|
|
|
|
return (value && value._isFragment && value.type === "event");
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseGas(value: string, params: any): string {
|
|
|
|
params.gas = null;
|
|
|
|
|
|
|
|
let comps = value.split("@");
|
|
|
|
if (comps.length !== 1) {
|
|
|
|
if (comps.length > 2) {
|
2020-04-15 16:49:03 -04:00
|
|
|
logger.throwArgumentError("invalid human-readable ABI signature", "value", value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
if (!comps[1].match(/^[0-9]+$/)) {
|
2020-04-22 02:42:25 -04:00
|
|
|
logger.throwArgumentError("invalid human-readable ABI signature gas", "value", value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
params.gas = BigNumber.from(comps[1]);
|
|
|
|
return comps[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseModifiers(value: string, params: any): void {
|
|
|
|
params.constant = false;
|
|
|
|
params.payable = false;
|
2019-05-24 15:51:45 -04:00
|
|
|
params.stateMutability = "nonpayable";
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
value.split(" ").forEach((modifier) => {
|
|
|
|
switch (modifier.trim()) {
|
|
|
|
case "constant":
|
|
|
|
params.constant = true;
|
|
|
|
break;
|
|
|
|
case "payable":
|
|
|
|
params.payable = true;
|
|
|
|
params.stateMutability = "payable";
|
|
|
|
break;
|
2020-06-12 04:41:53 -04:00
|
|
|
case "nonpayable":
|
|
|
|
params.payable = false;
|
|
|
|
params.stateMutability = "nonpayable";
|
|
|
|
break;
|
2019-05-14 18:25:46 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-15 16:49:03 -04:00
|
|
|
type StateInputValue = {
|
|
|
|
constant?: boolean;
|
|
|
|
payable?: boolean;
|
|
|
|
stateMutability?: string;
|
|
|
|
type?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type StateOutputValue = {
|
|
|
|
constant: boolean;
|
|
|
|
payable: boolean;
|
|
|
|
stateMutability: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
function verifyState(value: StateInputValue): StateOutputValue {
|
2019-07-20 19:53:07 -03:00
|
|
|
let result: any = {
|
|
|
|
constant: false,
|
|
|
|
payable: true,
|
|
|
|
stateMutability: "payable"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (value.stateMutability != null) {
|
|
|
|
result.stateMutability = value.stateMutability;
|
|
|
|
|
2020-04-03 21:28:53 -04:00
|
|
|
// Set (and check things are consistent) the constant property
|
2019-07-20 19:53:07 -03:00
|
|
|
result.constant = (result.stateMutability === "view" || result.stateMutability === "pure");
|
|
|
|
if (value.constant != null) {
|
|
|
|
if ((!!value.constant) !== result.constant) {
|
2020-04-15 16:49:03 -04:00
|
|
|
logger.throwArgumentError("cannot have constant function with mutability " + result.stateMutability, "value", value);
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 21:28:53 -04:00
|
|
|
// Set (and check things are consistent) the payable property
|
2019-07-20 19:53:07 -03:00
|
|
|
result.payable = (result.stateMutability === "payable");
|
|
|
|
if (value.payable != null) {
|
|
|
|
if ((!!value.payable) !== result.payable) {
|
2020-04-15 16:49:03 -04:00
|
|
|
logger.throwArgumentError("cannot have payable function with mutability " + result.stateMutability, "value", value);
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (value.payable != null) {
|
|
|
|
result.payable = !!value.payable;
|
2020-04-03 21:28:53 -04:00
|
|
|
|
|
|
|
// If payable we can assume non-constant; otherwise we can't assume
|
2020-04-03 22:01:33 -04:00
|
|
|
if (value.constant == null && !result.payable && value.type !== "constructor") {
|
2020-04-15 16:49:03 -04:00
|
|
|
logger.throwArgumentError("unable to determine stateMutability", "value", value);
|
2020-04-03 21:28:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
result.constant = !!value.constant;
|
|
|
|
|
|
|
|
if (result.constant) {
|
|
|
|
result.stateMutability = "view";
|
|
|
|
} else {
|
|
|
|
result.stateMutability = (result.payable ? "payable": "nonpayable");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.payable && result.constant) {
|
2020-04-15 16:49:03 -04:00
|
|
|
logger.throwArgumentError("cannot have constant payable function", "value", value);
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (value.constant != null) {
|
|
|
|
result.constant = !!value.constant;
|
|
|
|
result.payable = !result.constant;
|
|
|
|
result.stateMutability = (result.constant ? "view": "payable");
|
2020-04-03 21:28:53 -04:00
|
|
|
|
2020-04-03 22:01:33 -04:00
|
|
|
} else if (value.type !== "constructor") {
|
2020-04-15 16:49:03 -04:00
|
|
|
logger.throwArgumentError("unable to determine stateMutability", "value", value);
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-24 03:02:57 -04:00
|
|
|
interface _ConstructorFragment extends _Fragment {
|
|
|
|
stateMutability: string;
|
|
|
|
payable: boolean;
|
|
|
|
gas?: BigNumber;
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
export class ConstructorFragment extends Fragment {
|
|
|
|
stateMutability: string;
|
|
|
|
payable: boolean;
|
|
|
|
gas?: BigNumber;
|
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
format(format?: string): string {
|
|
|
|
if (!format) { format = FormatTypes.sighash; }
|
|
|
|
if (!FormatTypes[format]) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid format type", "format", format);
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format === FormatTypes.json) {
|
|
|
|
return JSON.stringify({
|
|
|
|
type: "constructor",
|
|
|
|
stateMutability: ((this.stateMutability !== "nonpayable") ? this.stateMutability: undefined),
|
2021-02-03 14:26:10 -05:00
|
|
|
payable: this.payable,
|
2019-07-20 19:53:07 -03:00
|
|
|
gas: (this.gas ? this.gas.toNumber(): undefined),
|
|
|
|
inputs: this.inputs.map((input) => JSON.parse(input.format(format)))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format === FormatTypes.sighash) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwError("cannot format a constructor for sighash", Logger.errors.UNSUPPORTED_OPERATION, {
|
2019-07-20 19:53:07 -03:00
|
|
|
operation: "format(sighash)"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = "constructor(" + this.inputs.map(
|
|
|
|
(input) => input.format(format)
|
|
|
|
).join((format === FormatTypes.full) ? ", ": ",") + ") ";
|
|
|
|
|
|
|
|
if (this.stateMutability && this.stateMutability !== "nonpayable") {
|
|
|
|
result += this.stateMutability + " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.trim();
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
static from(value: ConstructorFragment | JsonFragment | string): ConstructorFragment {
|
|
|
|
if (typeof(value) === "string") {
|
|
|
|
return ConstructorFragment.fromString(value);
|
|
|
|
}
|
|
|
|
return ConstructorFragment.fromObject(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromObject(value: ConstructorFragment | JsonFragment): ConstructorFragment {
|
2019-06-10 22:25:46 -04:00
|
|
|
if (ConstructorFragment.isConstructorFragment(value)) { return value; }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-15 16:49:03 -04:00
|
|
|
if (value.type !== "constructor") {
|
|
|
|
logger.throwArgumentError("invalid constructor object", "value", value);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
let state = verifyState(value);
|
|
|
|
if (state.constant) {
|
2020-04-15 16:49:03 -04:00
|
|
|
logger.throwArgumentError("constructor cannot be constant", "value", value);
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
|
2020-04-24 03:02:57 -04:00
|
|
|
const params: TypeCheck<_ConstructorFragment> = {
|
2020-01-06 18:48:36 -05:00
|
|
|
name: null,
|
2019-05-14 18:25:46 -04:00
|
|
|
type: value.type,
|
|
|
|
inputs: (value.inputs ? value.inputs.map(ParamType.fromObject): []),
|
2019-07-20 19:53:07 -03:00
|
|
|
payable: state.payable,
|
2020-04-24 03:02:57 -04:00
|
|
|
stateMutability: state.stateMutability,
|
2019-05-14 18:25:46 -04:00
|
|
|
gas: (value.gas ? BigNumber.from(value.gas): null)
|
2020-04-24 03:02:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return new ConstructorFragment(_constructorGuard, params);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static fromString(value: string): ConstructorFragment {
|
|
|
|
let params: any = { type: "constructor" };
|
|
|
|
|
|
|
|
value = parseGas(value, params);
|
|
|
|
|
|
|
|
let parens = value.match(regexParen);
|
2020-04-15 16:49:03 -04:00
|
|
|
if (!parens || parens[1].trim() !== "constructor") {
|
|
|
|
logger.throwArgumentError("invalid constructor string", "value", value);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
params.inputs = parseParams(parens[2].trim(), false);
|
|
|
|
|
|
|
|
parseModifiers(parens[3].trim(), params);
|
|
|
|
|
|
|
|
return ConstructorFragment.fromObject(params);
|
|
|
|
}
|
|
|
|
|
2019-06-10 22:25:46 -04:00
|
|
|
static isConstructorFragment(value: any): value is ConstructorFragment {
|
|
|
|
return (value && value._isFragment && value.type === "constructor");
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-04-24 03:02:57 -04:00
|
|
|
interface _FunctionFragment extends _ConstructorFragment {
|
|
|
|
constant: boolean;
|
|
|
|
outputs?: Array<ParamType>;
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
export class FunctionFragment extends ConstructorFragment {
|
|
|
|
constant: boolean;
|
|
|
|
outputs?: Array<ParamType>;
|
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
format(format?: string): string {
|
|
|
|
if (!format) { format = FormatTypes.sighash; }
|
|
|
|
if (!FormatTypes[format]) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid format type", "format", format);
|
2019-07-20 19:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format === FormatTypes.json) {
|
|
|
|
return JSON.stringify({
|
|
|
|
type: "function",
|
|
|
|
name: this.name,
|
|
|
|
constant: this.constant,
|
|
|
|
stateMutability: ((this.stateMutability !== "nonpayable") ? this.stateMutability: undefined),
|
2021-02-03 14:26:10 -05:00
|
|
|
payable: this.payable,
|
2019-07-20 19:53:07 -03:00
|
|
|
gas: (this.gas ? this.gas.toNumber(): undefined),
|
|
|
|
inputs: this.inputs.map((input) => JSON.parse(input.format(format))),
|
2021-02-03 14:26:10 -05:00
|
|
|
outputs: this.outputs.map((output) => JSON.parse(output.format(format))),
|
2019-07-20 19:53:07 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = "";
|
|
|
|
|
|
|
|
if (format !== FormatTypes.sighash) {
|
|
|
|
result += "function ";
|
|
|
|
}
|
|
|
|
|
|
|
|
result += this.name + "(" + this.inputs.map(
|
|
|
|
(input) => input.format(format)
|
|
|
|
).join((format === FormatTypes.full) ? ", ": ",") + ") ";
|
|
|
|
|
|
|
|
if (format !== FormatTypes.sighash) {
|
|
|
|
if (this.stateMutability) {
|
|
|
|
if (this.stateMutability !== "nonpayable") {
|
|
|
|
result += (this.stateMutability + " ");
|
|
|
|
}
|
|
|
|
} else if (this.constant) {
|
|
|
|
result += "view ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.outputs && this.outputs.length) {
|
|
|
|
result += "returns (" + this.outputs.map(
|
|
|
|
(output) => output.format(format)
|
|
|
|
).join(", ") + ") ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.gas != null) {
|
|
|
|
result += "@" + this.gas.toString() + " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.trim();
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
static from(value: FunctionFragment | JsonFragment | string): FunctionFragment {
|
|
|
|
if (typeof(value) === "string") {
|
|
|
|
return FunctionFragment.fromString(value);
|
|
|
|
}
|
|
|
|
return FunctionFragment.fromObject(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromObject(value: FunctionFragment | JsonFragment): FunctionFragment {
|
2019-06-10 22:25:46 -04:00
|
|
|
if (FunctionFragment.isFunctionFragment(value)) { return value; }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-15 16:49:03 -04:00
|
|
|
if (value.type !== "function") {
|
|
|
|
logger.throwArgumentError("invalid function object", "value", value);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-07-20 19:53:07 -03:00
|
|
|
let state = verifyState(value);
|
|
|
|
|
2020-04-24 03:02:57 -04:00
|
|
|
const params: TypeCheck<_FunctionFragment> = {
|
2019-05-14 18:25:46 -04:00
|
|
|
type: value.type,
|
|
|
|
name: verifyIdentifier(value.name),
|
2019-07-20 19:53:07 -03:00
|
|
|
constant: state.constant,
|
2019-05-14 18:25:46 -04:00
|
|
|
inputs: (value.inputs ? value.inputs.map(ParamType.fromObject): []),
|
|
|
|
outputs: (value.outputs ? value.outputs.map(ParamType.fromObject): [ ]),
|
2019-07-20 19:53:07 -03:00
|
|
|
payable: state.payable,
|
|
|
|
stateMutability: state.stateMutability,
|
2019-05-14 18:25:46 -04:00
|
|
|
gas: (value.gas ? BigNumber.from(value.gas): null)
|
2020-04-24 03:02:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return new FunctionFragment(_constructorGuard, params);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static fromString(value: string): FunctionFragment {
|
|
|
|
let params: any = { type: "function" };
|
|
|
|
value = parseGas(value, params);
|
|
|
|
|
|
|
|
let comps = value.split(" returns ");
|
2020-04-15 16:49:03 -04:00
|
|
|
if (comps.length > 2) {
|
|
|
|
logger.throwArgumentError("invalid function string", "value", value);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
let parens = comps[0].match(regexParen);
|
2020-04-15 16:49:03 -04:00
|
|
|
if (!parens) {
|
|
|
|
logger.throwArgumentError("invalid function signature", "value", value);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
params.name = parens[1].trim();
|
2020-04-15 16:49:03 -04:00
|
|
|
if (params.name) { verifyIdentifier(params.name); }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
params.inputs = parseParams(parens[2], false);
|
|
|
|
|
|
|
|
parseModifiers(parens[3].trim(), params);
|
|
|
|
|
|
|
|
// We have outputs
|
|
|
|
if (comps.length > 1) {
|
|
|
|
let returns = comps[1].match(regexParen);
|
|
|
|
if (returns[1].trim() != "" || returns[3].trim() != "") {
|
2020-04-15 16:49:03 -04:00
|
|
|
logger.throwArgumentError("unexpected tokens", "value", value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
params.outputs = parseParams(returns[2], false);
|
|
|
|
} else {
|
|
|
|
params.outputs = [ ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return FunctionFragment.fromObject(params);
|
|
|
|
}
|
2019-06-10 22:25:46 -04:00
|
|
|
|
|
|
|
static isFunctionFragment(value: any): value is FunctionFragment {
|
|
|
|
return (value && value._isFragment && value.type === "function");
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//export class StructFragment extends Fragment {
|
|
|
|
//}
|
|
|
|
|
2021-04-22 22:29:30 -04:00
|
|
|
function checkForbidden(fragment: ErrorFragment): ErrorFragment {
|
|
|
|
const sig = fragment.format();
|
|
|
|
if (sig === "Error(string)" || sig === "Panic(uint256)") {
|
|
|
|
logger.throwArgumentError(`cannot specify user defined ${ sig } error`, "fragment", fragment);
|
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ErrorFragment extends Fragment {
|
|
|
|
|
|
|
|
format(format?: string): string {
|
|
|
|
if (!format) { format = FormatTypes.sighash; }
|
|
|
|
if (!FormatTypes[format]) {
|
|
|
|
logger.throwArgumentError("invalid format type", "format", format);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format === FormatTypes.json) {
|
|
|
|
return JSON.stringify({
|
|
|
|
type: "error",
|
|
|
|
name: this.name,
|
|
|
|
inputs: this.inputs.map((input) => JSON.parse(input.format(format))),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = "";
|
|
|
|
|
|
|
|
if (format !== FormatTypes.sighash) {
|
|
|
|
result += "error ";
|
|
|
|
}
|
|
|
|
|
|
|
|
result += this.name + "(" + this.inputs.map(
|
|
|
|
(input) => input.format(format)
|
|
|
|
).join((format === FormatTypes.full) ? ", ": ",") + ") ";
|
|
|
|
|
|
|
|
return result.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
static from(value: ErrorFragment | JsonFragment | string): ErrorFragment {
|
|
|
|
if (typeof(value) === "string") {
|
|
|
|
return ErrorFragment.fromString(value);
|
|
|
|
}
|
|
|
|
return ErrorFragment.fromObject(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromObject(value: ErrorFragment | JsonFragment): ErrorFragment {
|
|
|
|
if (ErrorFragment.isErrorFragment(value)) { return value; }
|
|
|
|
|
|
|
|
if (value.type !== "error") {
|
|
|
|
logger.throwArgumentError("invalid error object", "value", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const params: TypeCheck<_Fragment> = {
|
|
|
|
type: value.type,
|
|
|
|
name: verifyIdentifier(value.name),
|
|
|
|
inputs: (value.inputs ? value.inputs.map(ParamType.fromObject): [])
|
|
|
|
};
|
|
|
|
|
|
|
|
return checkForbidden(new ErrorFragment(_constructorGuard, params));
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromString(value: string): ErrorFragment {
|
|
|
|
let params: any = { type: "error" };
|
|
|
|
|
|
|
|
let parens = value.match(regexParen);
|
|
|
|
if (!parens) {
|
|
|
|
logger.throwArgumentError("invalid error signature", "value", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
params.name = parens[1].trim();
|
|
|
|
if (params.name) { verifyIdentifier(params.name); }
|
|
|
|
|
|
|
|
params.inputs = parseParams(parens[2], false);
|
|
|
|
|
|
|
|
return checkForbidden(ErrorFragment.fromObject(params));
|
|
|
|
}
|
|
|
|
|
|
|
|
static isErrorFragment(value: any): value is ErrorFragment {
|
|
|
|
return (value && value._isFragment && value.type === "error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
function verifyType(type: string): string {
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:47:17 -04:00
|
|
|
// See: https://github.com/ethereum/solidity/blob/1f8f1a3db93a548d0555e3e14cfc55a10e25b60e/docs/grammar/SolidityLexer.g4#L234
|
|
|
|
const regexIdentifier = new RegExp("^[a-zA-Z$_][a-zA-Z0-9$_]*$");
|
2019-05-14 18:25:46 -04:00
|
|
|
function verifyIdentifier(value: string): string {
|
|
|
|
if (!value || !value.match(regexIdentifier)) {
|
2020-04-15 16:49:03 -04:00
|
|
|
logger.throwArgumentError(`invalid identifier "${ value }"`, "value", value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const regexParen = new RegExp("^([^)(]*)\\((.*)\\)([^)(]*)$");
|
|
|
|
|
|
|
|
function splitNesting(value: string): Array<any> {
|
|
|
|
value = value.trim();
|
|
|
|
|
|
|
|
let result = [];
|
|
|
|
let accum = "";
|
|
|
|
let depth = 0;
|
|
|
|
for (let offset = 0; offset < value.length; offset++) {
|
|
|
|
let c = value[offset];
|
|
|
|
if (c === "," && depth === 0) {
|
|
|
|
result.push(accum);
|
|
|
|
accum = "";
|
|
|
|
} else {
|
|
|
|
accum += c;
|
|
|
|
if (c === "(") {
|
|
|
|
depth++;
|
|
|
|
} else if (c === ")") {
|
|
|
|
depth--;
|
|
|
|
if (depth === -1) {
|
2020-04-22 02:42:25 -04:00
|
|
|
logger.throwArgumentError("unbalanced parenthesis", "value", value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (accum) { result.push(accum); }
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|