diff options
author | Fabio Berger <me@fabioberger.com> | 2018-06-20 19:25:29 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-06-20 19:25:29 +0800 |
commit | 2ffab2218584fac5d3ea2b264a7c746d963083d9 (patch) | |
tree | f7558147b811c06300e2c7697591466ae7cf48ba /packages/website/ts/blockchain_watcher.ts | |
parent | 5541327968ad6f974a37c49057253746f738a709 (diff) | |
parent | 096eaa20d785baee07b06b3f0848797e470fbda0 (diff) | |
download | dexon-0x-contracts-2ffab2218584fac5d3ea2b264a7c746d963083d9.tar dexon-0x-contracts-2ffab2218584fac5d3ea2b264a7c746d963083d9.tar.gz dexon-0x-contracts-2ffab2218584fac5d3ea2b264a7c746d963083d9.tar.bz2 dexon-0x-contracts-2ffab2218584fac5d3ea2b264a7c746d963083d9.tar.lz dexon-0x-contracts-2ffab2218584fac5d3ea2b264a7c746d963083d9.tar.xz dexon-0x-contracts-2ffab2218584fac5d3ea2b264a7c746d963083d9.tar.zst dexon-0x-contracts-2ffab2218584fac5d3ea2b264a7c746d963083d9.zip |
Merge branch 'v2-prototype' into feature/combinatorial-testing
* v2-prototype: (22 commits)
Fix closing parens in liborder
Update after rebase
ERC721Proxy Always call safeTransferFrom
Rename makerEpoch => orderEpoch
Make cancelOrdersUpTo compatible with sender abstraction
Update PR template
Use Image component instead of img tag
Assembler orderHash function
Optimize and remove redundant encodePacked
Fix linting issue
Fix bug where we do fetch balances on wallet login
Check network state immediately instead of waiting for delay
Fix onboarding persisting when changing routes
Consolidate account state messaging logic
Only elevate wallet zIndex when onboarding is in progress
Rebase and update feedback
Run linter
Add Portal v2 logging
Simplified handling of source < 32 edge case
Basic EIP712 encoder
...
Diffstat (limited to 'packages/website/ts/blockchain_watcher.ts')
-rw-r--r-- | packages/website/ts/blockchain_watcher.ts | 89 |
1 files changed, 45 insertions, 44 deletions
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)) { |