2019-08-25 02:39:20 -04:00
|
|
|
"use strict";
|
2020-04-23 23:35:39 -04:00
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
2020-04-25 03:54:54 -04:00
|
|
|
import { checkResultErrors, Indexed, Interface } from "@ethersproject/abi";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { Provider } from "@ethersproject/abstract-provider";
|
|
|
|
import { Signer, VoidSigner } from "@ethersproject/abstract-signer";
|
2020-06-01 05:04:12 -04:00
|
|
|
import { getAddress, getContractAddress } from "@ethersproject/address";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
|
|
|
import { concat, hexlify, isBytes, isHexString } from "@ethersproject/bytes";
|
2020-06-01 05:04:12 -04:00
|
|
|
//import { AddressZero } from "@ethersproject/constants";
|
2019-08-25 02:39:20 -04:00
|
|
|
import { defineReadOnly, deepCopy, getStatic, resolveProperties, shallowCopy } from "@ethersproject/properties";
|
2020-06-01 05:04:12 -04:00
|
|
|
// @TOOD remove dependences transactions
|
2019-08-25 02:39:20 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
2020-06-01 05:04:12 -04:00
|
|
|
;
|
|
|
|
;
|
2019-08-25 02:39:20 -04:00
|
|
|
///////////////////////////////
|
|
|
|
const allowedTransactionKeys = {
|
|
|
|
chainId: true, data: true, from: true, gasLimit: true, gasPrice: true, nonce: true, to: true, value: true
|
|
|
|
};
|
2020-06-01 05:04:12 -04:00
|
|
|
function resolveName(resolver, nameOrPromise) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const name = yield nameOrPromise;
|
|
|
|
// If it is already an address, just use it (after adding checksum)
|
|
|
|
try {
|
|
|
|
return getAddress(name);
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
if (!resolver) {
|
|
|
|
logger.throwError("a provider or signer is needed to resolve ENS names", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "resolveName"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return yield resolver.resolveName(name);
|
|
|
|
});
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
// Recursively replaces ENS names with promises to resolve the name and resolves all properties
|
2020-06-01 05:04:12 -04:00
|
|
|
function resolveAddresses(resolver, value, paramType) {
|
2019-08-25 02:39:20 -04:00
|
|
|
if (Array.isArray(paramType)) {
|
|
|
|
return Promise.all(paramType.map((paramType, index) => {
|
2020-06-01 05:04:12 -04:00
|
|
|
return resolveAddresses(resolver, ((Array.isArray(value)) ? value[index] : value[paramType.name]), paramType);
|
2019-08-25 02:39:20 -04:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (paramType.type === "address") {
|
2020-06-01 05:04:12 -04:00
|
|
|
return resolveName(resolver, value);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
if (paramType.type === "tuple") {
|
2020-06-01 05:04:12 -04:00
|
|
|
return resolveAddresses(resolver, value, paramType.components);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
if (paramType.baseType === "array") {
|
|
|
|
if (!Array.isArray(value)) {
|
|
|
|
throw new Error("invalid value for array");
|
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
return Promise.all(value.map((v) => resolveAddresses(resolver, v, paramType.arrayChildren)));
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
return Promise.resolve(value);
|
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
function populateTransaction(contract, fragment, args) {
|
2020-05-29 21:27:59 -04:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-01 05:04:12 -04:00
|
|
|
// If an extra argument is given, it is overrides
|
|
|
|
let overrides = {};
|
|
|
|
if (args.length === fragment.inputs.length + 1 && typeof (args[args.length - 1]) === "object") {
|
|
|
|
overrides = shallowCopy(args.pop());
|
|
|
|
}
|
|
|
|
// Make sure the parameter count matches
|
|
|
|
logger.checkArgumentCount(args.length, fragment.inputs.length, "passed to contract");
|
|
|
|
// Populate "from" override (allow promises)
|
|
|
|
if (contract.signer) {
|
|
|
|
if (overrides.from) {
|
|
|
|
// Contracts with a Signer are from the Signer's frame-of-reference;
|
|
|
|
// but we allow overriding "from" if it matches the signer
|
|
|
|
overrides.from = resolveProperties({
|
|
|
|
override: resolveName(contract.signer, overrides.from),
|
|
|
|
signer: contract.signer.getAddress()
|
|
|
|
}).then((check) => __awaiter(this, void 0, void 0, function* () {
|
|
|
|
if (getAddress(check.signer) !== check.override) {
|
|
|
|
logger.throwError("Contract with a Signer cannot override from", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "overrides.from"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return check.override;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
overrides.from = contract.signer.getAddress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (overrides.from) {
|
|
|
|
overrides.from = resolveName(contract.provider, overrides.from);
|
|
|
|
//} else {
|
|
|
|
// Contracts without a signer can override "from", and if
|
|
|
|
// unspecified the zero address is used
|
|
|
|
//overrides.from = AddressZero;
|
|
|
|
}
|
|
|
|
// Wait for all dependencies to be resolved (prefer the signer over the provider)
|
2020-05-29 21:27:59 -04:00
|
|
|
const resolved = yield resolveProperties({
|
|
|
|
args: resolveAddresses(contract.signer || contract.provider, args, fragment.inputs),
|
|
|
|
address: contract.resolvedAddress,
|
|
|
|
overrides: (resolveProperties(overrides) || {})
|
|
|
|
});
|
|
|
|
// The ABI coded transaction
|
|
|
|
const tx = {
|
|
|
|
data: contract.interface.encodeFunctionData(fragment, resolved.args),
|
|
|
|
to: resolved.address
|
|
|
|
};
|
|
|
|
// Resolved Overrides
|
|
|
|
const ro = resolved.overrides;
|
2020-06-01 05:04:12 -04:00
|
|
|
// Populate simple overrides
|
2020-05-29 21:27:59 -04:00
|
|
|
if (ro.nonce != null) {
|
|
|
|
tx.nonce = BigNumber.from(ro.nonce).toNumber();
|
|
|
|
}
|
|
|
|
if (ro.gasLimit != null) {
|
|
|
|
tx.gasLimit = BigNumber.from(ro.gasLimit);
|
|
|
|
}
|
|
|
|
if (ro.gasPrice != null) {
|
|
|
|
tx.gasPrice = BigNumber.from(ro.gasPrice);
|
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
if (ro.from != null) {
|
|
|
|
tx.from = ro.from;
|
|
|
|
}
|
|
|
|
// If there was no "gasLimit" override, but the ABI specifies a default, use it
|
2020-05-29 21:27:59 -04:00
|
|
|
if (tx.gasLimit == null && fragment.gas != null) {
|
|
|
|
tx.gasLimit = BigNumber.from(fragment.gas).add(21000);
|
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
// Populate "value" override
|
|
|
|
if (ro.value) {
|
|
|
|
const roValue = BigNumber.from(ro.value);
|
|
|
|
if (!roValue.isZero() && !fragment.payable) {
|
|
|
|
logger.throwError("non-payable method cannot override value", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "overrides.value",
|
|
|
|
value: overrides.value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
tx.value = roValue;
|
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
// Remvoe the overrides
|
|
|
|
delete overrides.nonce;
|
|
|
|
delete overrides.gasLimit;
|
|
|
|
delete overrides.gasPrice;
|
2020-06-01 05:04:12 -04:00
|
|
|
delete overrides.from;
|
|
|
|
delete overrides.value;
|
2020-05-29 21:27:59 -04:00
|
|
|
// Make sure there are no stray overrides, which may indicate a
|
|
|
|
// typo or using an unsupported key.
|
2020-06-03 03:47:17 -04:00
|
|
|
const leftovers = Object.keys(overrides).filter((key) => (overrides[key] != null));
|
2020-05-29 21:27:59 -04:00
|
|
|
if (leftovers.length) {
|
|
|
|
logger.throwError(`cannot override ${leftovers.map((l) => JSON.stringify(l)).join(",")}`, Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "overrides",
|
2020-06-01 05:04:12 -04:00
|
|
|
overrides: leftovers
|
2020-05-29 21:27:59 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return tx;
|
|
|
|
});
|
2019-11-20 18:57:38 +09:00
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
function buildPopulate(contract, fragment) {
|
|
|
|
return function (...args) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-01 05:04:12 -04:00
|
|
|
return populateTransaction(contract, fragment, args);
|
2020-05-29 21:27:59 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function buildEstimate(contract, fragment) {
|
|
|
|
const signerOrProvider = (contract.signer || contract.provider);
|
|
|
|
return function (...args) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
if (!signerOrProvider) {
|
2020-06-01 05:04:12 -04:00
|
|
|
logger.throwError("estimate require a provider or signer", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "estimateGas"
|
|
|
|
});
|
2020-05-29 21:27:59 -04:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
const tx = yield populateTransaction(contract, fragment, args);
|
2020-05-29 21:27:59 -04:00
|
|
|
return yield signerOrProvider.estimateGas(tx);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function buildCall(contract, fragment, collapseSimple) {
|
|
|
|
const signerOrProvider = (contract.signer || contract.provider);
|
|
|
|
return function (...args) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2020-06-01 05:04:12 -04:00
|
|
|
// Extract the "blockTag" override if present
|
2020-05-29 21:27:59 -04:00
|
|
|
let blockTag = undefined;
|
|
|
|
if (args.length === fragment.inputs.length + 1 && typeof (args[args.length - 1]) === "object") {
|
2020-06-01 05:04:12 -04:00
|
|
|
const overrides = shallowCopy(args.pop());
|
2020-06-03 03:47:17 -04:00
|
|
|
if (overrides.blockTag != null) {
|
2020-05-29 21:27:59 -04:00
|
|
|
blockTag = yield overrides.blockTag;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-06-03 03:47:17 -04:00
|
|
|
delete overrides.blockTag;
|
2020-06-01 05:04:12 -04:00
|
|
|
args.push(overrides);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
// If the contract was just deployed, wait until it is mined
|
|
|
|
if (contract.deployTransaction != null) {
|
|
|
|
yield contract._deployed(blockTag);
|
|
|
|
}
|
|
|
|
// Call a node and get the result
|
|
|
|
const tx = yield populateTransaction(contract, fragment, args);
|
|
|
|
const result = yield signerOrProvider.call(tx, blockTag);
|
2020-05-29 21:27:59 -04:00
|
|
|
try {
|
2020-06-01 05:04:12 -04:00
|
|
|
let value = contract.interface.decodeFunctionResult(fragment, result);
|
2020-05-29 21:27:59 -04:00
|
|
|
if (collapseSimple && fragment.outputs.length === 1) {
|
2020-06-01 05:04:12 -04:00
|
|
|
value = value[0];
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
return value;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
catch (error) {
|
|
|
|
if (error.code === Logger.errors.CALL_EXCEPTION) {
|
|
|
|
error.address = contract.address;
|
|
|
|
error.args = args;
|
|
|
|
error.transaction = tx;
|
|
|
|
}
|
|
|
|
throw error;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function buildSend(contract, fragment) {
|
|
|
|
return function (...args) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2019-08-25 02:39:20 -04:00
|
|
|
if (!contract.signer) {
|
2020-06-01 05:04:12 -04:00
|
|
|
logger.throwError("sending a transaction requires a signer", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "sendTransaction"
|
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
// If the contract was just deployed, wait until it is minded
|
|
|
|
if (contract.deployTransaction != null) {
|
|
|
|
yield contract._deployed();
|
2020-05-29 21:27:59 -04:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
const txRequest = yield populateTransaction(contract, fragment, args);
|
2020-05-29 21:27:59 -04:00
|
|
|
const tx = yield contract.signer.sendTransaction(txRequest);
|
|
|
|
// Tweak the tw.wait so the receipt has extra properties
|
|
|
|
const wait = tx.wait.bind(tx);
|
|
|
|
tx.wait = (confirmations) => {
|
|
|
|
return wait(confirmations).then((receipt) => {
|
|
|
|
receipt.events = receipt.logs.map((log) => {
|
|
|
|
let event = deepCopy(log);
|
|
|
|
let parsed = null;
|
|
|
|
try {
|
|
|
|
parsed = contract.interface.parseLog(log);
|
|
|
|
}
|
|
|
|
catch (e) { }
|
|
|
|
// Successfully parsed the event log; include it
|
|
|
|
if (parsed) {
|
|
|
|
event.args = parsed.args;
|
|
|
|
event.decode = (data, topics) => {
|
2020-06-03 03:47:17 -04:00
|
|
|
return contract.interface.decodeEventLog(parsed.eventFragment, data, topics);
|
2019-08-25 02:39:20 -04:00
|
|
|
};
|
2020-05-29 21:27:59 -04:00
|
|
|
event.event = parsed.name;
|
|
|
|
event.eventSignature = parsed.signature;
|
|
|
|
}
|
|
|
|
// Useful operations
|
|
|
|
event.removeListener = () => { return contract.provider; };
|
|
|
|
event.getBlock = () => {
|
|
|
|
return contract.provider.getBlock(receipt.blockHash);
|
|
|
|
};
|
|
|
|
event.getTransaction = () => {
|
|
|
|
return contract.provider.getTransaction(receipt.transactionHash);
|
|
|
|
};
|
|
|
|
event.getTransactionReceipt = () => {
|
|
|
|
return Promise.resolve(receipt);
|
|
|
|
};
|
|
|
|
return event;
|
2019-08-25 02:39:20 -04:00
|
|
|
});
|
2020-05-29 21:27:59 -04:00
|
|
|
return receipt;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return tx;
|
2019-08-25 02:39:20 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
function buildDefault(contract, fragment, collapseSimple) {
|
|
|
|
if (fragment.constant) {
|
|
|
|
return buildCall(contract, fragment, collapseSimple);
|
|
|
|
}
|
|
|
|
return buildSend(contract, fragment);
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
function getEventTag(filter) {
|
|
|
|
if (filter.address && (filter.topics == null || filter.topics.length === 0)) {
|
|
|
|
return "*";
|
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
return (filter.address || "*") + "@" + (filter.topics ? filter.topics.map((topic) => {
|
|
|
|
if (Array.isArray(topic)) {
|
|
|
|
return topic.join("|");
|
|
|
|
}
|
|
|
|
return topic;
|
|
|
|
}).join(":") : "");
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
class RunningEvent {
|
|
|
|
constructor(tag, filter) {
|
|
|
|
defineReadOnly(this, "tag", tag);
|
|
|
|
defineReadOnly(this, "filter", filter);
|
|
|
|
this._listeners = [];
|
|
|
|
}
|
|
|
|
addListener(listener, once) {
|
|
|
|
this._listeners.push({ listener: listener, once: once });
|
|
|
|
}
|
|
|
|
removeListener(listener) {
|
|
|
|
let done = false;
|
|
|
|
this._listeners = this._listeners.filter((item) => {
|
|
|
|
if (done || item.listener !== listener) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
done = true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
removeAllListeners() {
|
|
|
|
this._listeners = [];
|
|
|
|
}
|
|
|
|
listeners() {
|
|
|
|
return this._listeners.map((i) => i.listener);
|
|
|
|
}
|
|
|
|
listenerCount() {
|
|
|
|
return this._listeners.length;
|
|
|
|
}
|
|
|
|
run(args) {
|
2020-01-07 19:58:04 -05:00
|
|
|
const listenerCount = this.listenerCount();
|
2019-08-25 02:39:20 -04:00
|
|
|
this._listeners = this._listeners.filter((item) => {
|
2020-01-07 19:58:04 -05:00
|
|
|
const argsCopy = args.slice();
|
2019-08-25 02:39:20 -04:00
|
|
|
// Call the callback in the next event loop
|
|
|
|
setTimeout(() => {
|
|
|
|
item.listener.apply(this, argsCopy);
|
|
|
|
}, 0);
|
|
|
|
// Reschedule it if it not "once"
|
|
|
|
return !(item.once);
|
|
|
|
});
|
|
|
|
return listenerCount;
|
|
|
|
}
|
|
|
|
prepareEvent(event) {
|
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
// Returns the array that will be applied to an emit
|
|
|
|
getEmit(event) {
|
|
|
|
return [event];
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
class ErrorRunningEvent extends RunningEvent {
|
|
|
|
constructor() {
|
|
|
|
super("error", null);
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
// @TODO Fragment should inherit Wildcard? and just override getEmit?
|
|
|
|
// or have a common abstract super class, with enough constructor
|
|
|
|
// options to configure both.
|
|
|
|
// A Fragment Event will populate all the properties that Wildcard
|
|
|
|
// will, and additioanlly dereference the arguments when emitting
|
2019-08-25 02:39:20 -04:00
|
|
|
class FragmentRunningEvent extends RunningEvent {
|
|
|
|
constructor(address, contractInterface, fragment, topics) {
|
2020-01-07 19:58:04 -05:00
|
|
|
const filter = {
|
2019-08-25 02:39:20 -04:00
|
|
|
address: address
|
|
|
|
};
|
|
|
|
let topic = contractInterface.getEventTopic(fragment);
|
|
|
|
if (topics) {
|
|
|
|
if (topic !== topics[0]) {
|
|
|
|
logger.throwArgumentError("topic mismatch", "topics", topics);
|
|
|
|
}
|
|
|
|
filter.topics = topics.slice();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
filter.topics = [topic];
|
|
|
|
}
|
|
|
|
super(getEventTag(filter), filter);
|
|
|
|
defineReadOnly(this, "address", address);
|
|
|
|
defineReadOnly(this, "interface", contractInterface);
|
|
|
|
defineReadOnly(this, "fragment", fragment);
|
|
|
|
}
|
|
|
|
prepareEvent(event) {
|
|
|
|
super.prepareEvent(event);
|
|
|
|
event.event = this.fragment.name;
|
|
|
|
event.eventSignature = this.fragment.format();
|
|
|
|
event.decode = (data, topics) => {
|
|
|
|
return this.interface.decodeEventLog(this.fragment, data, topics);
|
|
|
|
};
|
2020-04-16 21:59:53 -04:00
|
|
|
try {
|
|
|
|
event.args = this.interface.decodeEventLog(this.fragment, event.data, event.topics);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
event.args = null;
|
|
|
|
event.decodeError = error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getEmit(event) {
|
2020-04-25 03:54:54 -04:00
|
|
|
const errors = checkResultErrors(event.args);
|
|
|
|
if (errors.length) {
|
|
|
|
throw errors[0].error;
|
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
const args = (event.args || []).slice();
|
|
|
|
args.push(event);
|
|
|
|
return args;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
// A Wildard Event will attempt to populate:
|
|
|
|
// - event The name of the event name
|
|
|
|
// - eventSignature The full signature of the event
|
|
|
|
// - decode A function to decode data and topics
|
|
|
|
// - args The decoded data and topics
|
2019-08-25 02:39:20 -04:00
|
|
|
class WildcardRunningEvent extends RunningEvent {
|
|
|
|
constructor(address, contractInterface) {
|
|
|
|
super("*", { address: address });
|
|
|
|
defineReadOnly(this, "address", address);
|
|
|
|
defineReadOnly(this, "interface", contractInterface);
|
|
|
|
}
|
|
|
|
prepareEvent(event) {
|
|
|
|
super.prepareEvent(event);
|
2020-04-16 21:59:53 -04:00
|
|
|
try {
|
|
|
|
const parsed = this.interface.parseLog(event);
|
2019-08-25 02:39:20 -04:00
|
|
|
event.event = parsed.name;
|
|
|
|
event.eventSignature = parsed.signature;
|
|
|
|
event.decode = (data, topics) => {
|
|
|
|
return this.interface.decodeEventLog(parsed.eventFragment, data, topics);
|
|
|
|
};
|
2020-01-07 19:58:04 -05:00
|
|
|
event.args = parsed.args;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
catch (error) {
|
|
|
|
// No matching event
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
export class Contract {
|
|
|
|
constructor(addressOrName, contractInterface, signerOrProvider) {
|
|
|
|
logger.checkNew(new.target, Contract);
|
|
|
|
// @TODO: Maybe still check the addressOrName looks like a valid address or name?
|
|
|
|
//address = getAddress(address);
|
|
|
|
defineReadOnly(this, "interface", getStatic((new.target), "getInterface")(contractInterface));
|
2020-06-01 05:04:12 -04:00
|
|
|
if (signerOrProvider == null) {
|
|
|
|
defineReadOnly(this, "provider", null);
|
|
|
|
defineReadOnly(this, "signer", null);
|
|
|
|
}
|
|
|
|
else if (Signer.isSigner(signerOrProvider)) {
|
2019-08-25 02:39:20 -04:00
|
|
|
defineReadOnly(this, "provider", signerOrProvider.provider || null);
|
|
|
|
defineReadOnly(this, "signer", signerOrProvider);
|
|
|
|
}
|
|
|
|
else if (Provider.isProvider(signerOrProvider)) {
|
|
|
|
defineReadOnly(this, "provider", signerOrProvider);
|
|
|
|
defineReadOnly(this, "signer", null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.throwArgumentError("invalid signer or provider", "signerOrProvider", signerOrProvider);
|
|
|
|
}
|
|
|
|
defineReadOnly(this, "callStatic", {});
|
2020-03-12 19:14:50 +01:00
|
|
|
defineReadOnly(this, "estimateGas", {});
|
2019-08-25 02:39:20 -04:00
|
|
|
defineReadOnly(this, "functions", {});
|
|
|
|
defineReadOnly(this, "populateTransaction", {});
|
|
|
|
defineReadOnly(this, "filters", {});
|
2019-11-20 18:57:38 +09:00
|
|
|
{
|
|
|
|
const uniqueFilters = {};
|
|
|
|
Object.keys(this.interface.events).forEach((eventSignature) => {
|
2020-01-07 19:58:04 -05:00
|
|
|
const event = this.interface.events[eventSignature];
|
2019-11-20 18:57:38 +09:00
|
|
|
defineReadOnly(this.filters, eventSignature, (...args) => {
|
|
|
|
return {
|
|
|
|
address: this.address,
|
|
|
|
topics: this.interface.encodeFilterTopics(event, args)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
if (!uniqueFilters[event.name]) {
|
|
|
|
uniqueFilters[event.name] = [];
|
|
|
|
}
|
|
|
|
uniqueFilters[event.name].push(eventSignature);
|
2019-08-25 02:39:20 -04:00
|
|
|
});
|
2019-11-20 18:57:38 +09:00
|
|
|
Object.keys(uniqueFilters).forEach((name) => {
|
|
|
|
const filters = uniqueFilters[name];
|
|
|
|
if (filters.length === 1) {
|
|
|
|
defineReadOnly(this.filters, name, this.filters[filters[0]]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.warn(`Duplicate definition of ${name} (${filters.join(", ")})`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
defineReadOnly(this, "_runningEvents", {});
|
|
|
|
defineReadOnly(this, "_wrappedEmits", {});
|
|
|
|
defineReadOnly(this, "address", addressOrName);
|
|
|
|
if (this.provider) {
|
2020-03-12 19:14:50 +01:00
|
|
|
defineReadOnly(this, "resolvedAddress", this.provider.resolveName(addressOrName).then((address) => {
|
2019-08-25 02:39:20 -04:00
|
|
|
if (address == null) {
|
|
|
|
throw new Error("name not found");
|
|
|
|
}
|
|
|
|
return address;
|
|
|
|
}).catch((error) => {
|
|
|
|
console.log("ERROR: Cannot find Contract - " + addressOrName);
|
|
|
|
throw error;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
2020-06-01 05:04:12 -04:00
|
|
|
defineReadOnly(this, "resolvedAddress", Promise.resolve(getAddress(addressOrName)));
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
// Without a provider, we cannot use ENS names
|
2020-06-01 05:04:12 -04:00
|
|
|
logger.throwError("provider is required to use ENS name as contract address", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "new Contract"
|
|
|
|
});
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
const uniqueNames = {};
|
|
|
|
const uniqueSignatures = {};
|
|
|
|
Object.keys(this.interface.functions).forEach((signature) => {
|
|
|
|
const fragment = this.interface.functions[signature];
|
|
|
|
// Check that the signature is unique; if not the ABI generation has
|
|
|
|
// not been cleaned or may be incorrectly generated
|
|
|
|
if (uniqueSignatures[signature]) {
|
|
|
|
logger.warn(`Duplicate ABI entry for ${JSON.stringify(name)}`);
|
|
|
|
return;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
uniqueSignatures[signature] = true;
|
|
|
|
// Track unique names; we only expose bare named functions if they
|
|
|
|
// are ambiguous
|
|
|
|
{
|
|
|
|
const name = fragment.name;
|
|
|
|
if (!uniqueNames[name]) {
|
|
|
|
uniqueNames[name] = [];
|
|
|
|
}
|
|
|
|
uniqueNames[name].push(signature);
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
if (this[signature] == null) {
|
2020-05-29 21:27:59 -04:00
|
|
|
defineReadOnly(this, signature, buildDefault(this, fragment, true));
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
// We do not collapse simple calls on this bucket, which allows
|
|
|
|
// frameworks to safely use this without introspection as well as
|
|
|
|
// allows decoding error recovery.
|
2020-05-03 17:53:58 -04:00
|
|
|
if (this.functions[signature] == null) {
|
2020-05-29 21:27:59 -04:00
|
|
|
defineReadOnly(this.functions, signature, buildDefault(this, fragment, false));
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
if (this.callStatic[signature] == null) {
|
2020-05-29 21:27:59 -04:00
|
|
|
defineReadOnly(this.callStatic, signature, buildCall(this, fragment, true));
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
if (this.populateTransaction[signature] == null) {
|
2020-05-29 21:27:59 -04:00
|
|
|
defineReadOnly(this.populateTransaction, signature, buildPopulate(this, fragment));
|
2020-05-03 17:53:58 -04:00
|
|
|
}
|
|
|
|
if (this.estimateGas[signature] == null) {
|
2020-05-29 21:27:59 -04:00
|
|
|
defineReadOnly(this.estimateGas, signature, buildEstimate(this, fragment));
|
2019-11-20 18:57:38 +09:00
|
|
|
}
|
|
|
|
});
|
2020-05-03 17:53:58 -04:00
|
|
|
Object.keys(uniqueNames).forEach((name) => {
|
|
|
|
// Ambiguous names to not get attached as bare names
|
|
|
|
const signatures = uniqueNames[name];
|
2019-11-20 18:57:38 +09:00
|
|
|
if (signatures.length > 1) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
const signature = signatures[0];
|
2019-11-20 18:57:38 +09:00
|
|
|
if (this[name] == null) {
|
2020-05-03 17:53:58 -04:00
|
|
|
defineReadOnly(this, name, this[signature]);
|
|
|
|
}
|
|
|
|
if (this.functions[name] == null) {
|
|
|
|
defineReadOnly(this.functions, name, this.functions[signature]);
|
|
|
|
}
|
|
|
|
if (this.callStatic[name] == null) {
|
|
|
|
defineReadOnly(this.callStatic, name, this.callStatic[signature]);
|
|
|
|
}
|
|
|
|
if (this.populateTransaction[name] == null) {
|
|
|
|
defineReadOnly(this.populateTransaction, name, this.populateTransaction[signature]);
|
|
|
|
}
|
|
|
|
if (this.estimateGas[name] == null) {
|
|
|
|
defineReadOnly(this.estimateGas, name, this.estimateGas[signature]);
|
2019-11-20 18:57:38 +09:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
static getContractAddress(transaction) {
|
|
|
|
return getContractAddress(transaction);
|
|
|
|
}
|
|
|
|
static getInterface(contractInterface) {
|
|
|
|
if (Interface.isInterface(contractInterface)) {
|
|
|
|
return contractInterface;
|
|
|
|
}
|
|
|
|
return new Interface(contractInterface);
|
|
|
|
}
|
|
|
|
// @TODO: Allow timeout?
|
|
|
|
deployed() {
|
|
|
|
return this._deployed();
|
|
|
|
}
|
|
|
|
_deployed(blockTag) {
|
|
|
|
if (!this._deployedPromise) {
|
|
|
|
// If we were just deployed, we know the transaction we should occur in
|
|
|
|
if (this.deployTransaction) {
|
|
|
|
this._deployedPromise = this.deployTransaction.wait().then(() => {
|
|
|
|
return this;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// @TODO: Once we allow a timeout to be passed in, we will wait
|
|
|
|
// up to that many blocks for getCode
|
|
|
|
// Otherwise, poll for our code to be deployed
|
|
|
|
this._deployedPromise = this.provider.getCode(this.address, blockTag).then((code) => {
|
|
|
|
if (code === "0x") {
|
|
|
|
logger.throwError("contract not deployed", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
contractAddress: this.address,
|
|
|
|
operation: "getDeployed"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._deployedPromise;
|
|
|
|
}
|
|
|
|
// @TODO:
|
|
|
|
// estimateFallback(overrides?: TransactionRequest): Promise<BigNumber>
|
|
|
|
// @TODO:
|
|
|
|
// estimateDeploy(bytecode: string, ...args): Promise<BigNumber>
|
|
|
|
fallback(overrides) {
|
|
|
|
if (!this.signer) {
|
2019-09-08 02:46:53 -04:00
|
|
|
logger.throwError("sending a transactions require a signer", Logger.errors.UNSUPPORTED_OPERATION, { operation: "sendTransaction(fallback)" });
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
const tx = shallowCopy(overrides || {});
|
2019-08-25 02:39:20 -04:00
|
|
|
["from", "to"].forEach(function (key) {
|
|
|
|
if (tx[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logger.throwError("cannot override " + key, Logger.errors.UNSUPPORTED_OPERATION, { operation: key });
|
|
|
|
});
|
2020-03-12 19:14:50 +01:00
|
|
|
tx.to = this.resolvedAddress;
|
2019-08-25 02:39:20 -04:00
|
|
|
return this.deployed().then(() => {
|
|
|
|
return this.signer.sendTransaction(tx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Reconnect to a different signer or provider
|
|
|
|
connect(signerOrProvider) {
|
|
|
|
if (typeof (signerOrProvider) === "string") {
|
|
|
|
signerOrProvider = new VoidSigner(signerOrProvider, this.provider);
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
const contract = new (this.constructor)(this.address, this.interface, signerOrProvider);
|
2019-08-25 02:39:20 -04:00
|
|
|
if (this.deployTransaction) {
|
|
|
|
defineReadOnly(contract, "deployTransaction", this.deployTransaction);
|
|
|
|
}
|
|
|
|
return contract;
|
|
|
|
}
|
|
|
|
// Re-attach to a different on-chain instance of this contract
|
|
|
|
attach(addressOrName) {
|
|
|
|
return new (this.constructor)(addressOrName, this.interface, this.signer || this.provider);
|
|
|
|
}
|
|
|
|
static isIndexed(value) {
|
|
|
|
return Indexed.isIndexed(value);
|
|
|
|
}
|
|
|
|
_normalizeRunningEvent(runningEvent) {
|
|
|
|
// Already have an instance of this event running; we can re-use it
|
|
|
|
if (this._runningEvents[runningEvent.tag]) {
|
|
|
|
return this._runningEvents[runningEvent.tag];
|
|
|
|
}
|
|
|
|
return runningEvent;
|
|
|
|
}
|
|
|
|
_getRunningEvent(eventName) {
|
|
|
|
if (typeof (eventName) === "string") {
|
|
|
|
// Listen for "error" events (if your contract has an error event, include
|
|
|
|
// the full signature to bypass this special event keyword)
|
|
|
|
if (eventName === "error") {
|
|
|
|
return this._normalizeRunningEvent(new ErrorRunningEvent());
|
|
|
|
}
|
2020-04-25 03:54:54 -04:00
|
|
|
// Listen for any event that is registered
|
|
|
|
if (eventName === "event") {
|
|
|
|
return this._normalizeRunningEvent(new RunningEvent("event", null));
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
// Listen for any event
|
|
|
|
if (eventName === "*") {
|
|
|
|
return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface));
|
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
// Get the event Fragment (throws if ambiguous/unknown event)
|
2020-01-07 19:58:04 -05:00
|
|
|
const fragment = this.interface.getEvent(eventName);
|
2019-08-25 02:39:20 -04:00
|
|
|
return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment));
|
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
// We have topics to filter by...
|
|
|
|
if (eventName.topics && eventName.topics.length > 0) {
|
|
|
|
// Is it a known topichash? (throws if no matching topichash)
|
|
|
|
try {
|
2020-05-29 21:27:59 -04:00
|
|
|
const topic = eventName.topics[0];
|
|
|
|
if (typeof (topic) !== "string") {
|
|
|
|
throw new Error("invalid topic"); // @TODO: May happen for anonymous events
|
|
|
|
}
|
|
|
|
const fragment = this.interface.getEvent(topic);
|
2020-04-16 21:59:53 -04:00
|
|
|
return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment, eventName.topics));
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
catch (error) { }
|
|
|
|
// Filter by the unknown topichash
|
|
|
|
const filter = {
|
|
|
|
address: this.address,
|
|
|
|
topics: eventName.topics
|
|
|
|
};
|
|
|
|
return this._normalizeRunningEvent(new RunningEvent(getEventTag(filter), filter));
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface));
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
_checkRunningEvents(runningEvent) {
|
|
|
|
if (runningEvent.listenerCount() === 0) {
|
|
|
|
delete this._runningEvents[runningEvent.tag];
|
2020-04-15 18:28:04 -04:00
|
|
|
// If we have a poller for this, remove it
|
|
|
|
const emit = this._wrappedEmits[runningEvent.tag];
|
|
|
|
if (emit) {
|
|
|
|
this.provider.off(runningEvent.filter, emit);
|
|
|
|
delete this._wrappedEmits[runningEvent.tag];
|
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
// Subclasses can override this to gracefully recover
|
|
|
|
// from parse errors if they wish
|
2019-08-25 02:39:20 -04:00
|
|
|
_wrapEvent(runningEvent, log, listener) {
|
2020-01-07 19:58:04 -05:00
|
|
|
const event = deepCopy(log);
|
2019-08-25 02:39:20 -04:00
|
|
|
event.removeListener = () => {
|
|
|
|
if (!listener) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
runningEvent.removeListener(listener);
|
|
|
|
this._checkRunningEvents(runningEvent);
|
|
|
|
};
|
|
|
|
event.getBlock = () => { return this.provider.getBlock(log.blockHash); };
|
|
|
|
event.getTransaction = () => { return this.provider.getTransaction(log.transactionHash); };
|
|
|
|
event.getTransactionReceipt = () => { return this.provider.getTransactionReceipt(log.transactionHash); };
|
2020-04-16 21:59:53 -04:00
|
|
|
// This may throw if the topics and data mismatch the signature
|
|
|
|
runningEvent.prepareEvent(event);
|
2019-08-25 02:39:20 -04:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
_addEventListener(runningEvent, listener, once) {
|
|
|
|
if (!this.provider) {
|
|
|
|
logger.throwError("events require a provider or a signer with a provider", Logger.errors.UNSUPPORTED_OPERATION, { operation: "once" });
|
|
|
|
}
|
|
|
|
runningEvent.addListener(listener, once);
|
|
|
|
// Track this running event and its listeners (may already be there; but no hard in updating)
|
|
|
|
this._runningEvents[runningEvent.tag] = runningEvent;
|
2020-04-16 21:59:53 -04:00
|
|
|
// If we are not polling the provider, start polling
|
2019-08-25 02:39:20 -04:00
|
|
|
if (!this._wrappedEmits[runningEvent.tag]) {
|
2020-01-07 19:58:04 -05:00
|
|
|
const wrappedEmit = (log) => {
|
2020-04-25 03:54:54 -04:00
|
|
|
let event = this._wrapEvent(runningEvent, log, listener);
|
|
|
|
// Try to emit the result for the parameterized event...
|
|
|
|
if (event.decodeError == null) {
|
|
|
|
try {
|
|
|
|
const args = runningEvent.getEmit(event);
|
|
|
|
this.emit(runningEvent.filter, ...args);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
event.decodeError = error.error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Always emit "event" for fragment-base events
|
|
|
|
if (runningEvent.filter != null) {
|
|
|
|
this.emit("event", event);
|
2020-04-16 21:59:53 -04:00
|
|
|
}
|
2020-04-25 03:54:54 -04:00
|
|
|
// Emit "error" if there was an error
|
|
|
|
if (event.decodeError != null) {
|
|
|
|
this.emit("error", event.decodeError, event);
|
2020-04-16 21:59:53 -04:00
|
|
|
}
|
2019-08-25 02:39:20 -04:00
|
|
|
};
|
|
|
|
this._wrappedEmits[runningEvent.tag] = wrappedEmit;
|
|
|
|
// Special events, like "error" do not have a filter
|
|
|
|
if (runningEvent.filter != null) {
|
|
|
|
this.provider.on(runningEvent.filter, wrappedEmit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
queryFilter(event, fromBlockOrBlockhash, toBlock) {
|
2020-01-07 19:58:04 -05:00
|
|
|
const runningEvent = this._getRunningEvent(event);
|
|
|
|
const filter = shallowCopy(runningEvent.filter);
|
2019-08-25 02:39:20 -04:00
|
|
|
if (typeof (fromBlockOrBlockhash) === "string" && isHexString(fromBlockOrBlockhash, 32)) {
|
|
|
|
if (toBlock != null) {
|
|
|
|
logger.throwArgumentError("cannot specify toBlock with blockhash", "toBlock", toBlock);
|
|
|
|
}
|
2020-05-21 00:07:41 -04:00
|
|
|
filter.blockHash = fromBlockOrBlockhash;
|
2019-08-25 02:39:20 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
filter.fromBlock = ((fromBlockOrBlockhash != null) ? fromBlockOrBlockhash : 0);
|
|
|
|
filter.toBlock = ((toBlock != null) ? toBlock : "latest");
|
|
|
|
}
|
|
|
|
return this.provider.getLogs(filter).then((logs) => {
|
|
|
|
return logs.map((log) => this._wrapEvent(runningEvent, log, null));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
on(event, listener) {
|
|
|
|
this._addEventListener(this._getRunningEvent(event), listener, false);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
once(event, listener) {
|
|
|
|
this._addEventListener(this._getRunningEvent(event), listener, true);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
emit(eventName, ...args) {
|
|
|
|
if (!this.provider) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
const runningEvent = this._getRunningEvent(eventName);
|
|
|
|
const result = (runningEvent.run(args) > 0);
|
2019-08-25 02:39:20 -04:00
|
|
|
// May have drained all the "once" events; check for living events
|
|
|
|
this._checkRunningEvents(runningEvent);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
listenerCount(eventName) {
|
|
|
|
if (!this.provider) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return this._getRunningEvent(eventName).listenerCount();
|
|
|
|
}
|
|
|
|
listeners(eventName) {
|
|
|
|
if (!this.provider) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
if (eventName == null) {
|
2020-01-07 19:58:04 -05:00
|
|
|
const result = [];
|
2019-08-25 02:39:20 -04:00
|
|
|
for (let tag in this._runningEvents) {
|
|
|
|
this._runningEvents[tag].listeners().forEach((listener) => {
|
|
|
|
result.push(listener);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return this._getRunningEvent(eventName).listeners();
|
|
|
|
}
|
|
|
|
removeAllListeners(eventName) {
|
|
|
|
if (!this.provider) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
if (eventName == null) {
|
2020-01-07 19:58:04 -05:00
|
|
|
for (const tag in this._runningEvents) {
|
|
|
|
const runningEvent = this._runningEvents[tag];
|
2019-08-25 02:39:20 -04:00
|
|
|
runningEvent.removeAllListeners();
|
|
|
|
this._checkRunningEvents(runningEvent);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
// Delete any listeners
|
2020-01-07 19:58:04 -05:00
|
|
|
const runningEvent = this._getRunningEvent(eventName);
|
2019-08-25 02:39:20 -04:00
|
|
|
runningEvent.removeAllListeners();
|
|
|
|
this._checkRunningEvents(runningEvent);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
off(eventName, listener) {
|
|
|
|
if (!this.provider) {
|
|
|
|
return this;
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
const runningEvent = this._getRunningEvent(eventName);
|
2019-08-25 02:39:20 -04:00
|
|
|
runningEvent.removeListener(listener);
|
|
|
|
this._checkRunningEvents(runningEvent);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
removeListener(eventName, listener) {
|
|
|
|
return this.off(eventName, listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export class ContractFactory {
|
|
|
|
constructor(contractInterface, bytecode, signer) {
|
|
|
|
let bytecodeHex = null;
|
|
|
|
if (typeof (bytecode) === "string") {
|
|
|
|
bytecodeHex = bytecode;
|
|
|
|
}
|
|
|
|
else if (isBytes(bytecode)) {
|
|
|
|
bytecodeHex = hexlify(bytecode);
|
|
|
|
}
|
|
|
|
else if (bytecode && typeof (bytecode.object) === "string") {
|
|
|
|
// Allow the bytecode object from the Solidity compiler
|
|
|
|
bytecodeHex = bytecode.object;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Crash in the next verification step
|
|
|
|
bytecodeHex = "!";
|
|
|
|
}
|
|
|
|
// Make sure it is 0x prefixed
|
|
|
|
if (bytecodeHex.substring(0, 2) !== "0x") {
|
|
|
|
bytecodeHex = "0x" + bytecodeHex;
|
|
|
|
}
|
|
|
|
// Make sure the final result is valid bytecode
|
|
|
|
if (!isHexString(bytecodeHex) || (bytecodeHex.length % 2)) {
|
|
|
|
logger.throwArgumentError("invalid bytecode", "bytecode", bytecode);
|
|
|
|
}
|
|
|
|
// If we have a signer, make sure it is valid
|
|
|
|
if (signer && !Signer.isSigner(signer)) {
|
|
|
|
logger.throwArgumentError("invalid signer", "signer", signer);
|
|
|
|
}
|
|
|
|
defineReadOnly(this, "bytecode", bytecodeHex);
|
|
|
|
defineReadOnly(this, "interface", getStatic((new.target), "getInterface")(contractInterface));
|
|
|
|
defineReadOnly(this, "signer", signer || null);
|
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
// @TODO: Future; rename to populteTransaction?
|
2019-08-25 02:39:20 -04:00
|
|
|
getDeployTransaction(...args) {
|
|
|
|
let tx = {};
|
|
|
|
// If we have 1 additional argument, we allow transaction overrides
|
2020-04-23 23:35:39 -04:00
|
|
|
if (args.length === this.interface.deploy.inputs.length + 1 && typeof (args[args.length - 1]) === "object") {
|
2019-08-25 02:39:20 -04:00
|
|
|
tx = shallowCopy(args.pop());
|
2020-01-07 19:58:04 -05:00
|
|
|
for (const key in tx) {
|
2019-08-25 02:39:20 -04:00
|
|
|
if (!allowedTransactionKeys[key]) {
|
|
|
|
throw new Error("unknown transaction override " + key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Do not allow these to be overridden in a deployment transaction
|
|
|
|
["data", "from", "to"].forEach((key) => {
|
|
|
|
if (tx[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logger.throwError("cannot override " + key, Logger.errors.UNSUPPORTED_OPERATION, { operation: key });
|
|
|
|
});
|
|
|
|
// Make sure the call matches the constructor signature
|
|
|
|
logger.checkArgumentCount(args.length, this.interface.deploy.inputs.length, " in Contract constructor");
|
|
|
|
// Set the data to the bytecode + the encoded constructor arguments
|
|
|
|
tx.data = hexlify(concat([
|
|
|
|
this.bytecode,
|
|
|
|
this.interface.encodeDeploy(args)
|
|
|
|
]));
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
deploy(...args) {
|
2020-04-23 23:35:39 -04:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
let overrides = {};
|
|
|
|
// If 1 extra parameter was passed in, it contains overrides
|
|
|
|
if (args.length === this.interface.deploy.inputs.length + 1) {
|
|
|
|
overrides = args.pop();
|
|
|
|
}
|
|
|
|
// Make sure the call matches the constructor signature
|
|
|
|
logger.checkArgumentCount(args.length, this.interface.deploy.inputs.length, " in Contract constructor");
|
|
|
|
// Resolve ENS names and promises in the arguments
|
|
|
|
const params = yield resolveAddresses(this.signer, args, this.interface.deploy.inputs);
|
|
|
|
params.push(overrides);
|
2019-08-25 02:39:20 -04:00
|
|
|
// Get the deployment transaction (with optional overrides)
|
2020-04-23 23:35:39 -04:00
|
|
|
const unsignedTx = this.getDeployTransaction(...params);
|
2019-08-25 02:39:20 -04:00
|
|
|
// Send the deployment transaction
|
2020-04-23 23:35:39 -04:00
|
|
|
const tx = yield this.signer.sendTransaction(unsignedTx);
|
|
|
|
const address = getStatic(this.constructor, "getContractAddress")(tx);
|
|
|
|
const contract = getStatic(this.constructor, "getContract")(address, this.interface, this.signer);
|
|
|
|
defineReadOnly(contract, "deployTransaction", tx);
|
|
|
|
return contract;
|
2019-08-25 02:39:20 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
attach(address) {
|
|
|
|
return (this.constructor).getContract(address, this.interface, this.signer);
|
|
|
|
}
|
|
|
|
connect(signer) {
|
|
|
|
return new (this.constructor)(this.interface, this.bytecode, signer);
|
|
|
|
}
|
|
|
|
static fromSolidity(compilerOutput, signer) {
|
|
|
|
if (compilerOutput == null) {
|
|
|
|
logger.throwError("missing compiler output", Logger.errors.MISSING_ARGUMENT, { argument: "compilerOutput" });
|
|
|
|
}
|
|
|
|
if (typeof (compilerOutput) === "string") {
|
|
|
|
compilerOutput = JSON.parse(compilerOutput);
|
|
|
|
}
|
2020-01-07 19:58:04 -05:00
|
|
|
const abi = compilerOutput.abi;
|
2019-08-25 02:39:20 -04:00
|
|
|
let bytecode = null;
|
|
|
|
if (compilerOutput.bytecode) {
|
|
|
|
bytecode = compilerOutput.bytecode;
|
|
|
|
}
|
|
|
|
else if (compilerOutput.evm && compilerOutput.evm.bytecode) {
|
|
|
|
bytecode = compilerOutput.evm.bytecode;
|
|
|
|
}
|
|
|
|
return new this(abi, bytecode, signer);
|
|
|
|
}
|
|
|
|
static getInterface(contractInterface) {
|
|
|
|
return Contract.getInterface(contractInterface);
|
|
|
|
}
|
|
|
|
static getContractAddress(tx) {
|
|
|
|
return getContractAddress(tx);
|
|
|
|
}
|
|
|
|
static getContract(address, contractInterface, signer) {
|
|
|
|
return new Contract(address, contractInterface, signer);
|
|
|
|
}
|
|
|
|
}
|