Add gas margin and remove undefined values

This commit is contained in:
Leonid Logvinov 2017-11-27 13:39:32 -06:00
parent 33a8c7a9fb
commit ee93f091ea
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4

View File

@ -5,6 +5,8 @@ import * as Web3 from 'web3';
import {AbiType} from './types'; import {AbiType} from './types';
const GAS_MARGIN = 30000;
export class Contract implements Web3.ContractInstance { export class Contract implements Web3.ContractInstance {
public address: string; public address: string;
public abi: Web3.ContractAbi; public abi: Web3.ContractAbi;
@ -63,21 +65,23 @@ export class Contract implements Web3.ContractInstance {
// 1 - method level // 1 - method level
// 2 - Library defaults // 2 - Library defaults
// 3 - estimate // 3 - estimate
const removeUndefinedProperties = _.pickBy;
txData = { txData = {
...this.defaults, ...removeUndefinedProperties(this.defaults),
...txData, ...removeUndefinedProperties(txData),
}; };
if (_.isUndefined(txData.gas)) { if (_.isUndefined(txData.gas)) {
try {
const estimatedGas = await estimateGasAsync.apply(this.contract, [...args, txData]); const estimatedGas = await estimateGasAsync.apply(this.contract, [...args, txData]);
txData.gas = estimatedGas; const gas = estimatedGas + GAS_MARGIN;
} txData.gas = gas;
const callback = (err: Error, data: any) => { console.log('withGas', txData);
if (_.isNull(err)) { } catch (err) {
resolve(data);
} else {
reject(err); reject(err);
return;
} }
}; }
const callback = (err: Error, data: any) => _.isNull(err) ? resolve(data) : reject(err);
args.push(txData); args.push(txData);
args.push(callback); args.push(callback);
web3CbStyleFunction.apply(this.contract, args); web3CbStyleFunction.apply(this.contract, args);