Fix gasPrice regression

This commit is contained in:
Leonid Logvinov
2017-09-05 11:38:28 +02:00
parent 876032a8a7
commit a12df1c73a
8 changed files with 54 additions and 21 deletions

View File

@@ -170,13 +170,16 @@ export class ZeroEx {
// We re-assign the send method so that Web3@1.0 providers work with 0x.js
(provider as any).sendAsync = (provider as any).send;
}
this._web3Wrapper = new Web3Wrapper(provider);
const gasPrice = _.isUndefined(config) ? undefined : config.gasPrice;
this.token = new TokenWrapper(this._web3Wrapper, gasPrice);
this.proxy = new TokenTransferProxyWrapper(this._web3Wrapper, gasPrice);
this.exchange = new ExchangeWrapper(this._web3Wrapper, this.token, gasPrice);
this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper, gasPrice);
this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.token, gasPrice);
const defaults = {
gasPrice,
};
this._web3Wrapper = new Web3Wrapper(provider, defaults);
this.token = new TokenWrapper(this._web3Wrapper);
this.proxy = new TokenTransferProxyWrapper(this._web3Wrapper);
this.exchange = new ExchangeWrapper(this._web3Wrapper, this.token);
this.tokenRegistry = new TokenRegistryWrapper(this._web3Wrapper);
this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.token);
}
/**
* Sets a new web3 provider for 0x.js. Updating the provider will stop all

View File

@@ -6,11 +6,13 @@ export class Contract implements Web3.ContractInstance {
public address: string;
public abi: Web3.ContractAbi;
private contract: Web3.ContractInstance;
private defaults: Partial<Web3.TxData>;
[name: string]: any;
constructor(web3ContractInstance: Web3.ContractInstance) {
constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<Web3.TxData>) {
this.contract = web3ContractInstance;
this.address = web3ContractInstance.address;
this.abi = web3ContractInstance.abi;
this.defaults = defaults;
this.populateEvents();
this.populateFunctions();
}
@@ -18,11 +20,12 @@ export class Contract implements Web3.ContractInstance {
const functionsAbi = _.filter(this.abi, abiPart => abiPart.type === 'function');
_.forEach(functionsAbi, (functionAbi: Web3.MethodAbi) => {
const cbStyleFunction = this.contract[functionAbi.name];
this[functionAbi.name] = promisify(cbStyleFunction, this.contract);
if (functionAbi.constant) {
this[functionAbi.name] = promisify(cbStyleFunction, this.contract);
const cbStyleCallFunction = this.contract[functionAbi.name].call;
this[functionAbi.name].call = promisify(cbStyleCallFunction, this.contract);
} else {
this[functionAbi.name] = this.promisifyWithDefaultParams(cbStyleFunction);
const cbStyleEstimateGasFunction = this.contract[functionAbi.name].estimateGas;
this[functionAbi.name].estimateGas =
promisify(cbStyleEstimateGasFunction, this.contract);
@@ -35,4 +38,31 @@ export class Contract implements Web3.ContractInstance {
this[eventAbi.name] = this.contract[eventAbi.name];
});
}
private promisifyWithDefaultParams(fn: (...args: any[]) => void): (...args: any[]) => Promise<any> {
const promisifiedWithDefaultParams = (...args: any[]) => {
const promise = new Promise((resolve, reject) => {
const lastArg = args[args.length - 1];
let txData: Partial<Web3.TxData> = {};
if (_.isObject(lastArg) && !_.isArray(lastArg) && !lastArg.isBigNumber) {
txData = args.pop();
}
txData = {
...txData,
...this.defaults,
};
const callback = (err: Error, data: any) => {
if (_.isNull(err)) {
resolve(data);
} else {
reject(err);
}
};
args.push(txData);
args.push(callback);
fn.apply(this.contract, args);
});
return promise;
};
return promisifiedWithDefaultParams;
}
}

View File

@@ -6,10 +6,8 @@ import {utils} from '../utils/utils';
export class ContractWrapper {
protected _web3Wrapper: Web3Wrapper;
private _gasPrice?: BigNumber.BigNumber;
constructor(web3Wrapper: Web3Wrapper, gasPrice?: BigNumber.BigNumber) {
constructor(web3Wrapper: Web3Wrapper) {
this._web3Wrapper = web3Wrapper;
this._gasPrice = gasPrice;
}
protected async _instantiateContractIfExistsAsync<A extends Web3.ContractInstance>(artifact: Artifact,
address?: string): Promise<A> {

View File

@@ -13,8 +13,8 @@ import * as EtherTokenArtifacts from '../artifacts/EtherToken.json';
export class EtherTokenWrapper extends ContractWrapper {
private _etherTokenContractIfExists?: EtherTokenContract;
private _tokenWrapper: TokenWrapper;
constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper, gasPrice?: BigNumber.BigNumber) {
super(web3Wrapper, gasPrice);
constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper) {
super(web3Wrapper);
this._tokenWrapper = tokenWrapper;
}
/**

View File

@@ -72,8 +72,8 @@ export class ExchangeWrapper extends ContractWrapper {
];
return [orderAddresses, orderValues];
}
constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper, gasPrice?: BigNumber.BigNumber) {
super(web3Wrapper, gasPrice);
constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper) {
super(web3Wrapper);
this._tokenWrapper = tokenWrapper;
this._orderValidationUtils = new OrderValidationUtils(tokenWrapper, this);
this._exchangeLogEventEmitters = [];

View File

@@ -11,8 +11,8 @@ import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json';
*/
export class TokenRegistryWrapper extends ContractWrapper {
private _tokenRegistryContractIfExists?: TokenRegistryContract;
constructor(web3Wrapper: Web3Wrapper, gasPrice?: BigNumber.BigNumber) {
super(web3Wrapper, gasPrice);
constructor(web3Wrapper: Web3Wrapper) {
super(web3Wrapper);
}
/**
* Retrieves all the tokens currently listed in the Token Registry smart contract

View File

@@ -31,8 +31,8 @@ export class TokenWrapper extends ContractWrapper {
public UNLIMITED_ALLOWANCE_IN_BASE_UNITS = constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS;
private _tokenContractsByAddress: {[address: string]: TokenContract};
private _tokenLogEventEmitters: ContractEventEmitter[];
constructor(web3Wrapper: Web3Wrapper, gasPrice?: BigNumber.BigNumber) {
super(web3Wrapper, gasPrice);
constructor(web3Wrapper: Web3Wrapper) {
super(web3Wrapper);
this._tokenContractsByAddress = {};
this._tokenLogEventEmitters = [];
}

View File

@@ -7,10 +7,12 @@ import {Contract} from './contract';
export class Web3Wrapper {
private web3: Web3;
private defaults: Partial<Web3.TxData>;
private networkIdIfExists?: number;
constructor(provider: Web3.Provider) {
constructor(provider: Web3.Provider, defaults: Partial<Web3.TxData>) {
this.web3 = new Web3();
this.web3.setProvider(provider);
this.defaults = defaults;
}
public setProvider(provider: Web3.Provider) {
delete this.networkIdIfExists;
@@ -97,7 +99,7 @@ export class Web3Wrapper {
}
private getContractInstance<A extends Web3.ContractInstance>(abi: Web3.ContractAbi, address: string): A {
const web3ContractInstance = this.web3.eth.contract(abi).at(address);
const contractInstance = new Contract(web3ContractInstance) as any as A;
const contractInstance = new Contract(web3ContractInstance, this.defaults) as any as A;
return contractInstance;
}
private async getNetworkAsync(): Promise<number> {