diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-05-11 18:12:39 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-11 18:12:39 +0800 |
commit | f42f608f3f97a5244f09f17ae5ee184c0f775de3 (patch) | |
tree | 08fd03d69f8a7dfcc7beadcd56c5d1624928c430 /packages/base-contract/src | |
parent | 44f17c1706e7b3208fdc0702c54a8cd943132fd3 (diff) | |
parent | c093aab350dfbd86972d6388c3923ec60fc4501a (diff) | |
download | dexon-sol-tools-f42f608f3f97a5244f09f17ae5ee184c0f775de3.tar dexon-sol-tools-f42f608f3f97a5244f09f17ae5ee184c0f775de3.tar.gz dexon-sol-tools-f42f608f3f97a5244f09f17ae5ee184c0f775de3.tar.bz2 dexon-sol-tools-f42f608f3f97a5244f09f17ae5ee184c0f775de3.tar.lz dexon-sol-tools-f42f608f3f97a5244f09f17ae5ee184c0f775de3.tar.xz dexon-sol-tools-f42f608f3f97a5244f09f17ae5ee184c0f775de3.tar.zst dexon-sol-tools-f42f608f3f97a5244f09f17ae5ee184c0f775de3.zip |
Merge pull request #574 from 0xProject/feature/rm-rf-deployer
Remove @0xproject/deployer.Deployer. Make contracts able to deploy themselves
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( |