Skip to content

Commit

Permalink
fix(wallet): Filter Select Account by Mainnet or Testnet (uplift to 1…
Browse files Browse the repository at this point in the history
….68.x) (#24123)

Uplift of #24096 (squashed) to beta
  • Loading branch information
brave-builds authored Jun 12, 2024
1 parent b4eadb4 commit 623dd3c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
10 changes: 10 additions & 0 deletions components/brave_wallet_ui/constants/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,16 @@ export const DAppSupportedPrimaryChains = [
BraveWallet.SOLANA_MAINNET
]

export const BitcoinMainnetKeyringIds = [
BraveWallet.KeyringId.kBitcoin84,
BraveWallet.KeyringId.kBitcoinImport
]

export const BitcoinTestnetKeyringIds = [
BraveWallet.KeyringId.kBitcoin84Testnet,
BraveWallet.KeyringId.kBitcoinImportTestnet
]

/**
* Should match BraveWallet.CoinType defined with "as const" to allow for use
* as a type-guard.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ import { getAssetIdKey } from '../../../../utils/asset-utils'
import {
getEntitiesListFromEntityState //
} from '../../../../utils/entities.utils'
import {
getAccountsForNetwork //
} from '../../../../utils/account-utils'

// Queries
import {
Expand Down Expand Up @@ -426,11 +429,8 @@ export const SelectTokenModal = React.forwardRef<HTMLDivElement, Props>(
if (!pendingSelectedAsset) {
return []
}
return accounts
.filter(
(account) => account.accountId.coin === pendingSelectedAsset.coin
)
.sort(function (a, b) {
return getAccountsForNetwork(pendingSelectedAsset, accounts).sort(
function (a, b) {
return new Amount(
getBalance(b.accountId, pendingSelectedAsset, tokenBalancesRegistry)
)
Expand All @@ -442,7 +442,8 @@ export const SelectTokenModal = React.forwardRef<HTMLDivElement, Props>(
)
)
.toNumber()
})
}
)
}, [accounts, pendingSelectedAsset, tokenBalancesRegistry])

// Methods
Expand Down
48 changes: 47 additions & 1 deletion components/brave_wallet_ui/utils/account-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { assertNotReached } from 'chrome://resources/js/assert.js'
import { getLocale } from '../../common/locale'

// types
import { BraveWallet, WalletAccountTypeName } from '../constants/types'
import {
BraveWallet,
WalletAccountTypeName,
BitcoinMainnetKeyringIds,
BitcoinTestnetKeyringIds
} from '../constants/types'

// constants
import registry from '../common/constants/registry'
Expand Down Expand Up @@ -212,3 +217,44 @@ export const isFVMAccount = (
account.accountId.keyringId === BraveWallet.KeyringId.kFilecoinTestnet)
)
}

export const getAccountsForNetwork = (
network: Pick<BraveWallet.NetworkInfo, 'chainId' | 'coin'>,
accounts: BraveWallet.AccountInfo[]
) => {
if (network.chainId === BraveWallet.BITCOIN_MAINNET) {
return accounts.filter((account) =>
BitcoinMainnetKeyringIds.includes(account.accountId.keyringId)
)
}
if (network.chainId === BraveWallet.BITCOIN_TESTNET) {
return accounts.filter((account) =>
BitcoinTestnetKeyringIds.includes(account.accountId.keyringId)
)
}
if (network.chainId === BraveWallet.Z_CASH_MAINNET) {
return accounts.filter(
(account) =>
account.accountId.keyringId === BraveWallet.KeyringId.kZCashMainnet
)
}
if (network.chainId === BraveWallet.Z_CASH_TESTNET) {
return accounts.filter(
(account) =>
account.accountId.keyringId === BraveWallet.KeyringId.kZCashTestnet
)
}
if (network.chainId === BraveWallet.FILECOIN_MAINNET) {
return accounts.filter(
(account) =>
account.accountId.keyringId === BraveWallet.KeyringId.kFilecoin
)
}
if (network.chainId === BraveWallet.FILECOIN_TESTNET) {
return accounts.filter(
(account) =>
account.accountId.keyringId === BraveWallet.KeyringId.kFilecoinTestnet
)
}
return accounts.filter((account) => account.accountId.coin === network.coin)
}

0 comments on commit 623dd3c

Please sign in to comment.