Updated on 2026-08-14

This commit is contained in:
Tangem 2023-05-02 08:00:37 +08:00
parent de449519a3
commit 64ac275eb3
14 changed files with 518 additions and 66 deletions

View file

@ -14,41 +14,36 @@ class AddCustomTokenService(
private val supportedTokenNetworkIds: List<String>,
) {
suspend fun findToken(
contractAddress: String,
networkId: String? = null,
active: Boolean? = null,
): List<CoinsResponse.Coin> = withContext(dispatchers.io) {
runCatching {
tangemTechApi.getCoins(
contractAddress = contractAddress,
networkIds = selectNetworksForSearch(networkId),
active = active,
)
suspend fun findToken(contractAddress: String, networkId: String?): List<CoinsResponse.Coin> {
return withContext(dispatchers.io) {
runCatching {
tangemTechApi.getCoins(
contractAddress = contractAddress,
networkIds = selectNetworksForSearch(networkId),
)
}
.fold(
onSuccess = { response ->
var coinsList = mutableListOf<CoinsResponse.Coin>()
response.coins.forEach { coin ->
val networksWithTheSameAddress = coin.networks
.filter { it.contractAddress != null || it.decimalCount != null }
.filter { it.contractAddress?.equals(contractAddress, ignoreCase = true) == true }
.filter { supportedTokenNetworkIds.contains(it.networkId) }
if (networksWithTheSameAddress.isNotEmpty()) {
val newToken = coin.copy(networks = networksWithTheSameAddress)
coinsList.add(newToken)
}
}
if (coinsList.size > 1) {
// https://tangem.slack.com/archives/GMXC6PP71/p1649672562078679
coinsList = mutableListOf(coinsList[0])
}
coinsList
},
onFailure = { emptyList() },
)
}
.onSuccess { response ->
var coinsList = mutableListOf<CoinsResponse.Coin>()
response.coins.forEach { coin ->
val networksWithTheSameAddress = coin.networks
.filter { it.contractAddress != null || it.decimalCount != null }
.filter { it.contractAddress?.equals(contractAddress, ignoreCase = true) == true }
.filter { supportedTokenNetworkIds.contains(it.networkId) }
if (networksWithTheSameAddress.isNotEmpty()) {
val newToken = coin.copy(networks = networksWithTheSameAddress)
coinsList.add(newToken)
}
}
if (coinsList.size > 1) {
// https://tangem.slack.com/archives/GMXC6PP71/p1649672562078679
coinsList = mutableListOf(coinsList[0])
}
return@withContext coinsList
}
.onFailure {
return@withContext emptyList()
}
error("Unreachable code because runCatching must return result")
}
private fun selectNetworksForSearch(networkId: String?): String {

View file

@ -342,7 +342,6 @@ internal class AddCustomTokenHub : BaseStoreHub<AddCustomTokenState>("AddCustomT
}
}
@Suppress("MagicNumber")
private suspend fun requestInfoAboutToken(contractAddress: String): List<CoinsResponse.Coin> {
val tangemTechServiceManager = requireNotNull(hubState.tangemTechServiceManager)
dispatchOnMain(Screen.UpdateTokenFields(listOf(ContractAddress to ViewStates.TokenField(isLoading = true))))
@ -354,7 +353,7 @@ internal class AddCustomTokenHub : BaseStoreHub<AddCustomTokenState>("AddCustomT
// simulate loading effect. It would be better if the delay would only run if tokenManager.checkAddress()
// got the result faster than 500ms and the delay would only be the difference between them.
delay(500)
delay(timeMillis = 500)
val result = tangemTechServiceManager.findToken(contractAddress, selectedNetworkId)
@ -450,7 +449,7 @@ internal class AddCustomTokenHub : BaseStoreHub<AddCustomTokenState>("AddCustomT
enableDisableTokenDetailFields(false)
}
private suspend fun enableDisableTokenDetailFields(isEnabled: Boolean = true) {
private suspend fun enableDisableTokenDetailFields(isEnabled: Boolean) {
val state = hubState
val action = Screen.UpdateTokenFields(
listOf(
@ -590,17 +589,18 @@ private class AddCustomTokenReducer(
is OnCreate -> {
val card = requireNotNull(globalState.scanResponse?.card)
val supportedTokenNetworkIds = card.supportedBlockchains()
.filter { it.canHandleTokens() }
.map { it.toNetworkId() }
.filter(Blockchain::canHandleTokens)
.map(Blockchain::toNetworkId)
val tangemTechServiceManager = AddCustomTokenService(
tangemTechApi = globalState.networkServices.tangemTechService.api,
dispatchers = AppCoroutineDispatcherProvider(),
supportedTokenNetworkIds = supportedTokenNetworkIds,
)
val form = Form(AddCustomTokenState.createFormFields(card, CustomTokenType.Blockchain))
state.copy(
cardDerivationStyle = card.derivationStyle,
form = form,
form = Form(AddCustomTokenState.createFormFields(card, CustomTokenType.Blockchain)),
tangemTechServiceManager = tangemTechServiceManager,
screenState = createInitialScreenState(card.settings.isHDWalletAllowed),
)

View file

@ -64,8 +64,6 @@ data class AddCustomTokenState(
fun getError(id: FieldId): AddCustomTokenError? = formErrors[id]
fun hasError(id: FieldId): Boolean = formErrors[id] != null
inline fun <reified T> visitDataConverter(converter: FieldDataConverter<T>): T {
form.visitDataConverter(converter)
return converter.getConvertedData()
@ -116,9 +114,12 @@ data class AddCustomTokenState(
return network.data.value != Blockchain.Unknown
}
fun getCustomTokenType(): CustomTokenType = when {
tokensAnyFieldsIsFilled() || tokensFieldsIsFilled() -> CustomTokenType.Token
else -> CustomTokenType.Blockchain
fun getCustomTokenType(): CustomTokenType {
return if (tokensAnyFieldsIsFilled() || tokensFieldsIsFilled()) {
CustomTokenType.Token
} else {
CustomTokenType.Blockchain
}
}
fun gatherUserToken(): CustomCurrency.CustomToken? = try {
@ -264,7 +265,7 @@ data class AddCustomTokenState(
private var builder: StringBuilder = StringBuilder()
override fun convert(action: Action, stateHolder: DomainState): String? {
val action = action as? AddCustomTokenAction ?: return null
if (action !is AddCustomTokenAction) return null
val state = stateHolder.addCustomTokensState
val fieldConverter =