Updated on 2026-08-14

This commit is contained in:
Tangem 2025-05-07 15:44:38 +05:00
parent ad8d026e40
commit 4b4b140182
9 changed files with 181 additions and 32 deletions

View file

@ -22,4 +22,5 @@ dependencies {
implementation(projects.domain.tokens)
implementation(projects.domain.tokens.models)
implementation(projects.domain.wallets.models)
implementation(projects.domain.quotes)
}

View file

@ -26,6 +26,8 @@ sealed class NFTSalePrice {
data class Value(
override val assetId: NFTAsset.Identifier,
val value: SerializedBigDecimal,
val fiatValue: SerializedBigDecimal?,
val symbol: String,
val decimals: Int,
) : NFTSalePrice()
}

View file

@ -0,0 +1,26 @@
package com.tangem.domain.nft
import arrow.core.Either
import com.tangem.domain.nft.repository.NFTRepository
import com.tangem.domain.quotes.single.SingleQuoteFetcher
import com.tangem.domain.tokens.model.Network
class FetchNFTPriceUseCase(
private val nftRepository: NFTRepository,
private val singleQuoteFetcher: SingleQuoteFetcher,
) {
suspend operator fun invoke(network: Network, appCurrencyId: String?): Either<Throwable, Unit> {
return Either.catch {
val nftCurrency = nftRepository.getNFTCurrency(network)
val rawId = nftCurrency.id.rawCurrencyId ?: error("Invalid nft currency id")
singleQuoteFetcher(
params = SingleQuoteFetcher.Params(
rawCurrencyId = rawId,
appCurrencyId = appCurrencyId,
),
)
}
}
}

View file

@ -0,0 +1,44 @@
package com.tangem.domain.nft
import arrow.core.Either
import com.tangem.domain.nft.models.NFTAsset
import com.tangem.domain.nft.models.NFTSalePrice
import com.tangem.domain.nft.repository.NFTRepository
import com.tangem.domain.quotes.single.SingleQuoteProducer
import com.tangem.domain.quotes.single.SingleQuoteSupplier
import com.tangem.domain.tokens.model.Quote
import com.tangem.domain.wallets.models.UserWalletId
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
class GetNFTPriceUseCase(
private val nftRepository: NFTRepository,
private val singleQuoteSupplier: SingleQuoteSupplier,
) {
suspend operator fun invoke(userWalletId: UserWalletId, nftAsset: NFTAsset): Either<Throwable, Flow<NFTSalePrice>> {
return Either.catch {
val nftCurrency = nftRepository.getNFTCurrency(nftAsset.network)
val rawId = nftCurrency.id.rawCurrencyId ?: error("Invalid nft currency id")
singleQuoteSupplier(
params = SingleQuoteProducer.Params(rawCurrencyId = rawId),
).map { quote ->
val nftPrice = nftRepository.getNFTSalePrice(
userWalletId = userWalletId,
network = nftAsset.network,
collectionId = nftAsset.collectionId,
assetId = nftAsset.id,
)
val quoteValue = quote as? Quote.Value
if (nftPrice !is NFTSalePrice.Value) {
nftPrice
} else {
nftPrice.copy(fiatValue = quoteValue?.fiatRate?.multiply(nftPrice.value))
}
}
}
}
}

View file

@ -3,6 +3,8 @@ package com.tangem.domain.nft.repository
import com.tangem.domain.nft.models.NFTAsset
import com.tangem.domain.nft.models.NFTCollection
import com.tangem.domain.nft.models.NFTCollections
import com.tangem.domain.nft.models.NFTSalePrice
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.model.Network
import com.tangem.domain.wallets.models.UserWalletId
import kotlinx.coroutines.flow.Flow
@ -10,6 +12,15 @@ import kotlinx.coroutines.flow.Flow
interface NFTRepository {
fun observeCollections(userWalletId: UserWalletId, networks: List<Network>): Flow<List<NFTCollections>>
fun getNFTCurrency(network: Network): CryptoCurrency
suspend fun getNFTSalePrice(
userWalletId: UserWalletId,
network: Network,
collectionId: NFTCollection.Identifier,
assetId: NFTAsset.Identifier,
): NFTSalePrice
suspend fun refreshCollections(userWalletId: UserWalletId, networks: List<Network>)
suspend fun refreshAssets(userWalletId: UserWalletId, network: Network, collectionId: NFTCollection.Identifier)