Updated on 2026-08-14
This commit is contained in:
parent
81cdcd0f38
commit
f6fd1a51fb
8 changed files with 24 additions and 57 deletions
|
|
@ -3,7 +3,7 @@ package com.tangem.tap.features.customtoken.impl.presentation.validators
|
|||
import com.tangem.blockchain.common.Blockchain
|
||||
import com.tangem.blockchain.common.address.AddressService
|
||||
import com.tangem.common.card.EllipticCurve
|
||||
import com.tangem.domain.AddCustomTokenError
|
||||
import com.tangem.domain.tokens.error.AddCustomTokenError
|
||||
|
||||
/**
|
||||
* Validator of contract address
|
||||
|
|
@ -15,9 +15,9 @@ object ContractAddressValidator {
|
|||
/** Validate a [address] using [blockchain] */
|
||||
fun validate(address: String, blockchain: Blockchain): ContractAddressValidatorResult {
|
||||
return when {
|
||||
address.isEmpty() -> ContractAddressValidatorResult.Error(type = AddCustomTokenError.FieldIsEmpty)
|
||||
address.isEmpty() -> ContractAddressValidatorResult.Error(type = AddCustomTokenError.FIELD_IS_EMPTY)
|
||||
validateAddress(blockchain, address) -> ContractAddressValidatorResult.Success
|
||||
else -> ContractAddressValidatorResult.Error(type = AddCustomTokenError.InvalidContractAddress)
|
||||
else -> ContractAddressValidatorResult.Error(type = AddCustomTokenError.INVALID_CONTRACT_ADDRESS)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.tangem.tap.features.customtoken.impl.presentation.validators
|
||||
|
||||
import com.tangem.domain.AddCustomTokenError
|
||||
import com.tangem.domain.tokens.error.AddCustomTokenError
|
||||
|
||||
/**
|
||||
* Result of validation contract address
|
||||
|
|
|
|||
|
|
@ -16,13 +16,13 @@ import com.tangem.blockchain.common.derivation.DerivationStyle
|
|||
import com.tangem.core.analytics.api.AnalyticsEventHandler
|
||||
import com.tangem.crypto.hdWallet.DerivationPath
|
||||
import com.tangem.crypto.hdWallet.HDWalletError
|
||||
import com.tangem.domain.AddCustomTokenError
|
||||
import com.tangem.domain.common.DerivationStyleProvider
|
||||
import com.tangem.domain.common.extensions.*
|
||||
import com.tangem.domain.common.util.cardTypesResolver
|
||||
import com.tangem.domain.common.util.derivationStyleProvider
|
||||
import com.tangem.domain.features.addCustomToken.CustomCurrency
|
||||
import com.tangem.domain.tokens.GetCryptoCurrenciesUseCase
|
||||
import com.tangem.domain.tokens.error.AddCustomTokenError
|
||||
import com.tangem.domain.tokens.model.CryptoCurrency
|
||||
import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase
|
||||
import com.tangem.tap.features.customtoken.impl.domain.CustomTokenInteractor
|
||||
|
|
@ -465,7 +465,7 @@ internal class AddCustomTokenViewModel @Inject constructor(
|
|||
address = uiState.form.contractAddressInputField.value,
|
||||
blockchain = networkSelectorValue,
|
||||
).let {
|
||||
it is ContractAddressValidatorResult.Error && it.type == AddCustomTokenError.FieldIsEmpty
|
||||
it is ContractAddressValidatorResult.Error && it.type == AddCustomTokenError.FIELD_IS_EMPTY
|
||||
}
|
||||
|
||||
val isSupportedToken = if (!isNetworkSelected()) {
|
||||
|
|
@ -621,7 +621,7 @@ internal class AddCustomTokenViewModel @Inject constructor(
|
|||
|
||||
private fun handleContractAddressErrorValidation(type: AddCustomTokenError) {
|
||||
when {
|
||||
isNetworkSelected() && type == AddCustomTokenError.InvalidContractAddress -> {
|
||||
isNetworkSelected() && type == AddCustomTokenError.INVALID_CONTRACT_ADDRESS -> {
|
||||
val isAnotherTokenFieldsFilled = isAnyTokenFieldsFilled()
|
||||
uiState = uiState.copySealed(
|
||||
form = uiState.form.copy(
|
||||
|
|
@ -644,7 +644,7 @@ internal class AddCustomTokenViewModel @Inject constructor(
|
|||
)
|
||||
}
|
||||
|
||||
!isNetworkSelected() || type == AddCustomTokenError.FieldIsEmpty -> {
|
||||
!isNetworkSelected() || type == AddCustomTokenError.FIELD_IS_EMPTY -> {
|
||||
uiState = uiState.copySealed(
|
||||
form = uiState.form.copy(
|
||||
contractAddressInputField = uiState.form.contractAddressInputField.copy(isError = false),
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
package com.tangem.domain
|
||||
|
||||
import com.tangem.common.module.ModuleError
|
||||
import com.tangem.common.module.ModuleErrorCode
|
||||
import com.tangem.common.module.ModuleMessage
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
* All DomainError descendants must use their own range of codes, but no more than 999 error codes for each.
|
||||
*/
|
||||
sealed interface DomainModuleMessage : ModuleMessage
|
||||
|
||||
sealed class DomainModuleError(
|
||||
subCode: Int,
|
||||
override val message: String,
|
||||
override val data: Any?,
|
||||
) : DomainModuleMessage, ModuleError() {
|
||||
override val code: Int = ModuleErrorCode.DOMAIN + subCode
|
||||
|
||||
companion object {
|
||||
// base code used for all errors in the module
|
||||
internal const val ERROR_CODE_ADD_CUSTOM_TOKEN = 100
|
||||
// const val CODE_ANY_OTHER = 200..299, 300..399 etc
|
||||
}
|
||||
}
|
||||
|
||||
sealed class AddCustomTokenError(
|
||||
subCode: Int = 0,
|
||||
message: String? = null,
|
||||
data: Any? = null,
|
||||
) : DomainModuleError(
|
||||
subCode = ERROR_CODE_ADD_CUSTOM_TOKEN + subCode,
|
||||
message = message ?: this::class.java.simpleName,
|
||||
data = data,
|
||||
) {
|
||||
|
||||
object FieldIsEmpty : AddCustomTokenError()
|
||||
object InvalidContractAddress : AddCustomTokenError()
|
||||
}
|
||||
|
|
@ -3,26 +3,28 @@ package com.tangem.domain.tokens
|
|||
import arrow.core.Either
|
||||
import arrow.core.raise.catch
|
||||
import arrow.core.raise.either
|
||||
import com.tangem.domain.AddCustomTokenError
|
||||
import com.tangem.domain.tokens.error.AddCustomTokenError
|
||||
import com.tangem.domain.tokens.repository.TokensListRepository
|
||||
|
||||
class ValidateContractAddressUseCase(private val tokensListRepository: TokensListRepository) {
|
||||
class ValidateContractAddressUseCase(
|
||||
private val tokensListRepository: TokensListRepository,
|
||||
) {
|
||||
operator fun invoke(address: String, networkId: String): Either<AddCustomTokenError, Unit> {
|
||||
return either {
|
||||
catch(
|
||||
block = {
|
||||
if (address.isEmpty()) raise(AddCustomTokenError.FieldIsEmpty)
|
||||
if (address.isEmpty()) raise(AddCustomTokenError.FIELD_IS_EMPTY)
|
||||
|
||||
if (!tokensListRepository.validateAddress(
|
||||
contractAddress = address,
|
||||
networkId = networkId,
|
||||
)
|
||||
) {
|
||||
raise(AddCustomTokenError.InvalidContractAddress)
|
||||
raise(AddCustomTokenError.INVALID_CONTRACT_ADDRESS)
|
||||
}
|
||||
},
|
||||
catch = {
|
||||
AddCustomTokenError.InvalidContractAddress
|
||||
AddCustomTokenError.INVALID_CONTRACT_ADDRESS
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
package com.tangem.domain.tokens.error
|
||||
|
||||
enum class AddCustomTokenError {
|
||||
FIELD_IS_EMPTY,
|
||||
INVALID_CONTRACT_ADDRESS,
|
||||
}
|
||||
|
|
@ -59,8 +59,6 @@ dependencies {
|
|||
implementation(projects.domain.settings)
|
||||
implementation(projects.domain.tokens)
|
||||
implementation(projects.domain.tokens.models)
|
||||
implementation(projects.domain.txhistory)
|
||||
implementation(projects.domain.txhistory.models)
|
||||
implementation(projects.domain.wallets)
|
||||
implementation(projects.domain.wallets.models)
|
||||
implementation(projects.domain.appCurrency)
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package com.tangem.managetokens.presentation.addcustomtoken.state.factory
|
|||
|
||||
import com.tangem.core.ui.event.consumedEvent
|
||||
import com.tangem.core.ui.event.triggeredEvent
|
||||
import com.tangem.domain.AddCustomTokenError
|
||||
import com.tangem.domain.tokens.error.AddCustomTokenError
|
||||
import com.tangem.domain.tokens.model.Network
|
||||
import com.tangem.domain.wallets.models.UserWallet
|
||||
import com.tangem.domain.wallets.models.UserWalletId
|
||||
|
|
@ -285,7 +285,7 @@ internal class AddCustomTokenStateFactory(
|
|||
fun handleAddressError(error: AddCustomTokenError): AddCustomTokenState {
|
||||
val uiState = currentStateProvider()
|
||||
return when (error) {
|
||||
AddCustomTokenError.InvalidContractAddress -> {
|
||||
AddCustomTokenError.INVALID_CONTRACT_ADDRESS -> {
|
||||
addTokenAddressFieldError(AddCustomTokenWarning.InvalidContractAddress)
|
||||
.copy(
|
||||
addTokenButton = uiState.addTokenButton.copy(isEnabled = false),
|
||||
|
|
@ -294,7 +294,7 @@ internal class AddCustomTokenStateFactory(
|
|||
.toPersistentSet(),
|
||||
)
|
||||
}
|
||||
AddCustomTokenError.FieldIsEmpty ->
|
||||
AddCustomTokenError.FIELD_IS_EMPTY ->
|
||||
removeTokenAddressError()
|
||||
.copy(
|
||||
addTokenButton = uiState.addTokenButton.copy(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue