diff options
author | Leonid <logvinov.leon@gmail.com> | 2018-01-19 20:39:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-19 20:39:57 +0800 |
commit | 6091f818da7cddcb4a22a573f7726ba9b76b398e (patch) | |
tree | 5769ca73d50c497e46e6c6c927700ae0fa00bec9 /packages/website/ts/web3_wrapper.ts | |
parent | a45f9b480233d207fa5ba80a8e47cc3193c243d8 (diff) | |
parent | 9ccf63b44afe1ae6ccc2b9298ee35697d6be9e30 (diff) | |
download | dexon-sol-tools-6091f818da7cddcb4a22a573f7726ba9b76b398e.tar dexon-sol-tools-6091f818da7cddcb4a22a573f7726ba9b76b398e.tar.gz dexon-sol-tools-6091f818da7cddcb4a22a573f7726ba9b76b398e.tar.bz2 dexon-sol-tools-6091f818da7cddcb4a22a573f7726ba9b76b398e.tar.lz dexon-sol-tools-6091f818da7cddcb4a22a573f7726ba9b76b398e.tar.xz dexon-sol-tools-6091f818da7cddcb4a22a573f7726ba9b76b398e.tar.zst dexon-sol-tools-6091f818da7cddcb4a22a573f7726ba9b76b398e.zip |
Merge branch 'development' into feature/separate-deployer
Diffstat (limited to 'packages/website/ts/web3_wrapper.ts')
-rw-r--r-- | packages/website/ts/web3_wrapper.ts | 72 |
1 files changed, 40 insertions, 32 deletions
diff --git a/packages/website/ts/web3_wrapper.ts b/packages/website/ts/web3_wrapper.ts index 6f260dc48..415df6e8b 100644 --- a/packages/website/ts/web3_wrapper.ts +++ b/packages/website/ts/web3_wrapper.ts @@ -1,6 +1,7 @@ import { BigNumber, intervalUtils, promisify } from '@0xproject/utils'; import * as _ from 'lodash'; import { Dispatcher } from 'ts/redux/dispatcher'; +import { utils } from 'ts/utils/utils'; import * as Web3 from 'web3'; export class Web3Wrapper { @@ -101,41 +102,48 @@ export class Web3Wrapper { let prevNodeVersion: string; this._prevUserEtherBalanceInEth = new BigNumber(0); this._dispatcher.updateNetworkId(this._prevNetworkId); - this._watchNetworkAndBalanceIntervalId = intervalUtils.setAsyncExcludingInterval(async () => { - // Check for network state changes - const currentNetworkId = await this.getNetworkIdIfExists(); - if (currentNetworkId !== this._prevNetworkId) { - this._prevNetworkId = currentNetworkId; - this._dispatcher.updateNetworkId(currentNetworkId); - } - - // Check for node version changes - const currentNodeVersion = await this.getNodeVersionAsync(); - if (currentNodeVersion !== prevNodeVersion) { - prevNodeVersion = currentNodeVersion; - this._dispatcher.updateNodeVersion(currentNodeVersion); - } - - if (this._shouldPollUserAddress) { - const userAddressIfExists = await this.getFirstAccountIfExistsAsync(); - // Update makerAddress on network change - if (this._prevUserAddress !== userAddressIfExists) { - this._prevUserAddress = userAddressIfExists; - this._dispatcher.updateUserAddress(userAddressIfExists); + this._watchNetworkAndBalanceIntervalId = intervalUtils.setAsyncExcludingInterval( + async () => { + // Check for network state changes + const currentNetworkId = await this.getNetworkIdIfExists(); + if (currentNetworkId !== this._prevNetworkId) { + this._prevNetworkId = currentNetworkId; + this._dispatcher.updateNetworkId(currentNetworkId); } - // Check for user ether balance changes - if (userAddressIfExists !== '') { - await this._updateUserEtherBalanceAsync(userAddressIfExists); + // Check for node version changes + const currentNodeVersion = await this.getNodeVersionAsync(); + if (currentNodeVersion !== prevNodeVersion) { + prevNodeVersion = currentNodeVersion; + this._dispatcher.updateNodeVersion(currentNodeVersion); } - } else { - // This logic is primarily for the Ledger, since we don't regularly poll for the address - // we simply update the balance for the last fetched address. - if (!_.isEmpty(this._prevUserAddress)) { - await this._updateUserEtherBalanceAsync(this._prevUserAddress); + + if (this._shouldPollUserAddress) { + const userAddressIfExists = await this.getFirstAccountIfExistsAsync(); + // Update makerAddress on network change + if (this._prevUserAddress !== userAddressIfExists) { + this._prevUserAddress = userAddressIfExists; + this._dispatcher.updateUserAddress(userAddressIfExists); + } + + // Check for user ether balance changes + if (userAddressIfExists !== '') { + await this._updateUserEtherBalanceAsync(userAddressIfExists); + } + } else { + // This logic is primarily for the Ledger, since we don't regularly poll for the address + // we simply update the balance for the last fetched address. + if (!_.isEmpty(this._prevUserAddress)) { + await this._updateUserEtherBalanceAsync(this._prevUserAddress); + } } - } - }, 5000); + }, + 5000, + (err: Error) => { + utils.consoleLog(`Watching network and balances failed: ${err}`); + this._stopEmittingNetworkConnectionAndUserBalanceStateAsync(); + }, + ); } private async _updateUserEtherBalanceAsync(userAddress: string) { const balance = await this.getBalanceInEthAsync(userAddress); @@ -145,6 +153,6 @@ export class Web3Wrapper { } } private _stopEmittingNetworkConnectionAndUserBalanceStateAsync() { - clearInterval(this._watchNetworkAndBalanceIntervalId); + intervalUtils.clearAsyncExcludingInterval(this._watchNetworkAndBalanceIntervalId); } } |