HenryNguyen5 01fc5f1a89 Move Nodes/Networks to Redux (#961)
* Start splitting networks into their own reducers

* Split out nodes and networks into their own reducers

* Cleanup file structure

* Make selectors for new state

* Change custom network typing

* re-type repo

* Fix up components to use selectors, work on fixing sagas

* Provide consistency in naming, fix more sagas

* Get non web3 node switching working

* Split config rehydration off into a different file for store

* Inline auth for custom nodes

* Include typing for app state

* moar selectors

* Get web3 working + cleanup sagas

* Cleanup tsc errors

* Use forof loop instead of foreach for clearing pruning custom networks

* Add reducer tests for new redux state

* Export needed variables

* Add console error

* Remove old comment

* Work on saga tests

* Get passing existing saga tests

* Fix more tests

* Remove irrlevant tests

* add console error

* Get rest of tests passing

* Fix merge errors

* Remove random text

* Fix store saving

* Fix selector lib only grabbing from static nodes

* Fix custom node removal crashing app

* Infer selected network via node

* Prune custom networks properly on node removal

* Infer network name from chainid from selecting state

* Cleanup tsc errors

* Remove MEW nodes for main and testnet
2018-02-12 14:43:07 -06:00

41 lines
1.6 KiB
TypeScript

import { apply, select, call } from 'redux-saga/effects';
import { AppState } from 'reducers';
import { INode } from 'libs/nodes/INode';
import { IWallet, WalletConfig } from 'libs/wallet';
import { TokenBalance } from 'selectors/wallet';
import { getCustomTokens } from 'selectors/customTokens';
import { getNodeLib } from 'selectors/config';
import { loadWalletConfig } from 'utils/localStorage';
import { TokenBalanceLookup } from './wallet';
import { Token } from 'types/network';
export function* getTokenBalances(wallet: IWallet, tokens: Token[]) {
const node: INode = yield select(getNodeLib);
const address: string = yield apply(wallet, wallet.getAddressString);
const tokenBalances: TokenBalance[] = yield apply(node, node.getTokenBalances, [address, tokens]);
return tokens.reduce((acc, t, i) => {
acc[t.symbol] = tokenBalances[i];
return acc;
}, {});
}
// Return an array of the tokens that meet any of the following conditions:
// 1. Non-zero balance
// 2. It was in the previous wallet's config
// 3. It's a custom token that the user added
export function* filterScannedTokenBalances(wallet: IWallet, balances: TokenBalanceLookup) {
const customTokens: AppState['customTokens'] = yield select(getCustomTokens);
const oldConfig: WalletConfig = yield call(loadWalletConfig, wallet);
return Object.keys(balances).filter(symbol => {
if (balances[symbol] && !balances[symbol].balance.isZero()) {
return true;
}
if (oldConfig.tokens && oldConfig.tokens.includes(symbol)) {
return true;
}
if (customTokens.find(token => token.symbol === symbol)) {
return true;
}
});
}