From 05598a248b38aaf637e89c5747afa23de627daa7 Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Tue, 31 Mar 2020 11:36:51 +0200 Subject: [PATCH] load all Tokens on discovery --- src/actions/TokenActions.js | 47 ++++++++++++++----- src/actions/ethereum/DiscoveryActions.js | 11 ++++- .../views/Account/Summary/ethereum/index.js | 2 +- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/actions/TokenActions.js b/src/actions/TokenActions.js index 409360c6..03f518bf 100644 --- a/src/actions/TokenActions.js +++ b/src/actions/TokenActions.js @@ -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 => { - const newState: Array = [...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, diff --git a/src/actions/ethereum/DiscoveryActions.js b/src/actions/ethereum/DiscoveryActions.js index 85864a31..563d4143 100644 --- a/src/actions/ethereum/DiscoveryActions.js +++ b/src/actions/ethereum/DiscoveryActions.js @@ -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; }; diff --git a/src/views/Wallet/views/Account/Summary/ethereum/index.js b/src/views/Wallet/views/Account/Summary/ethereum/index.js index ce29d448..ef144e40 100644 --- a/src/views/Wallet/views/Account/Summary/ethereum/index.js +++ b/src/views/Wallet/views/Account/Summary/ethereum/index.js @@ -173,7 +173,7 @@ const AccountSummary = (props: Props) => { {tokens.length < 1 && } {tokens.map(token => (