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
|
import java.math.BigDecimal
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts P2PEthPool API response to [StakingBalance].
|
* Converts P2PEthPool API responses to [StakingBalance].
|
||||||
*
|
*
|
||||||
* Uses [P2PEthPoolStakingAccountConverter] for account conversion to avoid duplication.
|
* Uses [P2PEthPoolStakingAccountConverter] for account conversion to avoid duplication.
|
||||||
*/
|
*/
|
||||||
internal object P2PEthPoolStakingBalanceConverter {
|
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(
|
val stakingId = StakingID(
|
||||||
integrationId = StakingIntegrationID.P2PEthPool.value,
|
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 ||
|
val hasActivePosition = accounts.any { account ->
|
||||||
account.exitQueue.total > BigDecimal.ZERO ||
|
account.stake.assets > BigDecimal.ZERO ||
|
||||||
account.availableToWithdraw > BigDecimal.ZERO
|
account.exitQueue.total > BigDecimal.ZERO ||
|
||||||
|
account.availableToWithdraw > BigDecimal.ZERO
|
||||||
|
}
|
||||||
|
|
||||||
return if (hasActivePosition) {
|
return if (hasActivePosition) {
|
||||||
StakingBalance.Data.P2PEthPool(
|
StakingBalance.Data.P2PEthPool(
|
||||||
stakingId = stakingId,
|
stakingId = stakingId,
|
||||||
source = source,
|
source = source,
|
||||||
account = account,
|
accounts = accounts,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
StakingBalance.Empty(
|
StakingBalance.Empty(
|
||||||
|
|
|
||||||
|
|
@ -46,12 +46,10 @@ internal class DefaultP2PEthPoolBalancesStore(
|
||||||
runtimeStore.store(
|
runtimeStore.store(
|
||||||
value = cachedData.map { (stringWalletId, responses) ->
|
value = cachedData.map { (stringWalletId, responses) ->
|
||||||
val key = UserWalletId(stringWalletId)
|
val key = UserWalletId(stringWalletId)
|
||||||
val value = responses.map { response ->
|
val value = P2PEthPoolStakingBalanceConverter.convertAll(
|
||||||
P2PEthPoolStakingBalanceConverter.convert(
|
responses = responses,
|
||||||
response = response,
|
source = StatusSource.CACHE,
|
||||||
source = StatusSource.CACHE,
|
)
|
||||||
)
|
|
||||||
}.toSet()
|
|
||||||
|
|
||||||
key to value
|
key to value
|
||||||
}.toMap(),
|
}.toMap(),
|
||||||
|
|
@ -116,12 +114,10 @@ internal class DefaultP2PEthPoolBalancesStore(
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun storeInRuntime(userWalletId: UserWalletId, values: Set<P2PEthPoolAccountResponse>) {
|
private suspend fun storeInRuntime(userWalletId: UserWalletId, values: Set<P2PEthPoolAccountResponse>) {
|
||||||
val newBalances = values.map { response ->
|
val newBalances = P2PEthPoolStakingBalanceConverter.convertAll(
|
||||||
P2PEthPoolStakingBalanceConverter.convert(
|
responses = values,
|
||||||
response = response,
|
source = StatusSource.ACTUAL,
|
||||||
source = StatusSource.ACTUAL,
|
)
|
||||||
)
|
|
||||||
}.toSet()
|
|
||||||
|
|
||||||
runtimeStore.update(default = emptyMap()) { saved ->
|
runtimeStore.update(default = emptyMap()) { saved ->
|
||||||
saved.toMutableMap().apply {
|
saved.toMutableMap().apply {
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,8 @@ internal fun YieldBalanceWrapperDTO.toDomain(source: StatusSource = StatusSource
|
||||||
internal fun P2PEthPoolAccountResponse.toDomain(
|
internal fun P2PEthPoolAccountResponse.toDomain(
|
||||||
source: StatusSource = StatusSource.CACHE,
|
source: StatusSource = StatusSource.CACHE,
|
||||||
): StakingBalance {
|
): StakingBalance {
|
||||||
return P2PEthPoolStakingBalanceConverter.convert(
|
return P2PEthPoolStakingBalanceConverter.convertAll(
|
||||||
response = this,
|
responses = setOf(this),
|
||||||
source = source,
|
source = source,
|
||||||
)
|
).first()
|
||||||
}
|
}
|
||||||
|
|
@ -55,18 +55,18 @@ sealed interface StakingBalance {
|
||||||
data class P2PEthPool(
|
data class P2PEthPool(
|
||||||
override val stakingId: StakingID,
|
override val stakingId: StakingID,
|
||||||
override val source: StatusSource,
|
override val source: StatusSource,
|
||||||
val account: P2PEthPoolStakingAccount,
|
val accounts: List<P2PEthPoolStakingAccount>,
|
||||||
) : Data {
|
) : 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