Skip to content

Commit

Permalink
chore(suite): update network symbol naming 7
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive authored and tomasklim committed Dec 6, 2024
1 parent a3649d6 commit 0fc5924
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import styled from 'styled-components';
import { Meta, StoryObj } from '@storybook/react';

import { StoryColumn } from '@trezor/components';
import { NetworkSymbol } from '@suite-common/wallet-config';

import { CoinLogo } from '../../index';
import { COINS } from './coins';
import { COINS, isCoinSymbol } from './coins';

const CoinName = styled.div`
margin-bottom: 0.5rem;
Expand Down Expand Up @@ -36,14 +35,16 @@ export const All: StoryObj = {
render: () => (
<StoryColumn minWidth={700}>
<WrapperIcons>
{Object.keys(COINS).map(symbol => (
<Icon key={symbol}>
<CoinName>{symbol}</CoinName>
<CoinLogo
symbol={symbol as NetworkSymbol}
data-testid={`coin-${symbol}`}
size={64}
/>
{Object.keys(COINS).map(coinSymbol => (
<Icon key={coinSymbol}>
<CoinName>{coinSymbol}</CoinName>
{isCoinSymbol(coinSymbol) && (
<CoinLogo
symbol={coinSymbol}
data-testid={`coin-${coinSymbol}`}
size={64}
/>
)}
</Icon>
))}
</WrapperIcons>
Expand Down
5 changes: 5 additions & 0 deletions packages/product-components/src/components/CoinLogo/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ export const COINS: Record<NetworkSymbol | LegacyNetworkSymbol, string> = {
xtz: require('../../images/coins/xtz.svg'),
zec: require('../../images/coins/zec.svg'),
};

export const isCoinSymbol = (
coinSymbol: string,
): coinSymbol is NetworkSymbol | LegacyNetworkSymbol =>
Object.prototype.hasOwnProperty.call(COINS, coinSymbol);
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import styled from 'styled-components';

import { spacings, spacingsPx } from '@trezor/theme';
import { AssetLogo, Badge, Column, Row, Text } from '@trezor/components';
import { NetworkSymbol } from '@suite-common/wallet-config';
import { getContractAddressForNetworkSymbol } from '@suite-common/wallet-utils';

import { CoinLogo } from '../CoinLogo/CoinLogo';
import { AssetOptionBaseProps } from './SelectAssetModal';
import { COINS } from '../CoinLogo/coins';
import { isCoinSymbol } from '../CoinLogo/coins';

const ClickableContainer = styled.div`
cursor: pointer;
Expand Down Expand Up @@ -42,8 +41,8 @@ export const AssetItem = ({
contractAddress,
handleClick,
}: AssetItemProps) => {
// TODO: use isCoinSymbol - prepared in chore(suite): update network symbol naming 7
const symbolHelper = symbol in COINS ? (symbol as NetworkSymbol) : undefined;
const getCoinLogo = () =>
isCoinSymbol(symbol) ? <CoinLogo size={24} symbol={symbol} /> : null;

return (
<ClickableContainer
Expand Down Expand Up @@ -71,7 +70,7 @@ export const AssetItem = ({
shouldTryToFetch={shouldTryToFetch}
/>
) : (
symbolHelper && <CoinLogo size={24} symbol={symbolHelper} />
getCoinLogo()
)}
<Column flex="1">
<Row gap={spacings.xs} alignItems="center">
Expand Down
4 changes: 2 additions & 2 deletions suite-common/graph/src/graphDataFetching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,9 @@ export const getMultipleAccountBalanceHistoryWithFiat = async ({
});

const coins = Array.from(coinsSet).map(coin => {
const [coinSymbol, contractId] = coin.split('-');
const [symbol, contractId] = coin.split('-') as [NetworkSymbol, TokenAddress | undefined];

return { coin: coinSymbol as NetworkSymbol, contractId: contractId as TokenAddress };
return { coin: symbol, contractId };
});

const pairs = await Promise.all(
Expand Down
18 changes: 8 additions & 10 deletions suite-common/token-definitions/src/__fixtures__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
import { NetworkSymbol } from '@suite-common/wallet-config';

import { DefinitionType } from '../tokenDefinitionsTypes';

export const isTokenDefinitionKnownFixtures = [
{
testName: 'Token definition known, case-insensitive network',
tokenDefinitions: ['0xa', '0xb'],
networkSymbol: 'eth' as NetworkSymbol,
networkSymbol: 'eth' as const,
contractAddress: '0xA',
result: true,
},
{
testName: 'Token definition unknown, case-sensitive network',
tokenDefinitions: ['0xa', '0xb'],
networkSymbol: 'sol' as NetworkSymbol,
networkSymbol: 'sol' as const,
contractAddress: '0xA',
result: false,
},
{
testName: 'Token definition known, case-sensitive network',
tokenDefinitions: ['0xA', '0xb'],
networkSymbol: 'sol' as NetworkSymbol,
networkSymbol: 'sol' as const,
contractAddress: '0xA',
result: true,
},
{
testName: 'Token definition not known',
tokenDefinitions: ['0xa', '0xb'],
networkSymbol: 'eth' as NetworkSymbol,
networkSymbol: 'eth' as const,
contractAddress: '0xC',
result: false,
},
{
testName: 'Token definitions are undefined',
tokenDefinitions: undefined,
networkSymbol: 'eth' as NetworkSymbol,
networkSymbol: 'eth' as const,
contractAddress: '0xA',
result: false,
},
Expand All @@ -43,20 +41,20 @@ export const isTokenDefinitionKnownFixtures = [
export const getSupportedDefinitionTypesFixtures = [
{
testName: 'Supports both token and NFT definitions',
networkSymbol: 'eth' as NetworkSymbol,
networkSymbol: 'eth' as const,
features: ['coin-definitions', 'nft-definitions'],
result: [DefinitionType.COIN, DefinitionType.NFT],
},
// Temporarily skipped while token definitions are disabled for Cardano.
// {
// testName: 'Supports only token definitions',
// networkSymbol: 'ada' as NetworkSymbol,
// networkSymbol: 'ada' as const,
// features: ['coin-definitions'],
// result: [DefinitionType.COIN],
// },
{
testName: 'Supports neither token nor NFT definitions',
networkSymbol: 'ltc' as NetworkSymbol,
networkSymbol: 'ltc' as const,
features: [],
result: [],
},
Expand Down
16 changes: 7 additions & 9 deletions suite-common/wallet-core/src/accounts/accountsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,13 @@ export const selectDeviceAccountKeyByDescriptorAndNetworkSymbol = createMemoized
account => account?.key ?? null,
);

export const selectAccountsSymbols = createMemoizedSelector(
[selectAccounts],
accounts =>
pipe(
accounts,
A.map(a => a.symbol),
A.uniq,
returnStableArrayIfEmpty,
) as NetworkSymbol[],
export const selectAccountsSymbols = createMemoizedSelector([selectAccounts], accounts =>
pipe(
accounts,
A.map(a => a.symbol),
A.uniq,
returnStableArrayIfEmpty,
),
);

export const selectIsDeviceAccountless = createMemoizedSelector(
Expand Down
34 changes: 16 additions & 18 deletions suite-common/wallet-core/src/device/deviceReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
hasBitcoinOnlyFirmware,
isBitcoinOnlyDevice,
} from '@trezor/device-utils';
import { NetworkSymbol, networks } from '@suite-common/wallet-config';
import { networkSymbolCollection } from '@suite-common/wallet-config';
import {
createReducerWithExtraDeps,
createWeakMapSelector,
Expand Down Expand Up @@ -789,24 +789,22 @@ export const selectDeviceStatus = createMemoizedSelector(

export const selectDeviceSupportedNetworks = createMemoizedSelector([selectDevice], device => {
const firmwareVersion = getFirmwareVersion(device);
const result = Object.entries(networks)
.filter(([symbol]) => {
const unavailableCapability = device?.unavailableCapabilities?.[symbol];
// if device does not have fw, do not show coins which are not supported by device in any case
if (!firmwareVersion && unavailableCapability === 'no-support') {
return false;
}
// if device has fw, do not show coins which are not supported by current fw
if (
firmwareVersion &&
['no-support', 'no-capability'].includes(unavailableCapability || '')
) {
return false;
}
const result = networkSymbolCollection.filter(symbol => {
const unavailableCapability = device?.unavailableCapabilities?.[symbol];
// if device does not have fw, do not show coins which are not supported by device in any case
if (!firmwareVersion && unavailableCapability === 'no-support') {
return false;
}
// if device has fw, do not show coins which are not supported by current fw
if (
firmwareVersion &&
['no-support', 'no-capability'].includes(unavailableCapability || '')
) {
return false;
}

return true;
})
.map(([symbol]) => symbol as NetworkSymbol);
return true;
});

return returnStableArrayIfEmpty(result);
});
Expand Down
6 changes: 3 additions & 3 deletions suite-common/wallet-core/src/fiat-rates/fiatRatesThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
groupTokensTransactionsByContractAddress,
isTestnet,
} from '@suite-common/wallet-utils';
import { getNetworkFeatures, NetworkSymbol } from '@suite-common/wallet-config';
import { getNetworkFeatures } from '@suite-common/wallet-config';
import { selectIsSpecificCoinDefinitionKnown } from '@suite-common/token-definitions';
import { TimerId } from '@trezor/type-utils';

Expand Down Expand Up @@ -61,7 +61,7 @@ export const updateTxsFiatRatesThunk = createThunk(
if (hasCoinDefinitions) {
const isTokenKnown = selectIsSpecificCoinDefinitionKnown(
getState(),
account.symbol as NetworkSymbol,
account.symbol,
token as TokenAddress,
);

Expand All @@ -75,7 +75,7 @@ export const updateTxsFiatRatesThunk = createThunk(
) as Timestamp[];
await fetchTransactionsRates(
{
symbol: account.symbol as NetworkSymbol,
symbol: account.symbol,
tokenAddress: token as TokenAddress,
},
tokenTimestamps,
Expand Down
16 changes: 7 additions & 9 deletions suite-common/wallet-utils/src/accountUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
type Bip43PathTemplate,
getNetwork,
type NetworkSymbolExtended,
networkSymbolCollection,
} from '@suite-common/wallet-config';
import {
Account,
Expand Down Expand Up @@ -439,13 +440,11 @@ export const formatTokenAmount = (tokenTransfer: TokenTransfer) => {
* - primary: by network `symbol`
* - secondary: by `accountType`
*/
export const sortByCoin = (accounts: Account[]) => {
const orderedNetworkSymbols = Object.keys(networks) as NetworkSymbol[];

return accounts.sort((a, b) => {
export const sortByCoin = (accounts: Account[]) =>
accounts.sort((a, b) => {
// primary sorting: by order of network keys
const aSymbolIndex = orderedNetworkSymbols.indexOf(a.symbol);
const bSymbolIndex = orderedNetworkSymbols.indexOf(b.symbol);
const aSymbolIndex = networkSymbolCollection.indexOf(a.symbol);
const bSymbolIndex = networkSymbolCollection.indexOf(b.symbol);
if (aSymbolIndex !== bSymbolIndex) return aSymbolIndex - bSymbolIndex;

// when it is sorted by network, sort by order of accountType keys within the same network
Expand All @@ -459,7 +458,6 @@ export const sortByCoin = (accounts: Account[]) => {
// if both are same, keep the original order in `accounts`
return a.index - b.index;
});
};

export const findAccountsByNetwork = (symbol: NetworkSymbol, accounts: Account[]) =>
accounts.filter(a => a.symbol === symbol);
Expand Down Expand Up @@ -655,7 +653,7 @@ export const getAccountTokensFiatBalance = (
// sum fiat value of all tokens
tokens?.forEach(t => {
const tokenFiatRateKey = getFiatRateKey(
account.symbol as NetworkSymbol,
account.symbol,
localCurrency as FiatCurrencyCode,
t.contract as TokenAddress,
);
Expand Down Expand Up @@ -712,7 +710,7 @@ export const getAccountFiatBalance = ({
shouldIncludeTokens?: boolean;
shouldIncludeStaking?: boolean;
}) => {
const coinFiatRateKey = getFiatRateKey(account.symbol as NetworkSymbol, localCurrency);
const coinFiatRateKey = getFiatRateKey(account.symbol, localCurrency);
const coinFiatRate = rates?.[coinFiatRateKey];

if (!coinFiatRate?.rate) return null;
Expand Down
13 changes: 9 additions & 4 deletions suite-native/accounts/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { A, D, G } from '@mobily/ts-belt';

import { AccountType, getNetwork, networks } from '@suite-common/wallet-config';
import {
AccountType,
getNetwork,
networks,
networkSymbolCollection,
} from '@suite-common/wallet-config';
import { formattedAccountTypeMap } from '@suite-common/wallet-core';
import { Account } from '@suite-common/wallet-types';
import { orderedNetworkSymbols, orderedAccountTypes } from '@suite-native/config';
import { orderedAccountTypes } from '@suite-native/config';

const accountTypeToSectionHeader: Readonly<Partial<Record<AccountType, string>>> = {
normal: 'default',
Expand Down Expand Up @@ -79,8 +84,8 @@ export const groupAccountsByNetworkAccountType = A.groupBy((account: Account) =>

export const sortAccountsByNetworksAndAccountTypes = (accounts: readonly Account[]) => {
return A.sort(accounts, (a, b) => {
const aOrder = orderedNetworkSymbols.indexOf(a.symbol) ?? Number.MAX_SAFE_INTEGER;
const bOrder = orderedNetworkSymbols.indexOf(b.symbol) ?? Number.MAX_SAFE_INTEGER;
const aOrder = networkSymbolCollection.indexOf(a.symbol) ?? Number.MAX_SAFE_INTEGER;
const bOrder = networkSymbolCollection.indexOf(b.symbol) ?? Number.MAX_SAFE_INTEGER;

if (aOrder === bOrder) {
const aAccountTypeOrder =
Expand Down
1 change: 0 additions & 1 deletion suite-native/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"@suite-common/wallet-utils": "workspace:*",
"@suite-native/accounts": "workspace:*",
"@suite-native/atoms": "workspace:*",
"@suite-native/config": "workspace:*",
"@suite-native/formatters": "workspace:*",
"@suite-native/icons": "workspace:*",
"@suite-native/intl": "workspace:*",
Expand Down
7 changes: 3 additions & 4 deletions suite-native/assets/src/assetsSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
selectTokenDefinitions,
TokenDefinitionsRootState,
} from '@suite-common/token-definitions';
import { NetworkSymbol } from '@suite-common/wallet-config';
import { NetworkSymbol, networkSymbolCollection } from '@suite-common/wallet-config';
import {
AccountsRootState,
DeviceRootState,
Expand All @@ -20,7 +20,6 @@ import {
import { getAccountFiatBalance } from '@suite-common/wallet-utils';
import { getAccountListSections } from '@suite-native/accounts';
import { sortAccountsByNetworksAndAccountTypes } from '@suite-native/accounts/src/utils';
import { orderedNetworkSymbols } from '@suite-native/config';
import { selectFiatCurrencyCode, SettingsSliceRootState } from '@suite-native/settings';
import { getAccountCryptoBalanceWithStaking, NativeStakingRootState } from '@suite-native/staking';

Expand Down Expand Up @@ -65,8 +64,8 @@ export const selectDeviceNetworksWithAssets = createMemoizedSelector(
A.map(account => account.symbol),
A.uniq,
A.sort((a, b) => {
const aOrder = orderedNetworkSymbols.indexOf(a) ?? Number.MAX_SAFE_INTEGER;
const bOrder = orderedNetworkSymbols.indexOf(b) ?? Number.MAX_SAFE_INTEGER;
const aOrder = networkSymbolCollection.indexOf(a) ?? Number.MAX_SAFE_INTEGER;
const bOrder = networkSymbolCollection.indexOf(b) ?? Number.MAX_SAFE_INTEGER;

return aOrder - bOrder;
}),
Expand Down
1 change: 0 additions & 1 deletion suite-native/assets/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
},
{ "path": "../accounts" },
{ "path": "../atoms" },
{ "path": "../config" },
{ "path": "../formatters" },
{ "path": "../icons" },
{ "path": "../intl" },
Expand Down
Loading

0 comments on commit 0fc5924

Please sign in to comment.