Updated on 2026-08-14

This commit is contained in:
Tangem 2024-07-08 13:59:55 +04:00
parent b6a69d7e69
commit 90486a54fc
5 changed files with 41 additions and 36 deletions

View file

@ -17,5 +17,5 @@ interface CustomTokenInteractor {
/** Save token [customCurrency] */
@Throws(TangemError::class)
suspend fun saveToken(customCurrency: CustomCurrency)
suspend fun saveToken(customCurrency: CustomCurrency): Result<Unit>
}

View file

@ -1,5 +1,7 @@
package com.tangem.tap.features.customtoken.impl.domain
import arrow.core.getOrElse
import arrow.core.raise.result
import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchainsdk.utils.toNetworkId
import com.tangem.data.tokens.utils.CryptoCurrencyFactory
@ -15,7 +17,6 @@ import com.tangem.tap.domain.model.Currency
import com.tangem.tap.features.customtoken.impl.domain.models.FoundToken
import com.tangem.tap.proxy.redux.DaggerGraphState
import com.tangem.tap.store
import timber.log.Timber
/**
* Default implementation of custom token interactor
@ -45,16 +46,18 @@ class DefaultCustomTokenInteractor(
)
}
override suspend fun saveToken(customCurrency: CustomCurrency) {
val userWallet = getSelectedWalletSyncUseCase().fold(ifLeft = { return }, ifRight = { it })
val currency = Currency.fromCustomCurrency(customCurrency)
val currencies = listOfNotNull(element = currency.toCryptoCurrency(userWallet.scanResponse))
derivePublicKeysUseCase(userWalletId = userWallet.walletId, currencies = currencies)
.onRight {
addCryptoCurrenciesUseCase(userWalletId = userWallet.walletId, currencies = currencies)
override suspend fun saveToken(customCurrency: CustomCurrency): Result<Unit> {
return result {
val userWallet = getSelectedWalletSyncUseCase().getOrElse {
error("Failed to get selected wallet: $it")
}
.onLeft { Timber.e("Failed to derive public keys: $it") }
val currency = Currency.fromCustomCurrency(customCurrency)
val currencies = listOfNotNull(element = currency.toCryptoCurrency(userWallet.scanResponse))
derivePublicKeysUseCase(userWalletId = userWallet.walletId, currencies = currencies).bind()
addCryptoCurrenciesUseCase(userWalletId = userWallet.walletId, currencies = currencies).bind()
}
}
private fun Currency.toCryptoCurrency(scanResponse: ScanResponse): CryptoCurrency? {

View file

@ -886,17 +886,26 @@ internal class AddCustomTokenViewModel @Inject constructor(
analyticsSender.sendWhenAddTokenButtonClicked(currency)
viewModelScope.launch(dispatchers.io) {
val oldButtonState = uiState.floatingButton
viewModelScope.launch {
uiState = uiState.copySealed(
floatingButton = uiState.floatingButton.copy(isEnabled = false, showProgress = true),
floatingButton = uiState.floatingButton.copy(
isEnabled = false,
showProgress = true,
),
)
runCatching { featureInteractor.saveToken(currency) }
val result = featureInteractor.saveToken(currency)
uiState = uiState.copySealed(
floatingButton = uiState.floatingButton.copy(
isEnabled = true,
showProgress = false,
),
)
result
.onSuccess { featureRouter.openWalletScreen() }
.onFailure {
uiState = uiState.copySealed(floatingButton = oldButtonState)
Timber.e(it)
}
.onFailure { Timber.e(it, "Unable to save custom token") }
}
}
}

View file

@ -5,7 +5,6 @@ import arrow.core.raise.Raise
import arrow.core.raise.catch
import arrow.core.raise.either
import arrow.core.toNonEmptyListOrNull
import com.tangem.domain.tokens.error.AddCurrencyError
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.model.Network
import com.tangem.domain.tokens.repository.CurrenciesRepository
@ -32,9 +31,9 @@ class AddCryptoCurrenciesUseCase(
*
* @param userWalletId The ID of the user's wallet.
* @param currency Cryptocurrency to add.
* @return Either an [AddCurrencyError] or [Unit] indicating the success of the operation.
* @return Either an [Throwable] or [Unit] indicating the success of the operation.
*/
suspend operator fun invoke(userWalletId: UserWalletId, currency: CryptoCurrency): Either<AddCurrencyError, Unit> {
suspend operator fun invoke(userWalletId: UserWalletId, currency: CryptoCurrency): Either<Throwable, Unit> {
return invoke(userWalletId, listOf(currency))
}
@ -47,13 +46,13 @@ class AddCryptoCurrenciesUseCase(
* @param userWalletId The ID of the user's wallet.
* @param cryptoCurrency Token to add.
* @param network Network where we add
* @return Either an [AddCurrencyError] or [Unit] indicating the success of the operation.
* @return Either an [Throwable] or [Unit] indicating the success of the operation.
*/
suspend operator fun invoke(
userWalletId: UserWalletId,
cryptoCurrency: CryptoCurrency.Token,
network: Network,
): Either<AddCurrencyError, Unit> = either {
): Either<Throwable, Unit> = either {
val tokenToAdd = currenciesRepository.createTokenCurrency(cryptoCurrency = cryptoCurrency, network = network)
invoke(userWalletId = userWalletId, currencies = listOf(tokenToAdd))
}
@ -66,14 +65,14 @@ class AddCryptoCurrenciesUseCase(
*
* @param userWalletId The ID of the user's wallet.
* @param currencies The list of cryptocurrencies to add.
* @return Either an [AddCurrencyError] or [Unit] indicating the success of the operation.
* @return Either an [Throwable] or [Unit] indicating the success of the operation.
*/
suspend operator fun invoke(
userWalletId: UserWalletId,
currencies: List<CryptoCurrency>,
): Either<AddCurrencyError, Unit> = either {
): Either<Throwable, Unit> = either {
val existingCurrencies = catch({ currenciesRepository.getMultiCurrencyWalletCurrenciesSync(userWalletId) }) {
raise(AddCurrencyError.DataError(it))
raise(it)
}
val currenciesToAdd = currencies
.filterNot(existingCurrencies::contains)
@ -81,7 +80,7 @@ class AddCryptoCurrenciesUseCase(
?: return@either
catch({ currenciesRepository.addCurrencies(userWalletId, currenciesToAdd) }) {
raise(AddCurrencyError.DataError(it))
raise(it)
}
refreshUpdatedNetworks(userWalletId, currenciesToAdd, existingCurrencies)
@ -91,7 +90,7 @@ class AddCryptoCurrenciesUseCase(
* Refreshes the network statuses for tokens that have corresponding coins in the
* [existingCurrencies] list.
*/
private suspend fun Raise<AddCurrencyError>.refreshUpdatedNetworks(
private suspend fun Raise<Throwable>.refreshUpdatedNetworks(
userWalletId: UserWalletId,
currenciesToAdd: List<CryptoCurrency>,
existingCurrencies: List<CryptoCurrency>,
@ -114,7 +113,7 @@ class AddCryptoCurrenciesUseCase(
)
},
) {
raise(AddCurrencyError.DataError(it))
raise(it)
}
}

View file

@ -1,6 +0,0 @@
package com.tangem.domain.tokens.error
sealed class AddCurrencyError {
data class DataError(val cause: Throwable) : AddCurrencyError()
}