Updated on 2026-08-14

This commit is contained in:
Tangem 2025-05-21 13:46:33 +04:00
parent fdfb07ee33
commit f3bbae831a
13 changed files with 59 additions and 58 deletions

View file

@ -1,13 +1,13 @@
package com.tangem.data.networks.converters
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.utils.converter.TwoWayConverter
import com.tangem.utils.extensions.mapNotNullValues
import java.math.BigDecimal
private typealias AmountsDataModel = Map<String, BigDecimal>
private typealias AmountsDomainModel = Map<CryptoCurrency.ID, CryptoCurrencyAmountStatus>
private typealias AmountsDomainModel = Map<CryptoCurrency.ID, NetworkStatus.Amount>
/**
* Converter from [AmountsDataModel] to [AmountsDomainModel] and vice versa
@ -19,7 +19,7 @@ internal object NetworkAmountsConverter : TwoWayConverter<AmountsDataModel, Amou
override fun convert(value: AmountsDataModel): AmountsDomainModel {
return value
.mapKeys { CryptoCurrency.ID.fromValue(value = it.key) }
.mapValues { (_, amount) -> CryptoCurrencyAmountStatus.Loaded(value = amount) }
.mapValues { (_, amount) -> NetworkStatus.Amount.Loaded(value = amount) }
}
override fun convertBack(value: AmountsDomainModel): AmountsDataModel {
@ -27,8 +27,8 @@ internal object NetworkAmountsConverter : TwoWayConverter<AmountsDataModel, Amou
.mapKeys { (id, _) -> id.value }
.mapNotNullValues { (_, amount) ->
when (amount) {
is CryptoCurrencyAmountStatus.Loaded -> amount.value
is CryptoCurrencyAmountStatus.NotFound -> null
is NetworkStatus.Amount.Loaded -> amount.value
is NetworkStatus.Amount.NotFound -> null
}
}
}

View file

@ -4,7 +4,6 @@ import com.tangem.domain.models.StatusSource
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.models.network.Network
import com.tangem.domain.models.network.TxInfo
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
import com.tangem.domain.tokens.model.NetworkAddress
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.walletmanager.model.Address
@ -89,7 +88,7 @@ object NetworkStatusFactory {
private fun formatAmounts(
amounts: Set<CryptoCurrencyAmount>,
currencies: Set<CryptoCurrency>,
): Map<CryptoCurrency.ID, CryptoCurrencyAmountStatus> {
): Map<CryptoCurrency.ID, NetworkStatus.Amount> {
return currencies.associate { currency ->
val amount = when (currency) {
is CryptoCurrency.Coin -> {
@ -106,9 +105,9 @@ object NetworkStatusFactory {
if (amount == null) {
Timber.w("Unable to find amount for cryptocurrency: $currency")
currency.id to CryptoCurrencyAmountStatus.NotFound
currency.id to NetworkStatus.Amount.NotFound
} else {
currency.id to CryptoCurrencyAmountStatus.Loaded(amount.value)
currency.id to NetworkStatus.Amount.Loaded(amount.value)
}
}
}

View file

@ -4,7 +4,8 @@ import com.google.common.truth.Truth
import com.tangem.domain.models.currency.CryptoCurrency.ID
import com.tangem.domain.models.currency.CryptoCurrency.ID.Body
import com.tangem.domain.models.currency.CryptoCurrency.ID.Prefix
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
import com.tangem.domain.tokens.model.NetworkStatus.Amount
import com.tangem.domain.tokens.model.NetworkStatus.Amount.Loaded
import org.junit.jupiter.api.Test
import java.math.BigDecimal
@ -30,12 +31,12 @@ internal class NetworkAmountsConverterTest {
prefix = Prefix.COIN_PREFIX,
body = Body.NetworkId(rawId = "BCH"),
suffix = ID.Suffix.RawID(rawId = "bitcoin-cash"),
) to CryptoCurrencyAmountStatus.Loaded(value = BigDecimal.ZERO),
) to Loaded(value = BigDecimal.ZERO),
ID(
prefix = Prefix.COIN_PREFIX,
body = Body.NetworkIdWithDerivationPath(rawId = "ETH", derivationPathHashCode = 12367123),
suffix = ID.Suffix.RawID(rawId = "ethereum"),
) to CryptoCurrencyAmountStatus.Loaded(value = BigDecimal.ONE),
) to Loaded(value = BigDecimal.ONE),
)
Truth.assertThat(actual).isEqualTo(expected)
@ -49,17 +50,17 @@ internal class NetworkAmountsConverterTest {
prefix = Prefix.COIN_PREFIX,
body = Body.NetworkId(rawId = "BCH"),
suffix = ID.Suffix.RawID(rawId = "bitcoin-cash"),
) to CryptoCurrencyAmountStatus.Loaded(value = BigDecimal.ZERO),
) to Loaded(value = BigDecimal.ZERO),
ID(
prefix = Prefix.COIN_PREFIX,
body = Body.NetworkIdWithDerivationPath(rawId = "ETH", derivationPathHashCode = 12367123),
suffix = ID.Suffix.RawID(rawId = "ethereum"),
) to CryptoCurrencyAmountStatus.Loaded(value = BigDecimal.ONE),
) to Loaded(value = BigDecimal.ONE),
ID(
prefix = Prefix.COIN_PREFIX,
body = Body.NetworkId(rawId = "BTC"),
suffix = ID.Suffix.RawID(rawId = "bitcoin"),
) to CryptoCurrencyAmountStatus.NotFound,
) to Amount.NotFound,
)
// Act

View file

@ -8,9 +8,9 @@ import com.tangem.domain.models.currency.CryptoCurrency.ID
import com.tangem.domain.models.currency.CryptoCurrency.ID.Body
import com.tangem.domain.models.currency.CryptoCurrency.ID.Prefix
import com.tangem.domain.models.network.Network
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
import com.tangem.domain.tokens.model.NetworkAddress
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.tokens.model.NetworkStatus.Amount
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
@ -51,12 +51,12 @@ internal class NetworkStatusDataModelConverterTest {
prefix = Prefix.COIN_PREFIX,
body = Body.NetworkId(rawId = "BCH"),
suffix = ID.Suffix.RawID(rawId = "bitcoin-cash"),
) to CryptoCurrencyAmountStatus.Loaded(value = BigDecimal.ZERO),
) to Amount.Loaded(value = BigDecimal.ZERO),
ID(
prefix = Prefix.COIN_PREFIX,
body = Body.NetworkId(rawId = "BTC"),
suffix = ID.Suffix.RawID(rawId = "bitcoin"),
) to CryptoCurrencyAmountStatus.NotFound,
) to Amount.NotFound,
),
pendingTransactions = mapOf(), // doesn't matter
source = StatusSource.ACTUAL, // doesn't matter

View file

@ -9,9 +9,9 @@ import com.tangem.domain.models.currency.CryptoCurrency.ID
import com.tangem.domain.models.currency.CryptoCurrency.ID.Body
import com.tangem.domain.models.currency.CryptoCurrency.ID.Prefix
import com.tangem.domain.models.network.Network
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
import com.tangem.domain.tokens.model.NetworkAddress
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.tokens.model.NetworkStatus.Amount
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
@ -96,12 +96,12 @@ internal class SimpleNetworkStatusConverterTest {
prefix = Prefix.COIN_PREFIX,
body = Body.NetworkId(rawId = "BCH"),
suffix = ID.Suffix.RawID(rawId = "bitcoin-cash"),
) to CryptoCurrencyAmountStatus.Loaded(value = BigDecimal.ZERO),
) to Amount.Loaded(value = BigDecimal.ZERO),
ID(
prefix = Prefix.COIN_PREFIX,
body = Body.NetworkIdWithDerivationPath(rawId = "ETH", derivationPathHashCode = 12367123),
suffix = ID.Suffix.RawID(rawId = "ethereum"),
) to CryptoCurrencyAmountStatus.Loaded(value = BigDecimal.ONE),
) to Amount.Loaded(value = BigDecimal.ONE),
),
pendingTransactions = emptyMap(),
source = StatusSource.CACHE,

View file

@ -11,7 +11,6 @@ import com.tangem.data.networks.store.NetworksStatusesStoreV2
import com.tangem.data.networks.store.setSourceAsOnlyCache
import com.tangem.data.networks.store.storeStatus
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
import com.tangem.domain.tokens.model.NetworkAddress
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.walletmanager.WalletManagersFacade
@ -144,7 +143,7 @@ internal class CommonNetworkStatusFetcherTest {
amounts = mapOf(
CryptoCurrency.ID.fromValue(
value = "token⟨ETH⟩NEVER-MIND⚓NEVER-MIND",
) to CryptoCurrencyAmountStatus.NotFound,
) to NetworkStatus.Amount.NotFound,
),
pendingTransactions = mapOf(
CryptoCurrency.ID.fromValue(value = "token⟨ETH⟩NEVER-MIND⚓NEVER-MIND") to emptySet(),

View file

@ -8,9 +8,9 @@ import com.tangem.domain.models.StatusSource
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.models.network.Network
import com.tangem.domain.models.network.TxInfo
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
import com.tangem.domain.tokens.model.NetworkAddress
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.tokens.model.NetworkStatus.Amount
import com.tangem.domain.walletmanager.model.Address
import com.tangem.domain.walletmanager.model.UpdateWalletManagerResult
import org.junit.Test
@ -214,8 +214,8 @@ internal class NetworkStatusFactoryTest(private val model: Model) {
),
),
amounts = mapOf(
currencies.first().id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.ONE),
currencies.last().id to CryptoCurrencyAmountStatus.NotFound,
currencies.first().id to Amount.Loaded(BigDecimal.ONE),
currencies.last().id to Amount.NotFound,
),
pendingTransactions = mapOf(
currencies.first().id to setOf(txInfo),

View file

@ -1,13 +0,0 @@
package com.tangem.domain.tokens.model
import java.math.BigDecimal
/**
* Represents possible statuses of cryptocurrency amount.
*/
sealed class CryptoCurrencyAmountStatus {
data class Loaded(val value: BigDecimal) : CryptoCurrencyAmountStatus()
data object NotFound : CryptoCurrencyAmountStatus()
}

