From ee93f091ea5fc78d039f4f91cb234a94f428ec6a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 27 Nov 2017 13:39:32 -0600 Subject: Add gas margin and remove undefined values --- packages/0x.js/src/contract.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'packages/0x.js/src') diff --git a/packages/0x.js/src/contract.ts b/packages/0x.js/src/contract.ts index 347f47aa0..6dc0dab66 100644 --- a/packages/0x.js/src/contract.ts +++ b/packages/0x.js/src/contract.ts @@ -5,6 +5,8 @@ import * as Web3 from 'web3'; import {AbiType} from './types'; +const GAS_MARGIN = 30000; + export class Contract implements Web3.ContractInstance { public address: string; public abi: Web3.ContractAbi; @@ -63,21 +65,23 @@ export class Contract implements Web3.ContractInstance { // 1 - method level // 2 - Library defaults // 3 - estimate + const removeUndefinedProperties = _.pickBy; txData = { - ...this.defaults, - ...txData, + ...removeUndefinedProperties(this.defaults), + ...removeUndefinedProperties(txData), }; if (_.isUndefined(txData.gas)) { - const estimatedGas = await estimateGasAsync.apply(this.contract, [...args, txData]); - txData.gas = estimatedGas; - } - const callback = (err: Error, data: any) => { - if (_.isNull(err)) { - resolve(data); - } else { + try { + const estimatedGas = await estimateGasAsync.apply(this.contract, [...args, txData]); + const gas = estimatedGas + GAS_MARGIN; + txData.gas = gas; + console.log('withGas', txData); + } catch (err) { reject(err); + return; } - }; + } + const callback = (err: Error, data: any) => _.isNull(err) ? resolve(data) : reject(err); args.push(txData); args.push(callback); web3CbStyleFunction.apply(this.contract, args); -- cgit v1.2.3