Updated on 2026-08-14

This commit is contained in:
Tangem 2024-05-30 15:08:00 +01:00
parent 682349e353
commit 0e370ae05f
2 changed files with 22 additions and 17 deletions

View file

@ -37,6 +37,8 @@ import com.tangem.domain.walletmanager.utils.WalletManagerFactory
import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.models.UserWallet
import com.tangem.domain.wallets.models.UserWalletId import com.tangem.domain.wallets.models.UserWalletId
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import timber.log.Timber import timber.log.Timber
import java.math.BigDecimal import java.math.BigDecimal
import java.util.EnumSet import java.util.EnumSet
@ -61,6 +63,8 @@ class DefaultWalletManagersFacade(
private val requirementsConditionConverter by lazy { SdkRequirementsConditionConverter() } private val requirementsConditionConverter by lazy { SdkRequirementsConditionConverter() }
private val estimationFeeAddressFactory by lazy { EstimationFeeAddressFactory() } private val estimationFeeAddressFactory by lazy { EstimationFeeAddressFactory() }
private val initMutex = Mutex()
override suspend fun update( override suspend fun update(
userWalletId: UserWalletId, userWalletId: UserWalletId,
network: Network, network: Network,
@ -329,24 +333,25 @@ class DefaultWalletManagersFacade(
blockchain: Blockchain, blockchain: Blockchain,
derivationPath: String?, derivationPath: String?,
): WalletManager? { ): WalletManager? {
val userWallet = getUserWallet(userWalletId) initMutex.withLock {
var walletManager = walletManagersStore.getSyncOrNull( val userWallet = getUserWallet(userWalletId)
userWalletId = userWalletId, var walletManager = walletManagersStore.getSyncOrNull(
blockchain = blockchain, userWalletId = userWalletId,
derivationPath = derivationPath,
)
if (walletManager == null) {
walletManager = walletManagerFactory.createWalletManager(
scanResponse = userWallet.scanResponse,
blockchain = blockchain, blockchain = blockchain,
derivationPath = derivationPath?.let { DerivationPath(rawPath = it) }, derivationPath = derivationPath,
) ?: return null )
if (walletManager == null) {
walletManager = walletManagerFactory.createWalletManager(
scanResponse = userWallet.scanResponse,
blockchain = blockchain,
derivationPath = derivationPath?.let { DerivationPath(rawPath = it) },
)
walletManager ?: return null
walletManagersStore.store(userWalletId, walletManager) walletManagersStore.store(userWalletId, walletManager)
}
return walletManager
} }
return walletManager
} }
@Deprecated("Will be removed in future") @Deprecated("Will be removed in future")

View file

@ -42,10 +42,10 @@ internal class DefaultBlockchainSDKFactory(
dispatchers: CoroutineDispatcherProvider, dispatchers: CoroutineDispatcherProvider,
) : BlockchainSDKFactory { ) : BlockchainSDKFactory {
private val walletManagerFactory: Flow<WalletManagerFactory?> by lazy(::createWalletManagerFactory)
private val mainScope = CoroutineScope(dispatchers.main) private val mainScope = CoroutineScope(dispatchers.main)
private val walletManagerFactory: Flow<WalletManagerFactory?> = createWalletManagerFactory()
override suspend fun init() { override suspend fun init() {
coroutineScope { coroutineScope {
updateBlockchainSDKConfig() updateBlockchainSDKConfig()