1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-13 20:08:56 +00:00

move helper methods from TokensReducer to ./reducers/utils

This commit is contained in:
Szymon Lesisz 2018-12-21 11:19:40 +01:00
parent 31e3c7a9a2
commit d9e809e6aa
7 changed files with 11 additions and 17 deletions

View File

@ -12,7 +12,7 @@ import { httpRequest } from 'utils/networkUtils';
import * as buildUtils from 'utils/build';
import * as storageUtils from 'utils/storage';
import { findAccountTokens } from 'reducers/TokensReducer';
import { getAccountTokens } from 'reducers/utils';
import type { Account } from 'reducers/AccountsReducer';
import type { Token } from 'reducers/TokensReducer';
import type { Discovery } from 'reducers/DiscoveryReducer';
@ -59,7 +59,7 @@ const KEY_BETA_MODAL: string = '/betaModalPrivacy'; // this key needs to be comp
const findAccounts = (devices: Array<TrezorDevice>, accounts: Array<Account>): Array<Account> => devices.reduce((arr, dev) => arr.concat(accounts.filter(a => a.deviceState === dev.state)), []);
const findTokens = (accounts: Array<Account>, tokens: Array<Token>): Array<Token> => accounts.reduce((arr, account) => arr.concat(findAccountTokens(tokens, account)), []);
const findTokens = (accounts: Array<Account>, tokens: Array<Token>): Array<Token> => accounts.reduce((arr, account) => arr.concat(getAccountTokens(tokens, account)), []);
const findDiscovery = (devices: Array<TrezorDevice>, discovery: Array<Discovery>): Array<Discovery> => devices.reduce((arr, dev) => arr.concat(discovery.filter(d => d.deviceState === dev.state && d.completed)), []);

View File

@ -210,7 +210,7 @@ export const observe = (prevState: State, action: Action): PayloadAction<boolean
const account = reducerUtils.getSelectedAccount(state);
const network = reducerUtils.getSelectedNetwork(state);
const discovery = reducerUtils.getDiscoveryProcess(state);
const tokens = reducerUtils.getAccountTokens(state, account);
const tokens = reducerUtils.getAccountTokens(state.tokens, account);
const pending = reducerUtils.getAccountPendingTx(state.pending, account);
// prepare new state for "selectedAccount" reducer

View File

@ -1,6 +1,6 @@
/* @flow */
import * as storageUtils from 'utils/storage';
import { findToken } from 'reducers/TokensReducer';
import { findToken } from 'reducers/utils';
import type { State as EthereumSendFormState } from 'reducers/SendFormEthereumReducer';
import type { State as RippleSendFormState } from 'reducers/SendFormRippleReducer';

View File

@ -8,7 +8,6 @@ import * as NOTIFICATION from 'actions/constants/notification';
import * as SEND from 'actions/constants/send';
import * as WEB3 from 'actions/constants/web3';
import { initialState } from 'reducers/SendFormEthereumReducer';
import { findToken } from 'reducers/TokensReducer';
import * as reducerUtils from 'reducers/utils';
import * as ethUtils from 'utils/ethUtils';
@ -76,7 +75,7 @@ export const observe = (prevState: ReducersState, action: Action): ThunkAction =
// make sure that this token is added into account
const { account, tokens } = getState().selectedAccount;
if (!account) return;
const token = findToken(tokens, account.descriptor, currentState.sendFormEthereum.currency, account.deviceState);
const token = reducerUtils.findToken(tokens, account.descriptor, currentState.sendFormEthereum.currency, account.deviceState);
if (!token) {
// token not found, re-init form
dispatch(init());
@ -466,7 +465,7 @@ export const onSend = (): AsyncAction => async (dispatch: Dispatch, getState: Ge
const txData = await dispatch(prepareEthereumTx({
network: network.shortcut,
token: isToken ? findToken(getState().tokens, account.descriptor, currentState.currency, account.deviceState) : null,
token: isToken ? reducerUtils.findToken(getState().tokens, account.descriptor, currentState.currency, account.deviceState) : null,
from: account.descriptor,
to: currentState.address,
amount: currentState.amount,

View File

@ -3,8 +3,7 @@
import BigNumber from 'bignumber.js';
import EthereumjsUtil from 'ethereumjs-util';
import EthereumjsUnits from 'ethereumjs-units';
import { findToken } from 'reducers/TokensReducer';
import { findDevice, getPendingAmount } from 'reducers/utils';
import { findDevice, getPendingAmount, findToken } from 'reducers/utils';
import * as SEND from 'actions/constants/send';
import * as ethUtils from 'utils/ethUtils';

View File

@ -6,7 +6,6 @@ import * as WALLET from 'actions/constants/wallet';
import * as TOKEN from 'actions/constants/token';
import type { Action, TrezorDevice } from 'flowtype';
import type { Account } from './AccountsReducer';
export type Token = {
loaded: boolean;
@ -24,11 +23,6 @@ export type State = Array<Token>;
const initialState: State = [];
// Helper for actions
export const findToken = (state: Array<Token>, address: string, symbol: string, deviceState: string): ?Token => state.find(t => t.ethAddress === address && t.symbol === symbol && t.deviceState === deviceState);
export const findAccountTokens = (state: Array<Token>, account: Account): Array<Token> => state.filter(t => t.ethAddress === account.descriptor && t.network === account.network && t.deviceState === account.deviceState);
const create = (state: State, token: Token): State => {
const newState: State = [...state];
newState.push(token);

View File

@ -101,10 +101,12 @@ export const getPendingAmount = (pending: Array<Transaction>, currency: string,
return value;
}, new BigNumber('0'));
export const getAccountTokens = (state: State, account: ?Account): Array<Token> => {
export const findToken = (state: Array<Token>, address: string, symbol: string, deviceState: string): ?Token => state.find(t => t.ethAddress === address && t.symbol === symbol && t.deviceState === deviceState);
export const getAccountTokens = (tokens: Array<Token>, account: ?Account): Array<Token> => {
const a = account;
if (!a) return [];
return state.tokens.filter(t => t.ethAddress === a.descriptor && t.network === a.network && t.deviceState === a.deviceState);
return tokens.filter(t => t.ethAddress === a.descriptor && t.network === a.network && t.deviceState === a.deviceState);
};
export const getWeb3 = (state: State): ?Web3Instance => {