aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/web3_wrapper.ts17
-rw-r--r--test/utils/constants.ts1
-rw-r--r--test/web3_wrapper_test.ts29
4 files changed, 43 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 16abcc0e9..413203e4c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@ v0.9.0 - TBD
------------------------
* Move `zeroEx.exchange.getAvailableContractAddressesAsync` to `zeroEx.getAvailableExchangeContractAddressesAsync` (#94)
* Move `zeroEx.exchange.getProxyAuthorizedContractAddressesAsync` to `zeroEx.getProxyAuthorizedExchangeContractAddressesAsync` (#94)
+ * Cache `net_version` requests and invalidate the cache on calls to `setProvider` (#95)
* Rename `zeroEx.exchange.batchCancelOrderAsync` to `zeroEx.exchange.batchCancelOrdersAsync`
* Rename `zeroEx.exchange.batchFillOrderAsync` to `zeroEx.exchange.batchFillOrdersAsync`
diff --git a/src/web3_wrapper.ts b/src/web3_wrapper.ts
index 630f0bef3..8c410eef7 100644
--- a/src/web3_wrapper.ts
+++ b/src/web3_wrapper.ts
@@ -5,11 +5,13 @@ import promisify = require('es6-promisify');
export class Web3Wrapper {
private web3: Web3;
+ private networkIdIfExists?: number;
constructor(provider: Web3.Provider) {
this.web3 = new Web3();
this.web3.setProvider(provider);
}
public setProvider(provider: Web3.Provider) {
+ this.networkIdIfExists = undefined;
this.web3.setProvider(provider);
}
public isAddress(address: string): boolean {
@@ -27,11 +29,16 @@ export class Web3Wrapper {
return this.web3.currentProvider;
}
public async getNetworkIdIfExistsAsync(): Promise<number|undefined> {
- try {
- const networkId = await this.getNetworkAsync();
- return Number(networkId);
- } catch (err) {
- return undefined;
+ if (_.isUndefined(this.networkIdIfExists)) {
+ try {
+ const networkId = await this.getNetworkAsync();
+ this.networkIdIfExists = Number(networkId);
+ return this.networkIdIfExists;
+ } catch (err) {
+ return undefined;
+ }
+ } else {
+ return this.networkIdIfExists;
}
}
public toWei(ethAmount: BigNumber.BigNumber): BigNumber.BigNumber {
diff --git a/test/utils/constants.ts b/test/utils/constants.ts
index 9b150b5c1..b677d7361 100644
--- a/test/utils/constants.ts
+++ b/test/utils/constants.ts
@@ -2,5 +2,6 @@ export const constants = {
NULL_ADDRESS: '0x0000000000000000000000000000000000000000',
RPC_HOST: 'localhost',
RPC_PORT: 8545,
+ TESTRPC_NETWORK_ID: 50,
KOVAN_RPC_URL: 'https://kovan.0xproject.com',
};
diff --git a/test/web3_wrapper_test.ts b/test/web3_wrapper_test.ts
new file mode 100644
index 000000000..d1c2e8e89
--- /dev/null
+++ b/test/web3_wrapper_test.ts
@@ -0,0 +1,29 @@
+import * as chai from 'chai';
+import {web3Factory} from './utils/web3_factory';
+import {ZeroEx} from '../src/';
+import {Web3Wrapper} from '../src/web3_wrapper';
+import {constants} from './utils/constants';
+
+chai.config.includeStack = true;
+const expect = chai.expect;
+
+describe('Web3Wrapper', () => {
+ const web3Provider = web3Factory.create().currentProvider;
+ describe('#getNetworkIdIfExistsAsync', () => {
+ it('caches network id requests', async () => {
+ const web3Wrapper = (new ZeroEx(web3Provider) as any)._web3Wrapper as Web3Wrapper;
+ expect((web3Wrapper as any).networkIdIfExists).to.be.undefined();
+ const networkIdIfExists = await web3Wrapper.getNetworkIdIfExistsAsync();
+ expect((web3Wrapper as any).networkIdIfExists).to.be.equal(constants.TESTRPC_NETWORK_ID);
+ });
+ it('invalidates network id cache on setProvider call', async () => {
+ const web3Wrapper = (new ZeroEx(web3Provider) as any)._web3Wrapper as Web3Wrapper;
+ expect((web3Wrapper as any).networkIdIfExists).to.be.undefined();
+ const networkIdIfExists = await web3Wrapper.getNetworkIdIfExistsAsync();
+ expect((web3Wrapper as any).networkIdIfExists).to.be.equal(constants.TESTRPC_NETWORK_ID);
+ const newProvider = web3Factory.create().currentProvider;
+ web3Wrapper.setProvider(newProvider);
+ expect((web3Wrapper as any).networkIdIfExists).to.be.undefined();
+ });
+ });
+});