Updated on 2026-08-14

This commit is contained in:
Tangem 2024-10-21 23:12:33 +04:00
parent 50ecb380a1
commit a0c455b82c
6 changed files with 31 additions and 25 deletions

View file

@ -16,12 +16,19 @@ class ValidateTokenFormUseCase(
networkId: Network.ID,
formValues: AddCustomTokenForm.Raw,
): EitherNel<CustomTokenFormValidationException, AddCustomTokenForm.Validated> = either {
if (formValues.name.isBlank() && formValues.symbol.isBlank() && formValues.decimals.isBlank()) {
val contractAddress = withError({ nonEmptyListOf(it) }) {
ensureIsContractAddressValid(formValues.contractAddress, networkId)
}
if (formValues.name.isBlank() &&
formValues.symbol.isBlank() &&
formValues.decimals.isBlank()
) {
if (formValues.contractAddress.isBlank()) {
AddCustomTokenForm.Validated.Empty
} else {
val contractAddress = withError({ nonEmptyListOf(it) }) {
ensureIsContractAddressValid(formValues.contractAddress, networkId)
}
AddCustomTokenForm.Validated.ContractAddress(contractAddress)
AddCustomTokenForm.Validated.ContractAddressOnly(contractAddress)
}
} else {
zipOrAccumulate(
{ ensureIsContractAddressValid(formValues.contractAddress, networkId) },
@ -43,10 +50,6 @@ class ValidateTokenFormUseCase(
contractAddress: String,
networkId: Network.ID,
): String {
ensure(contractAddress.isNotBlank()) {
CustomTokenFormValidationException.ContractAddress.Empty
}
val isValid = catch({ repository.validateContractAddress(contractAddress, networkId) }) {
raise(CustomTokenFormValidationException.DataError(it))
}

View file

@ -11,7 +11,9 @@ sealed class AddCustomTokenForm {
sealed class Validated : AddCustomTokenForm() {
data class ContractAddress(
data object Empty : Validated()
data class ContractAddressOnly(
val contractAddress: String,
) : Validated()

View file

@ -4,8 +4,6 @@ sealed class CustomTokenFormValidationException {
sealed class ContractAddress : CustomTokenFormValidationException() {
data object Empty : ContractAddress()
data object Invalid : ContractAddress()
}

View file

@ -61,15 +61,9 @@ internal class CustomTokenFormModel @Inject constructor(
if (params.network.canHandleTokens) {
observeTokenFormUpdates()
} else {
modelScope.launch {
customCurrencyValidator.createCoin(
userWalletId = params.userWalletId,
networkId = params.network.id,
derivationPath = getDerivationPath(),
)
}
}
createCoin()
}
private fun getInitialState(): CustomTokenFormUM {
@ -112,6 +106,7 @@ internal class CustomTokenFormModel @Inject constructor(
}
.distinctUntilChanged()
.sample(periodMillis = 1_000)
.drop(count = 1) // Skip initial state
.onEach { formValues ->
customCurrencyValidator.validateForm(
userWalletId = params.userWalletId,
@ -151,6 +146,14 @@ internal class CustomTokenFormModel @Inject constructor(
}
}
private fun createCoin() = modelScope.launch {
customCurrencyValidator.createCoin(
userWalletId = params.userWalletId,
networkId = params.network.id,
derivationPath = getDerivationPath(),
)
}
private fun updateStateWithCurrency(
currency: CryptoCurrency,
fillForm: Boolean,

View file

@ -75,9 +75,12 @@ internal class CustomCurrencyValidator @Inject constructor(
is AddCustomTokenForm.Validated.All -> {
findOrCreateCurrency(userWalletId, networkId, derivationPath, validatedForm)
}
is AddCustomTokenForm.Validated.ContractAddress -> {
is AddCustomTokenForm.Validated.ContractAddressOnly -> {
findToken(userWalletId, networkId, derivationPath, validatedForm)
}
is AddCustomTokenForm.Validated.Empty -> {
createCoin(userWalletId, networkId, derivationPath)
}
}
}.saveInAndJoin(validateFormJobHolder)
}
@ -132,7 +135,7 @@ internal class CustomCurrencyValidator @Inject constructor(
userWalletId: UserWalletId,
networkId: Network.ID,
derivationPath: Network.DerivationPath,
validatedForm: AddCustomTokenForm.Validated.ContractAddress,
validatedForm: AddCustomTokenForm.Validated.ContractAddressOnly,
) {
updateStatus(Status.SearchingToken)

View file

@ -94,9 +94,6 @@ internal fun CustomTokenFormUM.updateWithContractAddressException(
val updatedFields = fields.mutate {
it[Field.CONTRACT_ADDRESS] = it.getValue(Field.CONTRACT_ADDRESS).copy(
error = when (exception) {
CustomTokenFormValidationException.ContractAddress.Empty -> {
null // Should not display this error
}
CustomTokenFormValidationException.ContractAddress.Invalid -> {
resourceReference(R.string.custom_token_creation_error_invalid_contract_address)
}