Skip to content

Commit

Permalink
compute guarantees for staking/pool tx previews, optimize claim proce…
Browse files Browse the repository at this point in the history
…ssor logic
  • Loading branch information
jakub-rdx committed Jan 29, 2024
1 parent 06a1430 commit adee6f0
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ sealed interface Asset {
// - token
// - LSU
// - pool unit
sealed interface Fungible: Asset {
sealed interface Fungible : Asset {
override val resource: Resource.FungibleResource
}

// Asset that is non fungible and needs a local id from a collection
// - NFT
// - stake claim
sealed interface NonFungible: Asset {
sealed interface NonFungible : Asset {
override val resource: Resource.NonFungibleResource
}

}

data class Assets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.babylon.wallet.android.presentation.status.assets
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Spacer
Expand All @@ -17,7 +16,6 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.babylon.wallet.android.R
Expand Down Expand Up @@ -108,7 +106,6 @@ fun AssetDialog(
}
}


@Composable
fun Asset.displayTitle() = when (this) {
is Token -> resource.name
Expand Down Expand Up @@ -208,4 +205,4 @@ fun TagsSection(
Spacer(modifier = Modifier.height(RadixTheme.dimensions.paddingXLarge))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ fun NavController.fungibleAssetDialog(
underAccountAddress: String? = null
) {
val amountParam = if (amount != null) "&$ARG_AMOUNT=${amount.toPlainString()}" else ""
val underAccountAddressParam = if (underAccountAddress != null) "&$ARG_UNDER_ACCOUNT_ADDRESS=${underAccountAddress}" else ""
val underAccountAddressParam = if (underAccountAddress != null) "&$ARG_UNDER_ACCOUNT_ADDRESS=$underAccountAddress" else ""
navigate(
route = "$ROUTE/$ARG_RESOURCE_TYPE_VALUE_FUNGIBLE" +
"?${ARG_RESOURCE_ADDRESS}=$resourceAddress" +
"&${ARG_NEWLY_CREATED}=$isNewlyCreated" +
amountParam +
underAccountAddressParam
"?${ARG_RESOURCE_ADDRESS}=$resourceAddress" +
"&${ARG_NEWLY_CREATED}=$isNewlyCreated" +
amountParam +
underAccountAddressParam
)
}

Expand All @@ -45,13 +45,13 @@ fun NavController.nftAssetDialog(
underAccountAddress: String? = null
) {
val localIdParam = if (localId != null) "&$ARG_LOCAL_ID=${URLEncoder.encode(localId, Charsets.UTF_8.name())}" else ""
val underAccountAddressParam = if (underAccountAddress != null) "&$ARG_UNDER_ACCOUNT_ADDRESS=${underAccountAddress}" else ""
val underAccountAddressParam = if (underAccountAddress != null) "&$ARG_UNDER_ACCOUNT_ADDRESS=$underAccountAddress" else ""
navigate(
route = "$ROUTE/$ARG_RESOURCE_TYPE_VALUE_NFT" +
"?${ARG_RESOURCE_ADDRESS}=$resourceAddress" +
"&${ARG_NEWLY_CREATED}=$isNewlyCreated" +
localIdParam +
underAccountAddressParam
"?${ARG_RESOURCE_ADDRESS}=$resourceAddress" +
"&${ARG_NEWLY_CREATED}=$isNewlyCreated" +
localIdParam +
underAccountAddressParam
)
}

Expand All @@ -76,7 +76,7 @@ sealed interface AssetDialogArgs {
override val underAccountAddress: String?,
val localId: String?
) : AssetDialogArgs

companion object {
fun from(savedStateHandle: SavedStateHandle): AssetDialogArgs {
return when (requireNotNull(savedStateHandle[ARG_RESOURCE_TYPE])) {
Expand All @@ -85,13 +85,13 @@ sealed interface AssetDialogArgs {
isNewlyCreated = requireNotNull(savedStateHandle[ARG_NEWLY_CREATED]),
underAccountAddress = savedStateHandle[ARG_UNDER_ACCOUNT_ADDRESS],
amount = savedStateHandle.get<String>(ARG_AMOUNT)?.toBigDecimalOrNull()
);
)
ARG_RESOURCE_TYPE_VALUE_NFT -> NFT(
resourceAddress = requireNotNull(savedStateHandle[ARG_RESOURCE_ADDRESS]),
isNewlyCreated = requireNotNull(savedStateHandle[ARG_NEWLY_CREATED]),
underAccountAddress = savedStateHandle[ARG_UNDER_ACCOUNT_ADDRESS],
localId = savedStateHandle[ARG_LOCAL_ID]
);
)
else -> error("No type specified.")
}
}
Expand All @@ -103,11 +103,11 @@ fun NavGraphBuilder.assetDialog(
) {
dialog(
route = "$ROUTE/{$ARG_RESOURCE_TYPE}" +
"?$ARG_RESOURCE_ADDRESS={$ARG_RESOURCE_ADDRESS}" +
"&$ARG_NEWLY_CREATED={$ARG_NEWLY_CREATED}" +
"&$ARG_AMOUNT={$ARG_AMOUNT}" +
"&$ARG_LOCAL_ID={$ARG_LOCAL_ID}" +
"&$ARG_UNDER_ACCOUNT_ADDRESS={$ARG_UNDER_ACCOUNT_ADDRESS}",
"?$ARG_RESOURCE_ADDRESS={$ARG_RESOURCE_ADDRESS}" +
"&$ARG_NEWLY_CREATED={$ARG_NEWLY_CREATED}" +
"&$ARG_AMOUNT={$ARG_AMOUNT}" +
"&$ARG_LOCAL_ID={$ARG_LOCAL_ID}" +
"&$ARG_UNDER_ACCOUNT_ADDRESS={$ARG_UNDER_ACCOUNT_ADDRESS}",
arguments = listOf(
navArgument(ARG_RESOURCE_TYPE) {
type = NavType.StringType
Expand Down Expand Up @@ -141,4 +141,4 @@ fun NavGraphBuilder.assetDialog(
onDismiss = onDismiss
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,5 @@ class AssetDialogViewModel @Inject constructor(
private const val EPOCH_TIME_MINUTES = 5
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ fun FungibleDialogContent(
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,4 @@ private fun LSUResourceValue(
@Composable
fun LiquidStakeUnit?.name(): String = this?.name?.ifEmpty {
stringResource(id = R.string.account_poolUnits_unknownPoolUnitName)
} ?: stringResource(id = R.string.account_poolUnits_unknownPoolUnitName)
} ?: stringResource(id = R.string.account_poolUnits_unknownPoolUnitName)
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,13 @@ fun NonFungibleAssetDialogContent(

@Composable
private fun AssetDialogViewModel.State.ClaimState.description() = when (this) {
is AssetDialogViewModel.State.ClaimState.ReadyToClaim -> stringResource(id = R.string.assetDetails_staking_readyToClaim, amount.displayableQuantity())
is AssetDialogViewModel.State.ClaimState.ReadyToClaim -> stringResource(
id = R.string.assetDetails_staking_readyToClaim,
amount.displayableQuantity()
)
is AssetDialogViewModel.State.ClaimState.Unstaking -> stringResource(
id = R.string.assetDetails_staking_unstaking,
amount.displayableQuantity(),
approximateClaimMinutes
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,4 @@ fun PoolUnitDialogContent(
tags = poolUnit?.resource?.tags,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.babylon.wallet.android.presentation.transaction.analysis.processor

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.Asset
import com.babylon.wallet.android.domain.model.assets.PoolUnit
import com.babylon.wallet.android.domain.model.resources.Resource
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.assets.GetPoolDetailsUseCase
import com.babylon.wallet.android.domain.usecases.assets.ResolveAssetsFromAddressUseCase
import com.babylon.wallet.android.presentation.transaction.AccountWithTransferableResources
import com.babylon.wallet.android.presentation.transaction.PreviewType
import com.radixdlt.ret.DetailedManifestClass
Expand All @@ -21,37 +21,40 @@ import rdx.works.profile.domain.accountsOnCurrentNetwork
import javax.inject.Inject

class PoolContributionProcessor @Inject constructor(
private val getResourcesUseCase: GetResourcesUseCase,
private val resolveAssetsFromAddressUseCase: ResolveAssetsFromAddressUseCase,
private val getPoolDetailsUseCase: GetPoolDetailsUseCase,
private val getProfileUseCase: GetProfileUseCase
) : PreviewTypeProcessor<DetailedManifestClass.PoolContribution> {
override suspend fun process(summary: ExecutionSummary, classification: DetailedManifestClass.PoolContribution): PreviewType {
val resources = getResourcesUseCase(addresses = summary.involvedResourceAddresses).getOrThrow()
val assets = resolveAssetsFromAddressUseCase(
fungibleAddresses = summary.involvedFungibleAddresses,
nonFungibleIds = summary.involvedNonFungibleIds
).getOrThrow()
val involvedPools = getPoolDetailsUseCase(classification.poolAddresses.map { it.addressString() }.toSet()).getOrThrow()
val defaultDepositGuarantees = getProfileUseCase.invoke().first().appPreferences.transaction.defaultDepositGuarantee
val defaultDepositGuarantee = getProfileUseCase.invoke().first().appPreferences.transaction.defaultDepositGuarantee
val accountsWithdrawnFrom = summary.accountWithdraws.keys
val ownedAccountsWithdrawnFrom = getProfileUseCase.accountsOnCurrentNetwork().filter {
accountsWithdrawnFrom.contains(it.address)
}
val from = summary.extractWithdraws(ownedAccountsWithdrawnFrom, resources)
val from = summary.extractWithdraws(ownedAccountsWithdrawnFrom, assets.map { it.resource })
val to = summary.accountDeposits.map { depositsPerAddress ->
val ownedAccount = getProfileUseCase.accountOnCurrentNetwork(depositsPerAddress.key) ?: error("No account found")
val deposits = depositsPerAddress.value.mapNotNull { deposit ->
val deposits = depositsPerAddress.value.map { deposit ->
val resourceAddress = deposit.resourceAddress
val contributions = classification.poolContributions.filter {
it.poolUnitsResourceAddress.addressString() == resourceAddress
}
if (contributions.isEmpty()) {
null
resolveGeneralAsset(deposit, summary, assets, defaultDepositGuarantee)
} else {
val pool = involvedPools.find { it.address == contributions.first().poolAddress.addressString() }
?: error("No pool found")
val poolResource = resources.find { it.resourceAddress == pool.metadata.poolUnit() } as? Resource.FungibleResource
val poolResource = assets.find {
it.resource.resourceAddress == pool.metadata.poolUnit()
}?.resource as? Resource.FungibleResource
?: error("No pool resource found")
val contributedResourceAddresses = contributions.first().contributedResources.keys
val guaranteeType = (deposit as? ResourceIndicator.Fungible)?.guaranteeType(defaultDepositGuarantees)
?: GuaranteeType.Guaranteed

val guaranteeType = deposit.guaranteeType(defaultDepositGuarantee)
val poolUnitAmount = contributions.find {
it.poolUnitsResourceAddress.addressString() == poolResource.resourceAddress
}?.poolUnitsAmount?.asStr()?.toBigDecimalOrNull()
Expand Down Expand Up @@ -83,6 +86,24 @@ class PoolContributionProcessor @Inject constructor(
)
}

private fun resolveGeneralAsset(
deposit: ResourceIndicator,
summary: ExecutionSummary,
involvedAssets: List<Asset>,
defaultDepositGuarantee: Double
): Transferable.Depositing {
val asset = if (deposit.isNewlyCreated(summary = summary)) {
deposit.toNewlyCreatedTransferableAsset(deposit.newlyCreatedMetadata(summary = summary))
} else {
deposit.toTransferableAsset(involvedAssets)
}

return Transferable.Depositing(
transferable = asset,
guaranteeType = deposit.guaranteeType(defaultDepositGuarantee)
)
}

private fun ExecutionSummary.extractWithdraws(allOwnedAccounts: List<Network.Account>, resources: List<Resource>) =
accountWithdraws.entries.map { transferEntry ->
val accountOnNetwork = allOwnedAccounts.find { it.address == transferEntry.key }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,22 @@ class PoolRedemptionProcessor @Inject constructor(
resources: List<Resource>,
defaultGuarantee: Double
) = accountDeposits.entries.map { transferEntry ->
val accountOnNetwork = allOwnedAccounts.find { it.address == transferEntry.key }
val accountOnNetwork = allOwnedAccounts.find { it.address == transferEntry.key }

val depositing = transferEntry.value.map { resourceIndicator ->
Transferable.Depositing(
transferable = resourceIndicator.toTransferableResource(resources),
guaranteeType = resourceIndicator.guaranteeType(defaultGuarantee)
)
}
accountOnNetwork?.let { account ->
AccountWithTransferableResources.Owned(
account = account,
resources = depositing
)
} ?: AccountWithTransferableResources.Other(
address = transferEntry.key,
resources = depositing
val depositing = transferEntry.value.map { resourceIndicator ->
Transferable.Depositing(
transferable = resourceIndicator.toTransferableResource(resources),
guaranteeType = resourceIndicator.guaranteeType(defaultGuarantee)
)
}
accountOnNetwork?.let { account ->
AccountWithTransferableResources.Owned(
account = account,
resources = depositing
)
} ?: AccountWithTransferableResources.Other(
address = transferEntry.key,
resources = depositing
)
}
}
Loading

0 comments on commit adee6f0

Please sign in to comment.