Updated on 2026-08-14
This commit is contained in:
parent
9d73e02d69
commit
f9226ab6bf
19 changed files with 106 additions and 63 deletions
|
|
@ -0,0 +1,33 @@
|
|||
package com.tangem.common
|
||||
|
||||
import com.tangem.domain.models.currency.CryptoCurrencyStatus
|
||||
import com.tangem.domain.models.staking.StakingBalance
|
||||
import com.tangem.utils.extensions.orZero
|
||||
import java.math.BigDecimal
|
||||
|
||||
/**
|
||||
* Calculates the total fiat amount by adding the main fiat amount and the fiat value of the staked balance.
|
||||
*/
|
||||
fun CryptoCurrencyStatus.getTotalFiatAmount(): BigDecimal? {
|
||||
val fiatAmount = value.fiatAmount
|
||||
|
||||
val fiatStakedBalance = value.fiatRate?.times(getStakedBalance().orZero()) ?: return fiatAmount
|
||||
val totalAmount = fiatAmount?.plus(fiatStakedBalance) ?: return fiatStakedBalance
|
||||
|
||||
return totalAmount
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the total cryptocurrency amount by adding the main crypto amount and the staked balance.
|
||||
*/
|
||||
fun CryptoCurrencyStatus.getTotalCryptoAmount(): BigDecimal? {
|
||||
val cryptoAmount = value.amount
|
||||
|
||||
val cryptoStakedBalance = getStakedBalance() ?: return cryptoAmount
|
||||
val totalAmount = cryptoAmount?.plus(cryptoStakedBalance) ?: return cryptoStakedBalance
|
||||
|
||||
return totalAmount
|
||||
}
|
||||
|
||||
private fun CryptoCurrencyStatus.getStakedBalance() = (value.stakingBalance as? StakingBalance.Data)
|
||||
?.getTotalWithRewardsStakingBalance(blockchainId = currency.network.rawId)
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
package com.tangem.common
|
||||
|
||||
import com.tangem.domain.models.staking.BalanceType
|
||||
import com.tangem.domain.models.staking.StakingBalance
|
||||
import com.tangem.lib.crypto.BlockchainUtils
|
||||
import java.math.BigDecimal
|
||||
|
||||
/**
|
||||
* Provider-agnostic extension to get total balance including rewards.
|
||||
* Works for both StakeKit and P2PEthPool providers.
|
||||
*
|
||||
* Returns sum of all staking-related balances including rewards
|
||||
* (staked + unstaking + withdrawable + rewards).
|
||||
*
|
||||
* When [BlockchainUtils.isIncludeStakingTotalBalance] is false, the staked balance
|
||||
* is already included in the main wallet balance, so we only return rewards.
|
||||
*/
|
||||
fun StakingBalance.Data.getTotalWithRewardsStakingBalance(blockchainId: String): BigDecimal {
|
||||
return when (this) {
|
||||
is StakingBalance.Data.StakeKit -> getTotalWithRewardsStakingBalanceStakeKit(blockchainId)
|
||||
is StakingBalance.Data.P2PEthPool -> {
|
||||
val rewards = totalRewards
|
||||
if (BlockchainUtils.isIncludeStakingTotalBalance(blockchainId)) {
|
||||
totalStaked + unstakingAmount + withdrawableAmount + rewards
|
||||
} else {
|
||||
rewards
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider-agnostic extension to get total staking balance excluding rewards.
|
||||
* Works for both StakeKit and P2PEthPool providers.
|
||||
*
|
||||
* Returns sum of all staking-related balances (staked + unstaking + withdrawable)
|
||||
* excluding rewards.
|
||||
*/
|
||||
fun StakingBalance.Data.getTotalStakingBalance(blockchainId: String): BigDecimal {
|
||||
return when (this) {
|
||||
is StakingBalance.Data.StakeKit -> getTotalStakingBalanceStakeKit(blockchainId)
|
||||
is StakingBalance.Data.P2PEthPool -> totalStaked + unstakingAmount + withdrawableAmount
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* StakeKit-specific extension to get total balance including rewards.
|
||||
*/
|
||||
private fun StakingBalance.Data.StakeKit.getTotalWithRewardsStakingBalanceStakeKit(blockchainId: String): BigDecimal {
|
||||
return if (BlockchainUtils.isIncludeStakingTotalBalance(blockchainId = blockchainId)) {
|
||||
balance.items.sumOf { it.amount }
|
||||
} else {
|
||||
getRewardStakingBalance()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* StakeKit-specific extension to get total staked balance excluding rewards.
|
||||
*/
|
||||
private fun StakingBalance.Data.StakeKit.getTotalStakingBalanceStakeKit(blockchainId: String): BigDecimal {
|
||||
return if (BlockchainUtils.isIncludeStakingTotalBalance(blockchainId = blockchainId)) {
|
||||
balance.items
|
||||
.filterNot { it.type == BalanceType.REWARDS }
|
||||
.sumOf { it.amount }
|
||||
} else {
|
||||
balance.items
|
||||
.filterNot { it.type == BalanceType.REWARDS }
|
||||
.sumOf { it.amount } - getRewardStakingBalance()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* StakeKit-specific extension to get reward balance.
|
||||
*/
|
||||
fun StakingBalance.Data.StakeKit.getRewardStakingBalance(): BigDecimal {
|
||||
return balance.items
|
||||
.filter { it.type == BalanceType.REWARDS }
|
||||
.sumOf { it.amount }
|
||||
}
|
||||
|
||||
/**
|
||||
* StakeKit-specific extension to get validators count.
|
||||
*/
|
||||
fun StakingBalance.Data.StakeKit.getValidatorsCount(): Int {
|
||||
return balance.items
|
||||
.filterNot { it.validatorAddress.isNullOrBlank() }
|
||||
.distinctBy { it.validatorAddress }
|
||||
.size
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue