From 1392726d11396c5c98f19050f898470c9fb3f939 Mon Sep 17 00:00:00 2001 From: tomasklim Date: Tue, 6 Feb 2024 15:37:21 +0100 Subject: [PATCH] fixup! feat(suite): fetch token definitions for each token of an account --- suite-common/wallet-core/src/index.ts | 4 +- .../tokenDefinitionsMiddleware.ts | 2 +- .../tokenDefinitionsReducer.ts | 44 ++++++++++++------- ...ionsThunk.ts => tokenDefinitionsThunks.ts} | 18 +++++--- 4 files changed, 43 insertions(+), 25 deletions(-) rename suite-common/wallet-core/src/token-definitions/{tokenDefinitionsThunk.ts => tokenDefinitionsThunks.ts} (63%) diff --git a/suite-common/wallet-core/src/index.ts b/suite-common/wallet-core/src/index.ts index bcd5aa6cd50..cbdbe8a2f47 100644 --- a/suite-common/wallet-core/src/index.ts +++ b/suite-common/wallet-core/src/index.ts @@ -23,7 +23,7 @@ export * from './device/deviceActions'; export * from './device/deviceThunks'; export * from './device/deviceReducer'; export * from './device/deviceConstants'; -export * from './token-definitions/tokenDefinitionsActions'; +export * from './token-definitions/tokenDefinitionsSelectors'; export * from './token-definitions/tokenDefinitionsReducer'; -export * from './token-definitions/tokenDefinitionsThunk'; +export * from './token-definitions/tokenDefinitionsThunks'; export * from './token-definitions/tokenDefinitionsMiddleware'; diff --git a/suite-common/wallet-core/src/token-definitions/tokenDefinitionsMiddleware.ts b/suite-common/wallet-core/src/token-definitions/tokenDefinitionsMiddleware.ts index 0faae8a8ccf..0f52d8d65d0 100644 --- a/suite-common/wallet-core/src/token-definitions/tokenDefinitionsMiddleware.ts +++ b/suite-common/wallet-core/src/token-definitions/tokenDefinitionsMiddleware.ts @@ -1,7 +1,7 @@ import { createMiddlewareWithExtraDeps } from '@suite-common/redux-utils'; import { accountsActions } from '../accounts/accountsActions'; -import { getTokenDefinitionThunk } from './tokenDefinitionsThunk'; +import { getTokenDefinitionThunk } from './tokenDefinitionsThunks'; import { selectSpecificTokenDefinition } from './tokenDefinitionsSelectors'; export const prepareTokenDefinitionsMiddleware = createMiddlewareWithExtraDeps( diff --git a/suite-common/wallet-core/src/token-definitions/tokenDefinitionsReducer.ts b/suite-common/wallet-core/src/token-definitions/tokenDefinitionsReducer.ts index 977d4e2c271..e186d63c645 100644 --- a/suite-common/wallet-core/src/token-definitions/tokenDefinitionsReducer.ts +++ b/suite-common/wallet-core/src/token-definitions/tokenDefinitionsReducer.ts @@ -1,6 +1,6 @@ import { createReducerWithExtraDeps } from '@suite-common/redux-utils'; -import { getTokenDefinitionThunk } from './tokenDefinitionsThunk'; +import { getTokenDefinitionThunk } from './tokenDefinitionsThunks'; import { TokenDefinitionsState } from './tokenDefinitionsTypes'; const initialStatePredefined: Partial = {}; @@ -16,11 +16,15 @@ export const prepareTokenDefinitionsReducer = createReducerWithExtraDeps( state[network.symbol] = {}; } - state[network.symbol]![contractAddress] = { - isTokenKnown: undefined, - isLoading: true, - error: false, - }; + const networkDefinitions = state[network.symbol]; + + if (networkDefinitions) { + networkDefinitions[contractAddress] = { + isTokenKnown: undefined, + isLoading: true, + error: false, + }; + } }) .addCase(getTokenDefinitionThunk.fulfilled, (state, action) => { const { network, contractAddress } = action.meta.arg; @@ -29,11 +33,15 @@ export const prepareTokenDefinitionsReducer = createReducerWithExtraDeps( state[network.symbol] = {}; } - state[network.symbol]![contractAddress] = { - isTokenKnown: true, - isLoading: false, - error: false, - }; + const networkDefinitions = state[network.symbol]; + + if (networkDefinitions) { + networkDefinitions[contractAddress] = { + isTokenKnown: true, + isLoading: false, + error: false, + }; + } }) .addCase(getTokenDefinitionThunk.rejected, (state, action) => { const { network, contractAddress } = action.meta.arg; @@ -42,11 +50,15 @@ export const prepareTokenDefinitionsReducer = createReducerWithExtraDeps( state[network.symbol] = {}; } - state[network.symbol]![contractAddress] = { - isTokenKnown: undefined, - isLoading: false, - error: true, - }; + const networkDefinitions = state[network.symbol]; + + if (networkDefinitions) { + networkDefinitions[contractAddress] = { + isTokenKnown: undefined, + isLoading: false, + error: true, + }; + } }); }, ); diff --git a/suite-common/wallet-core/src/token-definitions/tokenDefinitionsThunk.ts b/suite-common/wallet-core/src/token-definitions/tokenDefinitionsThunks.ts similarity index 63% rename from suite-common/wallet-core/src/token-definitions/tokenDefinitionsThunk.ts rename to suite-common/wallet-core/src/token-definitions/tokenDefinitionsThunks.ts index e8fa74bdfe4..10767ff050a 100644 --- a/suite-common/wallet-core/src/token-definitions/tokenDefinitionsThunk.ts +++ b/suite-common/wallet-core/src/token-definitions/tokenDefinitionsThunks.ts @@ -7,22 +7,28 @@ const actionsPrefix = '@common/wallet-core/token-definitions'; export const getTokenDefinitionThunk = createThunk( `${actionsPrefix}/getTokenDefinition`, - async (params: { network: Network; contractAddress: string }, { getState }) => { + async ( + params: { network: Network; contractAddress: string }, + { getState, rejectWithValue }, + ) => { const { network, contractAddress } = params; - const { isTokenKnown, isLoading } = + const { isTokenKnown } = selectSpecificTokenDefinition(getState(), network.symbol, contractAddress) || {}; - // if token was never fetched and is not loading right now - if (isTokenKnown === undefined && !isLoading) { + if (isTokenKnown === undefined) { try { - await fetch( + const fetchedTokenDefinition = await fetch( `https://data.trezor.io/firmware/eth-definitions/chain-id/${ network.chainId }/token-${contractAddress.substring(2).toLowerCase()}.dat`, { method: 'HEAD' }, ); + + if (!fetchedTokenDefinition.ok) { + return rejectWithValue('Token definition not found or unavailable'); + } } catch (error) { - console.error(error); + return rejectWithValue(error.toString()); } } },