Updated on 2026-08-14
This commit is contained in:
parent
b3138bf560
commit
3088258c74
14 changed files with 339 additions and 97 deletions
|
|
@ -3,6 +3,8 @@ package com.tangem.data.account.converter
|
|||
import arrow.core.getOrElse
|
||||
import com.tangem.datasource.api.tangemTech.models.account.GetWalletAccountsResponse
|
||||
import com.tangem.domain.account.models.AccountList
|
||||
import com.tangem.domain.models.TokensGroupType
|
||||
import com.tangem.domain.models.TokensSortType
|
||||
import com.tangem.domain.models.wallet.UserWallet
|
||||
import com.tangem.utils.converter.Converter
|
||||
import dagger.assisted.Assisted
|
||||
|
|
@ -27,12 +29,15 @@ internal class AccountListConverter @AssistedInject constructor(
|
|||
}
|
||||
|
||||
override fun convert(value: GetWalletAccountsResponse): AccountList {
|
||||
val sortType = value.wallet.sort?.let(TokensSortTypeConverter::convert) ?: TokensSortType.NONE
|
||||
val groupType = value.wallet.group?.let(TokensGroupTypeConverter::convert) ?: TokensGroupType.NONE
|
||||
|
||||
return AccountList(
|
||||
userWalletId = userWallet.walletId,
|
||||
accounts = value.accounts.map(cryptoPortfolioConverter::convert),
|
||||
totalAccounts = value.wallet.totalAccounts,
|
||||
sortType = TokensSortTypeConverter.convert(value.wallet.sort),
|
||||
groupType = TokensGroupTypeConverter.convert(value.wallet.group),
|
||||
sortType = sortType,
|
||||
groupType = groupType,
|
||||
)
|
||||
.getOrElse {
|
||||
error("Failed to convert GetWalletAccountsResponse to AccountList: $it")
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import com.tangem.datasource.api.tangemTech.models.account.GetWalletAccountsResp
|
|||
import com.tangem.datasource.api.tangemTech.models.account.SaveWalletAccountsResponse
|
||||
import com.tangem.datasource.api.tangemTech.models.account.WalletAccountDTO
|
||||
import com.tangem.datasource.api.tangemTech.models.account.toUserTokensResponse
|
||||
import com.tangem.datasource.api.tangemTech.models.orDefault
|
||||
import com.tangem.datasource.utils.getSyncOrNull
|
||||
import com.tangem.domain.models.wallet.UserWalletId
|
||||
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
||||
|
|
@ -159,18 +160,18 @@ internal class DefaultWalletAccountsFetcher @Inject constructor(
|
|||
val response = defaultWalletAccountsResponseFactory.create(
|
||||
userWalletId = userWalletId,
|
||||
userTokensResponse = UserTokensResponse(
|
||||
group = accountsResponse.wallet.group,
|
||||
sort = accountsResponse.wallet.sort,
|
||||
group = accountsResponse.wallet.group.orDefault(),
|
||||
sort = accountsResponse.wallet.sort.orDefault(),
|
||||
tokens = accountsResponse.unassignedTokens,
|
||||
),
|
||||
)
|
||||
|
||||
store(userWalletId = userWalletId, response = response)
|
||||
|
||||
push(userWalletId = userWalletId, accounts = response.accounts)
|
||||
userTokensSaver.push(userWalletId = userWalletId, response = response.toUserTokensResponse())
|
||||
val syncedResponse = push(userWalletId = userWalletId, accounts = response.accounts) ?: response
|
||||
|
||||
store(userWalletId = userWalletId, response = syncedResponse)
|
||||
|
||||
return syncedResponse
|
||||
return response
|
||||
}
|
||||
|
||||
private suspend fun assignTokens(
|
||||
|
|
|
|||
|
|
@ -32,13 +32,30 @@ internal fun List<WalletAccountDTO>.assignTokens(
|
|||
userWalletId: UserWalletId,
|
||||
tokens: List<UserTokensResponse.Token>,
|
||||
): List<WalletAccountDTO> {
|
||||
val enrichedTokens = UserTokensResponseAccountIdEnricher(userWalletId, tokens)
|
||||
val enrichedTokensByAccountId = UserTokensResponseAccountIdEnricher(userWalletId, tokens)
|
||||
.groupBy { it.accountId }
|
||||
|
||||
return map { accountDTO ->
|
||||
accountDTO.copy(
|
||||
tokens = enrichedTokens[accountDTO.id].orEmpty(),
|
||||
)
|
||||
val accountTokens = enrichedTokensByAccountId[accountDTO.id].orEmpty()
|
||||
val isMainAccount = accountDTO.derivationIndex == 0
|
||||
|
||||
val tokens = if (isMainAccount) {
|
||||
val existingAccountIds = map(WalletAccountDTO::id).toSet()
|
||||
val unexistingAccountIds = enrichedTokensByAccountId.keys - existingAccountIds
|
||||
|
||||
val customTokens = unexistingAccountIds.flatMap {
|
||||
enrichedTokensByAccountId[it].orEmpty().map { token ->
|
||||
// Tokens from unexisting accounts should be copied to the main account
|
||||
token.copy(accountId = accountDTO.id)
|
||||
}
|
||||
}
|
||||
|
||||
accountTokens + customTokens
|
||||
} else {
|
||||
accountTokens
|
||||
}
|
||||
|
||||
accountDTO.copy(tokens = accountDTO.tokens.orEmpty() + tokens)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -49,5 +66,5 @@ internal fun WalletAccountDTO.assignTokens(
|
|||
val enrichedTokens = UserTokensResponseAccountIdEnricher(userWalletId, tokens)
|
||||
.filter { it.accountId == this.id }
|
||||
|
||||
return copy(tokens = enrichedTokens)
|
||||
return copy(tokens = this.tokens.orEmpty() + enrichedTokens)
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package com.tangem.data.account.utils
|
|||
import com.google.common.truth.Truth
|
||||
import com.tangem.data.account.converter.CryptoPortfolioConverter
|
||||
import com.tangem.data.account.converter.createWalletAccountDTO
|
||||
import com.tangem.data.account.utils.GetWalletAccountsResponseExtTest.Companion.createUserToken
|
||||
import com.tangem.data.common.currency.UserTokensResponseFactory
|
||||
import com.tangem.data.common.network.NetworkFactory
|
||||
import com.tangem.datasource.api.tangemTech.models.UserTokensResponse
|
||||
|
|
@ -10,7 +11,6 @@ import com.tangem.datasource.api.tangemTech.models.account.GetWalletAccountsResp
|
|||
import com.tangem.datasource.local.userwallet.UserWalletsStore
|
||||
import com.tangem.domain.account.models.AccountList
|
||||
import com.tangem.domain.models.account.Account
|
||||
import com.tangem.domain.models.currency.CryptoCurrency
|
||||
import com.tangem.domain.models.wallet.UserWallet
|
||||
import com.tangem.domain.models.wallet.UserWalletId
|
||||
import io.mockk.*
|
||||
|
|
@ -104,15 +104,16 @@ class DefaultWalletAccountsResponseFactoryTest {
|
|||
every { walletId } returns userWalletId
|
||||
}
|
||||
|
||||
coEvery { userWalletsStore.getSyncOrNull(userWalletId) } returns userWallet
|
||||
|
||||
val accounts = AccountList.empty(userWallet.walletId).accounts
|
||||
.filterIsInstance<Account.CryptoPortfolio>()
|
||||
|
||||
coEvery { userWalletsStore.getSyncOrNull(userWalletId) } returns userWallet
|
||||
|
||||
val token = createUserToken(accountIndex = 0)
|
||||
val defaultResponse = UserTokensResponse(
|
||||
group = UserTokensResponse.GroupType.NETWORK,
|
||||
sort = UserTokensResponse.SortType.BALANCE,
|
||||
tokens = listOf(mockk(relaxed = true)),
|
||||
tokens = listOf(token),
|
||||
)
|
||||
every {
|
||||
userTokensResponseFactory.createDefaultResponse(
|
||||
|
|
@ -135,7 +136,7 @@ class DefaultWalletAccountsResponseFactoryTest {
|
|||
sort = defaultResponse.sort,
|
||||
totalAccounts = 1,
|
||||
),
|
||||
accounts = listOf(accountsDTO),
|
||||
accounts = listOf(accountsDTO.copy(tokens = listOf(token))),
|
||||
unassignedTokens = emptyList(),
|
||||
)
|
||||
|
||||
|
|
@ -202,24 +203,20 @@ class DefaultWalletAccountsResponseFactoryTest {
|
|||
val userWallet = mockk<UserWallet>(relaxed = true) {
|
||||
every { walletId } returns userWalletId
|
||||
}
|
||||
val assignedTokens = listOf(mockk<CryptoCurrency.Token>(), mockk<CryptoCurrency.Token>())
|
||||
coEvery { userWalletsStore.getSyncOrNull(userWalletId) } returns userWallet
|
||||
|
||||
val userTokensResponse = UserTokensResponse(
|
||||
group = UserTokensResponse.GroupType.NETWORK,
|
||||
sort = UserTokensResponse.SortType.BALANCE,
|
||||
tokens = listOf(mockk(relaxed = true)),
|
||||
tokens = listOf(
|
||||
createUserToken(accountIndex = 0),
|
||||
createUserToken(accountIndex = 1),
|
||||
),
|
||||
)
|
||||
every {
|
||||
userTokensResponseFactory.createUserTokensResponse(
|
||||
currencies = assignedTokens,
|
||||
isGroupedByNetwork = false,
|
||||
isSortedByBalance = false,
|
||||
)
|
||||
} returns userTokensResponse
|
||||
|
||||
val accounts = AccountList.empty(userWallet.walletId).accounts
|
||||
.filterIsInstance<Account.CryptoPortfolio>()
|
||||
val accountsDTO = createWalletAccountDTO(userWalletId)
|
||||
val accountsDTO = createWalletAccountDTO(userWalletId = userWalletId)
|
||||
every { cryptoPortfolioConverter.convertListBack(accounts) } returns listOf(accountsDTO)
|
||||
|
||||
// Act
|
||||
|
|
@ -232,7 +229,7 @@ class DefaultWalletAccountsResponseFactoryTest {
|
|||
sort = userTokensResponse.sort,
|
||||
totalAccounts = 1,
|
||||
),
|
||||
accounts = listOf(accountsDTO),
|
||||
accounts = listOf(accountsDTO.copy(tokens = userTokensResponse.tokens)),
|
||||
unassignedTokens = emptyList(),
|
||||
)
|
||||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
|
|
|
|||
|
|
@ -70,9 +70,9 @@ class GetWalletAccountsResponseExtTest {
|
|||
@Test
|
||||
fun `flattenTokens returns all tokens from multiple accounts`() {
|
||||
// Arrange
|
||||
val token1 = createUserToken(id = "0")
|
||||
val token2 = createUserToken(id = "1")
|
||||
val token3 = createUserToken(id = "2")
|
||||
val token1 = createUserToken(accountIndex = 0)
|
||||
val token2 = createUserToken(accountIndex = 1)
|
||||
val token3 = createUserToken(accountIndex = 2)
|
||||
|
||||
val account1 = createWalletAccountDTO(derivationIndex = 0, tokens = listOf(token1, token2))
|
||||
val account2 = createWalletAccountDTO(derivationIndex = 1, tokens = listOf(token3))
|
||||
|
|
@ -131,10 +131,9 @@ class GetWalletAccountsResponseExtTest {
|
|||
@Test
|
||||
fun `toUserTokensResponse includes tokens from accounts and unassignedTokens`() {
|
||||
// Arrange
|
||||
val token1 = createUserToken(id = "0")
|
||||
val token2 = createUserToken(id = "1")
|
||||
val token1 = createUserToken(accountIndex = 0)
|
||||
val token2 = createUserToken(accountIndex = 1)
|
||||
val account = createWalletAccountDTO(derivationIndex = 0, tokens = listOf(token1))
|
||||
val unassignedToken = token2
|
||||
|
||||
val response = GetWalletAccountsResponse(
|
||||
wallet = GetWalletAccountsResponse.Wallet(
|
||||
|
|
@ -144,7 +143,7 @@ class GetWalletAccountsResponseExtTest {
|
|||
totalAccounts = 1,
|
||||
),
|
||||
accounts = listOf(account),
|
||||
unassignedTokens = listOf(unassignedToken),
|
||||
unassignedTokens = listOf(token2),
|
||||
)
|
||||
|
||||
// Act
|
||||
|
|
@ -169,8 +168,8 @@ class GetWalletAccountsResponseExtTest {
|
|||
fun `assignTokens correctly assigns tokens to accounts`() {
|
||||
// Arrange
|
||||
val accountId = "957B88B12730E646E0F33D3618B77DFA579E8231E3C59C7104BE7165611C8027"
|
||||
val token1 = createUserToken(id = "0", accountId = null)
|
||||
val token2 = createUserToken(id = "1", accountId = null)
|
||||
val token1 = createUserToken(accountIndex = 0, accountId = null)
|
||||
val token2 = createUserToken(accountIndex = 1, accountId = null)
|
||||
val account1 = createWalletAccountDTO(derivationIndex = 0)
|
||||
val account2 = createWalletAccountDTO(derivationIndex = 1)
|
||||
val response = GetWalletAccountsResponse(
|
||||
|
|
@ -194,10 +193,13 @@ class GetWalletAccountsResponseExtTest {
|
|||
account1.copy(
|
||||
tokens = listOf(
|
||||
token1.copy(accountId = accountId),
|
||||
),
|
||||
),
|
||||
account2.copy(
|
||||
tokens = listOf(
|
||||
token2.copy(accountId = accountId),
|
||||
),
|
||||
),
|
||||
account2,
|
||||
),
|
||||
unassignedTokens = emptyList(),
|
||||
)
|
||||
|
|
@ -232,6 +234,44 @@ class GetWalletAccountsResponseExtTest {
|
|||
|
||||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unassignedTokens contain tokens for unexisting accounts`() {
|
||||
// Arrange
|
||||
val token1 = createUserToken(accountIndex = 0, accountId = null)
|
||||
val token2 = createUserToken(accountIndex = 3, accountId = null)
|
||||
val account = createWalletAccountDTO(derivationIndex = 0)
|
||||
|
||||
val response = GetWalletAccountsResponse(
|
||||
wallet = GetWalletAccountsResponse.Wallet(
|
||||
version = 1,
|
||||
group = UserTokensResponse.GroupType.NONE,
|
||||
sort = UserTokensResponse.SortType.MANUAL,
|
||||
totalAccounts = 2,
|
||||
),
|
||||
accounts = listOf(account),
|
||||
unassignedTokens = listOf(token1, token2),
|
||||
)
|
||||
|
||||
// Act
|
||||
val actual = response.assignTokens(userWalletId)
|
||||
|
||||
// Assert
|
||||
val expected = GetWalletAccountsResponse(
|
||||
wallet = response.wallet,
|
||||
accounts = listOf(
|
||||
account.copy(
|
||||
tokens = listOf(
|
||||
token1.copy(accountId = "957B88B12730E646E0F33D3618B77DFA579E8231E3C59C7104BE7165611C8027"),
|
||||
token2.copy(accountId = "957B88B12730E646E0F33D3618B77DFA579E8231E3C59C7104BE7165611C8027"),
|
||||
),
|
||||
),
|
||||
),
|
||||
unassignedTokens = emptyList(),
|
||||
)
|
||||
|
||||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
|
@ -242,8 +282,8 @@ class GetWalletAccountsResponseExtTest {
|
|||
fun `assignTokens correctly assigns tokens to accounts`() {
|
||||
// Arrange
|
||||
val accountId = "957B88B12730E646E0F33D3618B77DFA579E8231E3C59C7104BE7165611C8027"
|
||||
val token1 = createUserToken(id = "0", accountId = null)
|
||||
val token2 = createUserToken(id = "1", accountId = null)
|
||||
val token1 = createUserToken(accountIndex = 0, accountId = null)
|
||||
val token2 = createUserToken(accountIndex = 1, accountId = null)
|
||||
val account1 = createWalletAccountDTO(derivationIndex = 0)
|
||||
val account2 = createWalletAccountDTO(derivationIndex = 1)
|
||||
|
||||
|
|
@ -258,10 +298,13 @@ class GetWalletAccountsResponseExtTest {
|
|||
account1.copy(
|
||||
tokens = listOf(
|
||||
token1.copy(accountId = accountId),
|
||||
),
|
||||
),
|
||||
account2.copy(
|
||||
tokens = listOf(
|
||||
token2.copy(accountId = accountId),
|
||||
),
|
||||
),
|
||||
account2,
|
||||
)
|
||||
|
||||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
|
|
@ -271,7 +314,7 @@ class GetWalletAccountsResponseExtTest {
|
|||
fun `assignTokens does not change accounts if there are no unassignedTokens`() {
|
||||
// Arrange
|
||||
val accountId = "957B88B12730E646E0F33D3618B77DFA579E8231E3C59C7104BE7165611C8027"
|
||||
val token1 = createUserToken(id = "0", accountId = accountId)
|
||||
val token1 = createUserToken(accountIndex = 0, accountId = accountId)
|
||||
val account1 = createWalletAccountDTO(derivationIndex = 0)
|
||||
|
||||
// Act
|
||||
|
|
@ -308,19 +351,19 @@ class GetWalletAccountsResponseExtTest {
|
|||
totalNetworks = 1,
|
||||
)
|
||||
|
||||
private fun createUserToken(id: String, accountId: String? = "account_id") = UserTokensResponse.Token(
|
||||
id = id,
|
||||
accountId = accountId,
|
||||
networkId = "ethereum",
|
||||
derivationPath = "m/44'/60'/0'/0/0",
|
||||
name = "Token",
|
||||
symbol = "T",
|
||||
contractAddress = "0x$id",
|
||||
decimals = 18,
|
||||
)
|
||||
companion object {
|
||||
|
||||
private companion object {
|
||||
private val userWalletId = UserWalletId("011")
|
||||
|
||||
val userWalletId = UserWalletId("011")
|
||||
fun createUserToken(accountIndex: Int, accountId: String? = "account_id") = UserTokensResponse.Token(
|
||||
id = accountIndex.toString(),
|
||||
accountId = accountId,
|
||||
networkId = "ethereum",
|
||||
derivationPath = "m/44'/60'/0'/0/$accountIndex",
|
||||
name = "Token",
|
||||
symbol = "T",
|
||||
contractAddress = "0x$accountIndex",
|
||||
decimals = 18,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -97,9 +97,9 @@ class UserTokensResponseAccountIdEnricherTest {
|
|||
val validNetworkId = mockCryptoCurrencyFactory.ethereum.network.rawId
|
||||
|
||||
val tokenWithUnknownNetworkId = mockCryptoCurrencyFactory.ethereum.toResponseToken(
|
||||
accountId = null,
|
||||
networkId = unknownNetworkId,
|
||||
derivationPath = "m/44'/60'/0'/0/0",
|
||||
accountId = null,
|
||||
)
|
||||
|
||||
val tokenWithValidNetworkId = mockCryptoCurrencyFactory.ethereum.toResponseToken(
|
||||
|
|
@ -122,6 +122,32 @@ class UserTokensResponseAccountIdEnricherTest {
|
|||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `skips tokens with existing account ids`() {
|
||||
// Arrange
|
||||
val tokenWithAccountId = mockCryptoCurrencyFactory.ethereum
|
||||
.toResponseToken(derivationPath = "m/44'/60'/0'/0/0")
|
||||
.enrichWithAccountId(accountIndex = 0)
|
||||
|
||||
val tokenWithoutAccountId = mockCryptoCurrencyFactory.ethereum.toResponseToken(
|
||||
accountId = null,
|
||||
derivationPath = "m/44'/60'/0'/0/0",
|
||||
)
|
||||
|
||||
val response = listOf(tokenWithAccountId, tokenWithoutAccountId).toResponse()
|
||||
|
||||
// Act
|
||||
val actual = UserTokensResponseAccountIdEnricher(userWalletId, response)
|
||||
|
||||
// Assert
|
||||
val expected = listOf(
|
||||
tokenWithAccountId,
|
||||
tokenWithoutAccountId.enrichWithAccountId(accountIndex = 0),
|
||||
).toResponse()
|
||||
|
||||
Truth.assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
|
||||
private fun CryptoCurrency.toResponseToken(
|
||||
accountId: AccountId? = null,
|
||||
networkId: String? = null,
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import com.tangem.datasource.api.common.response.getOrThrow
|
|||
import com.tangem.datasource.api.tangemTech.TangemTechApi
|
||||
import com.tangem.datasource.api.tangemTech.models.CoinsResponse
|
||||
import com.tangem.datasource.api.tangemTech.models.UserTokensResponse
|
||||
import com.tangem.datasource.api.tangemTech.models.orDefault
|
||||
import com.tangem.datasource.local.config.testnet.TestnetTokensStorage
|
||||
import com.tangem.datasource.local.token.UserTokensResponseStore
|
||||
import com.tangem.datasource.local.userwallet.UserWalletsStore
|
||||
|
|
@ -197,8 +198,8 @@ internal class DefaultManageTokensRepository(
|
|||
|
||||
val tokensResponse = response?.let {
|
||||
UserTokensResponse(
|
||||
group = response.wallet.group,
|
||||
sort = response.wallet.sort,
|
||||
group = response.wallet.group.orDefault(),
|
||||
sort = response.wallet.sort.orDefault(),
|
||||
tokens = accountDTO?.tokens.orEmpty(),
|
||||
)
|
||||
}
|
||||
|
|
@ -295,8 +296,8 @@ internal class DefaultManageTokensRepository(
|
|||
|
||||
val tokensResponse = response?.let {
|
||||
UserTokensResponse(
|
||||
group = response.wallet.group,
|
||||
sort = response.wallet.sort,
|
||||
group = response.wallet.group.orDefault(),
|
||||
sort = response.wallet.sort.orDefault(),
|
||||
tokens = accountDTO?.tokens.orEmpty(),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue