2019-05-14 18:25:46 -04:00
|
|
|
"use strict";
|
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
import { checkResultErrors, EventFragment, Fragment, FunctionFragment, Indexed, Interface, JsonFragment, LogDescription, ParamType, Result } from "@ethersproject/abi";
|
2020-02-06 18:21:34 -05:00
|
|
|
import { Block, BlockTag, Filter, FilterByBlockHash, Listener, Log, Provider, TransactionReceipt, TransactionRequest, TransactionResponse } from "@ethersproject/abstract-provider";
|
2019-05-14 18:25:46 -04:00
|
|
|
import { Signer, VoidSigner } from "@ethersproject/abstract-signer";
|
2020-06-01 04:46:37 -04:00
|
|
|
import { getAddress, getContractAddress } from "@ethersproject/address";
|
2019-05-14 18:25:46 -04:00
|
|
|
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
|
2020-09-26 02:17:05 -04:00
|
|
|
import { arrayify, BytesLike, concat, hexlify, isBytes, isHexString } from "@ethersproject/bytes";
|
2020-06-01 04:46:37 -04:00
|
|
|
import { Deferrable, defineReadOnly, deepCopy, getStatic, resolveProperties, shallowCopy } from "@ethersproject/properties";
|
2021-03-26 16:16:56 -04:00
|
|
|
import { AccessList, accessListify, AccessListish } from "@ethersproject/transactions";
|
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";
|
2020-02-25 13:51:33 -05:00
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
const logger = new Logger(version);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
export interface Overrides {
|
|
|
|
gasLimit?: BigNumberish | Promise<BigNumberish>;
|
|
|
|
gasPrice?: BigNumberish | Promise<BigNumberish>;
|
2021-06-10 17:42:20 -04:00
|
|
|
maxFeePerGas?: BigNumberish | Promise<BigNumberish>;
|
|
|
|
maxPriorityFeePerGas?: BigNumberish | Promise<BigNumberish>;
|
2019-05-14 18:25:46 -04:00
|
|
|
nonce?: BigNumberish | Promise<BigNumberish>;
|
2021-03-26 16:16:56 -04:00
|
|
|
type?: number;
|
|
|
|
accessList?: AccessListish;
|
2021-10-05 15:47:26 -04:00
|
|
|
customData?: Record<string, any>;
|
2022-03-04 16:15:47 -05:00
|
|
|
ccipReadEnabled?: boolean;
|
2020-06-01 04:46:37 -04:00
|
|
|
};
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
export interface PayableOverrides extends Overrides {
|
|
|
|
value?: BigNumberish | Promise<BigNumberish>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CallOverrides extends PayableOverrides {
|
|
|
|
blockTag?: BlockTag | Promise<BlockTag>;
|
2020-06-01 04:46:37 -04:00
|
|
|
from?: string | Promise<string>;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
// @TODO: Better hierarchy with: (in v6)
|
|
|
|
// - abstract-provider:TransactionRequest
|
|
|
|
// - transactions:Transaction
|
|
|
|
// - transaction:UnsignedTransaction
|
|
|
|
|
|
|
|
export interface PopulatedTransaction {
|
|
|
|
to?: string;
|
|
|
|
from?: string;
|
|
|
|
nonce?: number;
|
|
|
|
|
|
|
|
gasLimit?: BigNumber;
|
|
|
|
gasPrice?: BigNumber;
|
|
|
|
|
|
|
|
data?: string;
|
|
|
|
value?: BigNumber;
|
|
|
|
chainId?: number;
|
2021-03-26 16:16:56 -04:00
|
|
|
|
|
|
|
type?: number;
|
|
|
|
accessList?: AccessList;
|
2021-06-10 17:42:20 -04:00
|
|
|
|
|
|
|
maxFeePerGas?: BigNumber;
|
|
|
|
maxPriorityFeePerGas?: BigNumber;
|
2021-10-05 15:47:26 -04:00
|
|
|
|
|
|
|
customData?: Record<string, any>;
|
2022-03-04 16:15:47 -05:00
|
|
|
ccipReadEnabled?: boolean;
|
2020-06-01 04:46:37 -04:00
|
|
|
};
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
export type EventFilter = {
|
|
|
|
address?: string;
|
2020-05-29 21:12:15 -04:00
|
|
|
topics?: Array<string|Array<string>>;
|
2019-05-14 18:25:46 -04:00
|
|
|
};
|
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
|
|
|
|
export type ContractFunction<T = any> = (...args: Array<any>) => Promise<T>;
|
|
|
|
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
// The (n + 1)th parameter passed to contract event callbacks
|
|
|
|
export interface Event extends Log {
|
|
|
|
|
|
|
|
// The event name
|
|
|
|
event?: string;
|
|
|
|
|
|
|
|
// The event signature
|
|
|
|
eventSignature?: string;
|
|
|
|
|
|
|
|
// The parsed arguments to the event
|
2020-01-07 19:46:51 -05:00
|
|
|
args?: Result;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
// If parsing the arguments failed, this is the error
|
|
|
|
decodeError?: Error;
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
// A function that can be used to decode event data and topics
|
|
|
|
decode?: (data: string, topics?: Array<string>) => any;
|
|
|
|
|
|
|
|
// A function that will remove the listener responsible for this event (if any)
|
|
|
|
removeListener: () => void;
|
|
|
|
|
|
|
|
// Get blockchain details about this event's block and transaction
|
|
|
|
getBlock: () => Promise<Block>;
|
|
|
|
getTransaction: () => Promise<TransactionResponse>;
|
|
|
|
getTransactionReceipt: () => Promise<TransactionReceipt>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ContractReceipt extends TransactionReceipt {
|
|
|
|
events?: Array<Event>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ContractTransaction extends TransactionResponse {
|
|
|
|
wait(confirmations?: number): Promise<ContractReceipt>;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
|
|
|
|
const allowedTransactionKeys: { [ key: string ]: boolean } = {
|
2021-03-26 16:16:56 -04:00
|
|
|
chainId: true, data: true, from: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true,
|
|
|
|
type: true, accessList: true,
|
2021-10-05 15:47:26 -04:00
|
|
|
maxFeePerGas: true, maxPriorityFeePerGas: true,
|
2022-03-04 16:15:47 -05:00
|
|
|
customData: true,
|
|
|
|
ccipReadEnabled: true
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
async function resolveName(resolver: Signer | Provider, nameOrPromise: string | Promise<string>): Promise<string> {
|
|
|
|
const name = await nameOrPromise;
|
|
|
|
|
2021-10-16 01:30:06 -04:00
|
|
|
if (typeof(name) !== "string") {
|
|
|
|
logger.throwArgumentError("invalid address or ENS name", "name", name);
|
|
|
|
}
|
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
// 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"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-26 01:23:43 -04:00
|
|
|
const address = await resolver.resolveName(name);
|
|
|
|
|
|
|
|
if (address == null) {
|
|
|
|
logger.throwArgumentError("resolver or addr is not configured for ENS name", "name", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return address;
|
2020-06-01 04:46:37 -04:00
|
|
|
}
|
2020-04-25 03:25:42 -04:00
|
|
|
|
2019-05-23 18:59:04 -04:00
|
|
|
// Recursively replaces ENS names with promises to resolve the name and resolves all properties
|
2020-09-26 01:23:43 -04:00
|
|
|
async function resolveAddresses(resolver: Signer | Provider, value: any, paramType: ParamType | Array<ParamType>): Promise<any> {
|
2019-05-14 18:25:46 -04:00
|
|
|
if (Array.isArray(paramType)) {
|
2020-09-26 01:23:43 -04:00
|
|
|
return await Promise.all(paramType.map((paramType, index) => {
|
2019-05-14 18:25:46 -04:00
|
|
|
return resolveAddresses(
|
2020-06-01 04:46:37 -04:00
|
|
|
resolver,
|
2019-05-14 18:25:46 -04:00
|
|
|
((Array.isArray(value)) ? value[index]: value[paramType.name]),
|
|
|
|
paramType
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paramType.type === "address") {
|
2020-09-26 01:23:43 -04:00
|
|
|
return await resolveName(resolver, value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (paramType.type === "tuple") {
|
2020-09-26 01:23:43 -04:00
|
|
|
return await resolveAddresses(resolver, value, paramType.components);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (paramType.baseType === "array") {
|
2021-10-05 16:05:40 -04:00
|
|
|
if (!Array.isArray(value)) {
|
|
|
|
return Promise.reject(logger.makeError("invalid value for array", Logger.errors.INVALID_ARGUMENT, {
|
|
|
|
argument: "value",
|
|
|
|
value
|
|
|
|
}));
|
|
|
|
}
|
2020-09-26 01:23:43 -04:00
|
|
|
return await Promise.all(value.map((v) => resolveAddresses(resolver, v, paramType.arrayChildren)));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-09-26 01:23:43 -04:00
|
|
|
return value;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
async function populateTransaction(contract: Contract, fragment: FunctionFragment, args: Array<any>): Promise<PopulatedTransaction> {
|
|
|
|
// If an extra argument is given, it is overrides
|
|
|
|
let overrides: CallOverrides = { };
|
|
|
|
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(async (check) => {
|
|
|
|
if (getAddress(check.signer) !== check.override) {
|
|
|
|
logger.throwError("Contract with a Signer cannot override from", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "overrides.from"
|
|
|
|
});
|
|
|
|
}
|
2020-09-26 01:23:43 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
return check.override;
|
|
|
|
});
|
2020-09-26 01:23:43 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
} else {
|
|
|
|
overrides.from = contract.signer.getAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (overrides.from) {
|
|
|
|
overrides.from = resolveName(contract.provider, overrides.from);
|
2020-05-29 21:12:15 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
//} 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:12:15 -04:00
|
|
|
const resolved = await resolveProperties({
|
|
|
|
args: resolveAddresses(contract.signer || contract.provider, args, fragment.inputs),
|
|
|
|
address: contract.resolvedAddress,
|
|
|
|
overrides: (resolveProperties(overrides) || { })
|
|
|
|
});
|
|
|
|
|
|
|
|
// The ABI coded transaction
|
2020-09-26 02:17:05 -04:00
|
|
|
const data = contract.interface.encodeFunctionData(fragment, resolved.args);
|
2020-06-01 04:46:37 -04:00
|
|
|
const tx: PopulatedTransaction = {
|
2020-09-26 02:17:05 -04:00
|
|
|
data: data,
|
2020-05-29 21:12:15 -04:00
|
|
|
to: resolved.address
|
|
|
|
};
|
|
|
|
|
|
|
|
// Resolved Overrides
|
|
|
|
const ro = resolved.overrides;
|
2020-06-01 04:46:37 -04:00
|
|
|
|
|
|
|
// Populate simple overrides
|
2020-05-29 21:12:15 -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); }
|
2021-06-10 17:42:20 -04:00
|
|
|
if (ro.maxFeePerGas != null) { tx.maxFeePerGas = BigNumber.from(ro.maxFeePerGas); }
|
|
|
|
if (ro.maxPriorityFeePerGas != null) { tx.maxPriorityFeePerGas = BigNumber.from(ro.maxPriorityFeePerGas); }
|
2020-06-01 04:46:37 -04:00
|
|
|
if (ro.from != null) { tx.from = ro.from; }
|
2020-05-29 21:12:15 -04:00
|
|
|
|
2021-03-26 16:16:56 -04:00
|
|
|
if (ro.type != null) { tx.type = ro.type; }
|
|
|
|
if (ro.accessList != null) { tx.accessList = accessListify(ro.accessList); }
|
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
// If there was no "gasLimit" override, but the ABI specifies a default, use it
|
2020-05-29 21:12:15 -04:00
|
|
|
if (tx.gasLimit == null && fragment.gas != null) {
|
2021-10-04 11:46:24 -04:00
|
|
|
// Compute the intrinsic gas cost for this transaction
|
2020-09-26 02:17:05 -04:00
|
|
|
// @TODO: This is based on the yellow paper as of Petersburg; this is something
|
|
|
|
// we may wish to parameterize in v6 as part of the Network object. Since this
|
|
|
|
// is always a non-nil to address, we can ignore G_create, but may wish to add
|
|
|
|
// similar logic to the ContractFactory.
|
|
|
|
let intrinsic = 21000;
|
|
|
|
const bytes = arrayify(data);
|
|
|
|
for (let i = 0; i < bytes.length; i++) {
|
|
|
|
intrinsic += 4;
|
|
|
|
if (bytes[i]) { intrinsic += 64; }
|
|
|
|
}
|
|
|
|
tx.gasLimit = BigNumber.from(fragment.gas).add(intrinsic);
|
2020-05-29 21:12:15 -04:00
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-06-01 04:46:37 -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;
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:47:26 -04:00
|
|
|
if (ro.customData) {
|
|
|
|
tx.customData = shallowCopy(ro.customData);
|
|
|
|
}
|
|
|
|
|
2022-03-04 16:15:47 -05:00
|
|
|
if (ro.ccipReadEnabled) {
|
|
|
|
tx.ccipReadEnabled = !!ro.ccipReadEnabled;
|
|
|
|
}
|
|
|
|
|
2021-10-04 11:46:24 -04:00
|
|
|
// Remove the overrides
|
2020-05-29 21:12:15 -04:00
|
|
|
delete overrides.nonce;
|
|
|
|
delete overrides.gasLimit;
|
|
|
|
delete overrides.gasPrice;
|
2020-06-01 04:46:37 -04:00
|
|
|
delete overrides.from;
|
|
|
|
delete overrides.value;
|
2019-11-19 18:13:58 +09:00
|
|
|
|
2021-03-26 16:16:56 -04:00
|
|
|
delete overrides.type;
|
|
|
|
delete overrides.accessList;
|
|
|
|
|
2021-06-10 17:42:20 -04:00
|
|
|
delete overrides.maxFeePerGas;
|
|
|
|
delete overrides.maxPriorityFeePerGas;
|
|
|
|
|
2021-10-05 15:47:26 -04:00
|
|
|
delete overrides.customData;
|
2022-03-04 16:15:47 -05:00
|
|
|
delete overrides.ccipReadEnabled;
|
2021-10-05 15:47:26 -04:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
// Make sure there are no stray overrides, which may indicate a
|
|
|
|
// typo or using an unsupported key.
|
2020-06-03 03:33:28 -04:00
|
|
|
const leftovers = Object.keys(overrides).filter((key) => ((<any>overrides)[key] != null));
|
2020-05-29 21:12:15 -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 04:46:37 -04:00
|
|
|
overrides: leftovers
|
2020-05-29 21:12:15 -04:00
|
|
|
});
|
|
|
|
}
|
2019-11-19 18:13:58 +09:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
return tx;
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
function buildPopulate(contract: Contract, fragment: FunctionFragment): ContractFunction<PopulatedTransaction> {
|
2020-09-26 01:23:43 -04:00
|
|
|
return function(...args: Array<any>): Promise<PopulatedTransaction> {
|
2020-06-01 04:46:37 -04:00
|
|
|
return populateTransaction(contract, fragment, args);
|
2020-05-29 21:12:15 -04:00
|
|
|
};
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
function buildEstimate(contract: Contract, fragment: FunctionFragment): ContractFunction<BigNumber> {
|
|
|
|
const signerOrProvider = (contract.signer || contract.provider);
|
|
|
|
return async function(...args: Array<any>): Promise<BigNumber> {
|
|
|
|
if (!signerOrProvider) {
|
2020-06-01 04:46:37 -04:00
|
|
|
logger.throwError("estimate require a provider or signer", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "estimateGas"
|
|
|
|
})
|
2020-05-29 21:12:15 -04:00
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
const tx = await populateTransaction(contract, fragment, args);
|
2020-05-29 21:12:15 -04:00
|
|
|
return await signerOrProvider.estimateGas(tx);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-17 21:38:47 -04:00
|
|
|
function addContractWait(contract: Contract, tx: TransactionResponse) {
|
|
|
|
const wait = tx.wait.bind(tx);
|
|
|
|
tx.wait = (confirmations?: number) => {
|
|
|
|
return wait(confirmations).then((receipt: ContractReceipt) => {
|
|
|
|
receipt.events = receipt.logs.map((log) => {
|
|
|
|
let event: Event = (<Event>deepCopy(log));
|
|
|
|
let parsed: LogDescription = 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: BytesLike, topics?: Array<any>) => {
|
|
|
|
return contract.interface.decodeEventLog(parsed.eventFragment, data, topics);
|
|
|
|
};
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
return receipt;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
function buildCall(contract: Contract, fragment: FunctionFragment, collapseSimple: boolean): ContractFunction {
|
|
|
|
const signerOrProvider = (contract.signer || contract.provider);
|
|
|
|
|
|
|
|
return async function(...args: Array<any>): Promise<any> {
|
2020-06-01 04:46:37 -04:00
|
|
|
// Extract the "blockTag" override if present
|
2020-05-29 21:12:15 -04:00
|
|
|
let blockTag = undefined;
|
|
|
|
if (args.length === fragment.inputs.length + 1 && typeof(args[args.length - 1]) === "object") {
|
2020-06-01 04:46:37 -04:00
|
|
|
const overrides = shallowCopy(args.pop());
|
2020-06-03 03:33:28 -04:00
|
|
|
if (overrides.blockTag != null) {
|
2020-05-29 21:12:15 -04:00
|
|
|
blockTag = await overrides.blockTag;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2020-06-03 03:33:28 -04:00
|
|
|
delete overrides.blockTag;
|
2020-06-01 04:46:37 -04:00
|
|
|
args.push(overrides);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the contract was just deployed, wait until it is mined
|
|
|
|
if (contract.deployTransaction != null) {
|
|
|
|
await contract._deployed(blockTag);
|
2020-05-29 21:12:15 -04:00
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
// Call a node and get the result
|
|
|
|
const tx = await populateTransaction(contract, fragment, args);
|
|
|
|
const result = await signerOrProvider.call(tx, blockTag);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
try {
|
2020-06-01 04:46:37 -04:00
|
|
|
let value = contract.interface.decodeFunctionResult(fragment, result);
|
2020-05-29 21:12:15 -04:00
|
|
|
if (collapseSimple && fragment.outputs.length === 1) {
|
2020-06-01 04:46:37 -04:00
|
|
|
value = value[0];
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2020-06-01 04:46:37 -04:00
|
|
|
return value;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === Logger.errors.CALL_EXCEPTION) {
|
|
|
|
error.address = contract.address;
|
|
|
|
error.args = args;
|
|
|
|
error.transaction = tx;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2020-05-29 21:12:15 -04:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
function buildSend(contract: Contract, fragment: FunctionFragment): ContractFunction<TransactionResponse> {
|
|
|
|
return async function(...args: Array<any>): Promise<TransactionResponse> {
|
|
|
|
if (!contract.signer) {
|
2020-06-01 04:46:37 -04:00
|
|
|
logger.throwError("sending a transaction requires a signer", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "sendTransaction"
|
|
|
|
})
|
2020-05-29 21:12:15 -04:00
|
|
|
}
|
|
|
|
|
2021-10-04 11:46:24 -04:00
|
|
|
// If the contract was just deployed, wait until it is mined
|
2020-06-01 04:46:37 -04:00
|
|
|
if (contract.deployTransaction != null) {
|
|
|
|
await contract._deployed();
|
2020-05-29 21:12:15 -04:00
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
const txRequest = await populateTransaction(contract, fragment, args);
|
2019-06-28 15:47:42 -04:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
const tx = await contract.signer.sendTransaction(txRequest);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2021-10-04 11:46:24 -04:00
|
|
|
// Tweak the tx.wait so the receipt has extra properties
|
2021-10-17 21:38:47 -04:00
|
|
|
addContractWait(contract, tx);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
return tx;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildDefault(contract: Contract, fragment: FunctionFragment, collapseSimple: boolean): ContractFunction {
|
|
|
|
if (fragment.constant) {
|
|
|
|
return buildCall(contract, fragment, collapseSimple);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2020-05-29 21:12:15 -04:00
|
|
|
return buildSend(contract, fragment);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getEventTag(filter: EventFilter): string {
|
|
|
|
if (filter.address && (filter.topics == null || filter.topics.length === 0)) {
|
|
|
|
return "*";
|
|
|
|
}
|
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
return (filter.address || "*") + "@" + (filter.topics ? filter.topics.map((topic) => {
|
|
|
|
if (Array.isArray(topic)) {
|
|
|
|
return topic.join("|");
|
|
|
|
}
|
|
|
|
return topic;
|
|
|
|
}).join(":"): "");
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
class RunningEvent {
|
|
|
|
readonly tag: string;
|
|
|
|
readonly filter: EventFilter;
|
|
|
|
private _listeners: Array<{ listener: Listener, once: boolean }>;
|
|
|
|
|
|
|
|
constructor(tag: string, filter: EventFilter) {
|
|
|
|
defineReadOnly(this, "tag", tag);
|
|
|
|
defineReadOnly(this, "filter", filter);
|
|
|
|
this._listeners = [ ];
|
|
|
|
}
|
|
|
|
|
|
|
|
addListener(listener: Listener, once: boolean): void {
|
|
|
|
this._listeners.push({ listener: listener, once: once });
|
|
|
|
}
|
|
|
|
|
|
|
|
removeListener(listener: Listener): void {
|
|
|
|
let done = false;
|
|
|
|
this._listeners = this._listeners.filter((item) => {
|
|
|
|
if (done || item.listener !== listener) { return true; }
|
|
|
|
done = true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAllListeners(): void {
|
|
|
|
this._listeners = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners(): Array<Listener> {
|
|
|
|
return this._listeners.map((i) => i.listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
listenerCount(): number {
|
|
|
|
return this._listeners.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
run(args: Array<any>): number {
|
2020-01-07 19:46:51 -05:00
|
|
|
const listenerCount = this.listenerCount();
|
2019-05-23 18:24:31 -04:00
|
|
|
this._listeners = this._listeners.filter((item) => {
|
|
|
|
|
2020-01-07 19:46:51 -05:00
|
|
|
const argsCopy = args.slice();
|
2019-05-23 18:24:31 -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: Event): void {
|
|
|
|
}
|
2020-04-16 21:33:24 -04:00
|
|
|
|
|
|
|
// Returns the array that will be applied to an emit
|
|
|
|
getEmit(event: Event): Array<any> {
|
|
|
|
return [ event ];
|
|
|
|
}
|
2019-05-23 18:24:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class ErrorRunningEvent extends RunningEvent {
|
|
|
|
constructor() {
|
|
|
|
super("error", null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 03:25:42 -04:00
|
|
|
|
2020-04-16 21:33:24 -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
|
2021-10-04 11:46:24 -04:00
|
|
|
// will, and additionally dereference the arguments when emitting
|
2019-05-23 18:24:31 -04:00
|
|
|
class FragmentRunningEvent extends RunningEvent {
|
|
|
|
readonly address: string;
|
|
|
|
readonly interface: Interface;
|
|
|
|
readonly fragment: EventFragment;
|
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
constructor(address: string, contractInterface: Interface, fragment: EventFragment, topics?: Array<string|Array<string>>) {
|
2020-01-07 19:46:51 -05:00
|
|
|
const filter: EventFilter = {
|
2019-07-20 21:03:53 -03:00
|
|
|
address: address
|
|
|
|
}
|
|
|
|
|
|
|
|
let topic = contractInterface.getEventTopic(fragment);
|
|
|
|
if (topics) {
|
2019-08-01 18:04:06 -04:00
|
|
|
if (topic !== topics[0]) { logger.throwArgumentError("topic mismatch", "topics", topics); }
|
2019-07-20 21:03:53 -03:00
|
|
|
filter.topics = topics.slice();
|
|
|
|
} else {
|
|
|
|
filter.topics = [ topic ];
|
2019-05-23 18:24:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
super(getEventTag(filter), filter);
|
|
|
|
defineReadOnly(this, "address", address);
|
|
|
|
defineReadOnly(this, "interface", contractInterface);
|
|
|
|
defineReadOnly(this, "fragment", fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
prepareEvent(event: Event): void {
|
|
|
|
super.prepareEvent(event);
|
|
|
|
|
|
|
|
event.event = this.fragment.name;
|
|
|
|
event.eventSignature = this.fragment.format();
|
|
|
|
|
|
|
|
event.decode = (data: BytesLike, topics?: Array<string>) => {
|
|
|
|
return this.interface.decodeEventLog(this.fragment, data, topics);
|
|
|
|
};
|
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
try {
|
|
|
|
event.args = this.interface.decodeEventLog(this.fragment, event.data, event.topics);
|
|
|
|
} catch (error) {
|
|
|
|
event.args = null;
|
|
|
|
event.decodeError = error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getEmit(event: Event): Array<any> {
|
2020-04-25 03:25:42 -04:00
|
|
|
const errors = checkResultErrors(event.args);
|
|
|
|
if (errors.length) { throw errors[0].error; }
|
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
const args = (event.args || []).slice();
|
|
|
|
args.push(event);
|
|
|
|
return args;
|
2019-05-23 18:24:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 11:46:24 -04:00
|
|
|
// A Wildcard Event will attempt to populate:
|
2020-04-16 21:33:24 -04:00
|
|
|
// - 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-05-23 18:24:31 -04:00
|
|
|
class WildcardRunningEvent extends RunningEvent {
|
|
|
|
readonly address: string;
|
|
|
|
readonly interface: Interface;
|
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
constructor(address: string, contractInterface: Interface) {
|
2019-05-23 18:24:31 -04:00
|
|
|
super("*", { address: address });
|
|
|
|
defineReadOnly(this, "address", address);
|
|
|
|
defineReadOnly(this, "interface", contractInterface);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareEvent(event: Event): void {
|
|
|
|
super.prepareEvent(event);
|
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
try {
|
|
|
|
const parsed = this.interface.parseLog(event);
|
2019-05-23 18:24:31 -04:00
|
|
|
event.event = parsed.name;
|
|
|
|
event.eventSignature = parsed.signature;
|
|
|
|
|
|
|
|
event.decode = (data: BytesLike, topics?: Array<string>) => {
|
|
|
|
return this.interface.decodeEventLog(parsed.eventFragment, data, topics);
|
|
|
|
};
|
|
|
|
|
2020-01-07 19:46:51 -05:00
|
|
|
event.args = parsed.args;
|
2020-04-16 21:33:24 -04:00
|
|
|
} catch (error) {
|
|
|
|
// No matching event
|
2019-05-23 18:24:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2021-03-30 14:28:43 -04:00
|
|
|
export type ContractInterface = string | ReadonlyArray<Fragment | JsonFragment | string> | Interface;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-06-11 23:42:35 -04:00
|
|
|
type InterfaceFunc = (contractInterface: ContractInterface) => Interface;
|
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
|
2021-03-26 17:32:36 -04:00
|
|
|
export class BaseContract {
|
2019-05-14 18:25:46 -04:00
|
|
|
readonly address: string;
|
|
|
|
readonly interface: Interface;
|
|
|
|
|
|
|
|
readonly signer: Signer;
|
|
|
|
readonly provider: Provider;
|
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
readonly functions: { [ name: string ]: ContractFunction };
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
readonly callStatic: { [ name: string ]: ContractFunction };
|
|
|
|
readonly estimateGas: { [ name: string ]: ContractFunction<BigNumber> };
|
2020-06-01 04:46:37 -04:00
|
|
|
readonly populateTransaction: { [ name: string ]: ContractFunction<PopulatedTransaction> };
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
readonly filters: { [ name: string ]: (...args: Array<any>) => EventFilter };
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
// This will always be an address. This will only differ from
|
|
|
|
// address if an ENS name was used in the constructor
|
2020-03-10 19:02:53 +01:00
|
|
|
readonly resolvedAddress: Promise<string>;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
// This is only set if the contract was created with a call to deploy
|
|
|
|
readonly deployTransaction: TransactionResponse;
|
|
|
|
|
2020-05-29 21:12:15 -04:00
|
|
|
_deployedPromise: Promise<Contract>;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2021-10-04 11:46:24 -04:00
|
|
|
// A list of RunningEvents to track listeners for each event tag
|
2020-05-29 21:12:15 -04:00
|
|
|
_runningEvents: { [ eventTag: string ]: RunningEvent };
|
2019-05-23 18:59:04 -04:00
|
|
|
|
|
|
|
// Wrapped functions to call emit and allow deregistration from the provider
|
2020-05-29 21:12:15 -04:00
|
|
|
_wrappedEmits: { [ eventTag: string ]: (...args: Array<any>) => void };
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
constructor(addressOrName: string, contractInterface: ContractInterface, signerOrProvider?: Signer | Provider) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.checkNew(new.target, Contract);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
// @TODO: Maybe still check the addressOrName looks like a valid address or name?
|
|
|
|
//address = getAddress(address);
|
2019-06-11 23:42:35 -04:00
|
|
|
defineReadOnly(this, "interface", getStatic<InterfaceFunc>(new.target, "getInterface")(contractInterface));
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
if (signerOrProvider == null) {
|
|
|
|
defineReadOnly(this, "provider", null);
|
|
|
|
defineReadOnly(this, "signer", null);
|
|
|
|
} else if (Signer.isSigner(signerOrProvider)) {
|
2019-06-10 22:25:46 -04:00
|
|
|
defineReadOnly(this, "provider", signerOrProvider.provider || null);
|
2019-05-14 18:25:46 -04:00
|
|
|
defineReadOnly(this, "signer", signerOrProvider);
|
2019-06-10 22:25:46 -04:00
|
|
|
} else if (Provider.isProvider(signerOrProvider)) {
|
2019-05-14 18:25:46 -04:00
|
|
|
defineReadOnly(this, "provider", signerOrProvider);
|
|
|
|
defineReadOnly(this, "signer", null);
|
|
|
|
} else {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid signer or provider", "signerOrProvider", signerOrProvider);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
defineReadOnly(this, "callStatic", { });
|
2020-03-10 19:02:53 +01:00
|
|
|
defineReadOnly(this, "estimateGas", { });
|
2019-05-14 18:25:46 -04:00
|
|
|
defineReadOnly(this, "functions", { });
|
|
|
|
defineReadOnly(this, "populateTransaction", { });
|
|
|
|
|
|
|
|
defineReadOnly(this, "filters", { });
|
|
|
|
|
2019-11-19 18:13:58 +09:00
|
|
|
{
|
|
|
|
const uniqueFilters: { [ name: string ]: Array<string> } = { };
|
|
|
|
Object.keys(this.interface.events).forEach((eventSignature) => {
|
2020-01-07 19:46:51 -05:00
|
|
|
const event = this.interface.events[eventSignature];
|
2020-05-29 21:12:15 -04:00
|
|
|
defineReadOnly(this.filters, eventSignature, (...args: Array<any>) => {
|
2019-11-19 18:13:58 +09:00
|
|
|
return {
|
|
|
|
address: this.address,
|
|
|
|
topics: this.interface.encodeFilterTopics(event, args)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!uniqueFilters[event.name]) { uniqueFilters[event.name] = [ ]; }
|
|
|
|
uniqueFilters[event.name].push(eventSignature);
|
|
|
|
});
|
|
|
|
|
|
|
|
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-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
});
|
2019-11-19 18:13:58 +09:00
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
defineReadOnly(this, "_runningEvents", { });
|
|
|
|
defineReadOnly(this, "_wrappedEmits", { });
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2021-02-12 18:58:28 -05:00
|
|
|
if (addressOrName == null) {
|
|
|
|
logger.throwArgumentError("invalid contract address or ENS name", "addressOrName", addressOrName);
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
defineReadOnly(this, "address", addressOrName);
|
|
|
|
if (this.provider) {
|
2020-09-26 01:23:43 -04:00
|
|
|
defineReadOnly(this, "resolvedAddress", resolveName(this.provider, addressOrName));
|
2019-05-14 18:25:46 -04:00
|
|
|
} else {
|
|
|
|
try {
|
2020-06-01 04:46:37 -04:00
|
|
|
defineReadOnly(this, "resolvedAddress", Promise.resolve(getAddress(addressOrName)));
|
2019-05-14 18:25:46 -04:00
|
|
|
} catch (error) {
|
|
|
|
// Without a provider, we cannot use ENS names
|
2020-06-01 04:46:37 -04:00
|
|
|
logger.throwError("provider is required to use ENS name as contract address", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "new Contract"
|
|
|
|
});
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 03:51:53 -05:00
|
|
|
// Swallow bad ENS names to prevent Unhandled Exceptions
|
|
|
|
this.resolvedAddress.catch((e) => { });
|
|
|
|
|
2020-05-03 14:54:26 -04:00
|
|
|
const uniqueNames: { [ name: string ]: Array<string> } = { };
|
|
|
|
const uniqueSignatures: { [ signature: string ]: boolean } = { };
|
|
|
|
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]) {
|
2021-06-10 17:42:20 -04:00
|
|
|
logger.warn(`Duplicate ABI entry for ${ JSON.stringify(signature) }`);
|
2020-05-03 14:54:26 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
uniqueSignatures[signature] = true;
|
|
|
|
|
|
|
|
// Track unique names; we only expose bare named functions if they
|
|
|
|
// are ambiguous
|
|
|
|
{
|
|
|
|
const name = fragment.name;
|
2021-10-15 21:32:48 -04:00
|
|
|
if (!uniqueNames[`%${ name }`]) { uniqueNames[`%${ name }`] = [ ]; }
|
|
|
|
uniqueNames[`%${ name }`].push(signature);
|
2020-05-03 14:54:26 -04:00
|
|
|
}
|
|
|
|
|
2021-03-26 17:32:36 -04:00
|
|
|
if ((<Contract>this)[signature] == null) {
|
2020-05-29 21:12:15 -04:00
|
|
|
defineReadOnly<any, any>(this, signature, buildDefault(this, fragment, true));
|
2020-05-03 14:54:26 -04:00
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-29 21:12:15 -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 14:54:26 -04:00
|
|
|
if (this.functions[signature] == null) {
|
2020-05-29 21:12:15 -04:00
|
|
|
defineReadOnly(this.functions, signature, buildDefault(this, fragment, false));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 14:54:26 -04:00
|
|
|
if (this.callStatic[signature] == null) {
|
2020-05-29 21:12:15 -04:00
|
|
|
defineReadOnly(this.callStatic, signature, buildCall(this, fragment, true));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 14:54:26 -04:00
|
|
|
if (this.populateTransaction[signature] == null) {
|
2020-05-29 21:12:15 -04:00
|
|
|
defineReadOnly(this.populateTransaction, signature, buildPopulate(this, fragment));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 14:54:26 -04:00
|
|
|
if (this.estimateGas[signature] == null) {
|
2020-05-29 21:12:15 -04:00
|
|
|
defineReadOnly(this.estimateGas, signature, buildEstimate(this, fragment));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2020-05-03 14:54:26 -04:00
|
|
|
});
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-05-03 14:54:26 -04:00
|
|
|
Object.keys(uniqueNames).forEach((name) => {
|
|
|
|
// Ambiguous names to not get attached as bare names
|
|
|
|
const signatures = uniqueNames[name];
|
|
|
|
if (signatures.length > 1) { return; }
|
|
|
|
|
2021-10-15 21:32:48 -04:00
|
|
|
// Strip off the leading "%" used for prototype protection
|
|
|
|
name = name.substring(1);
|
|
|
|
|
2020-05-03 14:54:26 -04:00
|
|
|
const signature = signatures[0];
|
|
|
|
|
2021-03-26 16:37:08 -04:00
|
|
|
// If overwriting a member property that is null, swallow the error
|
|
|
|
try {
|
2021-03-26 17:32:36 -04:00
|
|
|
if ((<Contract>this)[name] == null) {
|
|
|
|
defineReadOnly(<Contract>this, name, (<Contract>this)[signature]);
|
2021-03-26 16:37:08 -04:00
|
|
|
}
|
|
|
|
} catch (e) { }
|
2019-11-19 18:13:58 +09:00
|
|
|
|
2020-05-03 14:54:26 -04:00
|
|
|
if (this.functions[name] == null) {
|
|
|
|
defineReadOnly(this.functions, name, this.functions[signature]);
|
|
|
|
}
|
2019-11-19 18:13:58 +09:00
|
|
|
|
2020-05-03 14:54:26 -04:00
|
|
|
if (this.callStatic[name] == null) {
|
|
|
|
defineReadOnly(this.callStatic, name, this.callStatic[signature]);
|
2019-11-19 18:13:58 +09:00
|
|
|
}
|
|
|
|
|
2020-05-03 14:54:26 -04:00
|
|
|
if (this.populateTransaction[name] == null) {
|
|
|
|
defineReadOnly(this.populateTransaction, name, this.populateTransaction[signature]);
|
2019-11-19 18:13:58 +09:00
|
|
|
}
|
|
|
|
|
2020-05-03 14:54:26 -04:00
|
|
|
if (this.estimateGas[name] == null) {
|
|
|
|
defineReadOnly(this.estimateGas, name, this.estimateGas[signature]);
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static getContractAddress(transaction: { from: string, nonce: BigNumberish }): string {
|
|
|
|
return getContractAddress(transaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getInterface(contractInterface: ContractInterface): Interface {
|
2019-06-10 22:25:46 -04:00
|
|
|
if (Interface.isInterface(contractInterface)) {
|
2019-05-14 18:25:46 -04:00
|
|
|
return contractInterface;
|
|
|
|
}
|
|
|
|
return new Interface(contractInterface);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: Allow timeout?
|
|
|
|
deployed(): Promise<Contract> {
|
|
|
|
return this._deployed();
|
|
|
|
}
|
|
|
|
|
|
|
|
_deployed(blockTag?: BlockTag): Promise<Contract> {
|
|
|
|
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") {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwError("contract not deployed", Logger.errors.UNSUPPORTED_OPERATION, {
|
2019-05-14 18:25:46 -04:00
|
|
|
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?: TransactionRequest): Promise<TransactionResponse> {
|
|
|
|
if (!this.signer) {
|
2019-09-06 18:46:34 -04:00
|
|
|
logger.throwError("sending a transactions require a signer", Logger.errors.UNSUPPORTED_OPERATION, { operation: "sendTransaction(fallback)" })
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-06-01 04:46:37 -04:00
|
|
|
const tx: Deferrable<TransactionRequest> = shallowCopy(overrides || {});
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
["from", "to"].forEach(function(key) {
|
|
|
|
if ((<any>tx)[key] == null) { return; }
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwError("cannot override " + key, Logger.errors.UNSUPPORTED_OPERATION, { operation: key })
|
2019-05-14 18:25:46 -04:00
|
|
|
});
|
|
|
|
|
2020-03-10 19:02:53 +01:00
|
|
|
tx.to = this.resolvedAddress;
|
2019-05-14 18:25:46 -04:00
|
|
|
return this.deployed().then(() => {
|
|
|
|
return this.signer.sendTransaction(tx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconnect to a different signer or provider
|
|
|
|
connect(signerOrProvider: Signer | Provider | string): Contract {
|
|
|
|
if (typeof(signerOrProvider) === "string") {
|
|
|
|
signerOrProvider = new VoidSigner(signerOrProvider, this.provider);
|
|
|
|
}
|
|
|
|
|
2020-01-07 19:46:51 -05:00
|
|
|
const contract = new (<{ new(...args: any[]): Contract }>(this.constructor))(this.address, this.interface, signerOrProvider);
|
2019-05-14 18:25:46 -04:00
|
|
|
if (this.deployTransaction) {
|
|
|
|
defineReadOnly(contract, "deployTransaction", this.deployTransaction);
|
|
|
|
}
|
2022-02-24 03:51:53 -05:00
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
return contract;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-attach to a different on-chain instance of this contract
|
|
|
|
attach(addressOrName: string): Contract {
|
|
|
|
return new (<{ new(...args: any[]): Contract }>(this.constructor))(addressOrName, this.interface, this.signer || this.provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isIndexed(value: any): value is Indexed {
|
2019-06-10 22:25:46 -04:00
|
|
|
return Indexed.isIndexed(value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
private _normalizeRunningEvent(runningEvent: RunningEvent): 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
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
private _getRunningEvent(eventName: EventFilter | string): RunningEvent {
|
2019-05-14 18:25:46 -04:00
|
|
|
if (typeof(eventName) === "string") {
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
// 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:25:42 -04:00
|
|
|
// Listen for any event that is registered
|
|
|
|
if (eventName === "event") {
|
|
|
|
return this._normalizeRunningEvent(new RunningEvent("event", null));
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
// Listen for any event
|
|
|
|
if (eventName === "*") {
|
2019-05-23 18:24:31 -04:00
|
|
|
return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
// Get the event Fragment (throws if ambiguous/unknown event)
|
2020-01-07 19:46:51 -05:00
|
|
|
const fragment = this.interface.getEvent(eventName)
|
2019-05-23 18:24:31 -04:00
|
|
|
return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
// We have topics to filter by...
|
|
|
|
if (eventName.topics && eventName.topics.length > 0) {
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
// Is it a known topichash? (throws if no matching topichash)
|
|
|
|
try {
|
2020-05-29 21:12:15 -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:33:24 -04:00
|
|
|
return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment, eventName.topics));
|
|
|
|
} catch (error) { }
|
|
|
|
|
|
|
|
// Filter by the unknown topichash
|
|
|
|
const filter: EventFilter = {
|
|
|
|
address: this.address,
|
|
|
|
topics: eventName.topics
|
2019-05-23 18:24:31 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
return this._normalizeRunningEvent(new RunningEvent(getEventTag(filter), filter));
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface));
|
2019-05-23 18:24:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_checkRunningEvents(runningEvent: RunningEvent): void {
|
|
|
|
if (runningEvent.listenerCount() === 0) {
|
|
|
|
delete this._runningEvents[runningEvent.tag];
|
|
|
|
|
2020-04-15 18:09:38 -04:00
|
|
|
// If we have a poller for this, remove it
|
|
|
|
const emit = this._wrappedEmits[runningEvent.tag];
|
2021-04-18 19:23:27 -04:00
|
|
|
if (emit && runningEvent.filter) {
|
2020-04-15 18:09:38 -04:00
|
|
|
this.provider.off(runningEvent.filter, emit);
|
|
|
|
delete this._wrappedEmits[runningEvent.tag];
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:33:24 -04:00
|
|
|
// Subclasses can override this to gracefully recover
|
|
|
|
// from parse errors if they wish
|
|
|
|
_wrapEvent(runningEvent: RunningEvent, log: Log, listener: Listener): Event {
|
2020-01-07 19:46:51 -05:00
|
|
|
const event = <Event>deepCopy(log);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
event.removeListener = () => {
|
|
|
|
if (!listener) { return; }
|
2019-05-23 18:24:31 -04:00
|
|
|
runningEvent.removeListener(listener);
|
|
|
|
this._checkRunningEvents(runningEvent);
|
2019-05-14 18:25:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
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:33:24 -04:00
|
|
|
// This may throw if the topics and data mismatch the signature
|
|
|
|
runningEvent.prepareEvent(event);
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
private _addEventListener(runningEvent: RunningEvent, listener: Listener, once: boolean): void {
|
2019-05-14 18:25:46 -04:00
|
|
|
if (!this.provider) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwError("events require a provider or a signer with a provider", Logger.errors.UNSUPPORTED_OPERATION, { operation: "once" })
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
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:33:24 -04:00
|
|
|
// If we are not polling the provider, start polling
|
2019-05-23 18:24:31 -04:00
|
|
|
if (!this._wrappedEmits[runningEvent.tag]) {
|
2020-01-07 19:46:51 -05:00
|
|
|
const wrappedEmit = (log: Log) => {
|
2020-04-25 03:25:42 -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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit "error" if there was an error
|
|
|
|
if (event.decodeError != null) {
|
|
|
|
this.emit("error", event.decodeError, event);
|
2020-04-16 21:33:24 -04:00
|
|
|
}
|
2019-05-23 18:24:31 -04:00
|
|
|
};
|
|
|
|
this._wrappedEmits[runningEvent.tag] = wrappedEmit;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
// Special events, like "error" do not have a filter
|
|
|
|
if (runningEvent.filter != null) {
|
|
|
|
this.provider.on(runningEvent.filter, wrappedEmit);
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
queryFilter(event: EventFilter, fromBlockOrBlockhash?: BlockTag | string, toBlock?: BlockTag): Promise<Array<Event>> {
|
2020-01-07 19:46:51 -05:00
|
|
|
const runningEvent = this._getRunningEvent(event);
|
|
|
|
const filter = shallowCopy(runningEvent.filter);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
if (typeof(fromBlockOrBlockhash) === "string" && isHexString(fromBlockOrBlockhash, 32)) {
|
|
|
|
if (toBlock != null) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("cannot specify toBlock with blockhash", "toBlock", toBlock);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2020-05-20 23:56:26 -04:00
|
|
|
(<FilterByBlockHash>filter).blockHash = fromBlockOrBlockhash;
|
2019-05-14 18:25:46 -04:00
|
|
|
} else {
|
2020-02-06 18:21:34 -05:00
|
|
|
(<Filter>filter).fromBlock = ((fromBlockOrBlockhash != null) ? fromBlockOrBlockhash: 0);
|
|
|
|
(<Filter>filter).toBlock = ((toBlock != null) ? toBlock: "latest");
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.provider.getLogs(filter).then((logs) => {
|
2019-05-24 15:59:04 -04:00
|
|
|
return logs.map((log) => this._wrapEvent(runningEvent, log, null));
|
2019-05-14 18:25:46 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
on(event: EventFilter | string, listener: Listener): this {
|
|
|
|
this._addEventListener(this._getRunningEvent(event), listener, false);
|
2019-05-14 18:25:46 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
once(event: EventFilter | string, listener: Listener): this {
|
|
|
|
this._addEventListener(this._getRunningEvent(event), listener, true);
|
2019-05-14 18:25:46 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(eventName: EventFilter | string, ...args: Array<any>): boolean {
|
|
|
|
if (!this.provider) { return false; }
|
|
|
|
|
2020-01-07 19:46:51 -05:00
|
|
|
const runningEvent = this._getRunningEvent(eventName);
|
|
|
|
const result = (runningEvent.run(args) > 0);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
// May have drained all the "once" events; check for living events
|
|
|
|
this._checkRunningEvents(runningEvent);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
listenerCount(eventName?: EventFilter | string): number {
|
|
|
|
if (!this.provider) { return 0; }
|
2020-12-18 03:56:14 -05:00
|
|
|
if (eventName == null) {
|
|
|
|
return Object.keys(this._runningEvents).reduce((accum, key) => {
|
|
|
|
return accum + this._runningEvents[key].listenerCount();
|
|
|
|
}, 0);
|
|
|
|
}
|
2019-05-23 18:24:31 -04:00
|
|
|
return this._getRunningEvent(eventName).listenerCount();
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
listeners(eventName?: EventFilter | string): Array<Listener> {
|
|
|
|
if (!this.provider) { return []; }
|
|
|
|
|
|
|
|
if (eventName == null) {
|
2020-01-07 19:46:51 -05:00
|
|
|
const result: Array<Listener> = [ ];
|
2019-05-23 18:24:31 -04:00
|
|
|
for (let tag in this._runningEvents) {
|
|
|
|
this._runningEvents[tag].listeners().forEach((listener) => {
|
|
|
|
result.push(listener)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
return this._getRunningEvent(eventName).listeners();
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
removeAllListeners(eventName?: EventFilter | string): this {
|
2019-05-14 18:25:46 -04:00
|
|
|
if (!this.provider) { return this; }
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
if (eventName == null) {
|
2020-01-07 19:46:51 -05:00
|
|
|
for (const tag in this._runningEvents) {
|
|
|
|
const runningEvent = this._runningEvents[tag];
|
2019-05-23 18:24:31 -04:00
|
|
|
runningEvent.removeAllListeners();
|
|
|
|
this._checkRunningEvents(runningEvent);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
2019-05-23 18:24:31 -04:00
|
|
|
return this;
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
// Delete any listeners
|
2020-01-07 19:46:51 -05:00
|
|
|
const runningEvent = this._getRunningEvent(eventName);
|
2019-05-23 18:24:31 -04:00
|
|
|
runningEvent.removeAllListeners();
|
|
|
|
this._checkRunningEvents(runningEvent);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
off(eventName: EventFilter | string, listener: Listener): this {
|
2019-05-14 18:25:46 -04:00
|
|
|
if (!this.provider) { return this; }
|
2020-01-07 19:46:51 -05:00
|
|
|
const runningEvent = this._getRunningEvent(eventName);
|
2019-05-23 18:24:31 -04:00
|
|
|
runningEvent.removeListener(listener);
|
|
|
|
this._checkRunningEvents(runningEvent);
|
2019-05-14 18:25:46 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-05-23 18:24:31 -04:00
|
|
|
removeListener(eventName: EventFilter | string, listener: Listener): this {
|
2019-05-14 18:25:46 -04:00
|
|
|
return this.off(eventName, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-26 17:32:36 -04:00
|
|
|
export class Contract extends BaseContract {
|
|
|
|
// The meta-class properties
|
|
|
|
readonly [ key: string ]: ContractFunction | any;
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
export class ContractFactory {
|
|
|
|
|
|
|
|
readonly interface: Interface;
|
|
|
|
readonly bytecode: string;
|
|
|
|
readonly signer: Signer;
|
|
|
|
|
|
|
|
constructor(contractInterface: ContractInterface, bytecode: BytesLike | { object: string }, signer?: Signer) {
|
|
|
|
|
|
|
|
let bytecodeHex: string = 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 = (<any>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)) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid bytecode", "bytecode", bytecode);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a signer, make sure it is valid
|
2019-06-10 22:25:46 -04:00
|
|
|
if (signer && !Signer.isSigner(signer)) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError("invalid signer", "signer", signer);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
defineReadOnly(this, "bytecode", bytecodeHex);
|
2019-06-11 23:42:35 -04:00
|
|
|
defineReadOnly(this, "interface", getStatic<InterfaceFunc>(new.target, "getInterface")(contractInterface));
|
2019-05-14 18:25:46 -04:00
|
|
|
defineReadOnly(this, "signer", signer || null);
|
|
|
|
}
|
|
|
|
|
2021-10-04 11:46:24 -04:00
|
|
|
// @TODO: Future; rename to populateTransaction?
|
2020-06-01 04:46:37 -04:00
|
|
|
getDeployTransaction(...args: Array<any>): TransactionRequest {
|
|
|
|
let tx: TransactionRequest = { };
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
// If we have 1 additional argument, we allow transaction overrides
|
2020-04-23 22:28:51 -04:00
|
|
|
if (args.length === this.interface.deploy.inputs.length + 1 && typeof(args[args.length - 1]) === "object") {
|
2019-05-14 18:25:46 -04:00
|
|
|
tx = shallowCopy(args.pop());
|
2020-01-07 19:46:51 -05:00
|
|
|
for (const key in tx) {
|
2019-05-14 18:25:46 -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 ((<any>tx)[key] == null) { return; }
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwError("cannot override " + key, Logger.errors.UNSUPPORTED_OPERATION, { operation: key })
|
2019-05-14 18:25:46 -04:00
|
|
|
});
|
|
|
|
|
2021-07-29 17:25:19 -04:00
|
|
|
if (tx.value) {
|
|
|
|
const value = BigNumber.from(tx.value);
|
|
|
|
if (!value.isZero() && !this.interface.deploy.payable) {
|
|
|
|
logger.throwError("non-payable constructor cannot override value", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "overrides.value",
|
|
|
|
value: tx.value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
// Make sure the call matches the constructor signature
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.checkArgumentCount(args.length, this.interface.deploy.inputs.length, " in Contract constructor");
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
// Set the data to the bytecode + the encoded constructor arguments
|
|
|
|
tx.data = hexlify(concat([
|
|
|
|
this.bytecode,
|
|
|
|
this.interface.encodeDeploy(args)
|
|
|
|
]));
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:28:51 -04:00
|
|
|
async deploy(...args: Array<any>): Promise<Contract> {
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-23 22:28:51 -04:00
|
|
|
let overrides: any = { };
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-04-23 22:28:51 -04:00
|
|
|
// 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 = await resolveAddresses(this.signer, args, this.interface.deploy.inputs);
|
|
|
|
params.push(overrides);
|
|
|
|
|
|
|
|
// Get the deployment transaction (with optional overrides)
|
|
|
|
const unsignedTx = this.getDeployTransaction(...params);
|
|
|
|
|
|
|
|
// Send the deployment transaction
|
|
|
|
const tx = await this.signer.sendTransaction(unsignedTx);
|
|
|
|
|
|
|
|
const address = getStatic<(tx: TransactionResponse) => string>(this.constructor, "getContractAddress")(tx);
|
|
|
|
const contract = getStatic<(address: string, contractInterface: ContractInterface, signer?: Signer) => Contract>(this.constructor, "getContract")(address, this.interface, this.signer);
|
2021-10-17 21:38:47 -04:00
|
|
|
|
|
|
|
// Add the modified wait that wraps events
|
|
|
|
addContractWait(contract, tx);
|
|
|
|
|
2020-04-23 22:28:51 -04:00
|
|
|
defineReadOnly(contract, "deployTransaction", tx);
|
|
|
|
return contract;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
attach(address: string): Contract {
|
|
|
|
return (<any>(this.constructor)).getContract(address, this.interface, this.signer);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(signer: Signer) {
|
|
|
|
return new (<{ new(...args: any[]): ContractFactory }>(this.constructor))(this.interface, this.bytecode, signer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromSolidity(compilerOutput: any, signer?: Signer): ContractFactory {
|
|
|
|
if (compilerOutput == null) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwError("missing compiler output", Logger.errors.MISSING_ARGUMENT, { argument: "compilerOutput" });
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(compilerOutput) === "string") {
|
|
|
|
compilerOutput = JSON.parse(compilerOutput);
|
|
|
|
}
|
|
|
|
|
2020-01-07 19:46:51 -05:00
|
|
|
const abi = compilerOutput.abi;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
let bytecode: any = 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: ContractInterface) {
|
|
|
|
return Contract.getInterface(contractInterface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getContractAddress(tx: { from: string, nonce: BytesLike | BigNumber | number }): string {
|
|
|
|
return getContractAddress(tx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getContract(address: string, contractInterface: ContractInterface, signer?: Signer): Contract {
|
|
|
|
return new Contract(address, contractInterface, signer);
|
|
|
|
}
|
|
|
|
}
|