Skip to content

Commit

Permalink
Add balance processing for unbonding and staking rewards (#412)
Browse files Browse the repository at this point in the history
Co-authored-by: mobile-build-bot-git <[email protected]>
  • Loading branch information
rosepuppy and mobile-build-bot authored Jun 4, 2024
1 parent f674eb6 commit 896b7d2
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.7.50"
version = "1.7.51"

repositories {
google()
Expand Down
22 changes: 22 additions & 0 deletions src/commonMain/kotlin/exchange.dydx.abacus/output/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,21 @@ data class StakingDelegation(
}
}

@JsExport
@Serializable
data class UnbondingDelegation(
var validator: String,
var completionTime: String,
var balance: String,
)

@JsExport
@Serializable
data class StakingRewards(
var validators: IList<String>,
var totalRewards: IList<AccountBalance>,
)

@JsExport
@Serializable
data class HistoricalTradingReward(
Expand Down Expand Up @@ -2063,6 +2078,8 @@ data class Account(
var balances: IMap<String, AccountBalance>?,
var stakingBalances: IMap<String, AccountBalance>?,
var stakingDelegations: IList<StakingDelegation>?,
var unbondingDelegation: IList<UnbondingDelegation>?,
var stakingRewards: StakingRewards?,
var subaccounts: IMap<String, Subaccount>?,
var groupedSubaccounts: IMap<String, Subaccount>?,
var tradingRewards: TradingRewards?,
Expand Down Expand Up @@ -2115,6 +2132,9 @@ data class Account(
tokensInfo,
)

val unbondingDelegations = data["unbondingDelegation"] as IList<UnbondingDelegation>?
val stakingRewards = data["stakingRewards"] as StakingRewards?

val tradingRewardsData = parser.asMap(data["tradingRewards"])
val tradingRewards = if (tradingRewardsData != null) {
TradingRewards.create(existing?.tradingRewards, parser, tradingRewardsData)
Expand Down Expand Up @@ -2170,6 +2190,8 @@ data class Account(
balances,
stakingBalances,
stakingDelegations,
unbondingDelegations,
stakingRewards,
subaccounts,
groupedSubaccounts,
tradingRewards,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ internal class WalletProcessor(parser: ParserProtocol) : BaseProcessor(parser) {
}
}

internal fun receivedUnbonding(
existing: Map<String, Any>?,
payload: List<Any>?,
): Map<String, Any>? {
return receivedObject(existing, "account", payload) { existing, payload ->
v4accountProcessor.receivedUnbonding(
parser.asNativeMap(existing),
payload as? List<Any>,
)
}
}

internal fun receivedStakingRewards(
existing: Map<String, Any>?,
payload: Map<String, Any>?,
): Map<String, Any>? {
return receivedObject(existing, "account", payload) { existing, payload ->
v4accountProcessor.receivedStakingRewards(
parser.asNativeMap(existing),
payload as? Map<String, Any>,
)
}
}

internal fun receivedHistoricalTradingRewards(
existing: Map<String, Any>?,
payload: List<Any>?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package exchange.dydx.abacus.processor.wallet.account

import com.ionspin.kotlin.bignum.decimal.toBigDecimal
import exchange.dydx.abacus.output.AccountBalance
import exchange.dydx.abacus.output.StakingRewards
import exchange.dydx.abacus.output.UnbondingDelegation
import exchange.dydx.abacus.processor.base.BaseProcessor
import exchange.dydx.abacus.protocols.ParserProtocol
import exchange.dydx.abacus.responses.SocketInfo
import exchange.dydx.abacus.state.manager.BlockAndTime
import exchange.dydx.abacus.utils.IMutableList
import exchange.dydx.abacus.utils.Numeric
import exchange.dydx.abacus.utils.mutable
import exchange.dydx.abacus.utils.safeSet
import kollections.iMutableListOf

/*
"account": {
Expand Down Expand Up @@ -952,6 +957,56 @@ internal class V4AccountProcessor(parser: ParserProtocol) : BaseProcessor(parser
return modified
}

internal fun receivedUnbonding(
existing: Map<String, Any>?,
payload: List<Any>?,
): Map<String, Any>? {
if (payload == null) {
return existing
}
val modified = existing?.mutable() ?: mutableMapOf()
val unbondingDelegations: IMutableList<UnbondingDelegation> = iMutableListOf()

for (validator in payload) {
val validatorAddress = parser.asString(parser.value(validator, "validatorAddress")) ?: continue
val entries = parser.asList(parser.value(validator, "entries")) ?: continue

for (entry in entries) {
val completionTime = parser.asString(parser.value(entry, "completionTime")) ?: continue
val balance = parser.asString(parser.value(entry, "balance")) ?: continue

unbondingDelegations.add(UnbondingDelegation(validatorAddress, completionTime, balance))
}
}
modified.safeSet("unbondingDelegation", unbondingDelegations)
return modified
}

internal fun receivedStakingRewards(
existing: Map<String, Any>?,
payload: Map<String, Any>?,
): Map<String, Any>? {
val rewards = parser.asList(parser.value(payload, "rewards"))
val total = parser.asList(parser.value(payload, "total"))
if (payload == null || rewards == null || total == null) {
return existing
}
val modified = existing?.mutable() ?: mutableMapOf()
val validators: IMutableList<String> = iMutableListOf()
val totalRewards: IMutableList<AccountBalance> = iMutableListOf()
for (validator in rewards) {
val validatorAddress = parser.asString(parser.value(validator, "validatorAddress")) ?: continue
validators.add(validatorAddress)
}
for (reward in total) {
val denom = parser.asString(parser.value(reward, "denom")) ?: continue
val amount = parser.asString(parser.value(reward, "amount")) ?: continue
totalRewards.add(AccountBalance(denom, amount))
}
modified.safeSet("stakingRewards", StakingRewards(validators, totalRewards))
return modified
}

internal fun receivedHistoricalPnls(
existing: Map<String, Any>?,
payload: Map<String, Any>?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ enum class QueryType(val rawValue: String) {
GetAccountBalances("getAccountBalances"),
GetMarketPrice("getMarketPrice"),
GetDelegations("getDelegatorDelegations"),
GetStakingRewards("getStakingRewards"),
GetCurrentUnstaking("getCurrentUnstaking"),
RewardsParams("getRewardsParams"),
GetNobleBalance("getNobleBalance"),
GetWithdrawalAndTransferGatingStatus("getWithdrawalAndTransferGatingStatus"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import exchange.dydx.abacus.state.model.onChainEquityTiers
import exchange.dydx.abacus.state.model.onChainFeeTiers
import exchange.dydx.abacus.state.model.onChainRewardTokenPrice
import exchange.dydx.abacus.state.model.onChainRewardsParams
import exchange.dydx.abacus.state.model.onChainStakingRewards
import exchange.dydx.abacus.state.model.onChainUnbonding
import exchange.dydx.abacus.state.model.onChainUserFeeTier
import exchange.dydx.abacus.state.model.onChainUserStats
import exchange.dydx.abacus.state.model.squidV2SdkInfo
Expand Down Expand Up @@ -474,6 +476,14 @@ class V4StateManagerAdaptor(
val oldState = stateMachine.state
update(stateMachine.onChainDelegations(response), oldState)
}
getOnChain(QueryType.GetCurrentUnstaking, paramsInJson) { response ->
val oldState = stateMachine.state
update(stateMachine.onChainUnbonding(response), oldState)
}
getOnChain(QueryType.GetStakingRewards, paramsInJson) { response ->
val oldState = stateMachine.state
update(stateMachine.onChainStakingRewards(response), oldState)
}
}

private fun pollNobleBalance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,26 @@ internal fun TradingStateMachine.onChainDelegations(payload: String): StateChang
StateChanges(iListOf())
}
}

internal fun TradingStateMachine.onChainUnbonding(payload: String): StateChanges {
val response = parser.decodeJsonObject(payload)
return try {
val unbonding = response?.get("unbondingResponses")?.let {
parser.asList(it)
} ?: iListOf()
this.wallet = walletProcessor.receivedUnbonding(wallet, unbonding)
return StateChanges(iListOf(Changes.accountBalances), null)
} catch (e: Exception) {
StateChanges(iListOf())
}
}

internal fun TradingStateMachine.onChainStakingRewards(payload: String): StateChanges {
val response = parser.decodeJsonObject(payload)
return try {
this.wallet = walletProcessor.receivedStakingRewards(wallet, response)
return StateChanges(iListOf(Changes.accountBalances), null)
} catch (e: Exception) {
StateChanges(iListOf())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,8 @@ open class TradingStateMachine(
account.balances,
account.stakingBalances,
account.stakingDelegations,
account.unbondingDelegation,
account.stakingRewards,
subaccounts,
groupedSubaccounts,
account.tradingRewards,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import exchange.dydx.abacus.state.model.account
import exchange.dydx.abacus.state.model.launchIncentivePoints
import exchange.dydx.abacus.state.model.onChainAccountBalances
import exchange.dydx.abacus.state.model.onChainDelegations
import exchange.dydx.abacus.state.model.onChainStakingRewards
import exchange.dydx.abacus.state.model.onChainUnbonding
import exchange.dydx.abacus.state.model.onChainUserFeeTier
import exchange.dydx.abacus.state.model.onChainUserStats
import exchange.dydx.abacus.state.model.receivedHistoricalTradingRewards
Expand Down Expand Up @@ -414,6 +416,14 @@ internal open class AccountSupervisor(
val oldState = stateMachine.state
update(stateMachine.onChainDelegations(response), oldState)
}
helper.getOnChain(QueryType.GetCurrentUnstaking, paramsInJson) { response ->
val oldState = stateMachine.state
update(stateMachine.onChainUnbonding(response), oldState)
}
helper.getOnChain(QueryType.GetStakingRewards, paramsInJson) { response ->
val oldState = stateMachine.state
update(stateMachine.onChainStakingRewards(response), oldState)
}
}

private fun retrieveNobleBalance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ class V4ForegroundCycleTests : NetworkTests() {
"getHeight",
"getAccountBalances",
"getDelegatorDelegations",
"getCurrentUnstaking",
"getStakingRewards",
"getNobleBalance"
]
""".trimIndent(),
Expand Down Expand Up @@ -417,6 +419,8 @@ class V4ForegroundCycleTests : NetworkTests() {
"getUserStats",
"getAccountBalances",
"getDelegatorDelegations",
"getCurrentUnstaking",
"getStakingRewards",
"getNobleBalance"
]
""".trimIndent(),
Expand Down Expand Up @@ -554,9 +558,13 @@ class V4ForegroundCycleTests : NetworkTests() {
"getUserStats",
"getAccountBalances",
"getDelegatorDelegations",
"getCurrentUnstaking",
"getStakingRewards",
"getNobleBalance",
"getAccountBalances",
"getDelegatorDelegations",
"getCurrentUnstaking",
"getStakingRewards",
"getNobleBalance"
]
""".trimIndent(),
Expand Down Expand Up @@ -630,6 +638,8 @@ class V4ForegroundCycleTests : NetworkTests() {
"getUserStats",
"getAccountBalances",
"getDelegatorDelegations",
"getCurrentUnstaking",
"getStakingRewards",
"getNobleBalance"
]
""".trimIndent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ class V4ForegroundCycleTests : NetworkTests() {
"getHeight",
"getAccountBalances",
"getDelegatorDelegations",
"getCurrentUnstaking",
"getStakingRewards",
"getNobleBalance"
]
""".trimIndent(),
Expand Down Expand Up @@ -480,6 +482,8 @@ class V4ForegroundCycleTests : NetworkTests() {
"getUserStats",
"getAccountBalances",
"getDelegatorDelegations",
"getCurrentUnstaking",
"getStakingRewards",
"getNobleBalance"
]
""".trimIndent(),
Expand Down Expand Up @@ -643,9 +647,13 @@ class V4ForegroundCycleTests : NetworkTests() {
"getUserStats",
"getAccountBalances",
"getDelegatorDelegations",
"getCurrentUnstaking",
"getStakingRewards",
"getNobleBalance",
"getAccountBalances",
"getDelegatorDelegations",
"getCurrentUnstaking",
"getStakingRewards",
"getNobleBalance"
]
""".trimIndent(),
Expand Down Expand Up @@ -719,6 +727,8 @@ class V4ForegroundCycleTests : NetworkTests() {
"getUserStats",
"getAccountBalances",
"getDelegatorDelegations",
"getCurrentUnstaking",
"getStakingRewards",
"getNobleBalance"
]
""".trimIndent(),
Expand Down

0 comments on commit 896b7d2

Please sign in to comment.