Skip to content

Commit

Permalink
Fix small balance threshold not being reactive (#5865)
Browse files Browse the repository at this point in the history
* fix for small balance threshold not being updated

* Apply suggestions from code review

* fix lint

* change to consume from static supportedCurrencies

* fix to support coinlist

* Update the asset amount thresholds

---------

Co-authored-by: jinchung <[email protected]>
  • Loading branch information
walmat and jinchung authored Jun 18, 2024
1 parent a0a06be commit 8217180
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/references/supportedCurrencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.ETH),
mask: '[09999999999]{.}[999999999999999999]',
placeholder: '0',
userAssetsSmallThreshold: 0.000005,
smallThreshold: 0.003,
symbol: 'Ξ',
glyph: 'Ξ',
Expand All @@ -24,6 +25,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.USD),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 0.05,
smallThreshold: 1,
symbol: '$',
glyph: '$',
Expand All @@ -38,6 +40,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.EUR),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 0.05,
smallThreshold: 1,
symbol: '€',
glyph: '€',
Expand All @@ -52,6 +55,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.GBP),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 0.05,
smallThreshold: 1,
symbol: '£',
glyph: '£',
Expand All @@ -66,6 +70,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.AUD),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 0.05,
smallThreshold: 1,
symbol: 'A$',
glyph: '$',
Expand All @@ -80,6 +85,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.CNY),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 1,
smallThreshold: 5,
symbol: '¥',
glyph: '¥',
Expand All @@ -94,6 +100,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.KRW),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 100,
smallThreshold: 1000,
symbol: '₩',
glyph: '₩',
Expand All @@ -108,6 +115,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.RUB),
mask: '[099999999999]{,}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 10,
smallThreshold: 75,
symbol: '₽',
glyph: '₽',
Expand All @@ -122,6 +130,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.INR),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 10,
smallThreshold: 75,
symbol: '₹',
glyph: '₹',
Expand All @@ -136,6 +145,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.JPY),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 10,
smallThreshold: 100,
symbol: '¥',
glyph: '¥',
Expand All @@ -150,6 +160,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.TRY),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 4,
smallThreshold: 8,
symbol: '₺',
glyph: '₺',
Expand All @@ -164,6 +175,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.CAD),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 0.05,
smallThreshold: 1,
symbol: 'CA$',
glyph: '$',
Expand All @@ -178,6 +190,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.NZD),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 0.1,
smallThreshold: 1.5,
symbol: 'NZ$',
glyph: '$',
Expand All @@ -192,6 +205,7 @@ export const supportedCurrencies = {
label: i18n.t(i18n.l.settings.currency.ZAR),
mask: '[099999999999]{.}[00]',
placeholder: '0.00',
userAssetsSmallThreshold: 2,
smallThreshold: 15,
symbol: 'R',
glyph: 'R',
Expand Down
3 changes: 0 additions & 3 deletions src/screens/SettingsSheet/components/CurrencySection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import MenuItem from './MenuItem';
import { analytics } from '@/analytics';
import { useAccountSettings } from '@/hooks';
import { ETH_ADDRESS, WBTC_ADDRESS, emojis, supportedNativeCurrencies } from '@/references';
import { BackgroundProvider, Box, Inline, Inset, Text } from '@/design-system';
import { SimpleSheet } from '@/components/sheet/SimpleSheet';
import * as i18n from '@/languages';
import { Network } from '@/networks/types';
import { useExternalToken } from '@/resources/assets/externalAssetsQuery';
import RainbowCoinIcon from '@/components/coin-icon/RainbowCoinIcon';
Expand Down
12 changes: 8 additions & 4 deletions src/state/assets/userAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { Address } from 'viem';
import { RainbowError, logger } from '@/logger';
import { createRainbowStore } from '@/state/internal/createRainbowStore';
import store from '@/redux/store';
import { SUPPORTED_CHAIN_IDS } from '@/references';
import { SUPPORTED_CHAIN_IDS, supportedNativeCurrencies } from '@/references';
import { ParsedSearchAsset, UniqueId, UserAssetFilter } from '@/__swaps__/types/assets';
import { ChainId } from '@/__swaps__/types/chains';

const SEARCH_CACHE_MAX_ENTRIES = 50;
const SMALL_BALANCE_THRESHOLD = store.getState().settings.nativeCurrency === 'ETH' ? 0.000005 : 0.02;

const getSearchQueryKey = ({ filter, searchQuery }: { filter: UserAssetFilter; searchQuery: string }) => `${filter}${searchQuery}`;

Expand Down Expand Up @@ -135,6 +134,8 @@ export const userAssetsStore = createRainbowStore<UserAssetsState>(
getFilteredUserAssetIds: () => {
const { filter, inputSearchQuery: rawSearchQuery, selectUserAssetIds, setSearchCache } = get();

const smallBalanceThreshold = supportedNativeCurrencies[store.getState().settings.nativeCurrency].userAssetsSmallThreshold;

const inputSearchQuery = rawSearchQuery.trim().toLowerCase();
const queryKey = getSearchQueryKey({ filter, searchQuery: inputSearchQuery });

Expand All @@ -151,7 +152,7 @@ export const userAssetsStore = createRainbowStore<UserAssetsState>(
const filteredIds = Array.from(
selectUserAssetIds(
asset =>
(+asset.native?.balance?.amount ?? 0) > SMALL_BALANCE_THRESHOLD &&
(+asset.native?.balance?.amount ?? 0) > smallBalanceThreshold &&
(!chainIdFilter || asset.chainId === chainIdFilter) &&
(!searchRegex || searchRegex.test(asset.name) || searchRegex.test(asset.symbol) || searchRegex.test(asset.address)),
filter
Expand All @@ -174,6 +175,7 @@ export const userAssetsStore = createRainbowStore<UserAssetsState>(
const { currentAbortController, idsByChain, userAssets } = get();

const assetIds = filter ? idsByChain.get(filter) || [] : idsByChain.get('all') || [];

for (const id of assetIds) {
if (currentAbortController?.signal.aborted) {
return;
Expand Down Expand Up @@ -264,9 +266,11 @@ export const userAssetsStore = createRainbowStore<UserAssetsState>(

idsByChain.set('all', allIdsArray);

const smallBalanceThreshold = supportedNativeCurrencies[store.getState().settings.nativeCurrency].userAssetsSmallThreshold;

const filteredAllIdsArray = allIdsArray.filter(id => {
const asset = userAssetsMap.get(id);
return asset && (+asset.native?.balance?.amount ?? 0) > SMALL_BALANCE_THRESHOLD;
return asset && (+asset.native?.balance?.amount ?? 0) > smallBalanceThreshold;
});

const searchCache = new Map<string, UniqueId[]>();
Expand Down

0 comments on commit 8217180

Please sign in to comment.