aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/src/contract.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-11-29 05:21:15 +0800
committerFabio Berger <me@fabioberger.com>2017-11-29 05:21:15 +0800
commit15ce862334be14a7effcd55840ca96e425912df8 (patch)
treeb51f48dfb6a0dc66745b62521c108472210dcfaa /packages/0x.js/src/contract.ts
parent72a00ac2df47ff793d74c2a82c7f403f501e784a (diff)
parentda0af12834b16c960d9ebf2936eec450825ba987 (diff)
downloaddexon-sol-tools-15ce862334be14a7effcd55840ca96e425912df8.tar
dexon-sol-tools-15ce862334be14a7effcd55840ca96e425912df8.tar.gz
dexon-sol-tools-15ce862334be14a7effcd55840ca96e425912df8.tar.bz2
dexon-sol-tools-15ce862334be14a7effcd55840ca96e425912df8.tar.lz
dexon-sol-tools-15ce862334be14a7effcd55840ca96e425912df8.tar.xz
dexon-sol-tools-15ce862334be14a7effcd55840ca96e425912df8.tar.zst
dexon-sol-tools-15ce862334be14a7effcd55840ca96e425912df8.zip
Merge branch 'development' into fix/refactorDocs
* development: (30 commits) Export TransactionOpts type Make website private Publish Update CHANGELOG Change interval to 1h Rename Add ifExists to cleanupJobInterval Add a cleanup job to an order watcher Improve the comment Add CHANGELOG comment Add a HACK comment Normalise subprovider names Remove a comment Fix a typo Pin testrpc version Remove gas params from tests Add fake gas estimate suprovider for tests Revert "Fix website linter errors" Fix website linter errors Fix tests ... # Conflicts: # packages/website/ts/utils/constants.ts
Diffstat (limited to 'packages/0x.js/src/contract.ts')
-rw-r--r--packages/0x.js/src/contract.ts43
1 files changed, 30 insertions, 13 deletions
diff --git a/packages/0x.js/src/contract.ts b/packages/0x.js/src/contract.ts
index e9c49c9f1..a4ee03910 100644
--- a/packages/0x.js/src/contract.ts
+++ b/packages/0x.js/src/contract.ts
@@ -5,6 +5,10 @@ import * as Web3 from 'web3';
import {AbiType} from './types';
+// HACK: Gas estimates on testrpc don't take into account gas refunds.
+// Our calls can trigger max 8 gas refunds for SSTORE per transaction for 15k gas each which gives 120k.
+const GAS_MARGIN = 120000;
+
export class Contract implements Web3.ContractInstance {
public address: string;
public abi: Web3.ContractAbi;
@@ -34,9 +38,10 @@ export class Contract implements Web3.ContractInstance {
} else {
const cbStyleFunction = this.contract[functionAbi.name];
const cbStyleEstimateGasFunction = this.contract[functionAbi.name].estimateGas;
+ const estimateGasAsync = promisify(cbStyleEstimateGasFunction, this.contract);
this[functionAbi.name] = {
- estimateGasAsync: promisify(cbStyleEstimateGasFunction, this.contract),
- sendTransactionAsync: this.promisifyWithDefaultParams(cbStyleFunction),
+ estimateGasAsync,
+ sendTransactionAsync: this.promisifyWithDefaultParams(cbStyleFunction, estimateGasAsync),
};
}
});
@@ -47,28 +52,40 @@ export class Contract implements Web3.ContractInstance {
this[eventAbi.name] = this.contract[eventAbi.name];
});
}
- private promisifyWithDefaultParams(fn: (...args: any[]) => void): (...args: any[]) => Promise<any> {
+ private promisifyWithDefaultParams(
+ web3CbStyleFunction: (...args: any[]) => void,
+ estimateGasAsync: (...args: any[]) => Promise<number>,
+ ): (...args: any[]) => Promise<any> {
const promisifiedWithDefaultParams = async (...args: any[]) => {
- const promise = new Promise((resolve, reject) => {
+ const promise = new Promise(async (resolve, reject) => {
const lastArg = args[args.length - 1];
let txData: Partial<Web3.TxData> = {};
- if (this.isTxData(lastArg)) {
+ if (!_.isUndefined(lastArg) && this.isTxData(lastArg)) {
txData = args.pop();
}
+ // 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;
txData = {
- ...this.defaults,
- ...txData,
+ ...removeUndefinedProperties(this.defaults),
+ ...removeUndefinedProperties(txData),
};
- const callback = (err: Error, data: any) => {
- if (_.isNull(err)) {
- resolve(data);
- } else {
+ if (_.isUndefined(txData.gas)) {
+ try {
+ const estimatedGas = await estimateGasAsync.apply(this.contract, [...args, txData]);
+ const gas = estimatedGas + GAS_MARGIN;
+ txData.gas = gas;
+ } catch (err) {
reject(err);
+ return;
}
- };
+ }
+ const callback = (err: Error, data: any) => _.isNull(err) ? resolve(data) : reject(err);
args.push(txData);
args.push(callback);
- fn.apply(this.contract, args);
+ web3CbStyleFunction.apply(this.contract, args);
});
return promise;
};