From 0c2ab2265624eb0dc18244b263fed3c8788b5c09 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 6 Feb 2018 16:39:45 +0100 Subject: Add base_contract.ts --- .../contract_wrappers/generated/base_contract.ts | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 packages/contracts/src/contract_wrappers/generated/base_contract.ts (limited to 'packages/contracts/src/contract_wrappers/generated') diff --git a/packages/contracts/src/contract_wrappers/generated/base_contract.ts b/packages/contracts/src/contract_wrappers/generated/base_contract.ts new file mode 100644 index 000000000..2d77b3ab1 --- /dev/null +++ b/packages/contracts/src/contract_wrappers/generated/base_contract.ts @@ -0,0 +1,35 @@ +import {TxData, TxDataPayable} from '@0xproject/types'; +import * as _ from 'lodash'; +import * as Web3 from 'web3'; + +export class BaseContract { + public address: string; + protected _web3ContractInstance: Web3.ContractInstance; + protected _defaults: Partial; + protected async _applyDefaultsToTxDataAsync( + txData: T, + estimateGasAsync?: (txData: T) => Promise, + ): Promise { + // Gas amount sourced with the following priorities: + // 1. Optional param passed in to public method call + // 2. Global config passed in at library instantiation + // 3. Gas estimate calculation + safety margin + const removeUndefinedProperties = _.pickBy; + const txDataWithDefaults = { + ...removeUndefinedProperties(this._defaults), + ...removeUndefinedProperties(txData as any), + // HACK: TS can't prove that T is spreadable. + // Awaiting https://github.com/Microsoft/TypeScript/pull/13288 to be merged + }; + if (_.isUndefined(txDataWithDefaults.gas) && !_.isUndefined(estimateGasAsync)) { + const estimatedGas = await estimateGasAsync(txData); + txDataWithDefaults.gas = estimatedGas; + } + return txDataWithDefaults; + } + constructor(web3ContractInstance: Web3.ContractInstance, defaults?: Partial) { + this.address = web3ContractInstance.address; + this._web3ContractInstance = web3ContractInstance; + this._defaults = defaults || {}; + } +} -- cgit v1.2.3