Skip to content

Commit

Permalink
feat(suite-native): make token selectors network agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
vytick committed Oct 1, 2024
1 parent 2187dc0 commit b903214
Show file tree
Hide file tree
Showing 30 changed files with 293 additions and 331 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
EthereumTokenAmountFormatter,
EthereumTokenToFiatAmountFormatter,
} from '@suite-native/formatters';
import { getEthereumTokenName, selectEthereumAccountTokenSymbol } from '@suite-native/tokens';
import { getTokenName, selectAccountTokenSymbol } from '@suite-native/tokens';

import { AccountsListItemBase } from './AccountsListItemBase';

Expand All @@ -30,7 +30,7 @@ export const AccountsListTokenItem = ({
isLast,
}: AccountListTokenItemProps) => {
const tokenSymbol = useSelector((state: AccountsRootState) =>
selectEthereumAccountTokenSymbol(state, account.key, token.contract),
selectAccountTokenSymbol(state, account.key, token.contract),
);
const balance = token.balance ?? '0';

Expand All @@ -41,7 +41,7 @@ export const AccountsListTokenItem = ({
isLast={isLast}
onPress={onSelectAccount}
icon={<RoundedIcon name={token.contract} />}
title={getEthereumTokenName(token.name)}
title={getTokenName(token.name)}
mainValue={
<EthereumTokenToFiatAmountFormatter value={balance} contract={token.contract} />
}
Expand Down
4 changes: 2 additions & 2 deletions suite-native/assets/src/components/AssetItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {
RootStackRoutes,
TabToStackCompositeNavigationProp,
} from '@suite-native/navigation';
import { selectHasDeviceAnyTokens } from '@suite-native/tokens';
import {
selectHasAnyDeviceAccountsWithStaking,
NativeStakingRootState,
} from '@suite-native/staking';
import { selectHasDeviceAnyTokensForNetwork } from '@suite-native/tokens';

import {
AssetsRootState,
Expand Down Expand Up @@ -91,7 +91,7 @@ export const AssetItem = React.memo(({ cryptoCurrencySymbol, onPress }: AssetIte
const accountsPerAsset = accountsKeysForNetworkSymbol.length;
const hasAnyTokens = useSelector(
(state: AccountsRootState & DeviceRootState & TokenDefinitionsRootState) =>
selectHasDeviceAnyTokens(state, cryptoCurrencySymbol),
selectHasDeviceAnyTokensForNetwork(state, cryptoCurrencySymbol),
);
const hasAnyAccountsWithStaking = useSelector((state: NativeStakingRootState) =>
selectHasAnyDeviceAccountsWithStaking(state, cryptoCurrencySymbol),
Expand Down
9 changes: 3 additions & 6 deletions suite-native/graph/src/components/TransactionEventTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import { SignValue } from '@suite-common/suite-types';
import { NetworkSymbol, getNetworkType } from '@suite-common/wallet-config';
import { AccountKey, TokenAddress } from '@suite-common/wallet-types';
import { AccountsRootState } from '@suite-common/wallet-core';
import {
selectEthereumAccountTokenInfo,
selectEthereumAccountTokenSymbol,
} from '@suite-native/tokens';
import { selectAccountTokenInfo, selectAccountTokenSymbol } from '@suite-native/tokens';

export type TransactionEventTooltipProps =
EventTooltipComponentProps<GroupedBalanceMovementEventPayload>;
Expand Down Expand Up @@ -71,10 +68,10 @@ const TokenAmountTooltipFormatter = ({
value: number;
}) => {
const tokenInfo = useSelector((state: AccountsRootState) =>
selectEthereumAccountTokenInfo(state, accountKey, tokenAddress),
selectAccountTokenInfo(state, accountKey, tokenAddress),
);
const tokenSymbol = useSelector((state: AccountsRootState) =>
selectEthereumAccountTokenSymbol(state, accountKey, tokenAddress),
selectAccountTokenSymbol(state, accountKey, tokenAddress),
);
const tokenDecimals = tokenInfo?.decimals;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NetworkSymbol } from '@suite-common/wallet-config';
import { AccountsRootState, selectAccountByKey } from '@suite-common/wallet-core';
import { AccountKey, TokenAddress, TokenSymbol } from '@suite-common/wallet-types';
import { DiscreetTextTrigger, VStack } from '@suite-native/atoms';
import { selectEthereumAccountTokenSymbol } from '@suite-native/tokens';
import { selectAccountTokenSymbol } from '@suite-native/tokens';
import { GraphFiatBalance } from '@suite-native/graph';

import { AccountDetailCryptoValue } from './AccountDetailCryptoValue';
Expand Down Expand Up @@ -52,7 +52,7 @@ export const AccountDetailGraphHeader = ({ accountKey, tokenAddress }: AccountBa
selectAccountByKey(state, accountKey),
);
const tokenSymbol = useSelector((state: AccountsRootState) =>
selectEthereumAccountTokenSymbol(state, accountKey, tokenAddress),
selectAccountTokenSymbol(state, accountKey, tokenAddress),
);
const setPoint = useSetAtom(selectedPointAtom);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
selectAccountByKey,
selectIsPortfolioTrackerDevice,
} from '@suite-common/wallet-core';
import { isEthereumAccountSymbol } from '@suite-common/wallet-utils';
import {
AppTabsParamList,
RootStackParamList,
Expand All @@ -23,6 +22,7 @@ import {
import { Translation } from '@suite-native/intl';
import { FeatureFlag, useFeatureFlag } from '@suite-native/feature-flags';
import { NetworkSymbol } from '@suite-common/wallet-config';
import { isCoinWithTokens } from '@suite-native/tokens';

import { AccountDetailGraph } from './AccountDetailGraph';
import { AccountDetailCryptoValue } from './AccountDetailCryptoValue';
Expand Down Expand Up @@ -132,7 +132,7 @@ export const TransactionListHeader = memo(
};

const isTokenDetail = !!tokenContract;
const isEthereumAccountDetail = !isTokenDetail && isEthereumAccountSymbol(account.symbol);
const canHaveTokens = !isTokenDetail && isCoinWithTokens(account.symbol);
const isPriceCardDisplayed = !isTestnetAccount && !isTokenDetail;

const isSendButtonDisplayed =
Expand Down Expand Up @@ -181,7 +181,7 @@ export const TransactionListHeader = memo(
)}
</VStack>

{isEthereumAccountDetail && accountHasTransactions && (
{canHaveTokens && accountHasTransactions && (
<IncludeTokensToggle
isToggled={areTokensIncluded}
onToggle={toggleIncludeTokenTransactions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@suite-common/wallet-core';
import { TokenAddress } from '@suite-common/wallet-types';
import { EventType, analytics } from '@suite-native/analytics';
import { selectEthereumAccountTokenInfo } from '@suite-native/tokens';
import { selectAccountTokenInfo } from '@suite-native/tokens';
import { Screen } from '@suite-native/navigation';
import { TransactionList } from '@suite-native/transactions';

Expand All @@ -34,7 +34,7 @@ export const AccountDetailContentScreen = ({
);

const token = useSelector((state: AccountsRootState) =>
selectEthereumAccountTokenInfo(state, accountKey, tokenContract),
selectAccountTokenInfo(state, accountKey, tokenContract),
);

useEffect(() => {
Expand Down
12 changes: 6 additions & 6 deletions suite-native/receive/src/components/TokenReceiveCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { prepareNativeStyle, useNativeStyles } from '@trezor/styles';
import { AccountKey, TokenAddress } from '@suite-common/wallet-types';
import { AccountsRootState, selectAccountLabel } from '@suite-common/wallet-core';
import {
getEthereumTokenName,
selectEthereumAccountTokenInfo,
selectEthereumAccountTokenSymbol,
getTokenName,
selectAccountTokenInfo,
selectAccountTokenSymbol,
} from '@suite-native/tokens';

type TokenReceiveCardProps = {
Expand All @@ -38,16 +38,16 @@ export const TokenReceiveCard = ({ contract, accountKey }: TokenReceiveCardProps
);

const token = useSelector((state: AccountsRootState) =>
selectEthereumAccountTokenInfo(state, accountKey, contract),
selectAccountTokenInfo(state, accountKey, contract),
);

const tokenSymbol = useSelector((state: AccountsRootState) =>
selectEthereumAccountTokenSymbol(state, accountKey, contract),
selectAccountTokenSymbol(state, accountKey, contract),
);

if (!token) return <ErrorMessage errorMessage="Token not found." />;

const tokenName = getEthereumTokenName(token.name);
const tokenName = getTokenName(token.name);

return (
<VStack>
Expand Down
8 changes: 4 additions & 4 deletions suite-native/receive/src/screens/ReceiveModalScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
selectAccountNetworkSymbol,
selectDeviceAccountKeyForNetworkSymbolAndAccountTypeWithIndex,
} from '@suite-common/wallet-core';
import { selectEthereumAccountTokenSymbol } from '@suite-native/tokens';
import { selectAccountTokenSymbol } from '@suite-native/tokens';
import { CryptoIcon } from '@suite-common/icons-deprecated';
import { prepareNativeStyle, useNativeStyles } from '@trezor/styles';

Expand Down Expand Up @@ -70,8 +70,8 @@ const ReceiveModalScreenSubHeader = ({ accountKey, tokenContract }: ScreenSubHea
const networkSymbol = useSelector((state: AccountsRootState) =>
selectAccountNetworkSymbol(state, accountKey),
);
const ethereumTokenSymbol = useSelector((state: AccountsRootState) =>
selectEthereumAccountTokenSymbol(state, accountKey, tokenContract),
const tokenSymbol = useSelector((state: AccountsRootState) =>
selectAccountTokenSymbol(state, accountKey, tokenContract),
);

useEffect(() => {
Expand All @@ -98,7 +98,7 @@ const ReceiveModalScreenSubHeader = ({ accountKey, tokenContract }: ScreenSubHea
{accountLabel && (
<Text variant="highlight">
{accountLabel}
{ethereumTokenSymbol && ` - ${ethereumTokenSymbol}`}
{tokenSymbol && ` - ${tokenSymbol}`}
</Text>
)}
</HStack>
Expand Down
1 change: 1 addition & 0 deletions suite-native/state/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@suite-native/module-send": "workspace:*",
"@suite-native/settings": "workspace:*",
"@suite-native/storage": "workspace:*",
"@suite-native/tokens": "workspace:*",
"@trezor/transport-native": "workspace:*",
"@trezor/utils": "workspace:*",
"expo-device": "6.0.2",
Expand Down
3 changes: 2 additions & 1 deletion suite-native/state/src/extraDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { mergeDeepObject } from '@trezor/utils';
import { NativeUsbTransport } from '@trezor/transport-native';
import { selectEnabledDiscoveryNetworkSymbols } from '@suite-native/discovery';
import { NETWORK_SYMBOLS_WITH_TOKENS } from '@suite-native/tokens';

const deviceType = Device.isDevice ? 'device' : 'emulator';

Expand All @@ -33,7 +34,7 @@ export const extraDependencies: ExtraDependencies = mergeDeepObject(extraDepende
// otherwise disableAccountsThunk might erase accounts not supported by current device
selectEnabledNetworks: selectEnabledDiscoveryNetworkSymbols,
// todo: this is temporary solution to make token definitions work on native in portfolio tracker
selectTokenDefinitionsEnabledNetworks: () => ['eth'],
selectTokenDefinitionsEnabledNetworks: () => NETWORK_SYMBOLS_WITH_TOKENS,
selectBitcoinAmountUnit: selectBitcoinUnits,
selectAreSatsAmountUnit,
selectLocalCurrency: selectFiatCurrencyCode,
Expand Down
1 change: 1 addition & 0 deletions suite-native/state/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
{ "path": "../module-send" },
{ "path": "../settings" },
{ "path": "../storage" },
{ "path": "../tokens" },
{
"path": "../../packages/transport-native"
},
Expand Down
1 change: 0 additions & 1 deletion suite-native/tokens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"@suite-common/wallet-config": "workspace:*",
"@suite-common/wallet-core": "workspace:*",
"@suite-common/wallet-types": "workspace:*",
"@suite-common/wallet-utils": "workspace:*",
"@trezor/blockchain-link": "workspace:*",
"proxy-memoize": "2.0.2",
"react": "18.2.0"
Expand Down
Loading

0 comments on commit b903214

Please sign in to comment.