Skip to content

Commit

Permalink
ABW-1975 - Depositing accounts are ordered in the same way as in the …
Browse files Browse the repository at this point in the history
…wallet dashboard (#616)

* ABW-1975 - Depositing accounts order not taken from RET but adheres to owned accounts order in the wallet.

* ABW-1975 - TODO added.
  • Loading branch information
raf-rdx authored Nov 1, 2023
1 parent b1c09cb commit 62acdc7
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,58 @@ private fun TransactionType.GeneralTransaction.resolveFromAccounts(
}
}

/**
* The account deposits order that comes from RET does not take into account the order we maintain in the app.
* This method sorts accounts in the same order they are appearing in the dashboard.
* TODO revisit if this can be done using Comparator.comparing { item -> allAccounts.map { it.address }.indexOf(item) }
*/
fun Map<String, List<ResourceTracker>>.sort(allAccounts: List<Network.Account>): Map<String, List<ResourceTracker>> {
val allAccountsAddresses = allAccounts.map { it.address }

// Only account deposits that we own
val ownedAccountDeposits = this.toList().filter {
allAccountsAddresses.indexOf(it.first) != -1
}

// Sorted owned accounts deposits according to the all accounts order
val ownedAccountDepositsSorted = ownedAccountDeposits.sortedBy {
allAccountsAddresses.indexOf(it.first)
}.toMap()

// The rest of account deposits (ones we do not own)
val thirdPartyAccountDeposits = this.minus(ownedAccountDepositsSorted.keys)

return ownedAccountDepositsSorted.plus(thirdPartyAccountDeposits)
}

private fun TransactionType.GeneralTransaction.resolveToAccounts(
allAssets: List<Assets>,
allAccounts: List<Network.Account>,
thirdPartyMetadata: Map<String, List<MetadataItem>> = emptyMap(),
defaultDepositGuarantees: Double
) = accountDeposits.map { depositEntry ->
val transferables = depositEntry.value.map {
it.toDepositingTransferableResource(
allAssets = allAssets,
newlyCreatedMetadata = metadataOfNewlyCreatedEntities,
newlyCreatedEntities = addressesOfNewlyCreatedEntities,
thirdPartyMetadata = thirdPartyMetadata,
defaultDepositGuarantees = defaultDepositGuarantees
)
}
): List<AccountWithTransferableResources> {
return accountDeposits.sort(allAccounts).map { depositEntry ->
val transferables = depositEntry.value.map {
it.toDepositingTransferableResource(
allAssets = allAssets,
newlyCreatedMetadata = metadataOfNewlyCreatedEntities,
newlyCreatedEntities = addressesOfNewlyCreatedEntities,
thirdPartyMetadata = thirdPartyMetadata,
defaultDepositGuarantees = defaultDepositGuarantees
)
}

val ownedAccount = allAccounts.find { it.address == depositEntry.key }
if (ownedAccount != null) {
AccountWithTransferableResources.Owned(
account = ownedAccount,
resources = transferables
)
} else {
AccountWithTransferableResources.Other(
address = depositEntry.key,
resources = transferables
)
val ownedAccount = allAccounts.find { it.address == depositEntry.key }
if (ownedAccount != null) {
AccountWithTransferableResources.Owned(
account = ownedAccount,
resources = transferables
)
} else {
AccountWithTransferableResources.Other(
address = depositEntry.key,
resources = transferables
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.babylon.wallet.android.presentation.transaction

import com.babylon.wallet.android.domain.SampleDataProvider
import com.babylon.wallet.android.presentation.transaction.analysis.sort
import com.radixdlt.ret.ResourceTracker
import kotlinx.coroutines.test.runTest
import org.junit.Test

class GeneralTransactionAnalysisTest {

@Test
fun `when deposit accounts are in the same order, order is retained`() = runTest {
val accountsDeposits = mapOf(
"rdx_t_1" to listOf<ResourceTracker>(),
"rdx_t_2" to listOf(),
)
val allAccounts = listOf(
SampleDataProvider().sampleAccount(
address = "rdx_t_1",
name = "One account"
),
SampleDataProvider().sampleAccount(
address = "rdx_t_2",
name = "Two account"
),
SampleDataProvider().sampleAccount(
address = "rdx_t_3",
name = "Three account"
)
)

val sortedAccountsDeposits = accountsDeposits.sort(allAccounts)
assert(sortedAccountsDeposits.keys.toList()[0] == allAccounts[0].address)
assert(sortedAccountsDeposits.keys.toList()[1] == allAccounts[1].address)
}

@Test
fun `when deposit accounts are not in the same order, order is taken after owned accounts`() = runTest {
val accountsDeposits = mapOf(
"rdx_t_2" to listOf<ResourceTracker>(),
"rdx_t_1" to listOf(),
)
val allAccounts = listOf(
SampleDataProvider().sampleAccount(
address = "rdx_t_1",
name = "One account"
),
SampleDataProvider().sampleAccount(
address = "rdx_t_2",
name = "Two account"
),
SampleDataProvider().sampleAccount(
address = "rdx_t_3",
name = "Three account"
)
)

val sortedAccountsDeposits = accountsDeposits.sort(allAccounts)
assert(sortedAccountsDeposits.keys.toList()[0] == allAccounts[0].address)
assert(sortedAccountsDeposits.keys.toList()[1] == allAccounts[1].address)
}

@Test
fun `when deposit accounts are not in the same order and have third party accounts, owned accounts shown first `() = runTest {
val accountsDeposits = mapOf(
"rdx_t_2" to listOf<ResourceTracker>(),
"rdx_t_1" to listOf(),
"rdx_t_third_party1" to listOf(),
"rdx_t_third_party2" to listOf()
)
val allAccounts = listOf(
SampleDataProvider().sampleAccount(
address = "rdx_t_1",
name = "One account"
),
SampleDataProvider().sampleAccount(
address = "rdx_t_2",
name = "Two account"
),
SampleDataProvider().sampleAccount(
address = "rdx_t_3",
name = "Three account"
)
)

val sortedAccountsDeposits = accountsDeposits.sort(allAccounts)
assert(sortedAccountsDeposits.keys.toList()[0] == allAccounts[0].address)
assert(sortedAccountsDeposits.keys.toList()[1] == allAccounts[1].address)
assert(sortedAccountsDeposits.keys.toList()[2] != allAccounts[2].address)
}
}

0 comments on commit 62acdc7

Please sign in to comment.