-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathHistoryItemMapper.kt
394 lines (360 loc) · 16.8 KB
/
HistoryItemMapper.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package org.p2p.wallet.sell.interactor
import android.content.res.Resources
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.withContext
import org.p2p.core.crypto.Base58String
import org.p2p.core.dispatchers.CoroutineDispatchers
import org.p2p.core.utils.Constants
import org.p2p.core.utils.formatFiat
import org.p2p.core.utils.formatToken
import org.p2p.ethereumkit.external.model.ERC20Tokens
import org.p2p.wallet.R
import org.p2p.wallet.common.date.isSameDayAs
import org.p2p.wallet.common.date.toDateString
import org.p2p.wallet.common.date.toZonedDateTime
import org.p2p.wallet.common.feature_toggles.toggles.remote.EthAddressEnabledFeatureToggle
import org.p2p.wallet.history.model.HistoryTransaction
import org.p2p.wallet.history.model.bridge.BridgeHistoryTransaction
import org.p2p.wallet.history.model.rpc.RpcHistoryTransaction
import org.p2p.wallet.history.ui.model.HistoryItem
import org.p2p.wallet.moonpay.model.SellTransaction
import org.p2p.wallet.moonpay.serversideapi.response.SellTransactionStatus
import org.p2p.wallet.sell.ui.lock.SellTransactionViewDetails
import org.p2p.wallet.user.repository.UserLocalRepository
import org.p2p.wallet.utils.cutMiddle
import org.p2p.wallet.utils.cutStart
import org.p2p.wallet.utils.getStatusIcon
private const val USDT_ETH_TOKEN_SYMBOL = "USDTet"
private const val USDC_ETH_TOKEN_SYMBOL = "USDCet"
class HistoryItemMapper(
private val resources: Resources,
private val dispatchers: CoroutineDispatchers,
private val userLocalRepository: UserLocalRepository,
private val ethAddressEnabledFeatureToggle: EthAddressEnabledFeatureToggle
) {
private val historyItemFlow = MutableStateFlow<List<HistoryItem>?>(null)
fun getHistoryAdapterItemFlow(): MutableStateFlow<List<HistoryItem>?> {
return historyItemFlow
}
suspend fun toAdapterItem(
tokenMintAddress: Base58String?,
transactions: List<HistoryTransaction>,
userSendLinksCount: Int
) {
val rpcHistoryItems = mutableListOf<HistoryItem>()
val sellHistoryItems = mutableListOf<HistoryItem>()
val bridgeHistoryItems = mutableListOf<HistoryItem>()
val filterBundleIds = transactions.filterIsInstance<BridgeHistoryTransaction>()
.filter { it.isProcessing() }
.map { it.getHistoryTransactionId() }
withContext(dispatchers.io) {
transactions.forEachIndexed { _, item ->
when (item) {
is RpcHistoryTransaction -> {
if (item.isNotProcessing(filterBundleIds)) {
parse(item, rpcHistoryItems)
}
}
is SellTransaction -> {
// Sell transactions with cancel reason, should not appear in history
if (!item.isCancelled()) {
parse(item, sellHistoryItems)
}
}
is BridgeHistoryTransaction -> {
parse(item, bridgeHistoryItems)
}
}
}
val swapBannerItem: HistoryItem.SwapBannerItem? = tokenMintAddress?.let(::createSwapBanner)
val userSendLinksItem: HistoryItem.UserSendLinksItem? = createUserSendLinksItem(userSendLinksCount)
val historyItems = listOfNotNull(swapBannerItem, userSendLinksItem)
.plus(sellHistoryItems)
.plus(bridgeHistoryItems)
.plus(rpcHistoryItems)
historyItemFlow.emit(historyItems)
}
}
private fun RpcHistoryTransaction.isNotProcessing(filterBundleIds: List<String>): Boolean {
return when (this) {
is RpcHistoryTransaction.WormholeReceive -> claimKey !in filterBundleIds
is RpcHistoryTransaction.WormholeSend -> message !in filterBundleIds
else -> true
}
}
private fun createSwapBanner(tokenMintAddress: Base58String): HistoryItem.SwapBannerItem? {
if (!ethAddressEnabledFeatureToggle.isFeatureEnabled) {
return null
}
return when (tokenMintAddress.base58Value) {
Constants.USDC_MINT -> {
HistoryItem.SwapBannerItem(
sourceTokenMintAddress = Constants.USDC_MINT,
sourceTokenSymbol = Constants.USDC_SYMBOL,
destinationTokenMintAddress = ERC20Tokens.USDC.mintAddress,
destinationTokenSymbol = USDC_ETH_TOKEN_SYMBOL
)
}
Constants.USDT_MINT -> {
HistoryItem.SwapBannerItem(
sourceTokenMintAddress = Constants.USDT_MINT,
sourceTokenSymbol = Constants.USDT_SYMBOL,
destinationTokenMintAddress = ERC20Tokens.USDT.mintAddress,
destinationTokenSymbol = USDT_ETH_TOKEN_SYMBOL
)
}
else -> {
null
}
}
}
private fun createUserSendLinksItem(userSendLinksCount: Int): HistoryItem.UserSendLinksItem? {
return HistoryItem.UserSendLinksItem(userSendLinksCount)
.takeIf { userSendLinksCount > 0 }
}
fun parse(transaction: RpcHistoryTransaction, cache: MutableList<HistoryItem>) {
val isCurrentAndPreviousTransactionOnSameDay =
cache.isNotEmpty() && cache.last().date.isSameDayAs(transaction.date)
var tokenIconUrl: String? = null
var sourceTokenIconUrl: String? = null
var destinationTokenIconUrl: String? = null
val startTitle: String?
val startSubtitle: String?
var endTopValue: String? = null
var endTopValueTextColor: Int? = null
var endBottomValue: String? = null
val iconRes: Int
when (transaction) {
is RpcHistoryTransaction.Swap -> with(transaction) {
sourceTokenIconUrl = tokenA.logoUrl
destinationTokenIconUrl = tokenB.logoUrl
iconRes = R.drawable.ic_swap_arrows
startTitle = "${tokenA.symbol} to ${tokenB.symbol}"
startSubtitle = resources.getString(getTypeName())
endTopValue = "+${getTokenBTotal()}"
endTopValueTextColor = getTextColor()
endBottomValue = getTokenATotal()
}
is RpcHistoryTransaction.Transfer -> with(transaction) {
tokenIconUrl = getTokenIconUrl()
iconRes = getIcon()
startTitle = getFormattedUsernameOrAddress()
startSubtitle = resources.getString(getTypeName())
endTopValue = getFormattedFiatValue()
endTopValueTextColor = getTextColor()
endBottomValue = getTotalWithSymbol()
}
is RpcHistoryTransaction.ReferralReward -> with(transaction) {
tokenIconUrl = iconUrl
iconRes = getIcon()
startTitle = resources.getString(R.string.transaction_details_referral_title)
startSubtitle = date.toDateString(resources)
endTopValue = getTotalWithSymbol()
endTopValueTextColor = getTextColor()
endBottomValue = getFormattedFiatValue()
}
is RpcHistoryTransaction.StakeUnstake -> with(transaction) {
tokenIconUrl = getTokenIconUrl()
iconRes = getIcon()
startTitle = resources.getString(getTypeName())
startSubtitle = resources.getString(R.string.transaction_history_vote_format, getAddress())
endTopValue = getValue()
endTopValueTextColor = getTextColor()
endBottomValue = getTotal()
}
is RpcHistoryTransaction.BurnOrMint -> with(transaction) {
tokenIconUrl = token.logoUrl
iconRes = R.drawable.ic_placeholder_v2
startTitle = resources.getString(getTitle())
startSubtitle = resources.getString(
R.string.transaction_history_signature_format,
signature.cutMiddle()
)
endTopValue = getFormattedAmountUsd()
endTopValueTextColor = getTextColor()
endBottomValue = getTotal()
}
is RpcHistoryTransaction.CreateAccount -> with(transaction) {
tokenIconUrl = token.logoUrl
iconRes = R.drawable.ic_transaction_create
startTitle = resources.getString(R.string.transaction_history_create)
startSubtitle = resources.getString(
R.string.transaction_history_signature_format,
signature.cutMiddle()
)
}
is RpcHistoryTransaction.CloseAccount -> with(transaction) {
tokenIconUrl = iconUrl
iconRes = R.drawable.ic_transaction_closed
startTitle = resources.getString(R.string.transaction_history_closed)
startSubtitle = resources.getString(
R.string.transaction_history_signature_format,
signature.cutMiddle()
)
}
is RpcHistoryTransaction.WormholeSend -> with(transaction) {
tokenIconUrl = token.logoUrl
iconRes = R.drawable.ic_transaction_send
startTitle = resources.getString(getTitle())
startSubtitle = resources.getString(getSubtitle())
endTopValue = getUsdAmount()
endTopValueTextColor = getTextColor()
endBottomValue = getTotal()
}
is RpcHistoryTransaction.WormholeReceive -> with(transaction) {
tokenIconUrl = token.logoUrl
iconRes = R.drawable.ic_transaction_send
startTitle = resources.getString(getTitle())
startSubtitle = resources.getString(getSubtitle())
endTopValue = getUsdAmount()
endTopValueTextColor = getTextColor()
endBottomValue = getTotalWithSymbol()
}
else -> {
iconRes = R.drawable.ic_transaction_unknown
startTitle = resources.getString(R.string.transaction_history_unknown_title)
startSubtitle = transaction.signature.cutMiddle(cutCount = 8)
}
}
val historyItem = HistoryItem.TransactionItem(
transactionId = transaction.getHistoryTransactionId(),
sourceIconUrl = sourceTokenIconUrl,
destinationIconUrl = destinationTokenIconUrl,
tokenIconUrl = tokenIconUrl,
iconRes = iconRes,
startTitle = startTitle,
startSubtitle = startSubtitle,
endTopValue = endTopValue,
endTopValueTextColor = endTopValueTextColor,
endBottomValue = endBottomValue,
statusIcon = transaction.status.getStatusIcon(),
date = transaction.date
)
if (isCurrentAndPreviousTransactionOnSameDay) {
cache.add(historyItem)
} else {
cache.addAll(
listOf(
HistoryItem.DateItem(transaction.date),
historyItem
)
)
}
}
fun parse(
transaction: SellTransaction,
cache: MutableList<HistoryItem>
) {
val receiverAddress = if (transaction is SellTransaction.WaitingForDepositTransaction) {
transaction.moonpayDepositWalletAddress.base58Value
} else {
resources.getString(R.string.sell_details_receiver_moonpay_bank)
}
val formattedSolAmount = transaction.amounts.tokenAmount.formatToken()
val formattedFiatAmount = transaction.amounts.amountInFiat.formatFiat()
val fiatUiName = transaction.selectedFiat.uiSymbol
val iconRes: Int
val backgroundRes: Int
val iconColor: Int
val titleStatus: String
val subtitleReceiver: String
var endTopValue: String = resources.getString(
R.string.transaction_history_moonpay_amount_sol,
formattedSolAmount,
)
when (transaction.status) {
SellTransactionStatus.WAITING_FOR_DEPOSIT -> {
titleStatus = resources.getString(R.string.transaction_history_moonpay_waiting_for_deposit_title)
subtitleReceiver = resources.getString(
R.string.transaction_history_moonpay_waiting_for_deposit_subtitle,
receiverAddress.cutStart()
)
iconRes = R.drawable.ic_alert_rounded
backgroundRes = R.drawable.bg_rounded_solid_rain_24
iconColor = R.color.icons_night
}
SellTransactionStatus.FAILED -> {
titleStatus = resources.getString(R.string.transaction_history_moonpay_failed_title)
subtitleReceiver = resources.getString(R.string.transaction_history_moonpay_failed_subtitle)
iconRes = R.drawable.ic_alert_rounded
backgroundRes = R.drawable.bg_rounded_solid_rose20_24
iconColor = R.color.icons_rose
}
SellTransactionStatus.PENDING -> {
titleStatus = resources.getString(R.string.transaction_history_moonpay_pending_title)
subtitleReceiver = resources.getString(R.string.transaction_history_moonpay_completed_subtitle)
iconRes = R.drawable.ic_action_schedule_filled
backgroundRes = R.drawable.bg_rounded_solid_rain_24
iconColor = R.color.icons_night
}
SellTransactionStatus.COMPLETED -> {
titleStatus = resources.getString(R.string.transaction_history_moonpay_completed_title)
subtitleReceiver = resources.getString(R.string.transaction_history_moonpay_completed_subtitle)
iconRes = R.drawable.ic_action_schedule_filled
backgroundRes = R.drawable.bg_rounded_solid_rain_24
iconColor = R.color.icons_night
endTopValue = resources.getString(
R.string.transaction_history_moonpay_amount_fiat,
formattedFiatAmount,
fiatUiName.uppercase()
)
}
}
cache.add(
HistoryItem.MoonpayTransactionItem(
transactionId = transaction.transactionId,
statusIconRes = iconRes,
statusBackgroundRes = backgroundRes,
statusIconColor = iconColor,
titleStatus = titleStatus,
subtitleReceiver = subtitleReceiver,
endTopValue = endTopValue,
date = transaction.updatedAt.toZonedDateTime()
)
)
}
fun parse(item: BridgeHistoryTransaction, cache: MutableList<HistoryItem>) {
if (item is BridgeHistoryTransaction.Send) {
val sendTokenSymbol = item.sendDetails.amount.symbol
val tokenIconUrl = userLocalRepository.getTokensData()
.firstOrNull { it.symbol == sendTokenSymbol }
?.iconUrl
val item = HistoryItem.BridgeSendItem(
id = item.id,
sendDetails = item.sendDetails,
tokenIconUrl = tokenIconUrl
)
cache.add(item)
} else if (item is BridgeHistoryTransaction.Claim) {
val claimTokenSymbol = item.bundle.resultAmount.symbol
val tokenIconUrl = userLocalRepository.getTokensData()
.firstOrNull { it.symbol == claimTokenSymbol }
?.iconUrl
val item = HistoryItem.BridgeClaimItem(
bundleId = item.bundleId,
bundle = item.bundle,
tokenIconUrl = tokenIconUrl
)
cache.add(item)
}
}
fun toSellDetailsModel(sellTransaction: SellTransaction): SellTransactionViewDetails {
val receiverAddress = if (sellTransaction is SellTransaction.WaitingForDepositTransaction) {
sellTransaction.moonpayDepositWalletAddress.base58Value
} else {
resources.getString(R.string.sell_details_receiver_moonpay_bank)
}
val formattedSolAmount = sellTransaction.amounts.tokenAmount.formatToken()
val formattedFiatAmount = sellTransaction.amounts.amountInFiat.formatFiat()
val fiatUiName = sellTransaction.selectedFiat.uiSymbol
return SellTransactionViewDetails(
transactionId = sellTransaction.transactionId,
status = sellTransaction.status,
formattedSolAmount = formattedSolAmount,
formattedFiatAmount = formattedFiatAmount,
fiatUiName = fiatUiName,
receiverAddress = receiverAddress,
updatedAt = sellTransaction.updatedAt,
)
}
}