diff options
Diffstat (limited to 'packages/base-contract/src')
-rw-r--r-- | packages/base-contract/src/index.ts | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts index e95b18db6..f9298f413 100644 --- a/packages/base-contract/src/index.ts +++ b/packages/base-contract/src/index.ts @@ -1,6 +1,7 @@ import { AbiDefinition, AbiType, + ConstructorAbi, ContractAbi, DataItem, MethodAbi, @@ -24,6 +25,8 @@ export class BaseContract { protected _web3Wrapper: Web3Wrapper; public abi: ContractAbi; public address: string; + public contractName: string; + public constructorArgs: any[] = []; protected static _formatABIDataItemList( abis: DataItem[], values: any[], @@ -37,11 +40,31 @@ export class BaseContract { protected static _bigNumberToString(type: string, value: any): any { return _.isObject(value) && value.isBigNumber ? value.toString() : value; } + protected static _lookupConstructorAbi(abi: ContractAbi): ConstructorAbi { + const constructorAbiIfExists = _.find( + abi, + (abiDefinition: AbiDefinition) => abiDefinition.type === AbiType.Constructor, + ) as ConstructorAbi | undefined; + if (!_.isUndefined(constructorAbiIfExists)) { + return constructorAbiIfExists; + } else { + // If the constructor is not explicitly defined, it won't be included in the ABI. It is + // still callable however, so we construct what the ABI would look like were it to exist. + const defaultConstructorAbi: ConstructorAbi = { + type: AbiType.Constructor, + stateMutability: 'nonpayable', + payable: false, + inputs: [], + }; + return defaultConstructorAbi; + } + } protected static _bnToBigNumber(type: string, value: any): any { return _.isObject(value) && value._bn ? new BigNumber(value.toString()) : value; } - protected async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>( + protected static async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>( txData: T, + txDefaults: Partial<TxData>, estimateGasAsync?: (txData: T) => Promise<number>, ): Promise<TxData> { // Gas amount sourced with the following priorities: @@ -49,13 +72,12 @@ export class BaseContract { // 2. Global config passed in at library instantiation // 3. Gas estimate calculation + safety margin const removeUndefinedProperties = _.pickBy; - const txDataWithDefaults = ({ - to: this.address, - ...removeUndefinedProperties(this._web3Wrapper.getContractDefaults()), + const txDataWithDefaults: TxData = { + ...removeUndefinedProperties(txDefaults), ...removeUndefinedProperties(txData as any), // HACK: TS can't prove that T is spreadable. // Awaiting https://github.com/Microsoft/TypeScript/pull/13288 to be merged - } as any) as TxData; + } as any; if (_.isUndefined(txDataWithDefaults.gas) && !_.isUndefined(estimateGasAsync)) { const estimatedGas = await estimateGasAsync(txData); txDataWithDefaults.gas = estimatedGas; @@ -82,8 +104,15 @@ export class BaseContract { }) as MethodAbi; return methodAbi; } - constructor(abi: ContractAbi, address: string, provider: Provider, defaults?: Partial<TxData>) { - this._web3Wrapper = new Web3Wrapper(provider, defaults); + constructor( + contractName: string, + abi: ContractAbi, + address: string, + provider: Provider, + txDefaults?: Partial<TxData>, + ) { + this.contractName = contractName; + this._web3Wrapper = new Web3Wrapper(provider, txDefaults); this.abi = abi; this.address = address; const methodAbis = this.abi.filter( |