diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-07-07 06:17:46 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-07-07 06:18:56 +0800 |
commit | 8b88ad835cf16786f8bd76157784fb09b63d1088 (patch) | |
tree | 5d5865fe781e9c656c806a423f580ebb4332fd8e | |
parent | 18050b7ea3eae19626a7976c004b237287ff5363 (diff) | |
download | dexon-sol-tools-8b88ad835cf16786f8bd76157784fb09b63d1088.tar dexon-sol-tools-8b88ad835cf16786f8bd76157784fb09b63d1088.tar.gz dexon-sol-tools-8b88ad835cf16786f8bd76157784fb09b63d1088.tar.bz2 dexon-sol-tools-8b88ad835cf16786f8bd76157784fb09b63d1088.tar.lz dexon-sol-tools-8b88ad835cf16786f8bd76157784fb09b63d1088.tar.xz dexon-sol-tools-8b88ad835cf16786f8bd76157784fb09b63d1088.tar.zst dexon-sol-tools-8b88ad835cf16786f8bd76157784fb09b63d1088.zip |
Cache networkId in web3Wrapper
-rw-r--r-- | src/web3_wrapper.ts | 17 |
1 files changed, 12 insertions, 5 deletions
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 { |