Skip to content

Commit

Permalink
US-2128: Change the order of tokens list to match the following seque…
Browse files Browse the repository at this point in the history
…nce: RIF, USDRIF, RBTC, BTC, RDOC & others (#886)

* sort tokens by symbol and define default tokens

* define a new type TokenOrBitcoinNetwork

* wrap with Number instead

* fix imports

* fix import

* get back symbol as string

* remove token filtering

* change sorting balances approach

* delete unused file

* some adjustments
  • Loading branch information
rodrigoncalves authored Feb 26, 2024
1 parent 794c2fd commit 6d97c49
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 13 deletions.
48 changes: 48 additions & 0 deletions src/components/token/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ChainID } from 'lib/eoaWallet'

import { TokenSymbol, getTokenSymbolByChainId } from 'screens/home/TokenImage'
import { TokenOrBitcoinNetwork } from 'shared/types'

/**
* Sorts balances by symbol in the following order:
* RIF, USDRIF, RBTC, BTC, RDOC and then alphabetically by symbol
* @param balances - array of balances
* @param chainId - chain id (30 or 31)
* @returns sorted array of balances
*/
export const sortBalancesBySymbol = (
balances: Array<TokenOrBitcoinNetwork>,
chainId: ChainID,
): Array<TokenOrBitcoinNetwork> => {
const rif = getTokenSymbolByChainId(TokenSymbol.RIF, chainId)
const usdrif = getTokenSymbolByChainId(TokenSymbol.USDRIF, chainId)
const rbtc = getTokenSymbolByChainId(TokenSymbol.RBTC, chainId)
const btc = getTokenSymbolByChainId(TokenSymbol.BTC, chainId)
const rdoc = getTokenSymbolByChainId(TokenSymbol.RDOC, chainId)

const defaultOrder = [rif, usdrif, rbtc, btc, rdoc]

return balances.sort((a, b) => {
const symbolA = getTokenSymbolByChainId(a.symbol, chainId)
const symbolB = getTokenSymbolByChainId(b.symbol, chainId)
const indexA = defaultOrder.indexOf(symbolA)
const indexB = defaultOrder.indexOf(symbolB)

if (indexA !== -1 && indexB !== -1) {
return indexA - indexB
}
if (indexA !== -1) {
return -1
}
if (indexB !== -1) {
return 1
}
if (symbolA < symbolB) {
return -1
}
if (symbolA > symbolB) {
return 1
}
return 0
})
}
18 changes: 9 additions & 9 deletions src/screens/home/PortfolioComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { BitcoinNetwork } from '@rsksmart/rif-wallet-bitcoin'
import { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { ScrollView, StyleProp, View, ViewStyle } from 'react-native'

import { PortfolioCard } from 'components/Porfolio/PortfolioCard'
import { sortBalancesBySymbol } from 'components/token/utils'
import { getTokenColor } from 'screens/home/tokenColor'
import { sharedColors } from 'shared/constants'
import { ITokenWithoutLogo } from 'store/slices/balancesSlice/types'
import { TokenOrBitcoinNetwork } from 'shared/types'
import { getCurrentChainId } from 'storage/ChainStorage'

interface Props {
setSelectedAddress: (token: string | undefined) => void
balances: Array<ITokenWithoutLogo | BitcoinNetwork>
balances: Array<TokenOrBitcoinNetwork>
totalUsdBalance: string
selectedAddress?: string
showTotalCard?: boolean
Expand All @@ -24,11 +25,14 @@ export const PortfolioComponent = ({
showTotalCard = true,
style,
}: Props) => {
const chainId = getCurrentChainId()
const { t } = useTranslation()
const [isTotalCardSelected, setIsTotalCardSelected] = useState<boolean>(
showTotalCard && !selectedAddress,
)

const usersTokens = sortBalancesBySymbol(balances, chainId)

const handleSelectedAddress = useCallback(
(contractAddress: string) => {
setIsTotalCardSelected(false)
Expand Down Expand Up @@ -59,13 +63,9 @@ export const PortfolioComponent = ({
isSelected={isTotalCardSelected}
/>
)}
{balances.map(
{usersTokens.map(
(
{
contractAddress,
symbol,
balance,
}: ITokenWithoutLogo | BitcoinNetwork,
{ contractAddress, symbol, balance }: TokenOrBitcoinNetwork,
i: number,
) => {
const isSelected =
Expand Down
23 changes: 23 additions & 0 deletions src/screens/home/TokenImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
ViewStyle,
} from 'react-native'

import { ChainID } from 'lib/eoaWallet'

import { FrownFaceIcon } from 'components/icons'
import { sharedColors } from 'shared/constants'
interface Props {
Expand Down Expand Up @@ -277,3 +279,24 @@ export const getIconSource = (
return undefined
}
}

export const getTokenSymbolByChainId = (
symbol: string | TokenSymbol,
chainId: ChainID,
) => {
const upperSymbol = symbol.toUpperCase()
const isMainnet = chainId === 30
switch (upperSymbol) {
case 'RIF':
case 'TRIF':
return isMainnet ? TokenSymbol.RIF : TokenSymbol.TRIF
case 'RBTC':
case 'TRBTC':
return isMainnet ? TokenSymbol.RBTC : TokenSymbol.TRBTC
case 'BTC':
case 'BTCT':
return isMainnet ? TokenSymbol.BTC : TokenSymbol.BTCT
default:
return TokenSymbol[upperSymbol as keyof typeof TokenSymbol] || upperSymbol
}
}
4 changes: 1 addition & 3 deletions src/screens/send/SendScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ export const SendScreen = ({

const balances = Object.values(useAppSelector(selectBalances))
const assets = contact
? balances.filter(b =>
isContactBitcoin ? isAssetBitcoin(b) : !isAssetBitcoin(b),
)
? balances.filter(b => isContactBitcoin !== isAssetBitcoin(b))
: balances

const contractAddress = route.params?.contractAddress || assets[0]
Expand Down
8 changes: 7 additions & 1 deletion src/shared/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { SendBitcoinRequest } from '@rsksmart/rif-wallet-bitcoin'
import {
BitcoinNetwork,
SendBitcoinRequest,
} from '@rsksmart/rif-wallet-bitcoin'

import { Request } from 'lib/eoaWallet'

import {
rootTabsRouteNames,
RootTabsScreenProps,
} from 'navigation/rootNavigator'
import { ITokenWithoutLogo } from 'store/slices/balancesSlice/types'

export interface ErrorWithMessage {
message: string
Expand All @@ -28,3 +32,5 @@ export type ContactWithAddressRequired = Partial<Omit<Contact, 'address'>> & {

export type ActivityMainScreenProps =
RootTabsScreenProps<rootTabsRouteNames.Activity>

export type TokenOrBitcoinNetwork = ITokenWithoutLogo | BitcoinNetwork

0 comments on commit 6d97c49

Please sign in to comment.