Skip to content

Commit

Permalink
Split exchange and wallet balance refresh actions and timers
Browse files Browse the repository at this point in the history
  • Loading branch information
greimela committed May 15, 2019
1 parent 956028c commit 818a1cf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 46 deletions.
8 changes: 4 additions & 4 deletions __tests__/modules/exchanges.balances.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { applyMiddleware, combineReducers, createStore } from 'redux';
import thunk from 'redux-thunk';
import { GlobalState } from '../../app/modules';
import exchanges, { fetchAllBalances } from '../../app/modules/exchanges';
import exchanges, { fetchAllExchangeBalances } from '../../app/modules/exchanges';
import { initialSettings } from '../../app/modules/settings.types';
import { getExchanges } from '../../app/selectors/selectGlobalState';
import { Store } from '../../app/store/configureStore';
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('fetchAllBalances', () => {
test('updates the balances in store', async () => {
fakeFetchTotalBalance.mockResolvedValue({ BTC: 5 });

await store.dispatch(fetchAllBalances());
await store.dispatch(fetchAllExchangeBalances());

expect(getExchanges(store.getState())).toMatchObject({
id: {
Expand All @@ -66,7 +66,7 @@ describe('fetchAllBalances', () => {
test('ignores empty balances', async () => {
fakeFetchTotalBalance.mockResolvedValue({ BTC: 5, LTC: 0 });

await store.dispatch(fetchAllBalances());
await store.dispatch(fetchAllExchangeBalances());

expect(getExchanges(store.getState())).toMatchObject({
id: {
Expand All @@ -82,7 +82,7 @@ describe('fetchAllBalances', () => {
test('stores the error message on failure', async () => {
fakeFetchTotalBalance.mockRejectedValue({ message: 'Error Message' });

await store.dispatch(fetchAllBalances());
await store.dispatch(fetchAllExchangeBalances());

expect(getExchanges(store.getState())).toMatchObject({
id: {
Expand Down
12 changes: 3 additions & 9 deletions app/containers/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { AppBar, List, Typography, WithStyles } from '@material-ui/core';
import Drawer from '@material-ui/core/Drawer';
import { StyleRulesCallback, withStyles } from '@material-ui/core/styles';
import {
AccountBalanceWallet,
Cloud,
Dashboard,
HelpOutline,
Settings,
SwapHoriz,
} from '@material-ui/icons';
import { AccountBalanceWallet, Cloud, Dashboard, HelpOutline, Settings } from '@material-ui/icons';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { RouteComponentProps, withRouter } from 'react-router';
import { Dispatch } from '../actions/actions.types';
import { DrawerItem } from '../components/DrawerItem';
import { getRaven } from '../helpers/sentry';
import { requestCoinList } from '../modules/coinlist';
import { continuouslyFetchBalances } from '../modules/exchanges';
import { continuouslyFetchBalances } from '../modules/init';
import { continuouslyUpdateTicker } from '../modules/ticker';

const icon = require('../resources/icon.png'); // tslint:disable-line:no-var-requires

const drawerWidth = 250;
Expand Down
32 changes: 6 additions & 26 deletions app/modules/exchanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import _ from 'lodash';
import { Action, Dispatch, GetState, ThunkAction } from '../actions/actions.types';
import { generateId } from '../helpers/reducers';
import { getExchangeBalances } from '../pages/portfolio/selectors/selectBalances';
import { getExchanges, getTimers, getWallets } from '../selectors/selectGlobalState';
import { getExchanges } from '../selectors/selectGlobalState';
import { unifySymbols } from '../utils/unifySymbols';
import {
AddExchangeAction,
Expand All @@ -26,11 +26,8 @@ import {
} from './exchanges.types';
import { GlobalState } from './index';
import { requestTickerUpdate } from './ticker';
import { setLastUpdate, startTimer } from './timer';
import { fetchBalanceForWallet } from './wallets';
import { EXCHANGE_BALANCE_TIMER, setLastUpdate } from './timer';

const BALANCE_REFRESH_MS = 30000;
// const TRADE_REFRESH_MS = 60000;

// Reducer
export default function reducer(state: Exchanges = {}, action: Action): Exchanges {
Expand Down Expand Up @@ -64,7 +61,7 @@ export function addExchange(type: string, credentials: ExchangeCredentials): Thu
exchangeType: type,
credentials,
});
dispatch(fetchAllBalances());
dispatch(fetchAllExchangeBalances());
};
}

Expand All @@ -78,7 +75,7 @@ export function updateExchangeCredentials(
id,
credentials,
});
dispatch(fetchAllBalances());
dispatch(fetchAllExchangeBalances());
};
}

Expand All @@ -97,15 +94,11 @@ export function deleteExchange(id: string): Action {
};
}

// TODO Split into exchange and wallet balances
export function fetchAllBalances(): ThunkAction {
export function fetchAllExchangeBalances(): ThunkAction {
return async (dispatch: Dispatch, getState: GetState) => {
dispatch(setLastUpdate('balances'));
dispatch(setLastUpdate(EXCHANGE_BALANCE_TIMER));
const exchanges = getConfiguredExchanges(getState());
await Promise.all(_.map(exchanges, exchange => dispatch(fetchBalancesForExchange(exchange))));

const wallets = getWallets(getState());
await Promise.all(_.map(wallets, wallet => dispatch(fetchBalanceForWallet(wallet))));
};
}

Expand Down Expand Up @@ -143,19 +136,6 @@ function getConfiguredExchanges(state: GlobalState): Exchanges {
return state.exchanges;
}

export function continuouslyFetchBalances(): ThunkAction {
return (dispatch: Dispatch, getState: GetState) => {
if (!getTimers(getState()).balances) {
const balanceTimer = window.setInterval(
() => dispatch(fetchAllBalances()),
BALANCE_REFRESH_MS
);

dispatch(startTimer('balances', balanceTimer));
}
};
}

async function fetchBalances(exchange: Exchange) {
const connector = new ccxt[exchange.type](exchange.credentials);
if (exchange.type === 'bitfinex') {
Expand Down
33 changes: 30 additions & 3 deletions app/modules/init.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
import { Dispatch, ThunkAction } from '../actions/actions.types';
import { fetchAllBalances } from './exchanges';
import { Dispatch, GetState, ThunkAction } from '../actions/actions.types';
import { getTimers } from '../selectors/selectGlobalState';
import { fetchAllExchangeBalances } from './exchanges';
import { requestTickerUpdate } from './ticker';
import { EXCHANGE_BALANCE_TIMER, startTimer, WALLET_BALANCE_TIMER } from './timer';
import { fetchAllWalletBalances } from './wallets';

export function rehydrationComplete(): ThunkAction {
return (dispatch: Dispatch) => {
dispatch(requestTickerUpdate());
dispatch(fetchAllBalances());
dispatch(fetchAllExchangeBalances());
};
}

const BALANCE_REFRESH_MS = 30000;

export function continuouslyFetchBalances(): ThunkAction {
return (dispatch: Dispatch, getState: GetState) => {
if (!getTimers(getState())[EXCHANGE_BALANCE_TIMER]) {
const balanceTimer = window.setInterval(
() => dispatch(fetchAllExchangeBalances()),
BALANCE_REFRESH_MS
);

dispatch(startTimer(EXCHANGE_BALANCE_TIMER, balanceTimer));
}

if (!getTimers(getState())[WALLET_BALANCE_TIMER]) {
const balanceTimer = window.setInterval(
() => dispatch(fetchAllWalletBalances()),
BALANCE_REFRESH_MS
);

dispatch(startTimer(WALLET_BALANCE_TIMER, balanceTimer));
}
};
}
3 changes: 3 additions & 0 deletions app/modules/timer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Action } from '../actions/actions.types';
import { Timers } from './timer.types';

export const EXCHANGE_BALANCE_TIMER = 'EXCHANGE_BALANCE_TIMER';
export const WALLET_BALANCE_TIMER = 'WALLET_BALANCE_TIMER';

// Reducer
export default function reducer(state: Timers = { timers: {} }, action: Action): Timers {
switch (action.type) {
Expand Down
17 changes: 13 additions & 4 deletions app/modules/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import _ from 'lodash';
import { Action, Dispatch, ThunkAction } from '../actions/actions.types';
import { Action, Dispatch, GetState, ThunkAction } from '../actions/actions.types';
import { generateId } from '../helpers/reducers';
import { fetchAllBalances } from './exchanges';
import { getWallets } from '../selectors/selectGlobalState';
import { requestTickerUpdate } from './ticker';
import { setLastUpdate, WALLET_BALANCE_TIMER } from './timer';
import {
AddWalletAction,
DeleteWalletAction,
Expand Down Expand Up @@ -36,7 +37,7 @@ export function addWallet(wallet: Wallet): ThunkAction {
type: 'ADD_WALLET',
wallet,
});
dispatch(fetchAllBalances());
dispatch(fetchAllWalletBalances());
};
}

Expand All @@ -48,7 +49,7 @@ export function editWallet(id: string, updatedWallet: Wallet): ThunkAction {
id,
updatedWallet,
});
dispatch(fetchAllBalances());
dispatch(fetchAllWalletBalances());
};
}

Expand All @@ -67,6 +68,14 @@ function failedRequest(id: string, error: string): FailedWalletRequestAction {
};
}

export function fetchAllWalletBalances(): ThunkAction {
return async (dispatch: Dispatch, getState: GetState) => {
dispatch(setLastUpdate(WALLET_BALANCE_TIMER));
const wallets = getWallets(getState());
await Promise.all(_.map(wallets, wallet => dispatch(fetchBalanceForWallet(wallet))));
};
}

export function fetchBalanceForWallet(wallet: Wallet): ThunkAction {
return async dispatch => {
if (wallet.currency === 'BTC' && wallet.address) {
Expand Down

0 comments on commit 818a1cf

Please sign in to comment.