load all Tokens on discovery

tmp/erc20-autoload
Szymon Lesisz 4 years ago
parent d70afe0d04
commit 05598a248b

@ -1,7 +1,9 @@
/* @flow */ /* @flow */
import * as TOKEN from 'actions/constants/token'; import * as TOKEN from 'actions/constants/token';
import { toDecimalAmount } from 'utils/formatUtils';
import type { TokenInfo } from 'trezor-connect';
import type { GetState, AsyncAction, Action, Dispatch } from 'flowtype'; import type { GetState, AsyncAction, Action, Dispatch } from 'flowtype';
import type { State, Token } from 'reducers/TokensReducer'; import type { State, Token } from 'reducers/TokensReducer';
import type { Account } from 'reducers/AccountsReducer'; import type { Account } from 'reducers/AccountsReducer';
@ -60,21 +62,19 @@ export const setBalance = (
ethAddress: string, ethAddress: string,
balance: string balance: string
): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => { ): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const newState: Array<Token> = [...getState().tokens]; const { tokens } = getState();
const token: ?Token = newState.find( const index = tokens.findIndex(t => t.address === tokenAddress && t.ethAddress === ethAddress);
t => t.address === tokenAddress && t.ethAddress === ethAddress if (index >= 0) {
); const token = tokens[index];
if (token) { const payload = tokens.slice();
const others = newState.filter(t => t !== token); payload[index] = {
...token,
loaded: true,
balance,
};
dispatch({ dispatch({
type: TOKEN.SET_BALANCE, type: TOKEN.SET_BALANCE,
payload: others.concat([ payload,
{
...token,
loaded: true,
balance,
},
]),
}); });
} }
}; };
@ -103,6 +103,27 @@ export const add = (token: NetworkToken, account: Account): AsyncAction => async
dispatch(setBalance(token.address, account.descriptor, tokenBalance)); dispatch(setBalance(token.address, account.descriptor, tokenBalance));
}; };
export const createAccountTokens = (account: Account, tokens: TokenInfo[]) => async (
dispatch: Dispatch
) => {
tokens.forEach(t => {
dispatch({
type: TOKEN.ADD,
payload: {
address: t.address,
balance: toDecimalAmount(t.balance || '0', t.decimals),
decimals: t.decimals,
deviceState: account.deviceState,
ethAddress: account.descriptor,
loaded: true,
name: t.name || '',
network: account.network,
symbol: t.symbol || '',
},
});
});
};
export const remove = (token: Token): Action => ({ export const remove = (token: Token): Action => ({
type: TOKEN.REMOVE, type: TOKEN.REMOVE,
token, token,

@ -2,6 +2,7 @@
import TrezorConnect from 'trezor-connect'; import TrezorConnect from 'trezor-connect';
import * as DISCOVERY from 'actions/constants/discovery'; import * as DISCOVERY from 'actions/constants/discovery';
import { createAccountTokens } from 'actions/TokenActions';
import { enhanceAccount } from 'utils/accountUtils'; import { enhanceAccount } from 'utils/accountUtils';
import type { PromiseAction, Dispatch, GetState, TrezorDevice, Network, Account } from 'flowtype'; import type { PromiseAction, Dispatch, GetState, TrezorDevice, Network, Account } from 'flowtype';
import type { Discovery } from 'reducers/DiscoveryReducer'; import type { Discovery } from 'reducers/DiscoveryReducer';
@ -41,7 +42,7 @@ export const discoverAccount = (
state: device.state, state: device.state,
}, },
path, path,
// details: 'tokenBalances', TODO: load ERC20 details: 'tokenBalances',
pageSize: 1, pageSize: 1,
keepSession: true, // acquire and hold session keepSession: true, // acquire and hold session
useEmptyPassphrase: device.useEmptyPassphrase, useEmptyPassphrase: device.useEmptyPassphrase,
@ -53,9 +54,15 @@ export const discoverAccount = (
throw new Error(response.payload.error); throw new Error(response.payload.error);
} }
return enhanceAccount(response.payload, { const account = enhanceAccount(response.payload, {
index: discoveryProcess.accountIndex, index: discoveryProcess.accountIndex,
network, network,
device, device,
}); });
if (response.payload.tokens) {
dispatch(createAccountTokens(account, response.payload.tokens));
}
return account;
}; };

@ -173,7 +173,7 @@ const AccountSummary = (props: Props) => {
{tokens.length < 1 && <AddTokenMessage />} {tokens.length < 1 && <AddTokenMessage />}
{tokens.map(token => ( {tokens.map(token => (
<AddedToken <AddedToken
key={token.symbol} key={token.address}
token={token} token={token}
pending={pending} pending={pending}
removeToken={props.removeToken} removeToken={props.removeToken}

Loading…
Cancel
Save