View file

@ -58,7 +58,7 @@ data class NetworkStatus(val network: Network, val value: Value) {
*/
data class Verified(
val address: NetworkAddress,
val amounts: Map<CryptoCurrency.ID, CryptoCurrencyAmountStatus>,
val amounts: Map<CryptoCurrency.ID, Amount>,
val pendingTransactions: Map<CryptoCurrency.ID, Set<TxInfo>>,
override val source: StatusSource,
) : Value()
@ -77,4 +77,18 @@ data class NetworkStatus(val network: Network, val value: Value) {
val errorMessage: String,
override val source: StatusSource,
) : Value()
/** Represents possible statuses of cryptocurrency amount */
sealed interface Amount {
/**
* Loaded amount
*
* @property value amount value
*/
data class Loaded(val value: BigDecimal) : Amount
/** Amount which failed to load */
data object NotFound : Amount
}
}

View file

@ -3,7 +3,6 @@ package com.tangem.domain.tokens.operations
import com.tangem.domain.models.StatusSource
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.staking.model.stakekit.YieldBalance
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
import com.tangem.domain.tokens.model.CryptoCurrencyStatus
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.tokens.model.Quote
@ -75,10 +74,10 @@ internal class CurrencyStatusOperations(
null -> {
return CryptoCurrencyStatus.Loading
}
is CryptoCurrencyAmountStatus.NotFound -> {
is NetworkStatus.Amount.NotFound -> {
return CryptoCurrencyStatus.NoAmount(priceChange = quote?.priceChange, fiatRate = quote?.fiatRate)
}
is CryptoCurrencyAmountStatus.Loaded -> amount.value
is NetworkStatus.Amount.Loaded -> amount.value
}
val hasCurrentNetworkTransactions = networkStatusValue.pendingTransactions.isNotEmpty()

View file

@ -4,9 +4,9 @@ import arrow.core.NonEmptySet
import arrow.core.nonEmptySetOf
import com.tangem.domain.models.StatusSource
import com.tangem.domain.models.network.Network
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
import com.tangem.domain.tokens.model.NetworkAddress
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.tokens.model.NetworkStatus.Amount
import java.math.BigDecimal
internal object MockNetworks {
@ -85,9 +85,9 @@ internal object MockNetworks {
get() = networkStatus1.copy(
value = NetworkStatus.Verified(
amounts = mapOf(
MockTokens.token1.id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.TEN),
MockTokens.token2.id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.TEN),
MockTokens.token3.id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.TEN),
MockTokens.token1.id to Amount.Loaded(BigDecimal.TEN),
MockTokens.token2.id to Amount.Loaded(BigDecimal.TEN),
MockTokens.token3.id to Amount.Loaded(BigDecimal.TEN),
),
pendingTransactions = emptyMap(),
address = NetworkAddress.Single(
@ -101,9 +101,9 @@ internal object MockNetworks {
get() = networkStatus2.copy(
value = NetworkStatus.Verified(
amounts = mapOf(
MockTokens.token4.id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.TEN),
MockTokens.token5.id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.TEN),
MockTokens.token6.id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.TEN),
MockTokens.token4.id to Amount.Loaded(BigDecimal.TEN),
MockTokens.token5.id to Amount.Loaded(BigDecimal.TEN),
MockTokens.token6.id to Amount.Loaded(BigDecimal.TEN),
),
pendingTransactions = emptyMap(),
address = NetworkAddress.Single(
@ -117,10 +117,10 @@ internal object MockNetworks {
get() = networkStatus3.copy(
value = NetworkStatus.Verified(
amounts = mapOf(
MockTokens.token7.id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.TEN),
MockTokens.token8.id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.TEN),
MockTokens.token9.id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.TEN),
MockTokens.token10.id to CryptoCurrencyAmountStatus.Loaded(BigDecimal.TEN),
MockTokens.token7.id to Amount.Loaded(BigDecimal.TEN),
MockTokens.token8.id to Amount.Loaded(BigDecimal.TEN),
MockTokens.token9.id to Amount.Loaded(BigDecimal.TEN),
MockTokens.token10.id to Amount.Loaded(BigDecimal.TEN),
),
pendingTransactions = emptyMap(),
address = NetworkAddress.Single(

View file

@ -1,7 +1,10 @@
package com.tangem.domain.tokens.mock
import arrow.core.nonEmptyListOf
import com.tangem.domain.tokens.model.*
import com.tangem.domain.tokens.model.CryptoCurrencyStatus
import com.tangem.domain.tokens.model.NetworkAddress
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.tokens.model.Quote
import java.math.BigDecimal
@Suppress("MemberVisibilityCanBePrivate")
@ -138,7 +141,7 @@ internal object MockTokensStates {
.first { it.network == status.currency.network }
val amount = (
(networkStatus.value as NetworkStatus.Verified).amounts[status.currency.id]!!
as? CryptoCurrencyAmountStatus.Loaded
as? NetworkStatus.Amount.Loaded
)?.value ?: BigDecimal.ZERO
val quote = MockQuotes.quotes.first { it.rawCurrencyId == status.currency.id.rawCurrencyId }

View file

@ -8,7 +8,6 @@ import com.tangem.domain.card.repository.CardSdkConfigRepository
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.networks.single.SingleNetworkStatusProducer
import com.tangem.domain.networks.single.SingleNetworkStatusSupplier
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.tokens.repository.CurrenciesRepository
import com.tangem.domain.transaction.error.AssociateAssetError
@ -67,7 +66,7 @@ class AssociateAssetUseCase(
?.amounts
?.get(currency.id)
return networkCoinAmountStatus is CryptoCurrencyAmountStatus.Loaded &&
return networkCoinAmountStatus is NetworkStatus.Amount.Loaded &&
networkCoinAmountStatus.value.isNullOrZero()
}
}