Convert FormatType mnemonic to string literals.
This commit is contained in:
parent
f30cdf6262
commit
5bdac36a99
@ -28,19 +28,18 @@ export interface JsonFragment {
|
|||||||
readonly gas?: string;
|
readonly gas?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export enum FormatType {
|
export type FormatType =
|
||||||
// Bare formatting, as is needed for computing a sighash of an event or function
|
// Bare formatting, as is needed for computing a sighash of an event or function
|
||||||
sighash = "sighash",
|
"sighash" |
|
||||||
|
|
||||||
// Human-Readable with Minimal spacing and without names (compact human-readable)
|
// Human-Readable with Minimal spacing and without names (compact human-readable)
|
||||||
minimal = "minimal",
|
"minimal" |
|
||||||
|
|
||||||
// Human-Readable with nice spacing, including all names
|
// Human-Readable with nice spacing, including all names
|
||||||
full = "full",
|
"full" |
|
||||||
|
|
||||||
// JSON-format a la Solidity
|
// JSON-format a la Solidity
|
||||||
json = "json"
|
"json";
|
||||||
};
|
|
||||||
|
|
||||||
// [ "a", "b" ] => { "a": 1, "b": 1 }
|
// [ "a", "b" ] => { "a": 1, "b": 1 }
|
||||||
function setify(items: Array<string>): ReadonlySet<string> {
|
function setify(items: Array<string>): ReadonlySet<string> {
|
||||||
@ -502,12 +501,8 @@ export class ParamType {
|
|||||||
// - sighash: "(uint256,address)"
|
// - sighash: "(uint256,address)"
|
||||||
// - minimal: "tuple(uint256,address) indexed"
|
// - minimal: "tuple(uint256,address) indexed"
|
||||||
// - full: "tuple(uint256 foo, address bar) indexed baz"
|
// - full: "tuple(uint256 foo, address bar) indexed baz"
|
||||||
format(format: FormatType = FormatType.sighash): string {
|
format(format: FormatType = "sighash"): string {
|
||||||
if (!FormatType[format]) {
|
if (format === "json") {
|
||||||
throwArgumentError("invalid format type", "format", format);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (format === FormatType.json) {
|
|
||||||
let result: any = {
|
let result: any = {
|
||||||
type: ((this.baseType === "tuple") ? "tuple": this.type),
|
type: ((this.baseType === "tuple") ? "tuple": this.type),
|
||||||
name: (this.name || undefined)
|
name: (this.name || undefined)
|
||||||
@ -527,18 +522,18 @@ export class ParamType {
|
|||||||
result += `[${ (this.arrayLength < 0 ? "": String(this.arrayLength)) }]`;
|
result += `[${ (this.arrayLength < 0 ? "": String(this.arrayLength)) }]`;
|
||||||
} else {
|
} else {
|
||||||
if (this.isTuple()) {
|
if (this.isTuple()) {
|
||||||
if (format !== FormatType.sighash) { result += this.type; }
|
if (format !== "sighash") { result += this.type; }
|
||||||
result += "(" + this.components.map(
|
result += "(" + this.components.map(
|
||||||
(comp) => comp.format(format)
|
(comp) => comp.format(format)
|
||||||
).join((format === FormatType.full) ? ", ": ",") + ")";
|
).join((format === "full") ? ", ": ",") + ")";
|
||||||
} else {
|
} else {
|
||||||
result += this.type;
|
result += this.type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format !== FormatType.sighash) {
|
if (format !== "sighash") {
|
||||||
if (this.indexed === true) { result += " indexed"; }
|
if (this.indexed === true) { result += " indexed"; }
|
||||||
if (format === FormatType.full && this.name) {
|
if (format === "full" && this.name) {
|
||||||
result += " " + this.name;
|
result += " " + this.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -858,7 +853,7 @@ export abstract class NamedFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function joinParams(format: FormatType, params: ReadonlyArray<ParamType>): string {
|
function joinParams(format: FormatType, params: ReadonlyArray<ParamType>): string {
|
||||||
return "(" + params.map((p) => p.format(format)).join((format === FormatType.full) ? ", ": ",") + ")";
|
return "(" + params.map((p) => p.format(format)).join((format === "full") ? ", ": ",") + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ErrorFragment extends NamedFragment {
|
export class ErrorFragment extends NamedFragment {
|
||||||
@ -866,12 +861,8 @@ export class ErrorFragment extends NamedFragment {
|
|||||||
super(guard, FragmentType.error, name, inputs);
|
super(guard, FragmentType.error, name, inputs);
|
||||||
}
|
}
|
||||||
|
|
||||||
format(format: FormatType = FormatType.sighash): string {
|
format(format: FormatType = "sighash"): string {
|
||||||
if (!FormatType[format]) {
|
if (format === "json") {
|
||||||
throwArgumentError("invalid format type", "format", format);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (format === FormatType.json) {
|
|
||||||
return JSON.stringify({
|
return JSON.stringify({
|
||||||
type: "error",
|
type: "error",
|
||||||
name: this.name,
|
name: this.name,
|
||||||
@ -880,7 +871,7 @@ export class ErrorFragment extends NamedFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = [ ];
|
const result = [ ];
|
||||||
if (format !== FormatType.sighash) { result.push("error"); }
|
if (format !== "sighash") { result.push("error"); }
|
||||||
result.push(this.name + joinParams(format, this.inputs));
|
result.push(this.name + joinParams(format, this.inputs));
|
||||||
return result.join(" ");
|
return result.join(" ");
|
||||||
}
|
}
|
||||||
@ -907,12 +898,8 @@ export class EventFragment extends NamedFragment {
|
|||||||
defineProperties<EventFragment>(this, { anonymous });
|
defineProperties<EventFragment>(this, { anonymous });
|
||||||
}
|
}
|
||||||
|
|
||||||
format(format: FormatType = FormatType.sighash): string {
|
format(format: FormatType = "sighash"): string {
|
||||||
if (!FormatType[format]) {
|
if (format === "json") {
|
||||||
throwArgumentError("invalid format type", "format", format);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (format === FormatType.json) {
|
|
||||||
return JSON.stringify({
|
return JSON.stringify({
|
||||||
type: "event",
|
type: "event",
|
||||||
anonymous: this.anonymous,
|
anonymous: this.anonymous,
|
||||||
@ -922,9 +909,9 @@ export class EventFragment extends NamedFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = [ ];
|
const result = [ ];
|
||||||
if (format !== FormatType.sighash) { result.push("event"); }
|
if (format !== "sighash") { result.push("event"); }
|
||||||
result.push(this.name + joinParams(format, this.inputs));
|
result.push(this.name + joinParams(format, this.inputs));
|
||||||
if (format !== FormatType.sighash && this.anonymous) { result.push("anonymous"); }
|
if (format !== "sighash" && this.anonymous) { result.push("anonymous"); }
|
||||||
return result.join(" ");
|
return result.join(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -952,18 +939,14 @@ export class ConstructorFragment extends Fragment {
|
|||||||
defineProperties<ConstructorFragment>(this, { payable, gas });
|
defineProperties<ConstructorFragment>(this, { payable, gas });
|
||||||
}
|
}
|
||||||
|
|
||||||
format(format: FormatType = FormatType.sighash): string {
|
format(format: FormatType = "sighash"): string {
|
||||||
if (!FormatType[format]) {
|
if (format === "sighash") {
|
||||||
throwArgumentError("invalid format type", "format", format);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (format === FormatType.sighash) {
|
|
||||||
throwError("cannot format a constructor for sighash", "UNSUPPORTED_OPERATION", {
|
throwError("cannot format a constructor for sighash", "UNSUPPORTED_OPERATION", {
|
||||||
operation: "format(sighash)"
|
operation: "format(sighash)"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format === FormatType.json) {
|
if (format === "json") {
|
||||||
return JSON.stringify({
|
return JSON.stringify({
|
||||||
type: "constructor",
|
type: "constructor",
|
||||||
stateMutability: (this.payable ? "payable": "undefined"),
|
stateMutability: (this.payable ? "payable": "undefined"),
|
||||||
@ -1014,12 +997,8 @@ export class FunctionFragment extends NamedFragment {
|
|||||||
defineProperties<FunctionFragment>(this, { constant, gas, outputs, payable, stateMutability });
|
defineProperties<FunctionFragment>(this, { constant, gas, outputs, payable, stateMutability });
|
||||||
}
|
}
|
||||||
|
|
||||||
format(format: FormatType = FormatType.sighash): string {
|
format(format: FormatType = "sighash"): string {
|
||||||
if (!FormatType[format]) {
|
if (format === "json") {
|
||||||
throwArgumentError("invalid format type", "format", format);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (format === FormatType.json) {
|
|
||||||
return JSON.stringify({
|
return JSON.stringify({
|
||||||
type: "function",
|
type: "function",
|
||||||
name: this.name,
|
name: this.name,
|
||||||
@ -1034,11 +1013,11 @@ export class FunctionFragment extends NamedFragment {
|
|||||||
|
|
||||||
const result = [];
|
const result = [];
|
||||||
|
|
||||||
if (format !== FormatType.sighash) { result.push("function"); }
|
if (format !== "sighash") { result.push("function"); }
|
||||||
|
|
||||||
result.push(this.name + joinParams(format, this.inputs));
|
result.push(this.name + joinParams(format, this.inputs));
|
||||||
|
|
||||||
if (format !== FormatType.sighash) {
|
if (format !== "sighash") {
|
||||||
if (this.stateMutability !== "nonpayable") {
|
if (this.stateMutability !== "nonpayable") {
|
||||||
result.push(this.stateMutability);
|
result.push(this.stateMutability);
|
||||||
}
|
}
|
||||||
|
@ -8,12 +8,12 @@ import {
|
|||||||
|
|
||||||
import { AbiCoder, defaultAbiCoder } from "./abi-coder.js";
|
import { AbiCoder, defaultAbiCoder } from "./abi-coder.js";
|
||||||
import { checkResultErrors, Result } from "./coders/abstract-coder.js";
|
import { checkResultErrors, Result } from "./coders/abstract-coder.js";
|
||||||
import { ConstructorFragment, ErrorFragment, EventFragment, FormatType, Fragment, FunctionFragment, ParamType } from "./fragments.js";
|
import { ConstructorFragment, ErrorFragment, EventFragment, Fragment, FunctionFragment, ParamType } from "./fragments.js";
|
||||||
import { Typed } from "./typed.js";
|
import { Typed } from "./typed.js";
|
||||||
|
|
||||||
import type { BigNumberish, BytesLike } from "../utils/index.js";
|
import type { BigNumberish, BytesLike } from "../utils/index.js";
|
||||||
|
|
||||||
import type { JsonFragment } from "./fragments.js";
|
import type { FormatType, JsonFragment } from "./fragments.js";
|
||||||
|
|
||||||
|
|
||||||
export { checkResultErrors, Result };
|
export { checkResultErrors, Result };
|
||||||
@ -225,15 +225,15 @@ export class Interface {
|
|||||||
}
|
}
|
||||||
// @TODO: multi sig?
|
// @TODO: multi sig?
|
||||||
format(format?: FormatType): string | Array<string> {
|
format(format?: FormatType): string | Array<string> {
|
||||||
if (!format) { format = FormatType.full; }
|
if (!format) { format = "full"; }
|
||||||
if (format === FormatType.sighash) {
|
if (format === "sighash") {
|
||||||
throwArgumentError("interface does not support formatting sighash", "format", format);
|
throwArgumentError("interface does not support formatting sighash", "format", format);
|
||||||
}
|
}
|
||||||
|
|
||||||
const abi = this.fragments.map((f) => f.format(format));
|
const abi = this.fragments.map((f) => f.format(format));
|
||||||
|
|
||||||
// We need to re-bundle the JSON fragments a bit
|
// We need to re-bundle the JSON fragments a bit
|
||||||
if (format === FormatType.json) {
|
if (format === "json") {
|
||||||
return JSON.stringify(abi.map((j) => JSON.parse(j)));
|
return JSON.stringify(abi.map((j) => JSON.parse(j)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -895,7 +895,7 @@ export class Interface {
|
|||||||
|
|
||||||
// Maybe an interface from an older version, or from a symlinked copy
|
// Maybe an interface from an older version, or from a symlinked copy
|
||||||
if (typeof((<any>value).format) === "function") {
|
if (typeof((<any>value).format) === "function") {
|
||||||
return new Interface((<any>value).format(FormatType.json));
|
return new Interface((<any>value).format("json"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Array of fragments
|
// Array of fragments
|
||||||
|
Loading…
Reference in New Issue
Block a user