Skip to content

Commit

Permalink
display guarantees for pool units
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-rdx committed Jan 15, 2024
1 parent 68573ec commit df475b1
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class SampleDataProvider {
)

val transferableDepositing = Transferable.Depositing(
transferable = TransferableResource.Amount(
transferable = TransferableResource.FungibleAmount(
amount = BigDecimal(69),
resource = Resource.FungibleResource(
resourceAddress = "resource_tdx_e_1tkawacgvcw7z9xztccgjrged25c7nqtnd4nllh750s2ny64m0cltmg",
Expand Down Expand Up @@ -136,7 +136,7 @@ class SampleDataProvider {

val transferableDepositingPool = Transferable.Depositing(
transferable = TransferableResource.PoolUnitAmount(
pool = PoolUnit(
poolUnit = PoolUnit(
Resource.FungibleResource(
resourceAddress = "resource_tdx_e_1tkawacgvcw7z9xztccgjrged25c7nqtnd4nllh750s2ny64m0cltmg",
ownedAmount = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sealed interface Transferable {
val predicted = guaranteeType as? GuaranteeType.Predicted ?: return null

when (val transferable = transferable) {
is TransferableResource.Amount -> GuaranteeAssertion.ForAmount(
is TransferableResource.FungibleAmount -> GuaranteeAssertion.ForAmount(
amount = transferable.amount * predicted.guaranteeOffset.toBigDecimal(),
instructionIndex = predicted.instructionIndex
)
Expand All @@ -36,6 +36,7 @@ sealed interface Transferable {
amount = transferable.amount * predicted.guaranteeOffset.toBigDecimal(),
instructionIndex = predicted.instructionIndex
)

is TransferableResource.StakeClaimNft -> null
is TransferableResource.PoolUnitAmount -> GuaranteeAssertion.ForAmount(
amount = transferable.amount * predicted.guaranteeOffset.toBigDecimal(),
Expand All @@ -48,6 +49,14 @@ sealed interface Transferable {
}
}

val hasEditableGuarantees: Boolean
get() {
return when (this) {
is Depositing -> guaranteeType is GuaranteeType.Predicted && !transferable.isNewlyCreated
is Withdrawing -> false
}
}

data class Depositing(
override val transferable: TransferableResource,
val guaranteeType: GuaranteeType = GuaranteeType.Guaranteed
Expand Down Expand Up @@ -107,19 +116,25 @@ sealed interface TransferableResource {
get() = resource.resourceAddress
val isNewlyCreated: Boolean

data class Amount(
val amount: BigDecimal,
data class FungibleAmount(
override val amount: BigDecimal,
override val resource: Resource.FungibleResource,
override val isNewlyCreated: Boolean
) : TransferableResource
) : TransferableResource, TransferableWithGuarantees {
override val fungibleResource: Resource.FungibleResource
get() = resource
}

data class LsuAmount(
val amount: BigDecimal,
override val amount: BigDecimal,
override val resource: Resource.FungibleResource,
val validatorDetail: ValidatorDetail,
val xrdWorth: BigDecimal,
override val isNewlyCreated: Boolean = false
) : TransferableResource
) : TransferableResource, TransferableWithGuarantees {
override val fungibleResource: Resource.FungibleResource
get() = resource
}

data class NFTs(
override val resource: Resource.NonFungibleResource,
Expand All @@ -134,12 +149,19 @@ sealed interface TransferableResource {
) : TransferableResource

data class PoolUnitAmount(
val amount: BigDecimal,
val pool: PoolUnit,
override val amount: BigDecimal,
val poolUnit: PoolUnit,
val contributionPerResource: Map<String, BigDecimal>,
override val isNewlyCreated: Boolean = false,
) : TransferableResource {
) : TransferableResource, TransferableWithGuarantees {
override val resource: Resource
get() = pool.stake
get() = poolUnit.stake
override val fungibleResource: Resource.FungibleResource
get() = poolUnit.stake
}
}

interface TransferableWithGuarantees {
val fungibleResource: Resource.FungibleResource
val amount: BigDecimal
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ private fun TransactionPreviewContent(
state = state,
onFungibleResourceClick = onFungibleResourceClick,
onNonFungibleResourceClick = onNonFungibleResourceClick,
onPromptForGuarantees = promptForGuarantees,
previewType = preview
)
ReceiptEdge(modifier = Modifier.fillMaxWidth(), color = RadixTheme.colors.gray5)
Expand All @@ -325,6 +326,7 @@ private fun TransactionPreviewContent(
modifier = Modifier.background(RadixTheme.colors.gray5),
state = state,
onFungibleResourceClick = onFungibleResourceClick,
onPromptForGuarantees = promptForGuarantees,
previewType = preview
)
ReceiptEdge(modifier = Modifier.fillMaxWidth(), color = RadixTheme.colors.gray5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.babylon.wallet.android.domain.model.GuaranteeAssertion
import com.babylon.wallet.android.domain.model.MessageFromDataChannel
import com.babylon.wallet.android.domain.model.Transferable
import com.babylon.wallet.android.domain.model.TransferableResource
import com.babylon.wallet.android.domain.model.TransferableWithGuarantees
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.Resource
Expand Down Expand Up @@ -365,7 +366,7 @@ class TransactionReviewViewModel @Inject constructor(
if (candidateAddressWithdrawn != null) {
val xrdResourceWithdrawn = candidateAddressWithdrawn.resources.map {
it.transferable
}.filterIsInstance<TransferableResource.Amount>().find { it.resource.isXrd }
}.filterIsInstance<TransferableResource.FungibleAmount>().find { it.resource.isXrd }

xrdResourceWithdrawn?.amount ?: BigDecimal.ZERO
} else {
Expand Down Expand Up @@ -503,9 +504,8 @@ data class AccountWithDepositSettingsChanges(

@Suppress("MagicNumber")
sealed interface AccountWithPredictedGuarantee {

val address: String
val transferableAmount: TransferableResource.Amount
val transferable: TransferableWithGuarantees
val instructionIndex: Long
val guaranteeAmountString: String

Expand All @@ -514,7 +514,7 @@ sealed interface AccountWithPredictedGuarantee {
get() = (guaranteeAmountString.toDoubleOrNull() ?: 0.0).div(100.0)

val guaranteedAmount: BigDecimal
get() = transferableAmount.amount * guaranteeOffsetDecimal.toBigDecimal()
get() = transferable.amount * guaranteeOffsetDecimal.toBigDecimal()

fun increase(): AccountWithPredictedGuarantee {
val newOffset = (guaranteeOffsetDecimal.toBigDecimal().plus(BigDecimal(0.001)))
Expand Down Expand Up @@ -549,11 +549,11 @@ sealed interface AccountWithPredictedGuarantee {
}

fun isTheSameGuaranteeItem(with: AccountWithPredictedGuarantee): Boolean = address == with.address &&
transferableAmount.resourceAddress == with.transferableAmount.resourceAddress
transferable.fungibleResource.resourceAddress == with.transferable.fungibleResource.resourceAddress

data class Owned(
val account: Network.Account,
override val transferableAmount: TransferableResource.Amount,
override val transferable: TransferableWithGuarantees,
override val instructionIndex: Long,
override val guaranteeAmountString: String
) : AccountWithPredictedGuarantee {
Expand All @@ -563,7 +563,7 @@ sealed interface AccountWithPredictedGuarantee {

data class Other(
override val address: String,
override val transferableAmount: TransferableResource.Amount,
override val transferable: TransferableWithGuarantees,
override val instructionIndex: Long,
override val guaranteeAmountString: String
) : AccountWithPredictedGuarantee
Expand Down Expand Up @@ -597,12 +597,12 @@ sealed interface AccountWithTransferableResources {
val resources = resources.mapWhen(
predicate = { depositing ->
resourcesWithGuaranteesForAccount.any {
it.address == address && it.transferableAmount.resourceAddress == depositing.transferable.resourceAddress
it.address == address && it.transferable.fungibleResource.resourceAddress == depositing.transferable.resourceAddress
}
},
mutation = { depositing ->
val accountWithGuarantee = resourcesWithGuaranteesForAccount.find {
it.transferableAmount.resourceAddress == depositing.transferable.resourceAddress
it.transferable.fungibleResource.resourceAddress == depositing.transferable.resourceAddress
}

if (accountWithGuarantee != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.babylon.wallet.android.presentation.transaction.analysis

import com.babylon.wallet.android.domain.model.GuaranteeType
import com.babylon.wallet.android.domain.model.Transferable
import com.babylon.wallet.android.domain.model.TransferableResource
import com.babylon.wallet.android.domain.model.assets.PoolUnit
Expand All @@ -10,28 +11,27 @@ import com.babylon.wallet.android.presentation.transaction.AccountWithTransferab
import com.babylon.wallet.android.presentation.transaction.PreviewType
import com.radixdlt.ret.DetailedManifestClass
import com.radixdlt.ret.ExecutionSummary
import com.radixdlt.ret.ResourceIndicator
import kotlinx.coroutines.flow.first
import rdx.works.profile.data.model.pernetwork.Network
import rdx.works.profile.domain.GetProfileUseCase
import rdx.works.profile.domain.accountOnCurrentNetwork
import rdx.works.profile.domain.accountsOnCurrentNetwork
import timber.log.Timber

suspend fun DetailedManifestClass.PoolContribution.resolve(
executionSummary: ExecutionSummary,
getProfileUseCase: GetProfileUseCase,
resources: List<Resource>,
involvedPools: List<Pool>
): PreviewType {
val defaultDepositGuarantees = getProfileUseCase.invoke().first().appPreferences.transaction.defaultDepositGuarantee
val accountsWithdrawnFrom = executionSummary.accountWithdraws.keys
val ownedAccountsWithdrawnFrom = getProfileUseCase.accountsOnCurrentNetwork().filter {
accountsWithdrawnFrom.contains(it.address)
}
val from = executionSummary.extractWithdraws(ownedAccountsWithdrawnFrom, resources)
val to = executionSummary.accountDeposits.map { depositsPerAddress ->
val ownedAccount = getProfileUseCase.accountOnCurrentNetwork(depositsPerAddress.key) ?: error("No account found")
depositsPerAddress.value.forEach {
Timber.d("depositing: ${it.resourceAddress}")
}
val deposits = depositsPerAddress.value.mapNotNull { deposit ->
val resourceAddress = deposit.resourceAddress
val contributions = poolContributions.filter { it.poolUnitsResourceAddress.addressString() == resourceAddress }
Expand All @@ -43,6 +43,8 @@ suspend fun DetailedManifestClass.PoolContribution.resolve(
val poolResource = resources.find { it.resourceAddress == pool.metadata.poolUnit() } as? Resource.FungibleResource
?: error("No pool resource found")
val contributedResourceAddresses = contributions.first().contributedResources.keys
val guaranteeType = (deposit as? ResourceIndicator.Fungible)?.indicator?.toGuaranteeType(defaultDepositGuarantees)
?: GuaranteeType.Guaranteed
Transferable.Depositing(
transferable = TransferableResource.PoolUnitAmount(
amount = contributions.map { it.poolUnitsAmount.asStr().toBigDecimal() }.sumOf { it },
Expand All @@ -54,7 +56,8 @@ suspend fun DetailedManifestClass.PoolContribution.resolve(
contributions.mapNotNull { it.contributedResources[contributedResourceAddress]?.asStr()?.toBigDecimal() }
.sumOf { it }
},
)
),
guaranteeType = guaranteeType,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private suspend fun extractWithdrawals(
account = ownedAccount,
resources = listOf(
element = Transferable.Withdrawing(
transferable = TransferableResource.Amount(
transferable = TransferableResource.FungibleAmount(
entry.value.sumOf { it.amount },
xrdResource,
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private suspend fun extractDeposits(
account = ownedAccount,
resources = listOf(
element = Transferable.Depositing(
transferable = TransferableResource.Amount(
transferable = TransferableResource.FungibleAmount(
entry.value.sumOf { it.amount },
xrdResource,
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fun ResourceIndicator.toTransferableResource(resources: List<Resource>): Transfe
resourceAddress = resourceAddress,
ownedAmount = BigDecimal.ZERO
)
TransferableResource.Amount(
TransferableResource.FungibleAmount(
amount = amount,
resource = resource,
isNewlyCreated = false
Expand Down Expand Up @@ -238,7 +238,7 @@ fun DetailedManifestClass.isConformingManifestType(): Boolean {
}
}

private fun FungibleResourceIndicator.toGuaranteeType(defaultDepositGuarantees: Double): GuaranteeType {
fun FungibleResourceIndicator.toGuaranteeType(defaultDepositGuarantees: Double): GuaranteeType {
return when (this) {
is FungibleResourceIndicator.Guaranteed -> GuaranteeType.Guaranteed
is FungibleResourceIndicator.Predicted -> GuaranteeType.Predicted(
Expand All @@ -258,14 +258,14 @@ private fun ResourceIndicator.Fungible.toTransferableResource(
resources: List<Resource>,
newlyCreatedMetadata: Map<String, Map<String, MetadataValue?>>,
newlyCreatedEntities: List<Address>
): TransferableResource.Amount {
): TransferableResource.FungibleAmount {
val resource = resources.findFungible(
resourceAddress.addressString()
) ?: Resource.FungibleResource.from(
resourceAddress = resourceAddress, metadata = newlyCreatedMetadata[resourceAddress.addressString()].orEmpty()
)

return TransferableResource.Amount(
return TransferableResource.FungibleAmount(
amount = amount,
resource = resource,
isNewlyCreated = resourceAddress.addressString() in newlyCreatedEntities.map { it.addressString() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ fun PoolTypeContent(
modifier: Modifier = Modifier,
state: TransactionReviewViewModel.State,
onFungibleResourceClick: (fungibleResource: Resource.FungibleResource, Boolean) -> Unit,
previewType: PreviewType.Transfer.Pool
previewType: PreviewType.Transfer.Pool,
onPromptForGuarantees: () -> Unit
) {
val poolSectionLabel = when (previewType.actionType) {
PreviewType.Transfer.Pool.ActionType.Contribution -> "Contributing to pools"
Expand All @@ -31,7 +32,7 @@ fun PoolTypeContent(
onFungibleResourceClick = onFungibleResourceClick,
onNonFungibleResourceClick = { _, _, _ -> },
previewType = previewType,
onPromptForGuarantees = {},
onPromptForGuarantees = onPromptForGuarantees,
middleSection = {
PoolsContent(
modifier = Modifier.padding(horizontal = RadixTheme.dimensions.paddingDefault),
Expand Down Expand Up @@ -60,6 +61,7 @@ fun PoolTypePreview() {
pools = persistentListOf(),
actionType = PreviewType.Transfer.Pool.ActionType.Contribution
),
onPromptForGuarantees = {},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ fun StakeTypeContent(
state: TransactionReviewViewModel.State,
onFungibleResourceClick: (fungibleResource: Resource.FungibleResource, Boolean) -> Unit,
onNonFungibleResourceClick: (nonFungibleResource: Resource.NonFungibleResource, Resource.NonFungibleResource.Item, Boolean) -> Unit,
previewType: PreviewType.Transfer.Staking
previewType: PreviewType.Transfer.Staking,
onPromptForGuarantees: () -> Unit
) {
val validatorSectionText = when (previewType.actionType) {
PreviewType.Transfer.Staking.ActionType.Stake -> stringResource(id = R.string.transactionReview_validators_stake).uppercase()
Expand All @@ -35,7 +36,7 @@ fun StakeTypeContent(
onFungibleResourceClick = onFungibleResourceClick,
onNonFungibleResourceClick = onNonFungibleResourceClick,
previewType = previewType,
onPromptForGuarantees = {},
onPromptForGuarantees = onPromptForGuarantees,
middleSection = {
ValidatorsContent(
modifier = Modifier.padding(horizontal = RadixTheme.dimensions.paddingDefault),
Expand Down Expand Up @@ -65,6 +66,7 @@ fun StakeUnstakeTypePreview() {
validators = persistentListOf(),
actionType = PreviewType.Transfer.Staking.ActionType.Stake
),
onPromptForGuarantees = {},
)
}
}
Loading

0 comments on commit df475b1

Please sign in to comment.