diff options
author | Brandon Millman <brandon@0xproject.com> | 2018-06-20 01:03:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-20 01:03:08 +0800 |
commit | e4acad60e91943c2dcbdd55b7766b543e78df6f6 (patch) | |
tree | b89a1b364aafc21647a1fe36b35ab2f30045d67c | |
parent | 66dc70c057d805f0abf84fe5dd13bf1aba82ab77 (diff) | |
parent | 3b73a0e26908a5c147cd377efb3dbe64b6febed4 (diff) | |
download | dexon-sol-tools-e4acad60e91943c2dcbdd55b7766b543e78df6f6.tar dexon-sol-tools-e4acad60e91943c2dcbdd55b7766b543e78df6f6.tar.gz dexon-sol-tools-e4acad60e91943c2dcbdd55b7766b543e78df6f6.tar.bz2 dexon-sol-tools-e4acad60e91943c2dcbdd55b7766b543e78df6f6.tar.lz dexon-sol-tools-e4acad60e91943c2dcbdd55b7766b543e78df6f6.tar.xz dexon-sol-tools-e4acad60e91943c2dcbdd55b7766b543e78df6f6.tar.zst dexon-sol-tools-e4acad60e91943c2dcbdd55b7766b543e78df6f6.zip |
Merge pull request #726 from 0xProject/bug/website/late-eth-balance
Check network state immediately instead of waiting for delay
-rw-r--r-- | packages/website/ts/blockchain.ts | 6 | ||||
-rw-r--r-- | packages/website/ts/blockchain_watcher.ts | 89 |
2 files changed, 48 insertions, 47 deletions
diff --git a/packages/website/ts/blockchain.ts b/packages/website/ts/blockchain.ts index 3ebdd1dee..46a4d6629 100644 --- a/packages/website/ts/blockchain.ts +++ b/packages/website/ts/blockchain.ts @@ -229,7 +229,7 @@ export class Blockchain { shouldPollUserAddress, ); this._contractWrappers.setProvider(provider, this.networkId); - this._blockchainWatcher.startEmittingNetworkConnectionAndUserBalanceState(); + await this._blockchainWatcher.startEmittingNetworkConnectionAndUserBalanceStateAsync(); this._dispatcher.updateProviderType(ProviderType.Ledger); } public async updateProviderToInjectedAsync(): Promise<void> { @@ -259,7 +259,7 @@ export class Blockchain { this._contractWrappers.setProvider(provider, this.networkId); await this.fetchTokenInformationAsync(); - this._blockchainWatcher.startEmittingNetworkConnectionAndUserBalanceState(); + await this._blockchainWatcher.startEmittingNetworkConnectionAndUserBalanceStateAsync(); this._dispatcher.updateProviderType(ProviderType.Injected); delete this._ledgerSubprovider; delete this._cachedProvider; @@ -816,7 +816,7 @@ export class Blockchain { this._userAddressIfExists = userAddresses[0]; this._dispatcher.updateUserAddress(this._userAddressIfExists); await this.fetchTokenInformationAsync(); - this._blockchainWatcher.startEmittingNetworkConnectionAndUserBalanceState(); + await this._blockchainWatcher.startEmittingNetworkConnectionAndUserBalanceStateAsync(); await this._rehydrateStoreWithContractEventsAsync(); } private _updateProviderName(injectedWeb3: Web3): void { diff --git a/packages/website/ts/blockchain_watcher.ts b/packages/website/ts/blockchain_watcher.ts index 3890a9e57..c576db6ac 100644 --- a/packages/website/ts/blockchain_watcher.ts +++ b/packages/website/ts/blockchain_watcher.ts @@ -34,56 +34,15 @@ export class BlockchainWatcher { public updatePrevUserAddress(userAddress: string): void { this._prevUserAddressIfExists = userAddress; } - public startEmittingNetworkConnectionAndUserBalanceState(): void { + public async startEmittingNetworkConnectionAndUserBalanceStateAsync(): Promise<void> { if (!_.isUndefined(this._watchNetworkAndBalanceIntervalId)) { return; // we are already emitting the state } - - let prevNodeVersion: string; this._prevUserEtherBalanceInWei = undefined; this._dispatcher.updateNetworkId(this._prevNetworkId); + await this._updateNetworkAndBalanceAsync(); this._watchNetworkAndBalanceIntervalId = intervalUtils.setAsyncExcludingInterval( - async () => { - // Check for network state changes - let currentNetworkId; - try { - currentNetworkId = await this._web3Wrapper.getNetworkIdAsync(); - } catch (err) { - // Noop - } - if (currentNetworkId !== this._prevNetworkId) { - this._prevNetworkId = currentNetworkId; - this._dispatcher.updateNetworkId(currentNetworkId); - } - - // Check for node version changes - const currentNodeVersion = await this._web3Wrapper.getNodeVersionAsync(); - if (currentNodeVersion !== prevNodeVersion) { - prevNodeVersion = currentNodeVersion; - this._dispatcher.updateNodeVersion(currentNodeVersion); - } - - if (this._shouldPollUserAddress) { - const addresses = await this._web3Wrapper.getAvailableAddressesAsync(); - const userAddressIfExists = addresses[0]; - // Update makerAddress on network change - if (this._prevUserAddressIfExists !== userAddressIfExists) { - this._prevUserAddressIfExists = userAddressIfExists; - this._dispatcher.updateUserAddress(userAddressIfExists); - } - - // Check for user ether balance changes - if (!_.isUndefined(userAddressIfExists)) { - await this._updateUserWeiBalanceAsync(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 (!_.isUndefined(this._prevUserAddressIfExists)) { - await this._updateUserWeiBalanceAsync(this._prevUserAddressIfExists); - } - } - }, + this._updateNetworkAndBalanceAsync.bind(this), 5000, (err: Error) => { logUtils.log(`Watching network and balances failed: ${err.stack}`); @@ -91,6 +50,48 @@ export class BlockchainWatcher { }, ); } + private async _updateNetworkAndBalanceAsync(): Promise<void> { + // Check for network state changes + let prevNodeVersion: string; + let currentNetworkId; + try { + currentNetworkId = await this._web3Wrapper.getNetworkIdAsync(); + } catch (err) { + // Noop + } + if (currentNetworkId !== this._prevNetworkId) { + this._prevNetworkId = currentNetworkId; + this._dispatcher.updateNetworkId(currentNetworkId); + } + + // Check for node version changes + const currentNodeVersion = await this._web3Wrapper.getNodeVersionAsync(); + if (currentNodeVersion !== prevNodeVersion) { + prevNodeVersion = currentNodeVersion; + this._dispatcher.updateNodeVersion(currentNodeVersion); + } + + if (this._shouldPollUserAddress) { + const addresses = await this._web3Wrapper.getAvailableAddressesAsync(); + const userAddressIfExists = addresses[0]; + // Update makerAddress on network change + if (this._prevUserAddressIfExists !== userAddressIfExists) { + this._prevUserAddressIfExists = userAddressIfExists; + this._dispatcher.updateUserAddress(userAddressIfExists); + } + + // Check for user ether balance changes + if (!_.isUndefined(userAddressIfExists)) { + await this._updateUserWeiBalanceAsync(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 (!_.isUndefined(this._prevUserAddressIfExists)) { + await this._updateUserWeiBalanceAsync(this._prevUserAddressIfExists); + } + } + } private async _updateUserWeiBalanceAsync(userAddress: string): Promise<void> { const balanceInWei = await this._web3Wrapper.getBalanceInWeiAsync(userAddress); if (_.isUndefined(this._prevUserEtherBalanceInWei) || !balanceInWei.eq(this._prevUserEtherBalanceInWei)) { |