Updated on 2026-08-14
This commit is contained in:
commit
91d8932543
7 changed files with 117 additions and 2 deletions
|
|
@ -1,8 +1,6 @@
|
|||
package com.tangem.tap.di.domain
|
||||
|
||||
import com.tangem.domain.card.repository.DerivationsRepository
|
||||
import com.tangem.domain.managetokens.SaveManagedTokensUseCase
|
||||
import com.tangem.domain.managetokens.GetManagedTokensUseCase
|
||||
import com.tangem.domain.managetokens.*
|
||||
import com.tangem.domain.managetokens.repository.CustomTokensRepository
|
||||
import com.tangem.domain.managetokens.repository.ManageTokensRepository
|
||||
|
|
@ -67,4 +65,20 @@ internal object ManageTokensDomainModule {
|
|||
derivationsRepository = derivationsRepository,
|
||||
)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideGetSupportedNetworksUseCase(
|
||||
customTokensRepository: CustomTokensRepository,
|
||||
): GetSupportedNetworksUseCase {
|
||||
return GetSupportedNetworksUseCase(customTokensRepository)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideValidateDerivationPathUseCase(
|
||||
customTokensRepository: CustomTokensRepository,
|
||||
): ValidateDerivationPathUseCase {
|
||||
return ValidateDerivationPathUseCase(customTokensRepository)
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package com.tangem.data.managetokens
|
|||
|
||||
import com.tangem.blockchain.common.Blockchain
|
||||
import com.tangem.blockchainsdk.utils.toNetworkId
|
||||
import com.tangem.crypto.hdWallet.DerivationPath
|
||||
import com.tangem.data.common.api.safeApiCall
|
||||
import com.tangem.data.common.currency.CryptoCurrencyFactory
|
||||
import com.tangem.data.common.currency.UserTokensResponseFactory
|
||||
|
|
@ -16,8 +17,10 @@ import com.tangem.datasource.local.preferences.PreferencesKeys
|
|||
import com.tangem.datasource.local.preferences.utils.getObjectSyncOrNull
|
||||
import com.tangem.datasource.local.preferences.utils.storeObject
|
||||
import com.tangem.datasource.local.userwallet.UserWalletsStore
|
||||
import com.tangem.domain.common.extensions.canHandleBlockchain
|
||||
import com.tangem.domain.common.extensions.supportedBlockchains
|
||||
import com.tangem.domain.common.util.cardTypesResolver
|
||||
import com.tangem.domain.common.util.derivationStyleProvider
|
||||
import com.tangem.domain.managetokens.model.AddCustomTokenForm
|
||||
import com.tangem.domain.managetokens.model.ManagedCryptoCurrency
|
||||
import com.tangem.domain.managetokens.repository.CustomTokensRepository
|
||||
|
|
@ -178,6 +181,33 @@ internal class DefaultCustomTokensRepository(
|
|||
}
|
||||
}
|
||||
|
||||
override suspend fun getSupportedNetworks(userWalletId: UserWalletId): List<Network> = withContext(dispatchers.io) {
|
||||
val userWallet = userWalletsStore.getSyncOrNull(userWalletId)
|
||||
?: error("User wallet not found")
|
||||
val scanResponse = userWallet.scanResponse
|
||||
|
||||
Blockchain.entries
|
||||
.mapNotNull { blockchain ->
|
||||
if (scanResponse.card.canHandleBlockchain(blockchain, scanResponse.cardTypesResolver)) {
|
||||
getNetwork(
|
||||
blockchain = blockchain,
|
||||
extraDerivationPath = null,
|
||||
derivationStyleProvider = scanResponse.derivationStyleProvider,
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun createDerivationPath(rawPath: String): Network.DerivationPath {
|
||||
val sdkPath = DerivationPath(rawPath)
|
||||
|
||||
return Network.DerivationPath.Custom(
|
||||
value = sdkPath.rawPath,
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun storeAndPushTokens(userWalletId: UserWalletId, response: UserTokensResponse) {
|
||||
val compatibleUserTokensResponse = userTokensBackwardCompatibility.applyCompatibilityAndGetUpdated(response)
|
||||
appPreferencesStore.storeObject(
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
package com.tangem.domain.managetokens
|
||||
|
||||
import arrow.core.Either
|
||||
import arrow.core.raise.catch
|
||||
import arrow.core.raise.either
|
||||
import arrow.core.raise.ensureNotNull
|
||||
import com.tangem.domain.managetokens.model.exceptoin.SupportedBlockchainException
|
||||
import com.tangem.domain.managetokens.repository.CustomTokensRepository
|
||||
import com.tangem.domain.tokens.model.Network
|
||||
import com.tangem.domain.wallets.models.UserWalletId
|
||||
|
||||
class GetSupportedNetworksUseCase(
|
||||
private val repository: CustomTokensRepository,
|
||||
) {
|
||||
|
||||
suspend operator fun invoke(userWalletId: UserWalletId): Either<SupportedBlockchainException, List<Network>> {
|
||||
return either {
|
||||
val networks = catch({ repository.getSupportedNetworks(userWalletId) }) {
|
||||
raise(SupportedBlockchainException.DataError(it))
|
||||
}
|
||||
|
||||
ensureNotNull(networks.takeIf { it.isNotEmpty() }) {
|
||||
SupportedBlockchainException.EmptyList
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.tangem.domain.managetokens
|
||||
|
||||
import arrow.core.Either
|
||||
import arrow.core.raise.catch
|
||||
import arrow.core.raise.either
|
||||
import arrow.core.raise.ensure
|
||||
import com.tangem.domain.managetokens.model.exceptoin.DerivationPathValidationException
|
||||
import com.tangem.domain.managetokens.repository.CustomTokensRepository
|
||||
import com.tangem.domain.tokens.model.Network
|
||||
|
||||
class ValidateDerivationPathUseCase(
|
||||
private val repository: CustomTokensRepository,
|
||||
) {
|
||||
|
||||
operator fun invoke(rawValue: String): Either<DerivationPathValidationException, Network.DerivationPath> {
|
||||
return either {
|
||||
ensure(rawValue.isNotEmpty()) { DerivationPathValidationException.Empty }
|
||||
|
||||
catch({ repository.createDerivationPath(rawValue) }) {
|
||||
raise(DerivationPathValidationException.Invalid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package com.tangem.domain.managetokens.model.exceptoin
|
||||
|
||||
sealed class DerivationPathValidationException {
|
||||
|
||||
data object Empty : DerivationPathValidationException()
|
||||
|
||||
data object Invalid : DerivationPathValidationException()
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package com.tangem.domain.managetokens.model.exceptoin
|
||||
|
||||
sealed class SupportedBlockchainException {
|
||||
|
||||
data object EmptyList : SupportedBlockchainException()
|
||||
|
||||
data class DataError(val cause: Throwable) : SupportedBlockchainException()
|
||||
}
|
||||
|
|
@ -33,4 +33,8 @@ interface CustomTokensRepository {
|
|||
): CryptoCurrency.Token
|
||||
|
||||
suspend fun removeCurrency(userWalletId: UserWalletId, currency: ManagedCryptoCurrency.Custom)
|
||||
|
||||
suspend fun getSupportedNetworks(userWalletId: UserWalletId): List<Network>
|
||||
|
||||
fun createDerivationPath(rawPath: String): Network.DerivationPath
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue