load all Tokens on discovery

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

@ -1,7 +1,9 @@
/* @flow */
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 { State, Token } from 'reducers/TokensReducer';
import type { Account } from 'reducers/AccountsReducer';
@ -60,21 +62,19 @@ export const setBalance = (
ethAddress: string,
balance: string
): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const newState: Array<Token> = [...getState().tokens];
const token: ?Token = newState.find(
t => t.address === tokenAddress && t.ethAddress === ethAddress
);
if (token) {
const others = newState.filter(t => t !== token);
const { tokens } = getState();
const index = tokens.findIndex(t => t.address === tokenAddress && t.ethAddress === ethAddress);
if (index >= 0) {
const token = tokens[index];
const payload = tokens.slice();
payload[index] = {
...token,
loaded: true,
balance,
};
dispatch({
type: TOKEN.SET_BALANCE,
payload: others.concat([
{
...token,
loaded: true,
balance,
},
]),
payload,
});
}
};
@ -103,6 +103,27 @@ export const add = (token: NetworkToken, account: Account): AsyncAction => async
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 => ({
type: TOKEN.REMOVE,
token,

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

Loading…
Cancel
Save