2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { getAddress } from "@ethersproject/address";
|
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
|
|
|
import { arrayify, concat, hexDataSlice, hexlify, hexZeroPad, isHexString } from "@ethersproject/bytes";
|
|
|
|
import { id } from "@ethersproject/hash";
|
|
|
|
import { keccak256 } from "@ethersproject/keccak256";
|
|
|
|
import { defineReadOnly, Description, getStatic } from "@ethersproject/properties";
|
|
|
|
import { defaultAbiCoder } from "./abi-coder";
|
2020-04-25 03:54:54 -04:00
|
|
|
import { checkResultErrors } from "./coders/abstract-coder";
|
2020-01-07 19:58:04 -05:00
|
|
|
import { ConstructorFragment, EventFragment, FormatTypes, Fragment, FunctionFragment, ParamType } from "./fragments";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2020-04-25 03:54:54 -04:00
|
|
|
export { checkResultErrors };
|
2019-08-25 02:39:20 -04:00
|
|
|
export class LogDescription extends Description {
|
|
|
|
}
|
|
|
|
export class TransactionDescription extends Description {
|
|
|
|
}
|
2021-06-24 02:13:06 -04:00
|
|
|
export class ErrorDescription extends Description {
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
export class Indexed extends Description {
|
|
|
|
static isIndexed(value) {
|
2019-06-11 17:57:04 -04:00
|
|
|
return !!(value && value._isIndexed);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2021-05-17 16:19:36 -04:00
|
|
|
const BuiltinErrors = {
|
|
|
|
"0x08c379a0": { signature: "Error(string)", name: "Error", inputs: ["string"], reason: true },
|
|
|
|
"0x4e487b71": { signature: "Panic(uint256)", name: "Panic", inputs: ["uint256"] }
|
|
|
|
};
|
2020-04-25 03:54:54 -04:00
|
|
|
function wrapAccessError(property, error) {
|
|
|
|
const wrap = new Error(`deferred error during ABI decoding triggered accessing ${property}`);
|
|
|
|
wrap.error = error;
|
|
|
|
return wrap;
|
|
|
|
}
|
2020-05-12 23:31:51 -04:00
|
|
|
/*
|
|
|
|
function checkNames(fragment: Fragment, type: "input" | "output", params: Array<ParamType>): void {
|
2020-04-25 03:54:54 -04:00
|
|
|
params.reduce((accum, param) => {
|
|
|
|
if (param.name) {
|
|
|
|
if (accum[param.name]) {
|
2020-05-12 23:31:51 -04:00
|
|
|
logger.throwArgumentError(`duplicate ${ type } parameter ${ JSON.stringify(param.name) } in ${ fragment.format("full") }`, "fragment", fragment);
|
2020-04-25 03:54:54 -04:00
|
|
|
}
|
|
|
|
accum[param.name] = true;
|
|
|
|
}
|
|
|
|
return accum;
|
2020-05-12 23:31:51 -04:00
|
|
|
}, <{ [ name: string ]: boolean }>{ });
|
2020-04-25 03:54:54 -04:00
|
|
|
}
|
2020-05-12 23:31:51 -04:00
|
|
|
*/
|
2019-08-25 02:39:20 -04:00
|
|
|
export class Interface {
|
|
|
|
constructor(fragments) {
|
|
|
|
let abi = [];
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (fragments) === "string") {
|
|
|
|
abi = JSON.parse(fragments);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
abi = fragments;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
defineReadOnly(this, "fragments", abi.map((fragment) => {
|
|
|
|
return Fragment.from(fragment);
|
|
|
|
}).filter((fragment) => (fragment != null)));
|
2021-10-16 02:29:27 -04:00
|
|
|
defineReadOnly(this, "_abiCoder", getStatic(new.target, "getAbiCoder")());
|
2019-08-25 02:39:20 -04:00
|
|
|
defineReadOnly(this, "functions", {});
|
|
|
|
defineReadOnly(this, "errors", {});
|
|
|
|
defineReadOnly(this, "events", {});
|
|
|
|
defineReadOnly(this, "structs", {});
|
2019-05-14 18:48:48 -04:00
|
|
|
// Add all fragments by their signature
|
2019-08-25 02:39:20 -04:00
|
|
|
this.fragments.forEach((fragment) => {
|
|
|
|
let bucket = null;
|
2019-05-14 18:48:48 -04:00
|
|
|
switch (fragment.type) {
|
|
|
|
case "constructor":
|
2019-08-25 02:39:20 -04:00
|
|
|
if (this.deploy) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.warn("duplicate definition - constructor");
|
2019-05-14 18:48:48 -04:00
|
|
|
return;
|
|
|
|
}
|
2020-05-12 23:31:51 -04:00
|
|
|
//checkNames(fragment, "input", fragment.inputs);
|
2019-08-25 02:39:20 -04:00
|
|
|
defineReadOnly(this, "deploy", fragment);
|
2019-05-14 18:48:48 -04:00
|
|
|
return;
|
|
|
|
case "function":
|
2020-05-12 23:31:51 -04:00
|
|
|
//checkNames(fragment, "input", fragment.inputs);
|
|
|
|
//checkNames(fragment, "output", (<FunctionFragment>fragment).outputs);
|
2019-08-25 02:39:20 -04:00
|
|
|
bucket = this.functions;
|
2019-05-14 18:48:48 -04:00
|
|
|
break;
|
|
|
|
case "event":
|
2020-05-12 23:31:51 -04:00
|
|
|
//checkNames(fragment, "input", fragment.inputs);
|
2019-08-25 02:39:20 -04:00
|
|
|
bucket = this.events;
|
2019-05-14 18:48:48 -04:00
|
|
|
break;
|
2021-05-17 16:19:36 -04:00
|
|
|
case "error":
|
|
|
|
bucket = this.errors;
|
|
|
|
break;
|
2019-05-14 18:48:48 -04:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let signature = fragment.format();
|
2019-05-14 18:48:48 -04:00
|
|
|
if (bucket[signature]) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.warn("duplicate definition - " + signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bucket[signature] = fragment;
|
|
|
|
});
|
2020-04-25 03:54:54 -04:00
|
|
|
// If we do not have a constructor add a default
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!this.deploy) {
|
2020-04-25 03:54:54 -04:00
|
|
|
defineReadOnly(this, "deploy", ConstructorFragment.from({
|
|
|
|
payable: false,
|
|
|
|
type: "constructor"
|
|
|
|
}));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
defineReadOnly(this, "_isInterface", true);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
format(format) {
|
|
|
|
if (!format) {
|
|
|
|
format = FormatTypes.full;
|
|
|
|
}
|
|
|
|
if (format === FormatTypes.sighash) {
|
2020-04-23 23:35:39 -04:00
|
|
|
logger.throwArgumentError("interface does not support formatting sighash", "format", format);
|
2020-01-07 19:58:04 -05:00
|
|
|
}
|
|
|
|
const abi = this.fragments.map((fragment) => fragment.format(format));
|
|
|
|
// We need to re-bundle the JSON fragments a bit
|
|
|
|
if (format === FormatTypes.json) {
|
|
|
|
return JSON.stringify(abi.map((j) => JSON.parse(j)));
|
|
|
|
}
|
|
|
|
return abi;
|
|
|
|
}
|
|
|
|
// Sub-classes can override these to handle other blockchains
|
2019-08-25 02:39:20 -04:00
|
|
|
static getAbiCoder() {
|
|
|
|
return defaultAbiCoder;
|
|
|
|
}
|
|
|
|
static getAddress(address) {
|
|
|
|
return getAddress(address);
|
|
|
|
}
|
2021-05-17 16:19:36 -04:00
|
|
|
static getSighash(fragment) {
|
|
|
|
return hexDataSlice(id(fragment.format()), 0, 4);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-02-26 10:06:48 +00:00
|
|
|
static getEventTopic(eventFragment) {
|
2019-08-25 02:39:20 -04:00
|
|
|
return id(eventFragment.format());
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Find a function definition by any means necessary (unless it is ambiguous)
|
2019-08-25 02:39:20 -04:00
|
|
|
getFunction(nameOrSignatureOrSighash) {
|
|
|
|
if (isHexString(nameOrSignatureOrSighash)) {
|
2019-11-20 18:57:38 +09:00
|
|
|
for (const name in this.functions) {
|
|
|
|
if (nameOrSignatureOrSighash === this.getSighash(name)) {
|
|
|
|
return this.functions[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.throwArgumentError("no matching function", "sighash", nameOrSignatureOrSighash);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
// It is a bare name, look up the function (will return null if ambiguous)
|
|
|
|
if (nameOrSignatureOrSighash.indexOf("(") === -1) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const name = nameOrSignatureOrSighash.trim();
|
|
|
|
const matching = Object.keys(this.functions).filter((f) => (f.split("(" /* fix:) */)[0] === name));
|
|
|
|
if (matching.length === 0) {
|
|
|
|
logger.throwArgumentError("no matching function", "name", name);
|
|
|
|
}
|
|
|
|
else if (matching.length > 1) {
|
|
|
|
logger.throwArgumentError("multiple matching functions", "name", name);
|
|
|
|
}
|
|
|
|
return this.functions[matching[0]];
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
// Normalize the signature and lookup the function
|
2019-11-20 18:57:38 +09:00
|
|
|
const result = this.functions[FunctionFragment.fromString(nameOrSignatureOrSighash).format()];
|
|
|
|
if (!result) {
|
|
|
|
logger.throwArgumentError("no matching function", "signature", nameOrSignatureOrSighash);
|
|
|
|
}
|
|
|
|
return result;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Find an event definition by any means necessary (unless it is ambiguous)
|
2019-08-25 02:39:20 -04:00
|
|
|
getEvent(nameOrSignatureOrTopic) {
|
|
|
|
if (isHexString(nameOrSignatureOrTopic)) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const topichash = nameOrSignatureOrTopic.toLowerCase();
|
|
|
|
for (const name in this.events) {
|
|
|
|
if (topichash === this.getEventTopic(name)) {
|
|
|
|
return this.events[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.throwArgumentError("no matching event", "topichash", topichash);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
// It is a bare name, look up the function (will return null if ambiguous)
|
|
|
|
if (nameOrSignatureOrTopic.indexOf("(") === -1) {
|
2019-11-20 18:57:38 +09:00
|
|
|
const name = nameOrSignatureOrTopic.trim();
|
|
|
|
const matching = Object.keys(this.events).filter((f) => (f.split("(" /* fix:) */)[0] === name));
|
|
|
|
if (matching.length === 0) {
|
|
|
|
logger.throwArgumentError("no matching event", "name", name);
|
|
|
|
}
|
|
|
|
else if (matching.length > 1) {
|
|
|
|
logger.throwArgumentError("multiple matching events", "name", name);
|
|
|
|
}
|
|
|
|
return this.events[matching[0]];
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
// Normalize the signature and lookup the function
|
2019-11-20 18:57:38 +09:00
|
|
|
const result = this.events[EventFragment.fromString(nameOrSignatureOrTopic).format()];
|
|
|
|
if (!result) {
|
|
|
|
logger.throwArgumentError("no matching event", "signature", nameOrSignatureOrTopic);
|
|
|
|
}
|
|
|
|
return result;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2021-05-17 16:19:36 -04:00
|
|
|
// Find a function definition by any means necessary (unless it is ambiguous)
|
|
|
|
getError(nameOrSignatureOrSighash) {
|
|
|
|
if (isHexString(nameOrSignatureOrSighash)) {
|
|
|
|
const getSighash = getStatic(this.constructor, "getSighash");
|
|
|
|
for (const name in this.errors) {
|
|
|
|
const error = this.errors[name];
|
|
|
|
if (nameOrSignatureOrSighash === getSighash(error)) {
|
|
|
|
return this.errors[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.throwArgumentError("no matching error", "sighash", nameOrSignatureOrSighash);
|
|
|
|
}
|
|
|
|
// It is a bare name, look up the function (will return null if ambiguous)
|
|
|
|
if (nameOrSignatureOrSighash.indexOf("(") === -1) {
|
|
|
|
const name = nameOrSignatureOrSighash.trim();
|
|
|
|
const matching = Object.keys(this.errors).filter((f) => (f.split("(" /* fix:) */)[0] === name));
|
|
|
|
if (matching.length === 0) {
|
|
|
|
logger.throwArgumentError("no matching error", "name", name);
|
|
|
|
}
|
|
|
|
else if (matching.length > 1) {
|
|
|
|
logger.throwArgumentError("multiple matching errors", "name", name);
|
|
|
|
}
|
|
|
|
return this.errors[matching[0]];
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
// Normalize the signature and lookup the function
|
2021-05-17 16:19:36 -04:00
|
|
|
const result = this.errors[FunctionFragment.fromString(nameOrSignatureOrSighash).format()];
|
|
|
|
if (!result) {
|
|
|
|
logger.throwArgumentError("no matching error", "signature", nameOrSignatureOrSighash);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Get the sighash (the bytes4 selector) used by Solidity to identify a function
|
2021-06-24 02:13:06 -04:00
|
|
|
getSighash(fragment) {
|
|
|
|
if (typeof (fragment) === "string") {
|
|
|
|
try {
|
|
|
|
fragment = this.getFunction(fragment);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
try {
|
|
|
|
fragment = this.getError(fragment);
|
|
|
|
}
|
|
|
|
catch (_) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-06-24 02:13:06 -04:00
|
|
|
return getStatic(this.constructor, "getSighash")(fragment);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Get the topic (the bytes32 hash) used by Solidity to identify an event
|
2019-08-25 02:39:20 -04:00
|
|
|
getEventTopic(eventFragment) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (eventFragment) === "string") {
|
|
|
|
eventFragment = this.getEvent(eventFragment);
|
|
|
|
}
|
2020-02-26 10:06:48 +00:00
|
|
|
return getStatic(this.constructor, "getEventTopic")(eventFragment);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2019-09-28 02:36:19 -04:00
|
|
|
_decodeParams(params, data) {
|
|
|
|
return this._abiCoder.decode(params, data);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
_encodeParams(params, values) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return this._abiCoder.encode(params, values);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
encodeDeploy(values) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return this._encodeParams(this.deploy.inputs, values || []);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2021-06-26 01:55:19 -04:00
|
|
|
decodeErrorResult(fragment, data) {
|
2021-06-24 02:13:06 -04:00
|
|
|
if (typeof (fragment) === "string") {
|
|
|
|
fragment = this.getError(fragment);
|
|
|
|
}
|
|
|
|
const bytes = arrayify(data);
|
|
|
|
if (hexlify(bytes.slice(0, 4)) !== this.getSighash(fragment)) {
|
|
|
|
logger.throwArgumentError(`data signature does not match error ${fragment.name}.`, "data", hexlify(bytes));
|
|
|
|
}
|
|
|
|
return this._decodeParams(fragment.inputs, bytes.slice(4));
|
|
|
|
}
|
2021-06-26 01:55:19 -04:00
|
|
|
encodeErrorResult(fragment, values) {
|
2021-06-24 02:13:06 -04:00
|
|
|
if (typeof (fragment) === "string") {
|
|
|
|
fragment = this.getError(fragment);
|
|
|
|
}
|
|
|
|
return hexlify(concat([
|
|
|
|
this.getSighash(fragment),
|
|
|
|
this._encodeParams(fragment.inputs, values || [])
|
|
|
|
]));
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Decode the data for a function call (e.g. tx.data)
|
2019-09-28 02:36:19 -04:00
|
|
|
decodeFunctionData(functionFragment, data) {
|
|
|
|
if (typeof (functionFragment) === "string") {
|
|
|
|
functionFragment = this.getFunction(functionFragment);
|
|
|
|
}
|
|
|
|
const bytes = arrayify(data);
|
|
|
|
if (hexlify(bytes.slice(0, 4)) !== this.getSighash(functionFragment)) {
|
|
|
|
logger.throwArgumentError(`data signature does not match function ${functionFragment.name}.`, "data", hexlify(bytes));
|
|
|
|
}
|
|
|
|
return this._decodeParams(functionFragment.inputs, bytes.slice(4));
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Encode the data for a function call (e.g. tx.data)
|
2019-08-25 02:39:20 -04:00
|
|
|
encodeFunctionData(functionFragment, values) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (functionFragment) === "string") {
|
|
|
|
functionFragment = this.getFunction(functionFragment);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
return hexlify(concat([
|
2019-05-14 18:48:48 -04:00
|
|
|
this.getSighash(functionFragment),
|
|
|
|
this._encodeParams(functionFragment.inputs, values || [])
|
|
|
|
]));
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Decode the result from a function call (e.g. from eth_call)
|
2019-08-25 02:39:20 -04:00
|
|
|
decodeFunctionResult(functionFragment, data) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (functionFragment) === "string") {
|
|
|
|
functionFragment = this.getFunction(functionFragment);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let bytes = arrayify(data);
|
|
|
|
let reason = null;
|
2022-04-13 00:25:25 -04:00
|
|
|
let message = "";
|
2021-05-17 16:19:36 -04:00
|
|
|
let errorArgs = null;
|
|
|
|
let errorName = null;
|
2019-08-25 02:39:20 -04:00
|
|
|
let errorSignature = null;
|
2019-05-14 18:48:48 -04:00
|
|
|
switch (bytes.length % this._abiCoder._getWordSize()) {
|
|
|
|
case 0:
|
|
|
|
try {
|
|
|
|
return this._abiCoder.decode(functionFragment.outputs, bytes);
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
break;
|
2021-05-17 16:19:36 -04:00
|
|
|
case 4: {
|
|
|
|
const selector = hexlify(bytes.slice(0, 4));
|
|
|
|
const builtin = BuiltinErrors[selector];
|
|
|
|
if (builtin) {
|
|
|
|
errorArgs = this._abiCoder.decode(builtin.inputs, bytes.slice(4));
|
|
|
|
errorName = builtin.name;
|
|
|
|
errorSignature = builtin.signature;
|
|
|
|
if (builtin.reason) {
|
|
|
|
reason = errorArgs[0];
|
|
|
|
}
|
2022-04-13 00:25:25 -04:00
|
|
|
if (errorName === "Error") {
|
|
|
|
message = `; VM Exception while processing transaction: reverted with reason string ${JSON.stringify(errorArgs[0])}`;
|
|
|
|
}
|
|
|
|
else if (errorName === "Panic") {
|
|
|
|
message = `; VM Exception while processing transaction: reverted with panic code ${errorArgs[0]}`;
|
|
|
|
}
|
2021-05-17 16:19:36 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
const error = this.getError(selector);
|
|
|
|
errorArgs = this._abiCoder.decode(error.inputs, bytes.slice(4));
|
|
|
|
errorName = error.name;
|
|
|
|
errorSignature = error.format();
|
|
|
|
}
|
2022-03-09 02:56:08 -05:00
|
|
|
catch (error) { }
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
break;
|
2021-05-17 16:19:36 -04:00
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2022-04-13 00:25:25 -04:00
|
|
|
return logger.throwError("call revert exception" + message, Logger.errors.CALL_EXCEPTION, {
|
2019-05-14 18:48:48 -04:00
|
|
|
method: functionFragment.format(),
|
2022-04-13 00:25:25 -04:00
|
|
|
data: hexlify(data), errorArgs, errorName, errorSignature, reason
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Encode the result for a function call (e.g. for eth_call)
|
2019-09-28 02:36:19 -04:00
|
|
|
encodeFunctionResult(functionFragment, values) {
|
|
|
|
if (typeof (functionFragment) === "string") {
|
|
|
|
functionFragment = this.getFunction(functionFragment);
|
|
|
|
}
|
|
|
|
return hexlify(this._abiCoder.encode(functionFragment.outputs, values || []));
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Create the filter for the event with search criteria (e.g. for eth_filterLog)
|
2019-08-25 02:39:20 -04:00
|
|
|
encodeFilterTopics(eventFragment, values) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (eventFragment) === "string") {
|
|
|
|
eventFragment = this.getEvent(eventFragment);
|
|
|
|
}
|
|
|
|
if (values.length > eventFragment.inputs.length) {
|
2019-08-25 02:39:20 -04:00
|
|
|
logger.throwError("too many arguments for " + eventFragment.format(), Logger.errors.UNEXPECTED_ARGUMENT, {
|
2019-05-14 18:48:48 -04:00
|
|
|
argument: "values",
|
|
|
|
value: values
|
|
|
|
});
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let topics = [];
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!eventFragment.anonymous) {
|
|
|
|
topics.push(this.getEventTopic(eventFragment));
|
|
|
|
}
|
2020-05-01 17:00:44 -04:00
|
|
|
const encodeTopic = (param, value) => {
|
|
|
|
if (param.type === "string") {
|
|
|
|
return id(value);
|
|
|
|
}
|
|
|
|
else if (param.type === "bytes") {
|
|
|
|
return keccak256(hexlify(value));
|
|
|
|
}
|
2022-06-17 02:02:09 -04:00
|
|
|
if (param.type === "bool" && typeof (value) === "boolean") {
|
|
|
|
value = (value ? "0x01" : "0x00");
|
|
|
|
}
|
|
|
|
if (param.type.match(/^u?int/)) {
|
|
|
|
value = BigNumber.from(value).toHexString();
|
|
|
|
}
|
2020-05-01 17:00:44 -04:00
|
|
|
// Check addresses are valid
|
|
|
|
if (param.type === "address") {
|
|
|
|
this._abiCoder.encode(["address"], [value]);
|
|
|
|
}
|
|
|
|
return hexZeroPad(hexlify(value), 32);
|
|
|
|
};
|
2019-08-25 02:39:20 -04:00
|
|
|
values.forEach((value, index) => {
|
|
|
|
let param = eventFragment.inputs[index];
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!param.indexed) {
|
|
|
|
if (value != null) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("cannot filter non-indexed parameters; must be null", ("contract." + param.name), value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (value == null) {
|
|
|
|
topics.push(null);
|
|
|
|
}
|
2020-05-01 17:00:44 -04:00
|
|
|
else if (param.baseType === "array" || param.baseType === "tuple") {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("filtering with tuples or arrays not supported", ("contract." + param.name), value);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-01 17:00:44 -04:00
|
|
|
else if (Array.isArray(value)) {
|
|
|
|
topics.push(value.map((value) => encodeTopic(param, value)));
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
else {
|
2020-05-01 17:00:44 -04:00
|
|
|
topics.push(encodeTopic(param, value));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// Trim off trailing nulls
|
|
|
|
while (topics.length && topics[topics.length - 1] === null) {
|
|
|
|
topics.pop();
|
|
|
|
}
|
|
|
|
return topics;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-04-25 03:54:54 -04:00
|
|
|
encodeEventLog(eventFragment, values) {
|
|
|
|
if (typeof (eventFragment) === "string") {
|
|
|
|
eventFragment = this.getEvent(eventFragment);
|
|
|
|
}
|
|
|
|
const topics = [];
|
|
|
|
const dataTypes = [];
|
|
|
|
const dataValues = [];
|
|
|
|
if (!eventFragment.anonymous) {
|
|
|
|
topics.push(this.getEventTopic(eventFragment));
|
|
|
|
}
|
|
|
|
if (values.length !== eventFragment.inputs.length) {
|
|
|
|
logger.throwArgumentError("event arguments/values mismatch", "values", values);
|
|
|
|
}
|
|
|
|
eventFragment.inputs.forEach((param, index) => {
|
|
|
|
const value = values[index];
|
|
|
|
if (param.indexed) {
|
|
|
|
if (param.type === "string") {
|
|
|
|
topics.push(id(value));
|
|
|
|
}
|
|
|
|
else if (param.type === "bytes") {
|
|
|
|
topics.push(keccak256(value));
|
|
|
|
}
|
|
|
|
else if (param.baseType === "tuple" || param.baseType === "array") {
|
2021-10-16 02:29:27 -04:00
|
|
|
// @TODO
|
2020-04-25 03:54:54 -04:00
|
|
|
throw new Error("not implemented");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
topics.push(this._abiCoder.encode([param.type], [value]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dataTypes.push(param);
|
|
|
|
dataValues.push(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
data: this._abiCoder.encode(dataTypes, dataValues),
|
|
|
|
topics: topics
|
|
|
|
};
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Decode a filter for the event and the search criteria
|
2019-08-25 02:39:20 -04:00
|
|
|
decodeEventLog(eventFragment, data, topics) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (eventFragment) === "string") {
|
|
|
|
eventFragment = this.getEvent(eventFragment);
|
|
|
|
}
|
|
|
|
if (topics != null && !eventFragment.anonymous) {
|
2019-08-25 02:39:20 -04:00
|
|
|
let topicHash = this.getEventTopic(eventFragment);
|
|
|
|
if (!isHexString(topics[0], 32) || topics[0].toLowerCase() !== topicHash) {
|
|
|
|
logger.throwError("fragment/topic mismatch", Logger.errors.INVALID_ARGUMENT, { argument: "topics[0]", expected: topicHash, value: topics[0] });
|
2019-05-23 19:13:44 -04:00
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
topics = topics.slice(1);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
let indexed = [];
|
|
|
|
let nonIndexed = [];
|
|
|
|
let dynamic = [];
|
|
|
|
eventFragment.inputs.forEach((param, index) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (param.indexed) {
|
|
|
|
if (param.type === "string" || param.type === "bytes" || param.baseType === "tuple" || param.baseType === "array") {
|
2019-08-25 02:39:20 -04:00
|
|
|
indexed.push(ParamType.fromObject({ type: "bytes32", name: param.name }));
|
2019-05-14 18:48:48 -04:00
|
|
|
dynamic.push(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
indexed.push(param);
|
|
|
|
dynamic.push(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nonIndexed.push(param);
|
|
|
|
dynamic.push(false);
|
|
|
|
}
|
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
let resultIndexed = (topics != null) ? this._abiCoder.decode(indexed, concat(topics)) : null;
|
2020-09-07 19:55:52 -04:00
|
|
|
let resultNonIndexed = this._abiCoder.decode(nonIndexed, data, true);
|
2019-08-25 02:39:20 -04:00
|
|
|
let result = [];
|
|
|
|
let nonIndexedIndex = 0, indexedIndex = 0;
|
|
|
|
eventFragment.inputs.forEach((param, index) => {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (param.indexed) {
|
|
|
|
if (resultIndexed == null) {
|
2019-06-11 17:57:04 -04:00
|
|
|
result[index] = new Indexed({ _isIndexed: true, hash: null });
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
else if (dynamic[index]) {
|
2019-06-11 17:57:04 -04:00
|
|
|
result[index] = new Indexed({ _isIndexed: true, hash: resultIndexed[indexedIndex++] });
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
else {
|
2020-04-25 03:54:54 -04:00
|
|
|
try {
|
|
|
|
result[index] = resultIndexed[indexedIndex++];
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
result[index] = error;
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-04-25 03:54:54 -04:00
|
|
|
try {
|
|
|
|
result[index] = resultNonIndexed[nonIndexedIndex++];
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
result[index] = error;
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-04-25 03:54:54 -04:00
|
|
|
// Add the keyword argument if named and safe
|
2020-01-07 19:58:04 -05:00
|
|
|
if (param.name && result[param.name] == null) {
|
2020-04-25 03:54:54 -04:00
|
|
|
const value = result[index];
|
|
|
|
// Make error named values throw on access
|
|
|
|
if (value instanceof Error) {
|
|
|
|
Object.defineProperty(result, param.name, {
|
2021-08-24 16:15:51 -03:00
|
|
|
enumerable: true,
|
2020-04-25 03:54:54 -04:00
|
|
|
get: () => { throw wrapAccessError(`property ${JSON.stringify(param.name)}`, value); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result[param.name] = value;
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2020-04-25 03:54:54 -04:00
|
|
|
// Make all error indexed values throw on access
|
|
|
|
for (let i = 0; i < result.length; i++) {
|
|
|
|
const value = result[i];
|
|
|
|
if (value instanceof Error) {
|
|
|
|
Object.defineProperty(result, i, {
|
2021-08-24 16:15:51 -03:00
|
|
|
enumerable: true,
|
2020-04-25 03:54:54 -04:00
|
|
|
get: () => { throw wrapAccessError(`index ${i}`, value); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 17:24:33 -04:00
|
|
|
return Object.freeze(result);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
// Given a transaction, find the matching function fragment (if any) and
|
|
|
|
// determine all its properties and call parameters
|
2019-08-25 02:39:20 -04:00
|
|
|
parseTransaction(tx) {
|
|
|
|
let fragment = this.getFunction(tx.data.substring(0, 10).toLowerCase());
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!fragment) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new TransactionDescription({
|
|
|
|
args: this._abiCoder.decode(fragment.inputs, "0x" + tx.data.substring(10)),
|
|
|
|
functionFragment: fragment,
|
|
|
|
name: fragment.name,
|
|
|
|
signature: fragment.format(),
|
|
|
|
sighash: this.getSighash(fragment),
|
2019-08-25 02:39:20 -04:00
|
|
|
value: BigNumber.from(tx.value || "0"),
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2021-05-17 16:19:36 -04:00
|
|
|
// @TODO
|
|
|
|
//parseCallResult(data: BytesLike): ??
|
2020-01-07 19:58:04 -05:00
|
|
|
// Given an event log, find the matching event fragment (if any) and
|
|
|
|
// determine all its properties and values
|
2019-08-25 02:39:20 -04:00
|
|
|
parseLog(log) {
|
|
|
|
let fragment = this.getEvent(log.topics[0]);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!fragment || fragment.anonymous) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// @TODO: If anonymous, and the only method, and the input count matches, should we parse?
|
2020-01-07 19:58:04 -05:00
|
|
|
// Probably not, because just because it is the only event in the ABI does
|
2021-10-16 02:29:27 -04:00
|
|
|
// not mean we have the full ABI; maybe just a fragment?
|
2019-05-14 18:48:48 -04:00
|
|
|
return new LogDescription({
|
|
|
|
eventFragment: fragment,
|
|
|
|
name: fragment.name,
|
|
|
|
signature: fragment.format(),
|
|
|
|
topic: this.getEventTopic(fragment),
|
2020-01-07 19:58:04 -05:00
|
|
|
args: this.decodeEventLog(fragment, log.data, log.topics)
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2021-06-24 02:13:06 -04:00
|
|
|
parseError(data) {
|
|
|
|
const hexData = hexlify(data);
|
|
|
|
let fragment = this.getError(hexData.substring(0, 10).toLowerCase());
|
|
|
|
if (!fragment) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new ErrorDescription({
|
|
|
|
args: this._abiCoder.decode(fragment.inputs, "0x" + hexData.substring(10)),
|
|
|
|
errorFragment: fragment,
|
|
|
|
name: fragment.name,
|
|
|
|
signature: fragment.format(),
|
|
|
|
sighash: this.getSighash(fragment),
|
|
|
|
});
|
|
|
|
}
|
2019-06-11 17:57:04 -04:00
|
|
|
/*
|
|
|
|
static from(value: Array<Fragment | string | JsonAbi> | string | Interface) {
|
|
|
|
if (Interface.isInterface(value)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (typeof(value) === "string") {
|
|
|
|
return new Interface(JSON.parse(value));
|
|
|
|
}
|
|
|
|
return new Interface(value);
|
|
|
|
}
|
|
|
|
*/
|
2019-08-25 02:39:20 -04:00
|
|
|
static isInterface(value) {
|
2019-06-11 17:57:04 -04:00
|
|
|
return !!(value && value._isInterface);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=interface.js.map
|