Updated on 2026-08-14

This commit is contained in:
Tangem 2025-06-23 10:27:46 +05:00
commit bab97ef893
4 changed files with 72 additions and 31 deletions

View file

@ -36,8 +36,10 @@ internal object ManageTokensDomainModule {
@Provides
@Singleton
fun provideCreateCurrencyUseCase(customTokensRepository: CustomTokensRepository): CreateCurrencyUseCase {
return CreateCurrencyUseCase(customTokensRepository)
fun provideCreateCryptoCurrencyUseCase(
customTokensRepository: CustomTokensRepository,
): CreateCryptoCurrencyUseCase {
return CreateCryptoCurrencyUseCase(customTokensRepository)
}
@Provides

View file

@ -0,0 +1,65 @@
package com.tangem.domain.managetokens
import arrow.core.Either
import com.tangem.domain.managetokens.model.AddCustomTokenForm
import com.tangem.domain.managetokens.model.ManagedCryptoCurrency
import com.tangem.domain.managetokens.repository.CustomTokensRepository
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.models.network.Network
import com.tangem.domain.wallets.models.UserWalletId
/**
* Creates cryptocurrency from manage token
*/
class CreateCryptoCurrencyUseCase(
private val customTokensRepository: CustomTokensRepository,
) {
/**
* Creates currency from default crypto currency
*
* @param token supported crypto currency info
* @param userWalletId selected user wallet id
*/
suspend operator fun invoke(
token: ManagedCryptoCurrency.Token,
userWalletId: UserWalletId,
): Either<Throwable, List<CryptoCurrency>> = Either.catch {
token.availableNetworks
.map { sourceNetwork ->
when (sourceNetwork) {
is ManagedCryptoCurrency.SourceNetwork.Default -> customTokensRepository.createToken(
managedCryptoCurrency = token,
sourceNetwork = sourceNetwork,
rawId = CryptoCurrency.RawID(token.id.value),
)
is ManagedCryptoCurrency.SourceNetwork.Main -> customTokensRepository.createCoin(
userWalletId = userWalletId,
networkId = sourceNetwork.id,
derivationPath = sourceNetwork.network.derivationPath,
)
}
}
}
/**
* Creates currency from custom crypto currency
*
* @param userWalletId selected user wallet id
* @param networkId currency network id
* @param derivationPath custom derivation path
* @param formValues token info (e.i. name, contract address, symbol, decimals)
*/
suspend operator fun invoke(
userWalletId: UserWalletId,
networkId: Network.ID,
derivationPath: Network.DerivationPath,
formValues: AddCustomTokenForm.Validated.All?,
): Either<Throwable, CryptoCurrency> = Either.catch {
if (formValues == null) {
customTokensRepository.createCoin(userWalletId, networkId, derivationPath)
} else {
customTokensRepository.createCustomToken(userWalletId, networkId, derivationPath, formValues)
}
}
}

View file

@ -1,26 +0,0 @@
package com.tangem.domain.managetokens
import arrow.core.Either
import com.tangem.domain.managetokens.model.AddCustomTokenForm
import com.tangem.domain.managetokens.repository.CustomTokensRepository
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.models.network.Network
import com.tangem.domain.wallets.models.UserWalletId
class CreateCurrencyUseCase(
private val repository: CustomTokensRepository,
) {
suspend operator fun invoke(
userWalletId: UserWalletId,
networkId: Network.ID,
derivationPath: Network.DerivationPath,
formValues: AddCustomTokenForm.Validated.All?,
): Either<Throwable, CryptoCurrency> = Either.catch {
if (formValues == null) {
repository.createCoin(userWalletId, networkId, derivationPath)
} else {
repository.createCustomToken(userWalletId, networkId, derivationPath, formValues)
}
}
}

View file

@ -3,7 +3,7 @@ package com.tangem.features.managetokens.utils
import arrow.core.getOrElse
import com.tangem.core.decompose.di.ModelScoped
import com.tangem.domain.managetokens.CheckIsCurrencyNotAddedUseCase
import com.tangem.domain.managetokens.CreateCurrencyUseCase
import com.tangem.domain.managetokens.CreateCryptoCurrencyUseCase
import com.tangem.domain.managetokens.FindTokenUseCase
import com.tangem.domain.managetokens.ValidateTokenFormUseCase
import com.tangem.domain.managetokens.model.AddCustomTokenForm
@ -23,7 +23,7 @@ import javax.inject.Inject
@ModelScoped
internal class CustomCurrencyValidator @Inject constructor(
private val validateTokenFormUseCase: ValidateTokenFormUseCase,
private val createCustomCurrencyUseCase: CreateCurrencyUseCase,
private val createCryptoCurrencyUseCase: CreateCryptoCurrencyUseCase,
private val findTokenUseCase: FindTokenUseCase,
private val checkIsCurrencyNotAddedUseCase: CheckIsCurrencyNotAddedUseCase,
) {
@ -164,7 +164,7 @@ internal class CustomCurrencyValidator @Inject constructor(
derivationPath: Network.DerivationPath,
validatedForm: AddCustomTokenForm.Validated.All?,
) {
val currency = createCustomCurrencyUseCase(
val currency = createCryptoCurrencyUseCase(
userWalletId = userWalletId,
networkId = networkId,
derivationPath = derivationPath,