Add gas margin and remove undefined values
This commit is contained in:
parent
33a8c7a9fb
commit
ee93f091ea
@ -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)) {
|
||||||
const estimatedGas = await estimateGasAsync.apply(this.contract, [...args, txData]);
|
try {
|
||||||
txData.gas = estimatedGas;
|
const estimatedGas = await estimateGasAsync.apply(this.contract, [...args, txData]);
|
||||||
}
|
const gas = estimatedGas + GAS_MARGIN;
|
||||||
const callback = (err: Error, data: any) => {
|
txData.gas = gas;
|
||||||
if (_.isNull(err)) {
|
console.log('withGas', txData);
|
||||||
resolve(data);
|
} catch (err) {
|
||||||
} 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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user