2018-06-13 15:39:39 -04:00
|
|
|
'use strict';
|
|
|
|
|
2018-09-24 15:55:17 -04:00
|
|
|
import { Zero } from './constants';
|
2018-06-13 20:02:28 -04:00
|
|
|
|
2018-09-24 15:55:17 -04:00
|
|
|
import * as errors from './errors';
|
|
|
|
|
|
|
|
import { defaultAbiCoder, formatSignature, parseSignature } from './utils/abi-coder';
|
|
|
|
import { getAddress, getContractAddress } from './utils/address';
|
|
|
|
import { BigNumber, bigNumberify } from './utils/bignumber';
|
2018-09-26 15:47:09 -04:00
|
|
|
import { hexDataLength, hexDataSlice, hexlify, isArrayish, isHexString } from './utils/bytes';
|
2018-09-24 15:55:17 -04:00
|
|
|
import { Indexed, Interface } from './utils/interface';
|
|
|
|
import { defineReadOnly, deepCopy, shallowCopy } from './utils/properties';
|
2018-09-26 15:47:09 -04:00
|
|
|
import { UnsignedTransaction } from './utils/transaction';
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-07-18 16:32:16 -04:00
|
|
|
|
2018-07-30 18:59:52 -04:00
|
|
|
///////////////////////////////
|
|
|
|
// Imported Abstracts
|
2018-07-18 16:32:16 -04:00
|
|
|
|
2018-10-11 16:03:18 -04:00
|
|
|
import { BlockTag, Provider } from './providers/abstract-provider';
|
2018-09-24 15:55:17 -04:00
|
|
|
import { Signer } from './abstract-signer';
|
2018-07-18 16:32:16 -04:00
|
|
|
|
2018-07-30 18:59:52 -04:00
|
|
|
///////////////////////////////
|
|
|
|
// Imported Types
|
2018-07-14 17:19:08 -04:00
|
|
|
|
2018-09-24 15:55:17 -04:00
|
|
|
import { Arrayish } from './utils/bytes';
|
|
|
|
import { EventDescription } from './utils/interface';
|
2020-01-10 21:12:58 -05:00
|
|
|
import { EventFragment, FunctionFragment, ParamType } from './utils/abi-coder';
|
2018-09-24 15:55:17 -04:00
|
|
|
import { Block, Listener, Log, TransactionReceipt, TransactionRequest, TransactionResponse } from './providers/abstract-provider';
|
2018-07-30 18:59:52 -04:00
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
// Exported Types
|
|
|
|
|
|
|
|
export type ContractFunction = (...params: Array<any>) => Promise<any>;
|
|
|
|
|
|
|
|
export type EventFilter = {
|
|
|
|
address?: string;
|
|
|
|
topics?: Array<string>;
|
|
|
|
// @TODO: Support OR-style topcis; backwards compatible to make this change
|
|
|
|
//topics?: Array<string | Array<string>>
|
|
|
|
};
|
|
|
|
|
|
|
|
// The (n + 1)th parameter passed to contract event callbacks
|
|
|
|
export interface Event extends Log {
|
2018-10-01 17:30:45 -04:00
|
|
|
args?: Array<any>;
|
|
|
|
decode?: (data: string, topics?: Array<string>) => any;
|
|
|
|
event?: string;
|
|
|
|
eventSignature?: string;
|
2018-07-30 18:59:52 -04:00
|
|
|
|
|
|
|
removeListener: () => void;
|
|
|
|
|
|
|
|
getBlock: () => Promise<Block>;
|
|
|
|
getTransaction: () => Promise<TransactionResponse>;
|
|
|
|
getTransactionReceipt: () => Promise<TransactionReceipt>;
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:54:15 -04:00
|
|
|
export interface ContractReceipt extends TransactionReceipt {
|
|
|
|
events?: Array<Event>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ContractTransaction extends TransactionResponse {
|
|
|
|
wait(confirmations?: number): Promise<ContractReceipt>;
|
|
|
|
}
|
|
|
|
|
2018-07-30 18:59:52 -04:00
|
|
|
///////////////////////////////
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-09-04 10:28:26 -04:00
|
|
|
export class VoidSigner extends Signer {
|
|
|
|
readonly address: string;
|
|
|
|
|
|
|
|
constructor(address: string, provider: Provider) {
|
|
|
|
super();
|
|
|
|
defineReadOnly(this, 'address', address);
|
|
|
|
defineReadOnly(this, 'provider', provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAddress(): Promise<string> {
|
|
|
|
return Promise.resolve(this.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
_fail(message: string, operation: string): Promise<any> {
|
|
|
|
return Promise.resolve().then(() => {
|
|
|
|
errors.throwError(message, errors.UNSUPPORTED_OPERATION, { operation: operation });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
signMessage(message: Arrayish | string): Promise<string> {
|
|
|
|
return this._fail('VoidSigner cannot sign messages', 'signMessage');
|
|
|
|
}
|
|
|
|
|
|
|
|
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
|
|
|
|
return this._fail('VoidSigner cannot sign transactions', 'sendTransaction');
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(provider: Provider): VoidSigner {
|
|
|
|
return new VoidSigner(this.address, provider);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 15:55:17 -04:00
|
|
|
const allowedTransactionKeys: { [ key: string ]: boolean } = {
|
2018-10-01 17:33:09 -04:00
|
|
|
chainId: true, data: true, from: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
// Recursively replaces ENS names with promises to resolve the name and
|
|
|
|
// stalls until all promises have returned
|
2018-06-13 20:02:28 -04:00
|
|
|
// @TODO: Expand this to resolve any promises too
|
2018-07-30 18:59:52 -04:00
|
|
|
function resolveAddresses(provider: Provider, value: any, paramType: ParamType | Array<ParamType>): Promise<any> {
|
2018-06-13 20:02:28 -04:00
|
|
|
if (Array.isArray(paramType)) {
|
2018-09-24 15:55:17 -04:00
|
|
|
let promises: Array<Promise<any>> = [];
|
2018-06-13 20:02:28 -04:00
|
|
|
paramType.forEach((paramType, index) => {
|
2018-09-24 15:55:17 -04:00
|
|
|
let v = null;
|
2018-06-13 20:02:28 -04:00
|
|
|
if (Array.isArray(value)) {
|
|
|
|
v = value[index];
|
|
|
|
} else {
|
|
|
|
v = value[paramType.name];
|
|
|
|
}
|
|
|
|
promises.push(resolveAddresses(provider, v, paramType));
|
|
|
|
});
|
|
|
|
return Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paramType.type === 'address') {
|
|
|
|
return provider.resolveName(value);
|
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-07-26 16:57:47 -04:00
|
|
|
if (paramType.type === 'tuple') {
|
2018-06-13 20:02:28 -04:00
|
|
|
return resolveAddresses(provider, value, paramType.components);
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:57:47 -04:00
|
|
|
// Strips one level of array indexing off the end to recuse into
|
|
|
|
let isArrayMatch = paramType.type.match(/(.*)(\[[0-9]*\]$)/);
|
|
|
|
if (isArrayMatch) {
|
|
|
|
if (!Array.isArray(value)) { throw new Error('invalid value for array'); }
|
|
|
|
var promises: Array<Promise<any>> = [];
|
|
|
|
var subParamType = {
|
|
|
|
components: paramType.components,
|
|
|
|
type: isArrayMatch[1],
|
|
|
|
};
|
|
|
|
value.forEach((v) => {
|
|
|
|
promises.push(resolveAddresses(provider, v, subParamType));
|
|
|
|
});
|
|
|
|
return Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
2018-06-13 20:02:28 -04:00
|
|
|
return Promise.resolve(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunFunction = (...params: Array<any>) => Promise<any>;
|
|
|
|
|
|
|
|
function runMethod(contract: Contract, functionName: string, estimateOnly: boolean): RunFunction {
|
2018-06-13 15:39:39 -04:00
|
|
|
let method = contract.interface.functions[functionName];
|
|
|
|
return function(...params): Promise<any> {
|
2018-10-11 16:03:18 -04:00
|
|
|
let tx: any = {}
|
|
|
|
|
|
|
|
let blockTag: BlockTag = null;
|
2018-06-13 15:39:39 -04:00
|
|
|
|
|
|
|
// If 1 extra parameter was passed in, it contains overrides
|
|
|
|
if (params.length === method.inputs.length + 1 && typeof(params[params.length - 1]) === 'object') {
|
2018-06-18 05:42:41 -04:00
|
|
|
tx = shallowCopy(params.pop());
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-10-11 16:03:18 -04:00
|
|
|
if (tx.blockTag != null) {
|
|
|
|
blockTag = tx.blockTag;
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:01:48 -05:00
|
|
|
delete tx.blockTag;
|
|
|
|
|
2018-06-13 15:39:39 -04:00
|
|
|
// Check for unexpected keys (e.g. using "gas" instead of "gasLimit")
|
2018-10-11 16:03:18 -04:00
|
|
|
for (let key in tx) {
|
2018-06-13 15:39:39 -04:00
|
|
|
if (!allowedTransactionKeys[key]) {
|
|
|
|
throw new Error('unknown transaction override ' + key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.length != method.inputs.length) {
|
|
|
|
throw new Error('incorrect number of arguments');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check overrides make sense
|
|
|
|
['data', 'to'].forEach(function(key) {
|
2018-06-18 05:42:41 -04:00
|
|
|
if (tx[key] != null) {
|
|
|
|
errors.throwError('cannot override ' + key, errors.UNSUPPORTED_OPERATION, { operation: key })
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-11 19:21:53 -05:00
|
|
|
tx.to = contract._deployed(blockTag).then(() => {
|
2018-08-27 13:38:48 +02:00
|
|
|
return contract.addressPromise;
|
|
|
|
});
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-06-13 20:02:28 -04:00
|
|
|
return resolveAddresses(contract.provider, params, method.inputs).then((params) => {
|
2018-06-18 05:42:41 -04:00
|
|
|
tx.data = method.encode(params);
|
2018-06-13 20:02:28 -04:00
|
|
|
if (method.type === 'call') {
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-06-13 20:02:28 -04:00
|
|
|
// Call (constant functions) always cost 0 ether
|
|
|
|
if (estimateOnly) {
|
2018-08-02 21:34:10 -04:00
|
|
|
return Promise.resolve(Zero);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
if (!contract.provider) {
|
|
|
|
errors.throwError('call (constant functions) require a provider or a signer with a provider', errors.UNSUPPORTED_OPERATION, { operation: 'call' })
|
|
|
|
}
|
|
|
|
|
2018-06-13 20:02:28 -04:00
|
|
|
// Check overrides make sense
|
|
|
|
['gasLimit', 'gasPrice', 'value'].forEach(function(key) {
|
2018-06-18 05:42:41 -04:00
|
|
|
if (tx[key] != null) {
|
2018-06-13 20:02:28 -04:00
|
|
|
throw new Error('call cannot override ' + key) ;
|
|
|
|
}
|
|
|
|
});
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
if (tx.from == null && contract.signer) {
|
|
|
|
tx.from = contract.signer.getAddress()
|
2018-06-13 20:02:28 -04:00
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-10-11 16:03:18 -04:00
|
|
|
return contract.provider.call(tx, blockTag).then((value) => {
|
2018-06-23 01:33:51 -04:00
|
|
|
if ((hexDataLength(value) % 32) === 4 && hexDataSlice(value, 0, 4) === '0x08c379a0') {
|
|
|
|
let reason = defaultAbiCoder.decode([ 'string' ], hexDataSlice(value, 4));
|
|
|
|
errors.throwError('call revert exception', errors.CALL_EXCEPTION, {
|
|
|
|
address: contract.address,
|
|
|
|
args: params,
|
2018-06-24 18:41:28 -04:00
|
|
|
method: method.signature,
|
2018-06-23 01:33:51 -04:00
|
|
|
errorSignature: 'Error(string)',
|
|
|
|
errorArgs: [ reason ],
|
2018-06-24 18:41:28 -04:00
|
|
|
reason: reason,
|
|
|
|
transaction: tx
|
2018-06-23 01:33:51 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
try {
|
|
|
|
let result = method.decode(value);
|
|
|
|
if (method.outputs.length === 1) {
|
|
|
|
result = result[0];
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
2018-06-18 05:42:41 -04:00
|
|
|
return result;
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
if (value === '0x' && method.outputs.length > 0) {
|
|
|
|
errors.throwError('call exception', errors.CALL_EXCEPTION, {
|
|
|
|
address: contract.address,
|
|
|
|
method: method.signature,
|
2018-06-23 01:33:51 -04:00
|
|
|
args: params
|
2018-06-18 05:42:41 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
});
|
|
|
|
|
2018-06-13 20:02:28 -04:00
|
|
|
} else if (method.type === 'transaction') {
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-06-13 20:02:28 -04:00
|
|
|
// Only computing the transaction estimate
|
|
|
|
if (estimateOnly) {
|
2018-06-18 05:42:41 -04:00
|
|
|
if (!contract.provider) {
|
|
|
|
errors.throwError('estimate gas require a provider or a signer with a provider', errors.UNSUPPORTED_OPERATION, { operation: 'estimateGas' })
|
2018-06-13 20:02:28 -04:00
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
if (tx.from == null && contract.signer) {
|
|
|
|
tx.from = contract.signer.getAddress()
|
2018-06-13 20:02:28 -04:00
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
return contract.provider.estimateGas(tx);
|
2018-06-13 20:02:28 -04:00
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-08-01 17:24:11 -04:00
|
|
|
if (tx.gasLimit == null && method.gas != null) {
|
2018-08-02 17:14:06 -04:00
|
|
|
tx.gasLimit = bigNumberify(method.gas).add(21000);
|
2018-08-01 17:24:11 -04:00
|
|
|
}
|
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
if (!contract.signer) {
|
2019-09-06 18:45:56 -04:00
|
|
|
errors.throwError('sending a transaction requires a signer', errors.UNSUPPORTED_OPERATION, { operation: 'sendTransaction' })
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
// Make sure they aren't overriding something they shouldn't
|
|
|
|
if (tx.from != null) {
|
|
|
|
errors.throwError('cannot override from in a transaction', errors.UNSUPPORTED_OPERATION, { operation: 'sendTransaction' })
|
2018-06-13 20:02:28 -04:00
|
|
|
}
|
|
|
|
|
2018-10-04 19:54:15 -04:00
|
|
|
return contract.signer.sendTransaction(tx).then((tx) => {
|
|
|
|
let 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));
|
|
|
|
|
2018-11-27 17:32:05 -05:00
|
|
|
let parsed = contract.interface.parseLog(log);
|
2018-10-04 19:54:15 -04:00
|
|
|
if (parsed) {
|
|
|
|
event.args = parsed.values;
|
|
|
|
event.decode = parsed.decode;
|
|
|
|
event.event = parsed.name;
|
|
|
|
event.eventSignature = parsed.signature;
|
|
|
|
}
|
|
|
|
|
2018-11-27 17:32:05 -05:00
|
|
|
event.removeListener = () => { return contract.provider; }
|
2018-10-04 19:54:15 -04:00
|
|
|
event.getBlock = () => {
|
2018-11-27 17:32:05 -05:00
|
|
|
return contract.provider.getBlock(receipt.blockHash);
|
2018-10-04 19:54:15 -04:00
|
|
|
}
|
|
|
|
event.getTransaction = () => {
|
2018-11-27 17:32:05 -05:00
|
|
|
return contract.provider.getTransaction(receipt.transactionHash);
|
2018-10-04 19:54:15 -04:00
|
|
|
}
|
|
|
|
event.getTransactionReceipt = () => {
|
|
|
|
return Promise.resolve(receipt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return event;
|
|
|
|
});
|
|
|
|
|
|
|
|
return receipt;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
});
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-13 20:02:28 -04:00
|
|
|
throw new Error('invalid type - ' + method.type);
|
|
|
|
return null;
|
|
|
|
});
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 02:52:43 -04:00
|
|
|
function getEventTag(filter: EventFilter): string {
|
2018-10-01 17:30:45 -04:00
|
|
|
if (filter.address && (filter.topics == null || filter.topics.length === 0)) {
|
|
|
|
return '*';
|
|
|
|
}
|
|
|
|
return (filter.address || '*') + '@' + (filter.topics ? filter.topics.join(':'): '');
|
2018-07-12 02:52:43 -04:00
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
|
|
|
|
interface Bucket<T> {
|
|
|
|
[name: string]: T;
|
|
|
|
}
|
|
|
|
|
2018-07-12 02:52:43 -04:00
|
|
|
type _EventFilter = {
|
2018-10-01 17:30:45 -04:00
|
|
|
prepareEvent: (event: Event) => Array<any>;
|
2018-07-12 02:52:43 -04:00
|
|
|
event?: EventDescription;
|
|
|
|
eventTag: string;
|
|
|
|
filter: EventFilter;
|
|
|
|
};
|
|
|
|
|
|
|
|
type _Event = {
|
|
|
|
eventFilter: _EventFilter;
|
|
|
|
listener: Listener;
|
|
|
|
once: boolean;
|
|
|
|
wrappedListener: Listener;
|
|
|
|
};
|
|
|
|
|
2018-06-13 15:39:39 -04:00
|
|
|
export class Contract {
|
|
|
|
readonly address: string;
|
|
|
|
readonly interface: Interface;
|
2018-06-18 05:42:41 -04:00
|
|
|
|
2018-06-13 15:39:39 -04:00
|
|
|
readonly signer: Signer;
|
2018-07-30 18:59:52 -04:00
|
|
|
readonly provider: Provider;
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-07-14 17:19:08 -04:00
|
|
|
readonly estimate: Bucket<(...params: Array<any>) => Promise<BigNumber>>;
|
2018-07-18 16:32:16 -04:00
|
|
|
readonly functions: Bucket<ContractFunction>;
|
2018-07-12 02:52:43 -04:00
|
|
|
|
2018-07-14 17:19:08 -04:00
|
|
|
readonly filters: Bucket<(...params: Array<any>) => EventFilter>;
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-07-18 16:32:16 -04:00
|
|
|
readonly [name: string]: ContractFunction | any;
|
|
|
|
|
2018-06-13 15:39:39 -04:00
|
|
|
readonly addressPromise: Promise<string>;
|
|
|
|
|
2018-06-13 21:10:41 -04:00
|
|
|
// This is only set if the contract was created with a call to deploy
|
|
|
|
readonly deployTransaction: TransactionResponse;
|
|
|
|
|
2019-02-11 19:21:53 -05:00
|
|
|
private _deployedPromise: Promise<Contract>;
|
2018-08-27 13:38:48 +02:00
|
|
|
|
2018-06-13 20:02:28 -04:00
|
|
|
// https://github.com/Microsoft/TypeScript/issues/5453
|
2018-06-18 05:42:41 -04:00
|
|
|
// Once this issue is resolved (there are open PR) we can do this nicer
|
|
|
|
// by making addressOrName default to null for 2 operand calls. :)
|
2018-06-13 20:02:28 -04:00
|
|
|
|
2020-01-29 22:01:42 -05:00
|
|
|
// ParamType is not actually correct here, but for legacy reasons,
|
|
|
|
// we need it. See #721.
|
|
|
|
constructor(addressOrName: string, contractInterface: Array<string | FunctionFragment | EventFragment | ParamType> | string | Interface, signerOrProvider: Signer | Provider) {
|
2018-06-13 21:10:41 -04:00
|
|
|
errors.checkNew(this, Contract);
|
2018-06-13 15:39:39 -04:00
|
|
|
|
|
|
|
// @TODO: Maybe still check the addressOrName looks like a valid address or name?
|
|
|
|
//address = getAddress(address);
|
2018-07-25 21:15:43 -04:00
|
|
|
|
|
|
|
if (Interface.isInterface(contractInterface)) {
|
2018-06-13 15:39:39 -04:00
|
|
|
defineReadOnly(this, 'interface', contractInterface);
|
|
|
|
} else {
|
|
|
|
defineReadOnly(this, 'interface', new Interface(contractInterface));
|
|
|
|
}
|
|
|
|
|
2018-07-25 21:15:43 -04:00
|
|
|
if (Signer.isSigner(signerOrProvider)) {
|
2018-06-13 21:10:41 -04:00
|
|
|
defineReadOnly(this, 'provider', signerOrProvider.provider);
|
|
|
|
defineReadOnly(this, 'signer', signerOrProvider);
|
2018-07-30 18:59:52 -04:00
|
|
|
} else if (Provider.isProvider(signerOrProvider)) {
|
2018-06-13 21:10:41 -04:00
|
|
|
defineReadOnly(this, 'provider', signerOrProvider);
|
|
|
|
defineReadOnly(this, 'signer', null);
|
2018-06-18 05:42:41 -04:00
|
|
|
} else {
|
|
|
|
errors.throwError('invalid signer or provider', errors.INVALID_ARGUMENT, { arg: 'signerOrProvider', value: signerOrProvider });
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
defineReadOnly(this, 'estimate', { });
|
|
|
|
defineReadOnly(this, 'functions', { });
|
|
|
|
|
2018-07-12 02:52:43 -04:00
|
|
|
defineReadOnly(this, 'filters', { });
|
|
|
|
|
|
|
|
Object.keys(this.interface.events).forEach((eventName) => {
|
|
|
|
let event = this.interface.events[eventName];
|
|
|
|
defineReadOnly(this.filters, eventName, (...args: Array<any>) => {
|
|
|
|
return {
|
|
|
|
address: this.address,
|
|
|
|
topics: event.encodeTopics(args)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this._events = [];
|
|
|
|
|
2018-06-18 05:42:41 -04:00
|
|
|
defineReadOnly(this, 'address', addressOrName);
|
2018-07-14 17:19:08 -04:00
|
|
|
if (this.provider) {
|
|
|
|
defineReadOnly(this, 'addressPromise', this.provider.resolveName(addressOrName).then((address) => {
|
|
|
|
if (address == null) { throw new Error('name not found'); }
|
|
|
|
return address;
|
|
|
|
}).catch((error: Error) => {
|
|
|
|
throw error;
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
defineReadOnly(this, 'addressPromise', Promise.resolve(getAddress(addressOrName)));
|
|
|
|
} catch (error) {
|
2018-09-26 15:47:09 -04:00
|
|
|
// Without a provider, we cannot use ENS names
|
2018-07-14 17:19:08 -04:00
|
|
|
errors.throwError('provider is required to use non-address contract address', errors.INVALID_ARGUMENT, { argument: 'addressOrName', value: addressOrName });
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 21:10:41 -04:00
|
|
|
|
2018-06-13 15:39:39 -04:00
|
|
|
Object.keys(this.interface.functions).forEach((name) => {
|
2018-08-27 13:38:48 +02:00
|
|
|
let run = runMethod(this, name, false);
|
2018-06-13 15:39:39 -04:00
|
|
|
|
2018-06-22 20:30:50 -04:00
|
|
|
if ((<any>this)[name] == null) {
|
2018-06-13 15:39:39 -04:00
|
|
|
defineReadOnly(this, name, run);
|
|
|
|
} else {
|
2018-12-27 15:48:38 -05:00
|
|
|
errors.warn('WARNING: Multiple definitions for ' + name);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.functions[name] == null) {
|
|
|
|
defineReadOnly(this.functions, name, run);
|
|
|
|
defineReadOnly(this.estimate, name, runMethod(this, name, true));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-30 23:05:22 -04:00
|
|
|
// @TODO: Allow timeout?
|
2018-07-03 15:48:37 -04:00
|
|
|
deployed(): Promise<Contract> {
|
2019-02-11 19:21:53 -05:00
|
|
|
return this._deployed();
|
|
|
|
}
|
|
|
|
|
|
|
|
_deployed(blockTag?: BlockTag): Promise<Contract> {
|
|
|
|
if (!this._deployedPromise) {
|
2018-07-14 17:19:08 -04:00
|
|
|
|
2018-08-27 13:38:48 +02:00
|
|
|
// If we were just deployed, we know the transaction we should occur in
|
|
|
|
if (this.deployTransaction) {
|
2019-02-11 19:21:53 -05:00
|
|
|
this._deployedPromise = this.deployTransaction.wait().then(() => {
|
2018-08-27 13:38:48 +02:00
|
|
|
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
|
2019-02-11 19:21:53 -05:00
|
|
|
this._deployedPromise = this.provider.getCode(this.address, blockTag).then((code) => {
|
2018-08-27 13:38:48 +02:00
|
|
|
if (code === '0x') {
|
|
|
|
errors.throwError('contract not deployed', errors.UNSUPPORTED_OPERATION, {
|
|
|
|
contractAddress: this.address,
|
|
|
|
operation: 'getDeployed'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
});
|
|
|
|
}
|
2018-06-30 23:05:22 -04:00
|
|
|
}
|
|
|
|
|
2019-02-11 19:21:53 -05:00
|
|
|
return this._deployedPromise;
|
2018-06-30 23:05:22 -04:00
|
|
|
}
|
|
|
|
|
2018-06-24 18:41:28 -04:00
|
|
|
// @TODO:
|
|
|
|
// estimateFallback(overrides?: TransactionRequest): Promise<BigNumber>
|
|
|
|
|
|
|
|
// @TODO:
|
|
|
|
// estimateDeploy(bytecode: string, ...args): Promise<BigNumber>
|
|
|
|
|
2018-06-22 02:10:46 -04:00
|
|
|
fallback(overrides?: TransactionRequest): Promise<TransactionResponse> {
|
|
|
|
if (!this.signer) {
|
2019-09-06 18:45:56 -04:00
|
|
|
errors.throwError('sending a transaction requires a signer', errors.UNSUPPORTED_OPERATION, { operation: 'sendTransaction(fallback)' })
|
2018-06-22 02:10:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var tx: TransactionRequest = shallowCopy(overrides || {});
|
|
|
|
|
|
|
|
['from', 'to'].forEach(function(key) {
|
2018-06-24 18:41:28 -04:00
|
|
|
if ((<any>tx)[key] == null) { return; }
|
2018-06-22 02:10:46 -04:00
|
|
|
errors.throwError('cannot override ' + key, errors.UNSUPPORTED_OPERATION, { operation: key })
|
|
|
|
});
|
|
|
|
|
|
|
|
tx.to = this.addressPromise;
|
2018-08-27 13:38:48 +02:00
|
|
|
return this.deployed().then(() => {
|
|
|
|
return this.signer.sendTransaction(tx);
|
|
|
|
});
|
2018-06-22 02:10:46 -04:00
|
|
|
}
|
|
|
|
|
2018-06-13 21:10:41 -04:00
|
|
|
// Reconnect to a different signer or provider
|
2018-09-04 10:28:26 -04:00
|
|
|
connect(signerOrProvider: Signer | Provider | string): Contract {
|
|
|
|
if (typeof(signerOrProvider) === 'string') {
|
|
|
|
signerOrProvider = new VoidSigner(signerOrProvider, this.provider);
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:38:48 +02:00
|
|
|
let contract = new Contract(this.address, this.interface, signerOrProvider);
|
|
|
|
if (this.deployTransaction) {
|
|
|
|
defineReadOnly(contract, 'deployTransaction', this.deployTransaction);
|
|
|
|
}
|
|
|
|
return contract;
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
|
2018-06-24 18:41:28 -04:00
|
|
|
// Re-attach to a different on=chain instance of this contract
|
|
|
|
attach(addressOrName: string): Contract {
|
|
|
|
return new Contract(addressOrName, this.interface, this.signer || this.provider);
|
|
|
|
}
|
|
|
|
|
2018-07-30 18:59:52 -04:00
|
|
|
static isIndexed(value: any): value is Indexed {
|
|
|
|
return Interface.isIndexed(value);
|
|
|
|
}
|
|
|
|
|
2018-07-12 02:52:43 -04:00
|
|
|
private _events: Array<_Event>;
|
|
|
|
|
2018-07-16 03:27:49 -04:00
|
|
|
private _getEventFilter(eventName: EventFilter | string): _EventFilter {
|
2018-07-12 02:52:43 -04:00
|
|
|
if (typeof(eventName) === 'string') {
|
|
|
|
|
|
|
|
// Listen for any event
|
|
|
|
if (eventName === '*') {
|
|
|
|
return {
|
2018-10-01 17:30:45 -04:00
|
|
|
prepareEvent: (e: Event) => {
|
|
|
|
let parsed = this.interface.parseLog(e);
|
|
|
|
if (parsed) {
|
|
|
|
e.args = parsed.values;
|
|
|
|
e.decode = parsed.decode;
|
|
|
|
e.event = parsed.name;
|
|
|
|
e.eventSignature = parsed.signature;
|
|
|
|
}
|
|
|
|
return [ e ];
|
2018-07-12 02:52:43 -04:00
|
|
|
},
|
|
|
|
eventTag: '*',
|
|
|
|
filter: { address: this.address },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize the eventName
|
|
|
|
if (eventName.indexOf('(') !== -1) {
|
|
|
|
eventName = formatSignature(parseSignature('event ' + eventName));
|
|
|
|
}
|
|
|
|
|
|
|
|
let event = this.interface.events[eventName];
|
|
|
|
if (!event) {
|
|
|
|
errors.throwError('unknown event - ' + eventName, errors.INVALID_ARGUMENT, { argumnet: 'eventName', value: eventName });
|
|
|
|
}
|
|
|
|
|
|
|
|
let filter = {
|
|
|
|
address: this.address,
|
|
|
|
topics: [ event.topic ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2018-10-01 17:30:45 -04:00
|
|
|
prepareEvent: (e: Event) => {
|
|
|
|
let args = event.decode(e.data, e.topics);
|
|
|
|
e.args = args;
|
|
|
|
|
|
|
|
let result = Array.prototype.slice.call(args);
|
|
|
|
result.push(e);
|
|
|
|
|
|
|
|
return result;
|
2018-07-12 02:52:43 -04:00
|
|
|
},
|
|
|
|
event: event,
|
|
|
|
eventTag: getEventTag(filter),
|
|
|
|
filter: filter
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let filter: EventFilter = {
|
|
|
|
address: this.address
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the matching event in the ABI; if none, we still allow filtering
|
|
|
|
// since it may be a filter for an otherwise unknown event
|
|
|
|
let event: EventDescription = null;
|
|
|
|
if (eventName.topics && eventName.topics[0]) {
|
|
|
|
filter.topics = eventName.topics;
|
2018-10-01 17:30:45 -04:00
|
|
|
for (let name in this.interface.events) {
|
2018-07-12 02:52:43 -04:00
|
|
|
if (name.indexOf('(') === -1) { continue; }
|
|
|
|
let e = this.interface.events[name];
|
|
|
|
if (e.topic === eventName.topics[0].toLowerCase()) {
|
|
|
|
event = e;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2018-10-01 17:30:45 -04:00
|
|
|
prepareEvent: (e: Event) => {
|
|
|
|
if (!event) { return [ e ]; }
|
|
|
|
|
|
|
|
let args = event.decode(e.data, e.topics);
|
|
|
|
e.args = args;
|
|
|
|
|
|
|
|
let result = Array.prototype.slice.call(args);
|
|
|
|
result.push(e);
|
|
|
|
|
|
|
|
return result;
|
2018-07-12 02:52:43 -04:00
|
|
|
},
|
|
|
|
event: event,
|
|
|
|
eventTag: getEventTag(filter),
|
|
|
|
filter: filter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 03:27:49 -04:00
|
|
|
private _addEventListener(eventFilter: _EventFilter, listener: Listener, once: boolean): void {
|
2018-07-12 02:52:43 -04:00
|
|
|
if (!this.provider) {
|
|
|
|
errors.throwError('events require a provider or a signer with a provider', errors.UNSUPPORTED_OPERATION, { operation: 'once' })
|
|
|
|
}
|
|
|
|
|
|
|
|
let wrappedListener = (log: Log) => {
|
|
|
|
|
2018-10-01 17:30:45 -04:00
|
|
|
let event: Event = (<Event>deepCopy(log));
|
|
|
|
|
|
|
|
let args = eventFilter.prepareEvent(event);
|
|
|
|
|
|
|
|
if (eventFilter.event) {
|
|
|
|
event.decode = eventFilter.event.decode;
|
|
|
|
event.event = eventFilter.event.name;
|
|
|
|
event.eventSignature = eventFilter.event.signature;
|
|
|
|
}
|
2018-07-12 02:52:43 -04:00
|
|
|
|
|
|
|
event.removeListener = () => { this.removeListener(eventFilter.filter, listener); };
|
|
|
|
|
|
|
|
event.getBlock = () => { return this.provider.getBlock(log.blockHash); }
|
2018-10-01 17:30:45 -04:00
|
|
|
event.getTransaction = () => { return this.provider.getTransaction(log.transactionHash); }
|
2018-07-12 02:52:43 -04:00
|
|
|
event.getTransactionReceipt = () => { return this.provider.getTransactionReceipt(log.transactionHash); }
|
|
|
|
|
2018-10-01 17:30:45 -04:00
|
|
|
this.emit(eventFilter.filter, ...args);
|
2018-07-12 02:52:43 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
this.provider.on(eventFilter.filter, wrappedListener);
|
|
|
|
this._events.push({ eventFilter: eventFilter, listener: listener, wrappedListener: wrappedListener, once: once });
|
|
|
|
}
|
|
|
|
|
|
|
|
on(event: EventFilter | string, listener: Listener): Contract {
|
|
|
|
this._addEventListener(this._getEventFilter(event), listener, false);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
once(event: EventFilter | string, listener: Listener): Contract {
|
|
|
|
this._addEventListener(this._getEventFilter(event), listener, true);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-09-04 10:28:26 -04:00
|
|
|
addListener(eventName: EventFilter | string, listener: Listener): Contract {
|
2018-07-12 02:52:43 -04:00
|
|
|
return this.on(eventName, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(eventName: EventFilter | string, ...args: Array<any>): boolean {
|
|
|
|
if (!this.provider) { return false; }
|
|
|
|
|
|
|
|
let result = false;
|
|
|
|
|
|
|
|
let eventFilter = this._getEventFilter(eventName);
|
|
|
|
this._events = this._events.filter((event) => {
|
2018-10-01 17:30:45 -04:00
|
|
|
|
|
|
|
// Not this event (keep it for later)
|
2018-07-12 02:52:43 -04:00
|
|
|
if (event.eventFilter.eventTag !== eventFilter.eventTag) { return true; }
|
2018-10-01 17:30:45 -04:00
|
|
|
|
|
|
|
// Call the callback in the next event loop
|
2018-07-12 02:52:43 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
event.listener.apply(this, args);
|
|
|
|
}, 0);
|
|
|
|
result = true;
|
2018-10-01 17:30:45 -04:00
|
|
|
|
|
|
|
// Reschedule it if it not "once"
|
2018-07-12 02:52:43 -04:00
|
|
|
return !(event.once);
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
listenerCount(eventName?: EventFilter | string): number {
|
|
|
|
if (!this.provider) { return 0; }
|
|
|
|
|
|
|
|
let eventFilter = this._getEventFilter(eventName);
|
|
|
|
return this._events.filter((event) => {
|
|
|
|
return event.eventFilter.eventTag === eventFilter.eventTag
|
|
|
|
}).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners(eventName: EventFilter | string): Array<Listener> {
|
|
|
|
if (!this.provider) { return []; }
|
|
|
|
|
|
|
|
let eventFilter = this._getEventFilter(eventName);
|
|
|
|
return this._events.filter((event) => {
|
|
|
|
return event.eventFilter.eventTag === eventFilter.eventTag
|
|
|
|
}).map((event) => { return event.listener; });
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAllListeners(eventName: EventFilter | string): Contract {
|
|
|
|
if (!this.provider) { return this; }
|
|
|
|
|
|
|
|
let eventFilter = this._getEventFilter(eventName);
|
|
|
|
this._events = this._events.filter((event) => {
|
2019-01-15 15:58:42 -05:00
|
|
|
|
|
|
|
// Keep all other events
|
|
|
|
if (event.eventFilter.eventTag !== eventFilter.eventTag) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deregister this event from the provider and filter it out
|
|
|
|
this.provider.removeListener(event.eventFilter.filter, event.wrappedListener);
|
|
|
|
return false;
|
2018-07-12 02:52:43 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeListener(eventName: any, listener: Listener): Contract {
|
|
|
|
if (!this.provider) { return this; }
|
|
|
|
|
|
|
|
let found = false;
|
|
|
|
|
|
|
|
let eventFilter = this._getEventFilter(eventName);
|
|
|
|
this._events = this._events.filter((event) => {
|
|
|
|
|
|
|
|
// Make sure this event and listener match
|
|
|
|
if (event.eventFilter.eventTag !== eventFilter.eventTag) { return true; }
|
|
|
|
if (event.listener !== listener) { return true; }
|
|
|
|
this.provider.removeListener(event.eventFilter.filter, event.wrappedListener);
|
|
|
|
|
|
|
|
// Already found a matching event in a previous loop
|
|
|
|
if (found) { return true; }
|
|
|
|
|
|
|
|
// REmove this event (returning false filters us out)
|
|
|
|
found = true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
2018-09-26 15:47:09 -04:00
|
|
|
|
|
|
|
export class ContractFactory {
|
|
|
|
|
|
|
|
readonly interface: Interface;
|
|
|
|
readonly bytecode: string;
|
|
|
|
readonly signer: Signer;
|
|
|
|
|
2020-01-29 22:01:42 -05:00
|
|
|
// ParamType is not actually correct here, but for legacy reasons,
|
|
|
|
// we need it. See #721.
|
|
|
|
constructor(contractInterface: Array<string | FunctionFragment | EventFragment | ParamType> | string | Interface, bytecode: Arrayish | string | { object: string }, signer?: Signer) {
|
2018-09-26 15:47:09 -04:00
|
|
|
|
|
|
|
let bytecodeHex: string = null;
|
|
|
|
|
|
|
|
// Allow the bytecode object from the Solidity compiler
|
|
|
|
if (typeof(bytecode) === 'string') {
|
|
|
|
bytecodeHex = bytecode;
|
|
|
|
} else if (isArrayish(bytecode)) {
|
|
|
|
bytecodeHex = hexlify(bytecode);
|
|
|
|
} else if (typeof(bytecode.object) === 'string') {
|
|
|
|
bytecodeHex = (<any>bytecode).object;
|
|
|
|
} else {
|
|
|
|
errors.throwError('bytecode must be a valid hex string', errors.INVALID_ARGUMENT, { arg: 'bytecode', value: bytecode });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it is 0x prefixed
|
|
|
|
if (bytecodeHex.substring(0, 2) !== '0x') {
|
|
|
|
bytecodeHex = '0x' + bytecodeHex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isHexString(bytecodeHex)) {
|
|
|
|
errors.throwError('bytecode must be a valid hex string', errors.INVALID_ARGUMENT, { arg: 'bytecode', value: bytecode });
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((bytecodeHex.length % 2) !== 0) {
|
|
|
|
errors.throwError('bytecode must be valid data (even length)', errors.INVALID_ARGUMENT, { arg: 'bytecode', value: bytecode });
|
|
|
|
}
|
|
|
|
|
|
|
|
defineReadOnly(this, 'bytecode', bytecodeHex);
|
|
|
|
|
|
|
|
if (Interface.isInterface(contractInterface)) {
|
|
|
|
defineReadOnly(this, 'interface', contractInterface);
|
|
|
|
} else {
|
|
|
|
defineReadOnly(this, 'interface', new Interface(contractInterface));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signer && !Signer.isSigner(signer)) {
|
|
|
|
errors.throwError('invalid signer', errors.INVALID_ARGUMENT, { arg: 'signer', value: null });
|
|
|
|
}
|
|
|
|
|
|
|
|
defineReadOnly(this, 'signer', signer || null);
|
|
|
|
}
|
|
|
|
|
|
|
|
getDeployTransaction(...args: Array<any>): UnsignedTransaction {
|
|
|
|
|
|
|
|
let tx: UnsignedTransaction = { };
|
|
|
|
|
|
|
|
// If we have 1 additional argument, we allow transaction overrides
|
|
|
|
if (args.length === this.interface.deployFunction.inputs.length + 1) {
|
|
|
|
tx = shallowCopy(args.pop());
|
|
|
|
for (let 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 ((<any>tx)[key] == null) { return; }
|
|
|
|
errors.throwError('cannot override ' + key, errors.UNSUPPORTED_OPERATION, { operation: key })
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure the call matches the constructor signature
|
2018-11-12 17:22:18 -05:00
|
|
|
errors.checkArgumentCount(args.length, this.interface.deployFunction.inputs.length, ' in Contract constructor');
|
2018-09-26 15:47:09 -04:00
|
|
|
|
|
|
|
// Set the data to the bytecode + the encoded constructor arguments
|
|
|
|
tx.data = this.interface.deployFunction.encode(this.bytecode, args);
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
deploy(...args: Array<any>): Promise<Contract> {
|
|
|
|
|
|
|
|
// Get the deployment transaction (with optional overrides)
|
|
|
|
let tx = this.getDeployTransaction(...args);
|
|
|
|
|
|
|
|
// Send the deployment transaction
|
|
|
|
return this.signer.sendTransaction(tx).then((tx) => {
|
|
|
|
let contract = new Contract(getContractAddress(tx), this.interface, this.signer);
|
|
|
|
defineReadOnly(contract, 'deployTransaction', tx);
|
|
|
|
return contract;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
attach(address: string): Contract {
|
|
|
|
return new Contract(address, this.interface, this.signer);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(signer: Signer) {
|
|
|
|
return new ContractFactory(this.interface, this.bytecode, signer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromSolidity(compilerOutput: any, signer?: Signer): ContractFactory {
|
|
|
|
if (compilerOutput == null) {
|
|
|
|
errors.throwError('missing compiler output', errors.MISSING_ARGUMENT, { argument: 'compilerOutput' });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(compilerOutput) === 'string') {
|
|
|
|
compilerOutput = JSON.parse(compilerOutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
let abi = compilerOutput.abi;
|
|
|
|
|
|
|
|
let bytecode: any = null;
|
|
|
|
if (compilerOutput.bytecode) {
|
|
|
|
bytecode = compilerOutput.bytecode;
|
|
|
|
} else if (compilerOutput.evm && compilerOutput.evm.bytecode) {
|
|
|
|
bytecode = compilerOutput.evm.bytecode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new ContractFactory(abi, bytecode, signer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|