Skip to content

Commit

Permalink
fix(suite): reduce fetching new tokens time
Browse files Browse the repository at this point in the history
  • Loading branch information
enjojoy authored and tomasklim committed Jan 14, 2025
1 parent ded9397 commit 360b22b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 37 deletions.
47 changes: 14 additions & 33 deletions packages/suite/src/actions/wallet/tokenActions.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
import { TokenInfo } from '@trezor/connect';
import * as accountUtils from '@suite-common/wallet-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import { accountsActions, updateFiatRatesThunk } from '@suite-common/wallet-core';
import { Timestamp, TokenAddress } from '@suite-common/wallet-types';
import { FiatCurrencyCode } from '@suite-common/suite-config';
import { accountsActions } from '@suite-common/wallet-core';

import { Dispatch } from 'src/types/suite';
import { Account } from 'src/types/wallet';

export const addToken =
(account: Account, tokenInfo: TokenInfo[], localCurrency: FiatCurrencyCode) =>
(dispatch: Dispatch) => {
dispatch(
accountsActions.updateAccount({
...account,
tokens: (account.tokens || []).concat(accountUtils.enhanceTokens(tokenInfo)),
}),
);
export const addToken = (account: Account, tokenInfo: TokenInfo[]) => (dispatch: Dispatch) => {
dispatch(
accountsActions.updateAccount({
...account,
tokens: (account.tokens || []).concat(accountUtils.enhanceTokens(tokenInfo)),
}),
);

dispatch(
updateFiatRatesThunk({
tickers: [
{
symbol: account.symbol,
tokenAddress: tokenInfo[0].contract as TokenAddress,
},
],
localCurrency,
rateType: 'current',
fetchAttemptTimestamp: Date.now() as Timestamp,
forceFetchToken: true,
}),
);

dispatch(
notificationsActions.addToast({
type: 'add-token-success',
}),
);
};
dispatch(
notificationsActions.addToast({
type: 'add-token-success',
}),
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { Translation } from 'src/components/suite/Translation';
import { useDispatch, useSelector, useTranslation } from 'src/hooks/suite';
import { Account } from 'src/types/wallet';
import { selectSelectedAccount } from 'src/reducers/wallet/selectedAccountReducer';
import { selectLocalCurrency } from 'src/reducers/wallet/settingsReducer';

interface AddTokenModalProps {
onCancel: () => void;
Expand All @@ -23,7 +22,6 @@ export const AddTokenModal = ({ onCancel }: AddTokenModalProps) => {
const [isFetching, setIsFetching] = useState(false);
const [error, setError] = useState<string | null>(null);
const account = useSelector(selectSelectedAccount);
const localCurrency = useSelector(selectLocalCurrency);
const dispatch = useDispatch();
const { translationString } = useTranslation();

Expand Down Expand Up @@ -99,7 +97,7 @@ export const AddTokenModal = ({ onCancel }: AddTokenModalProps) => {

const handleAddTokenButtonClick = () => {
if (tokenInfo) {
dispatch(addToken(account, tokenInfo, localCurrency));
dispatch(addToken(account, tokenInfo));
onCancel();

analytics.report({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { fetchAllTransactionsForAccountThunk } from '../transactions/transaction

export const prepareFiatRatesMiddleware = createMiddlewareWithExtraDeps(
(action, { dispatch, extra, next, getState }) => {
next(action); //next must be at the beginning, othervise tickers are not going to be updated and fiat rates wont fetch (the user will have to wait for 1m timeout)
const {
actions: { setWalletSettingsLocalCurrency },
selectors: { selectLocalCurrency },
Expand Down Expand Up @@ -126,6 +127,6 @@ export const prepareFiatRatesMiddleware = createMiddlewareWithExtraDeps(
);
}

return next(action);
return action;
},
);

0 comments on commit 360b22b

Please sign in to comment.