From 531fdf166ff2593233223cfbdb6477c3e6c392f9 Mon Sep 17 00:00:00 2001 From: Tangem Date: Fri, 19 Jan 2024 13:56:31 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../paging/TxHistoryPagingSource.kt | 51 +++++++++++-------- ...TransactionDataToTxHistoryItemConverter.kt | 7 +-- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/data/txhistory/src/main/kotlin/com/tangem/data/txhistory/repository/paging/TxHistoryPagingSource.kt b/data/txhistory/src/main/kotlin/com/tangem/data/txhistory/repository/paging/TxHistoryPagingSource.kt index 633173fb97..5452c7c385 100644 --- a/data/txhistory/src/main/kotlin/com/tangem/data/txhistory/repository/paging/TxHistoryPagingSource.kt +++ b/data/txhistory/src/main/kotlin/com/tangem/data/txhistory/repository/paging/TxHistoryPagingSource.kt @@ -68,51 +68,58 @@ internal class TxHistoryPagingSource( block = { fetch(pageToLoad, pageSize) }, ) - return requireNotNull(txHistoryItemsStore.getSyncOrNull(storeKey, pageToLoad)) { - "The transaction history page #$pageToLoad could not be retrieved" - } + return txHistoryItemsStore.getSync(pageToLoad) } private suspend fun fetch(pageToLoad: Int, pageSize: Int) { - var wrappedItems = walletManagersFacade.getTxHistoryItems( + val wrappedItems = walletManagersFacade.getTxHistoryItems( userWalletId = sourceParams.userWalletId, currency = sourceParams.currency, page = pageToLoad, pageSize = pageSize, ) - if (pageToLoad == 1) { - wrappedItems = wrappedItems.addRecentTransactions() - } - txHistoryItemsStore.store(key = storeKey, value = wrappedItems) } - private suspend fun PaginationWrapper.addRecentTransactions(): PaginationWrapper { - val recentTxHistoryItems = Blockchain.fromNetworkId(sourceParams.currency.network.backendId)?.let { - walletManagersFacade.getRecentTransactions( - userWalletId = sourceParams.userWalletId, - blockchain = it, - derivationPath = sourceParams.currency.network.derivationPath.value, - ) - .filterUnconfirmedTransaction() - .filterIfApiKnowsAboutTx(apiItems = items) - } ?: emptyList() + private suspend fun TxHistoryItemsStore.getSync(pageToLoad: Int): PaginationWrapper { + val storedItems = requireNotNull(getSyncOrNull(storeKey, pageToLoad)) { + "The transaction history page #$pageToLoad could not be retrieved" + } - return if (recentTxHistoryItems.isEmpty()) { + return if (pageToLoad == 1) storedItems.addRecentTransactions() else storedItems + } + + private suspend fun PaginationWrapper.addRecentTransactions(): PaginationWrapper { + val blockchain = Blockchain.fromNetworkId(sourceParams.currency.network.backendId) + + if (blockchain == null) { + Timber.e("Blockchain not found") + return this + } + + val recentItems = walletManagersFacade.getRecentTransactions( + userWalletId = sourceParams.userWalletId, + blockchain = blockchain, + derivationPath = sourceParams.currency.network.derivationPath.value, + ) + .filterUnconfirmedTransaction() + .filterIfTxAlreadyAdded(apiItems = items) + + return if (recentItems.isEmpty()) { Timber.d("Nothing to add to TxHistory") this } else { Timber.d( "Recent transactions were added to TxHistory: %s", - recentTxHistoryItems.joinToString( + recentItems.joinToString( prefix = "[", postfix = "]", transform = TxHistoryItem::txHash, ), ) - return copy(items = recentTxHistoryItems + items) + return copy(items = recentItems + items) } } @@ -120,7 +127,7 @@ internal class TxHistoryPagingSource( return filter { it.status == TxHistoryItem.TransactionStatus.Unconfirmed } } - private fun List.filterIfApiKnowsAboutTx(apiItems: List): List { + private fun List.filterIfTxAlreadyAdded(apiItems: List): List { return filter { item -> apiItems.none { it.txHash == item.txHash } } } diff --git a/domain/legacy/src/main/java/com/tangem/domain/walletmanager/utils/TransactionDataToTxHistoryItemConverter.kt b/domain/legacy/src/main/java/com/tangem/domain/walletmanager/utils/TransactionDataToTxHistoryItemConverter.kt index c9bf25084a..d686b942ec 100644 --- a/domain/legacy/src/main/java/com/tangem/domain/walletmanager/utils/TransactionDataToTxHistoryItemConverter.kt +++ b/domain/legacy/src/main/java/com/tangem/domain/walletmanager/utils/TransactionDataToTxHistoryItemConverter.kt @@ -23,7 +23,7 @@ internal class TransactionDataToTxHistoryItemConverter( override fun convert(value: TransactionData): TxHistoryItem? { val hash = value.hash ?: return null val millis = value.date?.timeInMillis ?: return null - val amount = getTransactionAmountValue(value.amount) ?: return null + val amount = getTransactionAmountValue(value.amount, value.fee?.amount) ?: return null val isOutgoing = value.sourceAddress in walletAddresses.map(Address::value) return TxHistoryItem( @@ -46,8 +46,9 @@ internal class TransactionDataToTxHistoryItemConverter( ) } - private fun getTransactionAmountValue(amount: Amount): BigDecimal? { - val value = amount.value + private fun getTransactionAmountValue(amount: Amount, feeAmount: Amount?): BigDecimal? { + val feeValue = feeAmount?.value ?: BigDecimal.ZERO + val value = amount.value?.plus(feeValue) if (value == null) { Timber.w("Transaction amount must not be null: ${amount.currencySymbol}")