"use strict"; import { Indexed, Interface } from "@ethersproject/abi"; import { Provider } from "@ethersproject/abstract-provider"; import { Signer, VoidSigner } from "@ethersproject/abstract-signer"; import { getContractAddress } from "@ethersproject/address"; import { BigNumber } from "@ethersproject/bignumber"; import { concat, hexlify, isBytes, isHexString } from "@ethersproject/bytes"; import { Zero } from "@ethersproject/constants"; import { defineReadOnly, deepCopy, getStatic, resolveProperties, shallowCopy } from "@ethersproject/properties"; import { Logger } from "@ethersproject/logger"; import { version } from "./_version"; const logger = new Logger(version); /////////////////////////////// const allowedTransactionKeys = { chainId: true, data: true, from: true, gasLimit: true, gasPrice: true, nonce: true, to: true, value: true }; // Recursively replaces ENS names with promises to resolve the name and resolves all properties function resolveAddresses(signerOrProvider, value, paramType) { if (Array.isArray(paramType)) { return Promise.all(paramType.map((paramType, index) => { return resolveAddresses(signerOrProvider, ((Array.isArray(value)) ? value[index] : value[paramType.name]), paramType); })); } if (paramType.type === "address") { return signerOrProvider.resolveName(value); } if (paramType.type === "tuple") { return resolveAddresses(signerOrProvider, value, paramType.components); } if (paramType.baseType === "array") { if (!Array.isArray(value)) { throw new Error("invalid value for array"); } return Promise.all(value.map((v) => resolveAddresses(signerOrProvider, v, paramType.arrayChildren))); } return Promise.resolve(value); } /* export function _populateTransaction(func: FunctionFragment, args: Array, overrides?: any): Promise { return null; } export function _sendTransaction(func: FunctionFragment, args: Array, overrides?: any): Promise { return null; } */ function runMethod(contract, functionName, options) { const method = contract.interface.functions[functionName]; return function (...params) { let tx = {}; let blockTag = null; // If 1 extra parameter was passed in, it contains overrides if (params.length === method.inputs.length + 1 && typeof (params[params.length - 1]) === "object") { tx = shallowCopy(params.pop()); if (tx.blockTag != null) { blockTag = tx.blockTag; } delete tx.blockTag; // Check for unexpected keys (e.g. using "gas" instead of "gasLimit") for (let key in tx) { if (!allowedTransactionKeys[key]) { logger.throwError(("unknown transaxction override - " + key), "overrides", tx); } } } logger.checkArgumentCount(params.length, method.inputs.length, "passed to contract"); // Check overrides make sense ["data", "to"].forEach(function (key) { if (tx[key] != null) { logger.throwError("cannot override " + key, Logger.errors.UNSUPPORTED_OPERATION, { operation: key }); } }); // If the contract was just deployed, wait until it is minded if (contract.deployTransaction != null) { tx.to = contract._deployed(blockTag).then(() => { return contract.resolvedAddress; }); } else { tx.to = contract.resolvedAddress; } return resolveAddresses(contract.signer || contract.provider, params, method.inputs).then((params) => { tx.data = contract.interface.encodeFunctionData(method, params); if (method.constant || options.callStatic) { // Call (constant functions) always cost 0 ether if (options.estimate) { return Promise.resolve(Zero); } if (!contract.provider && !contract.signer) { logger.throwError("call (constant functions) require a provider or signer", Logger.errors.UNSUPPORTED_OPERATION, { operation: "call" }); } // Check overrides make sense ["gasLimit", "gasPrice", "value"].forEach(function (key) { if (tx[key] != null) { throw new Error("call cannot override " + key); } }); if (options.transaction) { return resolveProperties(tx); } return (contract.signer || contract.provider).call(tx, blockTag).then((value) => { try { let result = contract.interface.decodeFunctionResult(method, value); if (method.outputs.length === 1) { result = result[0]; } return result; } catch (error) { if (error.code === Logger.errors.CALL_EXCEPTION) { error.address = contract.address; error.args = params; error.transaction = tx; } throw error; } }); } // Only computing the transaction estimate if (options.estimate) { if (!contract.provider && !contract.signer) { logger.throwError("estimate require a provider or signer", Logger.errors.UNSUPPORTED_OPERATION, { operation: "estimateGas" }); } return (contract.signer || contract.provider).estimateGas(tx); } if (tx.gasLimit == null && method.gas != null) { tx.gasLimit = BigNumber.from(method.gas).add(21000); } if (tx.value != null && !method.payable) { logger.throwArgumentError("contract method is not payable", "sendTransaction:" + method.format(), tx); } if (options.transaction) { return resolveProperties(tx); } if (!contract.signer) { logger.throwError("sending a transaction requires a signer", Logger.errors.UNSUPPORTED_OPERATION, { operation: "sendTransaction" }); } return contract.signer.sendTransaction(tx).then((tx) => { 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) { } if (parsed) { event.args = parsed.args; event.decode = (data, topics) => { return this.interface.decodeEventLog(parsed.eventFragment, data, topics); }; event.event = parsed.name; event.eventSignature = parsed.signature; } 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; }); }; return tx; }); }); }; } function getEventTag(filter) { if (filter.address && (filter.topics == null || filter.topics.length === 0)) { return "*"; } return (filter.address || "*") + "@" + (filter.topics ? filter.topics.join(":") : ""); } 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) { const listenerCount = this.listenerCount(); this._listeners = this._listeners.filter((item) => { const argsCopy = args.slice(); // 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) { } // Returns the array that will be applied to an emit getEmit(event) { return [event]; } } class ErrorRunningEvent extends RunningEvent { constructor() { super("error", null); } } // @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 class FragmentRunningEvent extends RunningEvent { constructor(address, contractInterface, fragment, topics) { const filter = { 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); }; try { event.args = this.interface.decodeEventLog(this.fragment, event.data, event.topics); } catch (error) { event.args = null; event.decodeError = error; throw error; } } getEmit(event) { const args = (event.args || []).slice(); args.push(event); return args; } } // 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 class WildcardRunningEvent extends RunningEvent { constructor(address, contractInterface) { super("*", { address: address }); defineReadOnly(this, "address", address); defineReadOnly(this, "interface", contractInterface); } prepareEvent(event) { super.prepareEvent(event); try { const parsed = this.interface.parseLog(event); event.event = parsed.name; event.eventSignature = parsed.signature; event.decode = (data, topics) => { return this.interface.decodeEventLog(parsed.eventFragment, data, topics); }; event.args = parsed.args; } catch (error) { // No matching event } } } 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)); if (Signer.isSigner(signerOrProvider)) { 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", {}); defineReadOnly(this, "estimateGas", {}); defineReadOnly(this, "functions", {}); defineReadOnly(this, "populateTransaction", {}); defineReadOnly(this, "filters", {}); { const uniqueFilters = {}; Object.keys(this.interface.events).forEach((eventSignature) => { const event = this.interface.events[eventSignature]; 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); }); 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(", ")})`); } }); } defineReadOnly(this, "_runningEvents", {}); defineReadOnly(this, "_wrappedEmits", {}); defineReadOnly(this, "address", addressOrName); if (this.provider) { defineReadOnly(this, "resolvedAddress", this.provider.resolveName(addressOrName).then((address) => { if (address == null) { throw new Error("name not found"); } return address; }).catch((error) => { console.log("ERROR: Cannot find Contract - " + addressOrName); throw error; })); } else { try { defineReadOnly(this, "resolvedAddress", Promise.resolve((this.interface.constructor).getAddress(addressOrName))); } catch (error) { // Without a provider, we cannot use ENS names logger.throwArgumentError("provider is required to use non-address contract address", "addressOrName", addressOrName); } } const uniqueFunctions = {}; Object.keys(this.interface.functions).forEach((name) => { const fragment = this.interface.functions[name]; // @TODO: This should take in fragment const run = runMethod(this, name, {}); if (this[name] == null) { defineReadOnly(this, name, run); } if (this.functions[name] == null) { defineReadOnly(this.functions, name, run); } if (this.callStatic[name] == null) { defineReadOnly(this.callStatic, name, runMethod(this, name, { callStatic: true })); } if (this.populateTransaction[name] == null) { defineReadOnly(this.populateTransaction, name, runMethod(this, name, { transaction: true })); } if (this.estimateGas[name] == null) { defineReadOnly(this.estimateGas, name, runMethod(this, name, { estimate: true })); } if (!uniqueFunctions[fragment.name]) { uniqueFunctions[fragment.name] = []; } uniqueFunctions[fragment.name].push(name); }); Object.keys(uniqueFunctions).forEach((name) => { const signatures = uniqueFunctions[name]; if (signatures.length > 1) { logger.warn(`Duplicate definition of ${name} (${signatures.join(", ")})`); return; } if (this[name] == null) { defineReadOnly(this, name, this[signatures[0]]); } defineReadOnly(this.functions, name, this.functions[signatures[0]]); defineReadOnly(this.callStatic, name, this.callStatic[signatures[0]]); defineReadOnly(this.populateTransaction, name, this.populateTransaction[signatures[0]]); defineReadOnly(this.estimateGas, name, this.estimateGas[signatures[0]]); }); } 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 // @TODO: // estimateDeploy(bytecode: string, ...args): Promise fallback(overrides) { if (!this.signer) { logger.throwError("sending a transactions require a signer", Logger.errors.UNSUPPORTED_OPERATION, { operation: "sendTransaction(fallback)" }); } const tx = shallowCopy(overrides || {}); ["from", "to"].forEach(function (key) { if (tx[key] == null) { return; } logger.throwError("cannot override " + key, Logger.errors.UNSUPPORTED_OPERATION, { operation: key }); }); tx.to = this.resolvedAddress; 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); } const contract = new (this.constructor)(this.address, this.interface, signerOrProvider); 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()); } // Listen for any event if (eventName === "*") { return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface)); } // Get the event Fragment (throws if ambiguous/unknown event) const fragment = this.interface.getEvent(eventName); return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment)); } // We have topics to filter by... if (eventName.topics && eventName.topics.length > 0) { // Is it a known topichash? (throws if no matching topichash) try { const fragment = this.interface.getEvent(eventName.topics[0]); return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment, eventName.topics)); } catch (error) { } // Filter by the unknown topichash const filter = { address: this.address, topics: eventName.topics }; return this._normalizeRunningEvent(new RunningEvent(getEventTag(filter), filter)); } return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface)); } _checkRunningEvents(runningEvent) { if (runningEvent.listenerCount() === 0) { delete this._runningEvents[runningEvent.tag]; // 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]; } } } // Subclasses can override this to gracefully recover // from parse errors if they wish _wrapEvent(runningEvent, log, listener) { const event = deepCopy(log); 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); }; // This may throw if the topics and data mismatch the signature runningEvent.prepareEvent(event); 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; // If we are not polling the provider, start polling if (!this._wrappedEmits[runningEvent.tag]) { const wrappedEmit = (log) => { let event = null; try { event = this._wrapEvent(runningEvent, log, listener); } catch (error) { // There was an error decoding the data and topics this.emit("error", error, event); return; } const args = runningEvent.getEmit(event); this.emit(runningEvent.filter, ...args); }; 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) { const runningEvent = this._getRunningEvent(event); const filter = shallowCopy(runningEvent.filter); if (typeof (fromBlockOrBlockhash) === "string" && isHexString(fromBlockOrBlockhash, 32)) { if (toBlock != null) { logger.throwArgumentError("cannot specify toBlock with blockhash", "toBlock", toBlock); } filter.blockhash = fromBlockOrBlockhash; } 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; } const runningEvent = this._getRunningEvent(eventName); const result = (runningEvent.run(args) > 0); // 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) { const result = []; 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) { for (const tag in this._runningEvents) { const runningEvent = this._runningEvents[tag]; runningEvent.removeAllListeners(); this._checkRunningEvents(runningEvent); } return this; } // Delete any listeners const runningEvent = this._getRunningEvent(eventName); runningEvent.removeAllListeners(); this._checkRunningEvents(runningEvent); return this; } off(eventName, listener) { if (!this.provider) { return this; } const runningEvent = this._getRunningEvent(eventName); 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); } getDeployTransaction(...args) { let tx = {}; // If we have 1 additional argument, we allow transaction overrides if (args.length === this.interface.deploy.inputs.length + 1) { tx = shallowCopy(args.pop()); for (const key in tx) { 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) { return resolveAddresses(this.signer, args, this.interface.deploy.inputs).then((args) => { // Get the deployment transaction (with optional overrides) const tx = this.getDeployTransaction(...args); // Send the deployment transaction return this.signer.sendTransaction(tx).then((tx) => { const address = (this.constructor).getContractAddress(tx); const contract = (this.constructor).getContract(address, this.interface, this.signer); defineReadOnly(contract, "deployTransaction", tx); return contract; }); }); } 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); } const abi = compilerOutput.abi; 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); } }