Skip to content

Commit

Permalink
Fix filtering blockchains and tokens by TokenType
Browse files Browse the repository at this point in the history
  • Loading branch information
omurovch committed Dec 11, 2023
1 parent c2fed50 commit 7fa10d7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ fun BlockchainType.supports(accountType: AccountType): Boolean {
else -> false
}
}
is AccountType.BitcoinAddress -> this === accountType.blockchainType

is AccountType.BitcoinAddress -> {
this === accountType.blockchainType
}

is AccountType.EvmAddress ->
this == BlockchainType.Ethereum
|| this == BlockchainType.BinanceSmartChain
Expand Down Expand Up @@ -380,8 +384,47 @@ val FullCoin.iconPlaceholder: Int
R.drawable.coin_placeholder
}

fun Token.supports(accountType: AccountType): Boolean {
return when (accountType) {
is AccountType.BitcoinAddress -> {
tokenQuery.tokenType == accountType.tokenType
}

is AccountType.HdExtendedKey -> {
when (blockchainType) {
BlockchainType.Bitcoin,
BlockchainType.Litecoin -> {
val type = type
if (type is TokenType.Derived) {
if (!accountType.hdExtendedKey.purposes.contains(type.derivation.purpose)) {
false
} else if (blockchainType == BlockchainType.Bitcoin) {
accountType.hdExtendedKey.coinTypes.contains(ExtendedKeyCoinType.Bitcoin)
} else {
accountType.hdExtendedKey.coinTypes.contains(ExtendedKeyCoinType.Litecoin)
}
} else {
false
}
}

BlockchainType.BitcoinCash,
BlockchainType.ECash,
BlockchainType.Dash -> {
accountType.hdExtendedKey.purposes.contains(HDWallet.Purpose.BIP44)
}

else -> false
}
}

else -> true
}
}

fun FullCoin.eligibleTokens(accountType: AccountType): List<Token> {
return supportedTokens.filter { it.blockchainType.supports(accountType) }
return supportedTokens
.filter { it.supports(accountType) && it.blockchainType.supports(accountType) }
}

val HsPointTimePeriod.title: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class ManageWalletsService(
val tokens = if (filter.isNotBlank()) {
eligibleTokens
} else if (
eligibleTokens.all { it.type is TokenType.Derived } ||
eligibleTokens.all { it.type is TokenType.AddressTyped }
accountType !is AccountType.HdExtendedKey &&
(eligibleTokens.all { it.type is TokenType.Derived } || eligibleTokens.all { it.type is TokenType.AddressTyped })
) {
eligibleTokens.filter { isEnabled(it) || it.type.isDefault }
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class RestoreBlockchainsService(
.map { it.nativeTokenQueries }
.flatten()

tokens = marketKit.tokens(tokenQueries)
tokens = marketKit.tokens(tokenQueries).filter { it.supports(accountType) }
}

private fun handleApproveTokens(blockchain: Blockchain, tokens: List<Token>) {
Expand Down

0 comments on commit 7fa10d7

Please sign in to comment.