Updated on 2026-08-14

This commit is contained in:
Tangem 2026-02-25 18:48:48 +04:00
parent 8b02a0825a
commit 40da3bbe0f
6 changed files with 74 additions and 51 deletions

View file

@ -51,6 +51,16 @@ internal class DefaultTangemPayStorage @Inject constructor(
private val withdrawStoreDataConverter by lazy { WithdrawStoreDataConverter() }
private val withdrawStateConverter by lazy { WithdrawStateConverter() }
private val listType by lazy {
Types.newParameterizedType(List::class.java, WithdrawStoreData::class.java)
}
private val mapType by lazy {
Types.newParameterizedType(Map::class.java, String::class.java, listType)
}
private val adapter: JsonAdapter<Map<String, List<WithdrawStoreData>>> by lazy {
appPreferencesStore.moshi.adapter(mapType)
}
override suspend fun storeCustomerWalletAddress(userWalletId: UserWalletId, customerWalletAddress: String) {
withContext(dispatcherProvider.io) {
appPreferencesStore
@ -152,15 +162,19 @@ internal class DefaultTangemPayStorage @Inject constructor(
}
override suspend fun storeWithdrawOrder(userWalletId: UserWalletId, data: TangemPayWithdrawState) {
val listType = Types.newParameterizedType(List::class.java, WithdrawStoreData::class.java)
val mapType = Types.newParameterizedType(Map::class.java, String::class.java, listType)
val adapter: JsonAdapter<Map<String, List<WithdrawStoreData>>> = appPreferencesStore.moshi.adapter(mapType)
appPreferencesStore.editData { prefs ->
val walletKey = createWithdrawOrderIdKey(userWalletId)
val currentMap = prefs[PreferencesKeys.TANGEM_PAY_WITHDRAW_ORDERS_KEY]?.let(adapter::fromJson).orEmpty()
val updatedList = currentMap[walletKey].orEmpty() + withdrawStoreDataConverter.convert(data)
prefs[PreferencesKeys.TANGEM_PAY_WITHDRAW_ORDERS_KEY] = adapter
.toJson(currentMap + (walletKey to updatedList))
val currentMap = prefs[PreferencesKeys.TANGEM_PAY_WITHDRAW_ORDERS_KEY]
?.let(adapter::fromJson)
.orEmpty()
val newItem = withdrawStoreDataConverter.convert(data)
val currentList = currentMap[walletKey].orEmpty()
val updatedList = buildList(currentList.size + 1) {
for (item in currentList) { if (item.orderId != newItem.orderId) add(item) }
add(newItem)
}
prefs[PreferencesKeys.TANGEM_PAY_WITHDRAW_ORDERS_KEY] =
adapter.toJson(currentMap + (walletKey to updatedList))
}
}
@ -170,9 +184,6 @@ internal class DefaultTangemPayStorage @Inject constructor(
}
override suspend fun getWithdrawOrders(userWalletId: UserWalletId): List<TangemPayWithdrawState> {
val listType = Types.newParameterizedType(List::class.java, WithdrawStoreData::class.java)
val mapType = Types.newParameterizedType(Map::class.java, String::class.java, listType)
val adapter: JsonAdapter<Map<String, List<WithdrawStoreData>>> = appPreferencesStore.moshi.adapter(mapType)
val map = appPreferencesStore.data.firstOrNull()
?.get(PreferencesKeys.TANGEM_PAY_WITHDRAW_ORDERS_KEY)?.let(adapter::fromJson).orEmpty()
return map[createWithdrawOrderIdKey(userWalletId)].orEmpty().map(withdrawStateConverter::convert)
@ -190,15 +201,22 @@ internal class DefaultTangemPayStorage @Inject constructor(
}
override suspend fun deleteWithdrawOrder(userWalletId: UserWalletId, orderId: String) {
appPreferencesStore.editData { mutablePreferences ->
val orders = mutablePreferences.getObjectMap<WithdrawStoreData>(
PreferencesKeys.TANGEM_PAY_WITHDRAW_ORDERS_KEY,
)
.minus(createWithdrawOrderIdKey(userWalletId))
mutablePreferences.setObjectMap(
key = PreferencesKeys.TANGEM_PAY_WITHDRAW_ORDERS_KEY,
value = orders,
)
appPreferencesStore.editData { prefs ->
val walletKey = createWithdrawOrderIdKey(userWalletId)
val currentMap = prefs[PreferencesKeys.TANGEM_PAY_WITHDRAW_ORDERS_KEY]?.let(adapter::fromJson)
.orEmpty()
val currentList = currentMap[walletKey].orEmpty()
val updatedList = buildList(currentList.size) {
for (item in currentList) {
if (item.orderId != orderId) add(item)
}
}
val updatedMap = if (updatedList.isEmpty()) {
currentMap - walletKey
} else {
currentMap + (walletKey to updatedList)
}
prefs[PreferencesKeys.TANGEM_PAY_WITHDRAW_ORDERS_KEY] = adapter.toJson(updatedMap)
}
}

View file

@ -7,6 +7,7 @@ import com.squareup.moshi.JsonClass
data class WithdrawStoreData(
@Json(name = "orderId") val orderId: String,
@Json(name = "exchangeData") val exchangeData: ExchangeStoreData?,
@Json(name = "txHash") val txHash: String?,
)
@JsonClass(generateAdapter = false)

View file

@ -110,12 +110,15 @@ internal class DefaultTangemPayWithdrawRepository @Inject constructor(
val storeData = TangemPayWithdrawState(
orderId = orderId,
exchangeData = exchangeData,
txHash = withdrawTxHash,
)
if (order != null && !withdrawTxHash.isNullOrEmpty()) {
finalizeWithdraw(userWallet = userWallet, txHash = withdrawTxHash, exchangeData = exchangeData)
.onLeft {
tangemPayStorage.storeWithdrawOrder(userWalletId = userWallet.walletId, data = storeData)
}
finalizeWithdraw(
userWallet = userWallet,
txHash = withdrawTxHash,
exchangeData = exchangeData,
orderId = orderId,
)
} else {
tangemPayStorage.storeWithdrawOrder(userWalletId = userWallet.walletId, data = storeData)
}
@ -124,6 +127,7 @@ internal class DefaultTangemPayWithdrawRepository @Inject constructor(
private suspend fun finalizeWithdraw(
userWallet: UserWallet,
orderId: String,
txHash: String,
exchangeData: TangemPayWithdrawExchangeState,
): Either<ExpressDataError, Unit> {
@ -135,10 +139,9 @@ internal class DefaultTangemPayWithdrawRepository @Inject constructor(
payInAddress = exchangeData.payInAddress,
txHash = txHash,
payInExtraId = exchangeData.payInExtraId,
)
.onLeft { error ->
Timber.tag(TAG).e(error.toString())
}
).also {
tangemPayStorage.deleteWithdrawOrder(userWallet.walletId, orderId)
}
}
override suspend fun hasWithdrawOrder(userWallet: UserWallet): Boolean {
@ -168,19 +171,23 @@ internal class DefaultTangemPayWithdrawRepository @Inject constructor(
private suspend fun pollWithdrawOrderIfNeeds(userWallet: UserWallet, data: TangemPayWithdrawState) {
val exchangeData = data.exchangeData ?: return
val storedHash = data.txHash
val orderId = data.orderId
val order = orderRepository.getOrderData(userWalletId = userWallet.walletId, orderId = orderId).getOrNull()
?: return
val txHash = order.withdrawTxHash
val txHash = if (storedHash.isNullOrEmpty()) {
val order = orderRepository.getOrderData(userWalletId = userWallet.walletId, orderId = orderId).getOrNull()
?: return
order.withdrawTxHash.also { fetchedHash ->
tangemPayStorage.storeWithdrawOrder(
userWalletId = userWallet.walletId,
data = data.copy(txHash = fetchedHash),
)
}
} else {
storedHash
}
if (!txHash.isNullOrEmpty()) {
finalizeWithdraw(userWallet = userWallet, txHash = txHash, exchangeData = exchangeData)
.onRight {
tangemPayStorage.deleteWithdrawOrder(userWalletId = userWallet.walletId, orderId = orderId)
}
.onLeft {
startWithdrawOrderPolling(userWallet = userWallet, orderId = orderId, exchangeData = exchangeData)
}
finalizeWithdraw(userWallet = userWallet, txHash = txHash, exchangeData = exchangeData, orderId = orderId)
} else {
startWithdrawOrderPolling(userWallet = userWallet, orderId = orderId, exchangeData = exchangeData)
}
@ -204,20 +211,14 @@ internal class DefaultTangemPayWithdrawRepository @Inject constructor(
.onRight { order ->
val txHash = order.withdrawTxHash
if (txHash.isNullOrEmpty()) return@onRight
finalizeWithdraw(userWallet = userWallet, txHash = txHash, exchangeData = exchangeData)
.onRight {
tangemPayStorage.deleteWithdrawOrder(
userWalletId = userWallet.walletId,
orderId = orderId,
)
withdrawPollingMutex.withLock { withdrawPollingJobs.remove(orderId) }
return@launch
}
.onLeft { error ->
Timber.tag(TAG).e("finalizeWithdraw error: $error")
withdrawPollingMutex.withLock { withdrawPollingJobs.remove(orderId) }
return@launch
}
finalizeWithdraw(
userWallet = userWallet,
txHash = txHash,
exchangeData = exchangeData,
orderId = orderId,
)
withdrawPollingMutex.withLock { withdrawPollingJobs.remove(orderId) }
return@launch
}
.onLeft { error ->
Timber.tag(TAG).e("getOrderData error ${error.errorCode}")

View file

@ -18,5 +18,6 @@ class WithdrawStateConverter : Converter<WithdrawStoreData, TangemPayWithdrawSta
payInExtraId = exchangeData.payInExtraId,
)
},
txHash = value.txHash,
)
}

View file

@ -18,5 +18,6 @@ class WithdrawStoreDataConverter : Converter<TangemPayWithdrawState, WithdrawSto
payInExtraId = exchangeData.payInExtraId,
)
},
txHash = value.txHash,
)
}

View file

@ -3,6 +3,7 @@ package com.tangem.domain.pay
data class TangemPayWithdrawState(
val orderId: String,
val exchangeData: TangemPayWithdrawExchangeState?,
val txHash: String? = null,
)
data class TangemPayWithdrawExchangeState(