mirror of
https://github.com/trezor/trezor-wallet
synced 2024-11-28 03:08:30 +00:00
move helper methods from TokensReducer to ./reducers/utils
This commit is contained in:
parent
31e3c7a9a2
commit
d9e809e6aa
@ -12,7 +12,7 @@ import { httpRequest } from 'utils/networkUtils';
|
|||||||
import * as buildUtils from 'utils/build';
|
import * as buildUtils from 'utils/build';
|
||||||
import * as storageUtils from 'utils/storage';
|
import * as storageUtils from 'utils/storage';
|
||||||
|
|
||||||
import { findAccountTokens } from 'reducers/TokensReducer';
|
import { getAccountTokens } from 'reducers/utils';
|
||||||
import type { Account } from 'reducers/AccountsReducer';
|
import type { Account } from 'reducers/AccountsReducer';
|
||||||
import type { Token } from 'reducers/TokensReducer';
|
import type { Token } from 'reducers/TokensReducer';
|
||||||
import type { Discovery } from 'reducers/DiscoveryReducer';
|
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 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)), []);
|
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)), []);
|
||||||
|
|
||||||
|
@ -210,7 +210,7 @@ export const observe = (prevState: State, action: Action): PayloadAction<boolean
|
|||||||
const account = reducerUtils.getSelectedAccount(state);
|
const account = reducerUtils.getSelectedAccount(state);
|
||||||
const network = reducerUtils.getSelectedNetwork(state);
|
const network = reducerUtils.getSelectedNetwork(state);
|
||||||
const discovery = reducerUtils.getDiscoveryProcess(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);
|
const pending = reducerUtils.getAccountPendingTx(state.pending, account);
|
||||||
|
|
||||||
// prepare new state for "selectedAccount" reducer
|
// prepare new state for "selectedAccount" reducer
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
import * as storageUtils from 'utils/storage';
|
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 EthereumSendFormState } from 'reducers/SendFormEthereumReducer';
|
||||||
import type { State as RippleSendFormState } from 'reducers/SendFormRippleReducer';
|
import type { State as RippleSendFormState } from 'reducers/SendFormRippleReducer';
|
||||||
|
@ -8,7 +8,6 @@ import * as NOTIFICATION from 'actions/constants/notification';
|
|||||||
import * as SEND from 'actions/constants/send';
|
import * as SEND from 'actions/constants/send';
|
||||||
import * as WEB3 from 'actions/constants/web3';
|
import * as WEB3 from 'actions/constants/web3';
|
||||||
import { initialState } from 'reducers/SendFormEthereumReducer';
|
import { initialState } from 'reducers/SendFormEthereumReducer';
|
||||||
import { findToken } from 'reducers/TokensReducer';
|
|
||||||
import * as reducerUtils from 'reducers/utils';
|
import * as reducerUtils from 'reducers/utils';
|
||||||
import * as ethUtils from 'utils/ethUtils';
|
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
|
// make sure that this token is added into account
|
||||||
const { account, tokens } = getState().selectedAccount;
|
const { account, tokens } = getState().selectedAccount;
|
||||||
if (!account) return;
|
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) {
|
if (!token) {
|
||||||
// token not found, re-init form
|
// token not found, re-init form
|
||||||
dispatch(init());
|
dispatch(init());
|
||||||
@ -466,7 +465,7 @@ export const onSend = (): AsyncAction => async (dispatch: Dispatch, getState: Ge
|
|||||||
|
|
||||||
const txData = await dispatch(prepareEthereumTx({
|
const txData = await dispatch(prepareEthereumTx({
|
||||||
network: network.shortcut,
|
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,
|
from: account.descriptor,
|
||||||
to: currentState.address,
|
to: currentState.address,
|
||||||
amount: currentState.amount,
|
amount: currentState.amount,
|
||||||
|
@ -3,8 +3,7 @@
|
|||||||
import BigNumber from 'bignumber.js';
|
import BigNumber from 'bignumber.js';
|
||||||
import EthereumjsUtil from 'ethereumjs-util';
|
import EthereumjsUtil from 'ethereumjs-util';
|
||||||
import EthereumjsUnits from 'ethereumjs-units';
|
import EthereumjsUnits from 'ethereumjs-units';
|
||||||
import { findToken } from 'reducers/TokensReducer';
|
import { findDevice, getPendingAmount, findToken } from 'reducers/utils';
|
||||||
import { findDevice, getPendingAmount } from 'reducers/utils';
|
|
||||||
import * as SEND from 'actions/constants/send';
|
import * as SEND from 'actions/constants/send';
|
||||||
import * as ethUtils from 'utils/ethUtils';
|
import * as ethUtils from 'utils/ethUtils';
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ import * as WALLET from 'actions/constants/wallet';
|
|||||||
import * as TOKEN from 'actions/constants/token';
|
import * as TOKEN from 'actions/constants/token';
|
||||||
|
|
||||||
import type { Action, TrezorDevice } from 'flowtype';
|
import type { Action, TrezorDevice } from 'flowtype';
|
||||||
import type { Account } from './AccountsReducer';
|
|
||||||
|
|
||||||
export type Token = {
|
export type Token = {
|
||||||
loaded: boolean;
|
loaded: boolean;
|
||||||
@ -24,11 +23,6 @@ export type State = Array<Token>;
|
|||||||
|
|
||||||
const initialState: State = [];
|
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 create = (state: State, token: Token): State => {
|
||||||
const newState: State = [...state];
|
const newState: State = [...state];
|
||||||
newState.push(token);
|
newState.push(token);
|
||||||
|
@ -101,10 +101,12 @@ export const getPendingAmount = (pending: Array<Transaction>, currency: string,
|
|||||||
return value;
|
return value;
|
||||||
}, new BigNumber('0'));
|
}, 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;
|
const a = account;
|
||||||
if (!a) return [];
|
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 => {
|
export const getWeb3 = (state: State): ?Web3Instance => {
|
||||||
|
Loading…
Reference in New Issue
Block a user