Skip to content

Commit

Permalink
fixup! feat(suite): fetch token definitions for each token of an account
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklim committed Feb 6, 2024
1 parent dc437c6 commit 1392726
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
4 changes: 2 additions & 2 deletions suite-common/wallet-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TokenDefinitionsState> = {};
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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,
};
}
});
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
},
Expand Down

0 comments on commit 1392726

Please sign in to comment.