Updated on 2026-08-14

This commit is contained in:
Tangem 2025-03-14 17:11:22 +03:00
parent 76209eb454
commit 64d429f642
15 changed files with 120 additions and 110 deletions

View file

@ -23,6 +23,7 @@ dependencies {
implementation(projects.domain.models)
implementation(projects.domain.wallets.models)
implementation(projects.domain.appCurrency.models)
implementation(projects.domain.tokens.models)
/** Project - Utils */
implementation(projects.core.utils)

View file

@ -43,11 +43,9 @@ internal class DefaultVisaRepository @Inject constructor(
private val dispatchers: CoroutineDispatcherProvider,
private val visaApiRequestMaker: VisaApiRequestMaker,
private val visaApi: TangemVisaApi,
private val visaCurrencyFactory: VisaCurrencyFactory,
) : VisaRepository {
private val currencyFactory by lazy(mode = LazyThreadSafetyMode.NONE) {
VisaCurrencyFactory()
}
private val txDetailsFactory by lazy(mode = LazyThreadSafetyMode.NONE) {
VisaTxDetailsFactory()
}
@ -79,20 +77,22 @@ internal class DefaultVisaRepository @Inject constructor(
private suspend fun fetchVisaCurrency(userWalletId: UserWalletId, address: String) {
val contractInfoProvider = visaLibLoader.getOrCreateProvider()
val paymentAccountAddress = getPaymentAccountAddress(userWalletId)
val userWallet = findVisaUserWallet(userWalletId)
parZip(
dispatchers.io,
{
contractInfoProvider.getContractInfo(
walletAddress = address,
paymentAccountAddress = getPaymentAccountAddress(userWalletId),
paymentAccountAddress = paymentAccountAddress,
)
},
{ getFiatRate() },
{ contractInfo, fiatRate ->
fetchedCurrencies.update { value ->
value.apply {
put(address, currencyFactory.create(contractInfo, fiatRate))
put(address, visaCurrencyFactory.create(userWallet, contractInfo, fiatRate))
}
}
},
@ -157,7 +157,7 @@ internal class DefaultVisaRepository @Inject constructor(
)
}
// TODO select correct account when multiple accounts are available (will be implemented when backend is ready)
// There will be only one payment account (backend has filtered for us), so we can take the first one
customerInfo.paymentAccounts.firstOrNull()?.paymentAccountAddress
}.getOrNull()
@ -187,14 +187,14 @@ internal class DefaultVisaRepository @Inject constructor(
}
}
private suspend fun getFiatRate(): BigDecimal? {
private suspend fun getFiatRate(): BigDecimal {
val fiatCurrencyId = VisaConstants.fiatCurrency.code.lowercase()
val quotes = tangemTechApi.getQuotes(
currencyId = fiatCurrencyId,
coinIds = VisaConstants.TOKEN_ID,
).getOrThrow()
return quotes.quotes[VisaConstants.TOKEN_ID]?.price
return quotes.quotes[VisaConstants.TOKEN_ID]?.price ?: error("No price found")
}
private fun makeWalletAddresses(userWallet: UserWallet): Set<Address> {

View file

@ -1,16 +1,28 @@
package com.tangem.data.visa.utils
import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchainsdk.utils.ExcludedBlockchains
import com.tangem.data.common.currency.CryptoCurrencyFactory
import com.tangem.data.common.currency.getNetwork
import com.tangem.domain.common.util.derivationStyleProvider
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.model.NetworkAddress
import com.tangem.domain.visa.model.VisaCurrency
import com.tangem.domain.wallets.models.UserWallet
import com.tangem.lib.visa.model.VisaContractInfo
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.Instant
import java.math.BigDecimal
import java.math.BigInteger
import javax.inject.Inject
internal class VisaCurrencyFactory {
internal class VisaCurrencyFactory @Inject constructor(
private val cryptoCurrencyFactory: CryptoCurrencyFactory,
private val excludedBlockchains: ExcludedBlockchains,
) {
fun create(contractInfo: VisaContractInfo, fiatRate: BigDecimal?): VisaCurrency {
fun create(userWallet: UserWallet, contractInfo: VisaContractInfo, fiatRate: BigDecimal): VisaCurrency {
val now = Instant.now()
val currentLimit = if (contractInfo.limitsChangeDate > now) {
contractInfo.oldLimits
@ -19,11 +31,30 @@ internal class VisaCurrencyFactory {
}
val remainingOtpLimit = getRemainingOtp(currentLimit, now)
val currencyNetwork = getNetwork(
blockchain = Blockchain.Polygon,
extraDerivationPath = null,
derivationStyleProvider = userWallet.scanResponse.derivationStyleProvider,
excludedBlockchains = excludedBlockchains,
canHandleTokens = true,
) ?: error("Unable to create network for Visa currency")
val cryptoCurrency = cryptoCurrencyFactory.createToken(
network = currencyNetwork,
rawId = CryptoCurrency.RawID(VisaConstants.TOKEN_ID),
name = contractInfo.token.name,
symbol = contractInfo.token.symbol,
decimals = contractInfo.token.decimals,
contractAddress = contractInfo.token.address,
)
return VisaCurrency(
cryptoCurrency = cryptoCurrency,
symbol = contractInfo.token.symbol,
networkName = VisaConstants.NETWORK_NAME,
decimals = contractInfo.token.decimals,
fiatRate = fiatRate,
priceChange = BigDecimal.ZERO, // [Second Visa Iteration] pass to create() params if needed
fiatCurrency = VisaConstants.fiatCurrency,
balances = with(contractInfo) {
VisaCurrency.Balances(
@ -40,6 +71,12 @@ internal class VisaCurrencyFactory {
singleTransaction = currentLimit.singleTransactionLimit,
expirationDate = getLimitsExpirationDate(currentLimit, now),
),
paymentAccountAddress = NetworkAddress.Single(
NetworkAddress.Address(
value = contractInfo.paymentAccountAddress,
type = NetworkAddress.Address.Type.Primary,
),
),
)
}