Updated on 2026-08-14
This commit is contained in:
parent
68b0ef56e7
commit
a1c0fa83e5
4 changed files with 40 additions and 29 deletions
|
|
@ -8,29 +8,44 @@ import com.tangem.domain.staking.model.StakingIntegrationID
|
|||
import java.math.BigDecimal
|
||||
|
||||
/**
|
||||
* Converts P2PEthPool API response to [StakingBalance].
|
||||
* Converts P2PEthPool API responses to [StakingBalance].
|
||||
*
|
||||
* Uses [P2PEthPoolStakingAccountConverter] for account conversion to avoid duplication.
|
||||
*/
|
||||
internal object P2PEthPoolStakingBalanceConverter {
|
||||
|
||||
fun convert(response: P2PEthPoolAccountResponse, source: StatusSource): StakingBalance {
|
||||
fun convertAll(responses: Set<P2PEthPoolAccountResponse>, source: StatusSource): Set<StakingBalance> {
|
||||
return responses
|
||||
.groupBy { it.delegatorAddress }
|
||||
.map { (delegatorAddress, accountResponses) ->
|
||||
convert(delegatorAddress, accountResponses, source)
|
||||
}
|
||||
.toSet()
|
||||
}
|
||||
|
||||
private fun convert(
|
||||
delegatorAddress: String,
|
||||
responses: List<P2PEthPoolAccountResponse>,
|
||||
source: StatusSource,
|
||||
): StakingBalance {
|
||||
val stakingId = StakingID(
|
||||
integrationId = StakingIntegrationID.P2PEthPool.value,
|
||||
address = response.delegatorAddress,
|
||||
address = delegatorAddress,
|
||||
)
|
||||
|
||||
val account = P2PEthPoolStakingAccountConverter.convert(response)
|
||||
val accounts = responses.map { P2PEthPoolStakingAccountConverter.convert(it) }
|
||||
|
||||
val hasActivePosition = account.stake.assets > BigDecimal.ZERO ||
|
||||
account.exitQueue.total > BigDecimal.ZERO ||
|
||||
account.availableToWithdraw > BigDecimal.ZERO
|
||||
val hasActivePosition = accounts.any { account ->
|
||||
account.stake.assets > BigDecimal.ZERO ||
|
||||
account.exitQueue.total > BigDecimal.ZERO ||
|
||||
account.availableToWithdraw > BigDecimal.ZERO
|
||||
}
|
||||
|
||||
return if (hasActivePosition) {
|
||||
StakingBalance.Data.P2PEthPool(
|
||||
stakingId = stakingId,
|
||||
source = source,
|
||||
account = account,
|
||||
accounts = accounts,
|
||||
)
|
||||
} else {
|
||||
StakingBalance.Empty(
|
||||
|
|
|
|||
|
|
@ -46,12 +46,10 @@ internal class DefaultP2PEthPoolBalancesStore(
|
|||
runtimeStore.store(
|
||||
value = cachedData.map { (stringWalletId, responses) ->
|
||||
val key = UserWalletId(stringWalletId)
|
||||
val value = responses.map { response ->
|
||||
P2PEthPoolStakingBalanceConverter.convert(
|
||||
response = response,
|
||||
source = StatusSource.CACHE,
|
||||
)
|
||||
}.toSet()
|
||||
val value = P2PEthPoolStakingBalanceConverter.convertAll(
|
||||
responses = responses,
|
||||
source = StatusSource.CACHE,
|
||||
)
|
||||
|
||||
key to value
|
||||
}.toMap(),
|
||||
|
|
@ -116,12 +114,10 @@ internal class DefaultP2PEthPoolBalancesStore(
|
|||
}
|
||||
|
||||
private suspend fun storeInRuntime(userWalletId: UserWalletId, values: Set<P2PEthPoolAccountResponse>) {
|
||||
val newBalances = values.map { response ->
|
||||
P2PEthPoolStakingBalanceConverter.convert(
|
||||
response = response,
|
||||
source = StatusSource.ACTUAL,
|
||||
)
|
||||
}.toSet()
|
||||
val newBalances = P2PEthPoolStakingBalanceConverter.convertAll(
|
||||
responses = values,
|
||||
source = StatusSource.ACTUAL,
|
||||
)
|
||||
|
||||
runtimeStore.update(default = emptyMap()) { saved ->
|
||||
saved.toMutableMap().apply {
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ internal fun YieldBalanceWrapperDTO.toDomain(source: StatusSource = StatusSource
|
|||
internal fun P2PEthPoolAccountResponse.toDomain(
|
||||
source: StatusSource = StatusSource.CACHE,
|
||||
): StakingBalance {
|
||||
return P2PEthPoolStakingBalanceConverter.convert(
|
||||
response = this,
|
||||
return P2PEthPoolStakingBalanceConverter.convertAll(
|
||||
responses = setOf(this),
|
||||
source = source,
|
||||
)
|
||||
).first()
|
||||
}
|
||||
|
|
@ -55,18 +55,18 @@ sealed interface StakingBalance {
|
|||
data class P2PEthPool(
|
||||
override val stakingId: StakingID,
|
||||
override val source: StatusSource,
|
||||
val account: P2PEthPoolStakingAccount,
|
||||
val accounts: List<P2PEthPoolStakingAccount>,
|
||||
) : Data {
|
||||
|
||||
override val totalStaked: SerializedBigDecimal = account.stake.assets
|
||||
override val totalStaked: SerializedBigDecimal = accounts.sumOf { it.stake.assets }
|
||||
|
||||
override val totalRewards: SerializedBigDecimal = account.stake.totalEarnedAssets
|
||||
override val totalRewards: SerializedBigDecimal = accounts.sumOf { it.stake.totalEarnedAssets }
|
||||
|
||||
override val unstakingAmount: SerializedBigDecimal = account.exitQueue.total
|
||||
override val unstakingAmount: SerializedBigDecimal = accounts.sumOf { it.exitQueue.total }
|
||||
|
||||
override val withdrawableAmount: SerializedBigDecimal = account.availableToWithdraw
|
||||
override val withdrawableAmount: SerializedBigDecimal = accounts.sumOf { it.availableToWithdraw }
|
||||
|
||||
override val entries: List<StakingBalanceEntry> = account.toStakingBalanceEntries()
|
||||
override val entries: List<StakingBalanceEntry> = accounts.flatMap { it.toStakingBalanceEntries() }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue