Updated on 2026-08-14

This commit is contained in:
Tangem 2024-01-19 13:56:31 +03:00
parent a17f1290ed
commit 531fdf166f
2 changed files with 33 additions and 25 deletions

View file

@ -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<TxHistoryItem>.addRecentTransactions(): PaginationWrapper<TxHistoryItem> {
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<TxHistoryItem> {
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<TxHistoryItem>.addRecentTransactions(): PaginationWrapper<TxHistoryItem> {
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<TxHistoryItem>.filterIfApiKnowsAboutTx(apiItems: List<TxHistoryItem>): List<TxHistoryItem> {
private fun List<TxHistoryItem>.filterIfTxAlreadyAdded(apiItems: List<TxHistoryItem>): List<TxHistoryItem> {
return filter { item -> apiItems.none { it.txHash == item.txHash } }
}

View file

@ -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}")