aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/blockchain_watcher.ts
blob: 5d029d4f1cdbd13e7011b01c604903b87a0eb225 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { BigNumber, intervalUtils, logUtils } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as _ from 'lodash';
import { Dispatcher } from 'ts/redux/dispatcher';

export class BlockchainWatcher {
    private _dispatcher: Dispatcher;
    private _web3Wrapper: Web3Wrapper;
    private _prevNetworkId: number;
    private _isWatchingNetworkId: boolean = false;
    private _shouldPollUserAddress: boolean;
    private _watchBalanceIntervalId: NodeJS.Timer;
    private _prevUserEtherBalanceInWei?: BigNumber;
    private _prevUserAddressIfExists: string;
    constructor(
        dispatcher: Dispatcher,
        web3Wrapper: Web3Wrapper,
        networkIdIfExists: number,
        shouldPollUserAddress: boolean,
    ) {
        this._dispatcher = dispatcher;
        this._shouldPollUserAddress = shouldPollUserAddress;
        this._prevNetworkId = networkIdIfExists;
        this._web3Wrapper = web3Wrapper;
    }
    public destroy(): void {
        this._stopEmittingUserBalanceState();
        // HACK: stop() is only available on providerEngine instances
        const provider = this._web3Wrapper.getProvider();
        if (!_.isUndefined((provider as any).stop)) {
            (provider as any).stop();
        }
    }
    // This should only be called from the LedgerConfigDialog
    public updatePrevUserAddress(userAddress: string): void {
        this._prevUserAddressIfExists = userAddress;
    }
    // public async startEmittingInjectedProviderNetworkIdAsync(injectedProvider: any): Promise<void> {
    //     if (this._isWatchingNetworkId) {
    //         return; // we are already watching the network id
    //     }
    //     const observable = injectedProvider.publicConfigStore;
    //     if (observable && observable.subscribe) {
    //         observable.subscribe(this._handleInjectedProviderUpdate.bind(this));
    //         this._isWatchingNetworkId = true;
    //     }
    // }
    public async startEmittingUserBalanceStateAsync(): Promise<void> {
        if (!_.isUndefined(this._watchBalanceIntervalId)) {
            return; // we are already emitting the state
        }
        this._prevUserEtherBalanceInWei = undefined;
        await this._updateBalanceAsync();
        this._watchBalanceIntervalId = intervalUtils.setAsyncExcludingInterval(
            this._updateBalanceAsync.bind(this),
            5000,
            (err: Error) => {
                logUtils.log(`Watching network and balances failed: ${err.stack}`);
                this._stopEmittingUserBalanceState();
            },
        );
    }
    // private _handleInjectedProviderUpdate(update: InjectedProviderUpdate): void {
    //     const updatedNetworkId = _.parseInt(update.networkVersion);
    //     if (this._prevNetworkId === updatedNetworkId) {
    //         return;
    //     }
    //     this._prevNetworkId = updatedNetworkId;
    //     this._dispatcher.updateNetworkId(updatedNetworkId);
    //     this._updateBalanceAsync();
    // }
    private async _updateBalanceAsync(): Promise<void> {
        let prevNodeVersion: string;
        // 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)) {
            this._prevUserEtherBalanceInWei = balanceInWei;
            this._dispatcher.updateUserWeiBalance(balanceInWei);
        }
    }
    private _stopEmittingUserBalanceState(): void {
        if (!_.isUndefined(this._watchBalanceIntervalId)) {
            intervalUtils.clearAsyncExcludingInterval(this._watchBalanceIntervalId);
        }
    }
}