Updated on 2026-08-14

This commit is contained in:
Tangem 2024-07-19 16:34:21 +05:00
commit 0e980df356
3 changed files with 52 additions and 21 deletions

View file

@ -75,7 +75,10 @@ internal object TransactionDomainModule {
@Provides
@Singleton
fun provideEstimateFeeUseCase(walletManagersFacade: WalletManagersFacade): EstimateFeeUseCase {
return EstimateFeeUseCase(walletManagersFacade)
return EstimateFeeUseCase(
walletManagersFacade = walletManagersFacade,
demoConfig = DemoConfig(),
)
}
@Provides

View file

@ -8,10 +8,12 @@ import com.tangem.blockchain.common.AmountType
import com.tangem.blockchain.common.Token
import com.tangem.blockchain.common.transaction.TransactionFee
import com.tangem.blockchain.extensions.Result
import com.tangem.domain.demo.DemoConfig
import com.tangem.domain.demo.DemoTransactionSender
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.transaction.error.GetFeeError
import com.tangem.domain.walletmanager.WalletManagersFacade
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.domain.wallets.models.UserWallet
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import java.math.BigDecimal
@ -21,18 +23,27 @@ import java.math.BigDecimal
*/
class EstimateFeeUseCase(
private val walletManagersFacade: WalletManagersFacade,
private val demoConfig: DemoConfig,
) {
suspend operator fun invoke(
amount: BigDecimal,
userWalletId: UserWalletId,
userWallet: UserWallet,
cryptoCurrency: CryptoCurrency,
): Flow<Either<GetFeeError, TransactionFee>> {
return flow {
val result = walletManagersFacade.estimateFee(
amount = convertCryptoCurrencyToAmount(cryptoCurrency, amount),
userWalletId = userWalletId,
network = cryptoCurrency.network,
)
val amountData = convertCryptoCurrencyToAmount(cryptoCurrency, amount)
val result = if (demoConfig.isDemoCardId(userWallet.scanResponse.card.cardId)) {
demoTransactionSender(userWallet, cryptoCurrency).estimateFee(
amount = amountData,
destination = "",
)
} else {
walletManagersFacade.estimateFee(
amount = amountData,
userWalletId = userWallet.walletId,
network = cryptoCurrency.network,
)
}
val maybeFee = when (result) {
is Result.Success -> result.data.right()
@ -43,6 +54,17 @@ class EstimateFeeUseCase(
}
}
private suspend fun demoTransactionSender(
userWallet: UserWallet,
cryptoCurrency: CryptoCurrency,
): DemoTransactionSender {
return DemoTransactionSender(
walletManagersFacade
.getOrCreateWalletManager(userWallet.walletId, cryptoCurrency.network)
?: error("WalletManager is null"),
)
}
private fun convertCryptoCurrencyToAmount(cryptoCurrency: CryptoCurrency, amount: BigDecimal) = Amount(
currencySymbol = cryptoCurrency.symbol,
value = amount,

View file

@ -1003,10 +1003,10 @@ internal class SwapInteractorImpl @Inject constructor(
val fromToken = fromTokenStatus.currency
val toToken = toTokenStatus.currency
return coroutineScope {
val txFeeResult = getSelectedWalletSyncUseCase().getOrNull()?.walletId?.let { userWalletId ->
val txFeeResult = getSelectedWalletSyncUseCase().getOrNull()?.let { userWallet ->
getUnhandledFee(
amount = amount.value,
userWalletId = userWalletId,
userWallet = userWallet,
cryptoCurrency = fromToken,
)
}
@ -1263,15 +1263,21 @@ internal class SwapInteractorImpl @Inject constructor(
toAddress = toToken.value.networkAddress?.defaultAddress?.value.orEmpty(),
).fold(
ifRight = { swapData ->
val feeData = transactionManager.getFee(
networkId = networkId,
amountToSend = amount.value,
currencyToSend = swapCurrencyConverter.convert(fromToken.currency),
destinationAddress = swapData.transaction.txTo,
increaseBy = INCREASE_GAS_LIMIT_BY,
data = (swapData.transaction as ExpressTransactionModel.DEX).txData,
derivationPath = fromToken.currency.network.derivationPath.value,
)
val userWallet = getSelectedWallet()
val cardId = userWallet?.scanResponse?.card?.cardId
val feeData = if (cardId != null && isDemoCardUseCase(cardId)) {
getDemoFees(fromToken.currency)
} else {
transactionManager.getFee(
networkId = networkId,
amountToSend = amount.value,
currencyToSend = swapCurrencyConverter.convert(fromToken.currency),
destinationAddress = swapData.transaction.txTo,
increaseBy = INCREASE_GAS_LIMIT_BY,
data = (swapData.transaction as ExpressTransactionModel.DEX).txData,
derivationPath = fromToken.currency.network.derivationPath.value,
)
}
val txFeeState = when (feeData) {
is ProxyFees.MultipleFees -> feeData.proxyFeesToFeeState(fromToken.currency)
is ProxyFees.SingleFee -> feeData.proxyFeesToFeeState(fromToken.currency)
@ -1380,12 +1386,12 @@ internal class SwapInteractorImpl @Inject constructor(
private suspend fun getUnhandledFee(
amount: BigDecimal,
userWalletId: UserWalletId,
userWallet: UserWallet,
cryptoCurrency: CryptoCurrency,
): Either<GetFeeError, TransactionFee>? {
return estimateFeeUseCase(
amount = amount,
userWalletId = userWalletId,
userWallet = userWallet,
cryptoCurrency = cryptoCurrency,
).firstOrNull()
}