Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unknown pools sheet and merge functionality with unknown components sheet #768

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import android.net.Uri
import androidx.core.net.toUri
import com.babylon.wallet.android.data.gateway.model.ExplicitMetadataKey
import com.babylon.wallet.android.utils.toAddressOrNull
import com.radixdlt.ret.EntityType.GLOBAL_MULTI_RESOURCE_POOL
import com.radixdlt.ret.EntityType.GLOBAL_ONE_RESOURCE_POOL
import com.radixdlt.ret.EntityType.GLOBAL_TWO_RESOURCE_POOL
import com.radixdlt.ret.EntityType.GLOBAL_VALIDATOR
import rdx.works.core.ret.isPool
import java.math.BigDecimal

private fun List<Metadata>.findPrimitive(key: ExplicitMetadataKey, type: MetadataType): Metadata.Primitive? = find {
Expand Down Expand Up @@ -66,7 +64,7 @@ fun List<Metadata>.poolAddress(): String? = findPrimitive(
key = ExplicitMetadataKey.POOL,
type = MetadataType.Address
)?.value?.takeIf { value ->
value.toAddressOrNull()?.entityType() in setOf(GLOBAL_ONE_RESOURCE_POOL, GLOBAL_TWO_RESOURCE_POOL, GLOBAL_MULTI_RESOURCE_POOL)
value.toAddressOrNull()?.entityType()?.isPool() == true
}

fun List<Metadata>.ownerBadge(): OwnerBadge? = findPrimitive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.babylon.wallet.android.domain.model.DApp
import rdx.works.core.then
import javax.inject.Inject

class ResolveDAppInTransactionUseCase @Inject constructor(
class ResolveComponentAddressesUseCase @Inject constructor(
private val stateRepository: StateRepository
) {

Expand All @@ -17,9 +17,9 @@ class ResolveDAppInTransactionUseCase @Inject constructor(
* - validate that account_type is "dapp definition"
* - check if componentAddress is within claimed_entities metadata of dAppDefinitionAddress metadata
*/
suspend operator fun invoke(
suspend fun invoke(
componentAddress: String
): Result<Pair<DApp, Boolean>> = stateRepository.getDAppsDetails(
): Result<Pair<String, DApp?>> = stateRepository.getDAppsDetails(
definitionAddresses = listOf(componentAddress),
isRefreshing = true
).then { components ->
Expand All @@ -30,7 +30,8 @@ class ResolveDAppInTransactionUseCase @Inject constructor(
isRefreshing = true
).mapCatching { dApps ->
val dApp = dApps.first()
dApp to dApp.claimedEntities.contains(componentAddress)

componentAddress to dApp.takeIf { it.claimedEntities.contains(componentAddress) }
}
} else {
Result.failure(RadixWalletException.DappVerificationException.WrongAccountType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.babylon.wallet.android.presentation.settings.authorizeddapps.dappdetail

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand All @@ -26,51 +24,63 @@ import com.babylon.wallet.android.designsystem.theme.RadixWalletTheme
import com.babylon.wallet.android.presentation.ui.composables.ActionableAddressView
import com.babylon.wallet.android.presentation.ui.composables.BottomDialogHeader
import com.babylon.wallet.android.presentation.ui.composables.Thumbnail
import com.radixdlt.ret.Address
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import rdx.works.core.ret.isPool

@Composable
fun UnknownDAppComponentsSheetContent(
fun UnknownComponentsSheetContent(
onBackClick: () -> Unit,
modifier: Modifier = Modifier,
unknownDAppComponents: ImmutableList<String>
unknownComponentAddresses: ImmutableList<String>
) {
val addresses = runCatching {
unknownComponentAddresses.associateWith { Address(it) }
}.getOrNull()

val isPools = remember(addresses) {
addresses?.values?.all { it.entityType().isPool() } ?: false
}

Column(modifier = modifier) {
val title = stringResource(id = R.string.transactionReview_unknownComponents, unknownDAppComponents.size)
BottomDialogHeader(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = RadixTheme.dimensions.paddingSmall),
title = title,
title = stringResource(
id = if (isPools) R.string.transactionReview_unknownPools else R.string.transactionReview_unknownComponents,
unknownComponentAddresses.size
),
onDismissRequest = onBackClick
)
HorizontalDivider(color = RadixTheme.colors.gray5)
val lastItem = unknownDAppComponents.last()
LazyColumn(
contentPadding = PaddingValues(vertical = RadixTheme.dimensions.paddingDefault),
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.padding(RadixTheme.dimensions.paddingDefault)
) {
items(unknownDAppComponents) { unknownComponentAddress ->
LazyColumn {
itemsIndexed(unknownComponentAddresses) { index, unknownComponentAddress ->
Column {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(RadixTheme.dimensions.paddingSmall)
.padding(vertical = RadixTheme.dimensions.paddingDefault),
verticalAlignment = Alignment.CenterVertically
.padding(RadixTheme.dimensions.paddingDefault),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(RadixTheme.dimensions.paddingDefault)
) {
Thumbnail.DApp(
modifier = Modifier.size(44.dp),
dapp = null,
shape = RadixTheme.shapes.roundedRectXSmall
)
Spacer(modifier = Modifier.width(RadixTheme.dimensions.paddingDefault))

Column {
val isPool = remember(unknownComponentAddresses) {
addresses?.get(unknownComponentAddress)?.entityType()?.isPool() ?: false
}
Text(
text = stringResource(id = R.string.common_component),
text = if (isPool) {
stringResource(id = R.string.common_pool)
} else {
stringResource(id = R.string.common_component)
},
style = RadixTheme.typography.body1Header,
color = RadixTheme.colors.gray1,
overflow = TextOverflow.Ellipsis
Expand All @@ -84,10 +94,9 @@ fun UnknownDAppComponentsSheetContent(
}
}

if (lastItem != unknownComponentAddress) {
if (index != unknownComponentAddresses.lastIndex) {
HorizontalDivider(
modifier = Modifier
.fillMaxWidth(),
modifier = Modifier.fillMaxWidth(),
color = RadixTheme.colors.gray5
)
}
Expand All @@ -101,9 +110,9 @@ fun UnknownDAppComponentsSheetContent(
@Composable
fun UnknownDAppComponentsSheetContentPreview() {
RadixWalletTheme {
UnknownDAppComponentsSheetContent(
UnknownComponentsSheetContent(
onBackClick = {},
unknownDAppComponents = persistentListOf(
unknownComponentAddresses = persistentListOf(
"component_tdx_b_1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq8z96qp",
"component_tdx_b_1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq8z16qp"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import com.babylon.wallet.android.domain.model.TransactionManifestData
import com.babylon.wallet.android.domain.model.resources.Resource
import com.babylon.wallet.android.domain.userFriendlyMessage
import com.babylon.wallet.android.presentation.common.FullscreenCircularProgressContent
import com.babylon.wallet.android.presentation.settings.authorizeddapps.dappdetail.UnknownDAppComponentsSheetContent
import com.babylon.wallet.android.presentation.settings.authorizeddapps.dappdetail.UnknownComponentsSheetContent
import com.babylon.wallet.android.presentation.status.signing.FactorSourceInteractionBottomDialog
import com.babylon.wallet.android.presentation.transaction.TransactionReviewViewModel.State
import com.babylon.wallet.android.presentation.transaction.composables.AccountDepositSettingsTypeContent
Expand Down Expand Up @@ -108,7 +108,7 @@ fun TransactionReviewScreen(
onGuaranteeValueIncreased = viewModel::onGuaranteeValueIncreased,
onGuaranteeValueDecreased = viewModel::onGuaranteeValueDecreased,
onDAppClick = onDAppClick,
onUnknownDAppsClick = viewModel::onUnknownDAppsClick,
onUnknownComponentsClick = viewModel::onUnknownComponentsClick,
onFungibleResourceClick = viewModel::onFungibleResourceClick,
onNonFungibleResourceClick = viewModel::onNonFungibleResourceClick,
onChangeFeePayerClick = viewModel::onChangeFeePayerClick,
Expand Down Expand Up @@ -168,7 +168,7 @@ private fun TransactionPreviewContent(
onGuaranteeValueIncreased: (AccountWithPredictedGuarantee) -> Unit,
onGuaranteeValueDecreased: (AccountWithPredictedGuarantee) -> Unit,
onDAppClick: (DApp) -> Unit,
onUnknownDAppsClick: (ImmutableList<String>) -> Unit,
onUnknownComponentsClick: (ImmutableList<String>) -> Unit,
onFungibleResourceClick: (Resource.FungibleResource, Boolean) -> Unit,
onNonFungibleResourceClick: (Resource.NonFungibleResource, Resource.NonFungibleResource.Item, Boolean) -> Unit,
onChangeFeePayerClick: () -> Unit,
Expand Down Expand Up @@ -315,7 +315,9 @@ private fun TransactionPreviewContent(
preview = preview,
onPromptForGuarantees = promptForGuarantees,
onDAppClick = onDAppClick,
onUnknownDAppsClick = onUnknownDAppsClick,
onUnknownComponentsClick = { componentAddresses ->
onUnknownComponentsClick(componentAddresses.toPersistentList())
},
onFungibleResourceClick = onFungibleResourceClick,
onNonFungibleResourceClick = onNonFungibleResourceClick
)
Expand Down Expand Up @@ -352,7 +354,10 @@ private fun TransactionPreviewContent(
onFungibleResourceClick = onFungibleResourceClick,
onPromptForGuarantees = promptForGuarantees,
previewType = preview,
onDAppClick = onDAppClick
onDAppClick = onDAppClick,
onUnknownPoolsClick = { pools ->
onUnknownComponentsClick(pools.map { it.address }.toPersistentList())
}
)
ReceiptEdge(modifier = Modifier.fillMaxWidth(), color = RadixTheme.colors.gray5)
}
Expand Down Expand Up @@ -457,11 +462,11 @@ private fun BottomSheetContent(
)
}

is State.Sheet.UnknownDAppComponents -> {
UnknownDAppComponentsSheetContent(
is State.Sheet.UnknownComponents -> {
UnknownComponentsSheetContent(
modifier = modifier,
onBackClick = onCloseBottomSheetClick,
unknownDAppComponents = sheetState.unknownComponentAddresses
unknownComponentAddresses = sheetState.unknownComponentAddresses
)
}

Expand Down Expand Up @@ -523,7 +528,7 @@ fun TransactionPreviewContentPreview() {
promptForGuarantees = {},
onCustomizeClick = {},
onDAppClick = {},
onUnknownDAppsClick = {},
onUnknownComponentsClick = {},
onFungibleResourceClick = { _, _ -> },
onNonFungibleResourceClick = { _, _, _ -> },
onGuaranteeValueChanged = { _, _ -> },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.babylon.wallet.android.domain.model.Transferable
import com.babylon.wallet.android.domain.model.TransferableAsset
import com.babylon.wallet.android.domain.model.assets.ValidatorDetail
import com.babylon.wallet.android.domain.model.resources.Badge
import com.babylon.wallet.android.domain.model.resources.Pool
import com.babylon.wallet.android.domain.model.resources.Resource
import com.babylon.wallet.android.domain.model.resources.Resource.FungibleResource
import com.babylon.wallet.android.domain.model.resources.Resource.NonFungibleResource
Expand Down Expand Up @@ -217,9 +216,9 @@ class TransactionReviewViewModel @Inject constructor(
}
}

fun onUnknownDAppsClick(unknownComponentAddresses: ImmutableList<String>) {
fun onUnknownComponentsClick(unknownComponentAddresses: ImmutableList<String>) {
_state.update {
it.copy(sheetState = State.Sheet.UnknownDAppComponents(unknownComponentAddresses))
it.copy(sheetState = State.Sheet.UnknownComponents(unknownComponentAddresses))
}
}

Expand Down Expand Up @@ -447,7 +446,7 @@ class TransactionReviewViewModel @Inject constructor(
}
}

data class UnknownDAppComponents(
data class UnknownComponents(
val unknownComponentAddresses: ImmutableList<String>
) : Sheet
}
Expand Down Expand Up @@ -538,7 +537,7 @@ sealed interface PreviewType {
override val from: List<AccountWithTransferableResources>,
override val to: List<AccountWithTransferableResources>,
val badges: List<Badge> = emptyList(),
val dApps: List<Pair<DApp, Boolean>> = emptyList()
val dApps: List<Pair<String, DApp?>> = emptyList()
) : Transfer
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.babylon.wallet.android.presentation.transaction.analysis.processor

import com.babylon.wallet.android.domain.usecases.ResolveDAppInTransactionUseCase
import com.babylon.wallet.android.domain.usecases.ResolveComponentAddressesUseCase
import com.babylon.wallet.android.domain.usecases.assets.ResolveAssetsFromAddressUseCase
import com.babylon.wallet.android.domain.usecases.transaction.GetTransactionBadgesUseCase
import com.babylon.wallet.android.presentation.transaction.PreviewType
Expand All @@ -18,13 +18,11 @@ class GeneralTransferProcessor @Inject constructor(
private val resolveAssetsFromAddressUseCase: ResolveAssetsFromAddressUseCase,
private val getTransactionBadgesUseCase: GetTransactionBadgesUseCase,
private val getProfileUseCase: GetProfileUseCase,
private val resolveDAppInTransactionUseCase: ResolveDAppInTransactionUseCase
private val resolveComponentAddressesUseCase: ResolveComponentAddressesUseCase
) : PreviewTypeProcessor<DetailedManifestClass.General> {
override suspend fun process(summary: ExecutionSummary, classification: DetailedManifestClass.General): PreviewType {
val badges = getTransactionBadgesUseCase(accountProofs = summary.presentedProofs)
val dApps = summary.resolveDApps(resolveDAppInTransactionUseCase).distinctBy {
it.first.definitionAddresses
}
val dApps = summary.resolveDApps()
val involvedAccountAddresses = summary.accountWithdraws.keys + summary.accountDeposits.keys
val allOwnedAccounts = getProfileUseCase.accountsOnCurrentNetwork().filter {
involvedAccountAddresses.contains(it.address)
Expand All @@ -50,16 +48,15 @@ class GeneralTransferProcessor @Inject constructor(
)
}

private suspend fun ExecutionSummary.resolveDApps(
resolveDAppInTransactionUseCase: ResolveDAppInTransactionUseCase
) = coroutineScope {
private suspend fun ExecutionSummary.resolveDApps() = coroutineScope {
encounteredEntities.filter { it.isGlobalComponent() }
.map { address ->
async {
resolveDAppInTransactionUseCase.invoke(address.addressString())
resolveComponentAddressesUseCase.invoke(address.addressString())
}
}
.awaitAll()
.mapNotNull { it.getOrNull() }
.distinctBy { it.first }
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.babylon.wallet.android.presentation.transaction.analysis.processor

import com.babylon.wallet.android.domain.model.DApp
import com.babylon.wallet.android.domain.model.GuaranteeType
import com.babylon.wallet.android.domain.model.Transferable
import com.babylon.wallet.android.domain.model.TransferableAsset
import com.babylon.wallet.android.domain.model.assets.PoolUnit
import com.babylon.wallet.android.domain.model.resources.Pool
import com.babylon.wallet.android.domain.model.resources.Resource
import com.babylon.wallet.android.domain.model.resources.metadata.dAppDefinition
import com.babylon.wallet.android.domain.model.resources.metadata.poolUnit
import com.babylon.wallet.android.domain.usecases.GetResourcesUseCase
import com.babylon.wallet.android.domain.usecases.ResolveDAppInTransactionUseCase
import com.babylon.wallet.android.domain.usecases.assets.GetPoolDetailsUseCase
import com.babylon.wallet.android.presentation.transaction.AccountWithTransferableResources
import com.babylon.wallet.android.presentation.transaction.PreviewType
Expand Down Expand Up @@ -101,16 +97,3 @@ class PoolContributionProcessor @Inject constructor(
)
}
}

suspend fun List<Pool>.resolveDApps(
resolveDAppInTransactionUseCase: ResolveDAppInTransactionUseCase
): Map<Pool, DApp?> = mapNotNull { pool ->
val dapp = pool.metadata.dAppDefinition()?.let { address ->
resolveDAppInTransactionUseCase(address)
}?.getOrNull()
if (dapp == null || !dapp.second) {
pool to null
} else {
pool to dapp.first
}
}.toMap()
Loading
Loading