Skip to content

Commit

Permalink
Add equity tiers in configs output (#388)
Browse files Browse the repository at this point in the history
Co-authored-by: Rui <[email protected]>
  • Loading branch information
2 people authored and yogurtandjam committed May 31, 2024
1 parent 2b3554c commit 99e667e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
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.33"
version = "1.7.34"

repositories {
google()
Expand Down
109 changes: 108 additions & 1 deletion src/commonMain/kotlin/exchange.dydx.abacus/output/Configs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import exchange.dydx.abacus.protocols.ParserProtocol
import exchange.dydx.abacus.utils.IList
import exchange.dydx.abacus.utils.Logger
import kollections.JsExport
import kollections.iListOf
import kollections.iMutableListOf
import kotlinx.serialization.Serializable

Expand Down Expand Up @@ -247,6 +248,103 @@ data class FeeTier(
}
}

@JsExport
@Serializable
data class EquityTiers(
val shortTermOrderEquityTiers: IList<EquityTier>,
val statefulOrderEquityTiers: IList<EquityTier>,
) {
companion object {
internal fun create(
existing: EquityTiers?,
parser: ParserProtocol,
data: Map<*, *>?,
): EquityTiers? {
data?.let {
val shortTermOrderEquityTiers = parser.asNativeList(data["shortTermOrderEquityTiers"])?.let { tiers ->
create(existing?.shortTermOrderEquityTiers, parser, tiers)
} ?: iListOf()

val statefulOrderEquityTiers = parser.asNativeList(data["statefulOrderEquityTiers"])?.let { tiers ->
create(existing?.statefulOrderEquityTiers, parser, tiers)
} ?: iListOf()

return EquityTiers(shortTermOrderEquityTiers, statefulOrderEquityTiers)
}

Logger.d { "Equity Tiers not valid" }
return null
}

internal fun create(
existing: IList<EquityTier>?,
parser: ParserProtocol,
data: List<*>?
): IList<EquityTier>? {
data?.let {
val equityTiers = iMutableListOf<EquityTier>()
for (i in data.indices) {
val item = data[i]
val nextItem = data.getOrNull(i + 1)
parser.asMap(item)?.let {
val tier = existing?.getOrNull(i)
val nextTierData = parser.asMap(nextItem)
EquityTier.create(tier, parser, it, nextTierData)?.let { equityTier ->
equityTiers.add(equityTier)
}
}
}
return equityTiers
}
Logger.d { "Equity Tiers not valid" }
return null
}
}
}

@JsExport
@Serializable
data class EquityTier(
val requiredTotalNetCollateralUSD: Double,
val nextLevelRequiredTotalNetCollateralUSD: Double?,
val maxOrders: Int,
) {
companion object {
internal fun create(
existing: EquityTier?,
parser: ParserProtocol,
data: Map<*, *>?,
nextTierData: Map<*, *>?
): EquityTier? {
data?.let {
val requiredTotalNetCollateralUSD = parser.asDouble(data["requiredTotalNetCollateralUSD"])
val nextLevelRequiredTotalNetCollateralUSD = nextTierData?.let {
parser.asDouble(nextTierData["requiredTotalNetCollateralUSD"])
}
val maxOrders = parser.asInt(data["maxOrders"])

if (requiredTotalNetCollateralUSD != null && maxOrders != null) {
return if (
existing?.requiredTotalNetCollateralUSD != requiredTotalNetCollateralUSD ||
existing?.nextLevelRequiredTotalNetCollateralUSD != nextLevelRequiredTotalNetCollateralUSD ||
existing?.maxOrders != maxOrders
) {
EquityTier(
requiredTotalNetCollateralUSD,
nextLevelRequiredTotalNetCollateralUSD,
maxOrders,
)
} else {
existing
}
}
}
Logger.d { "Equity Tier not valid" }
return null
}
}
}

@JsExport
@Serializable
data class WithdrawalGating(
Expand Down Expand Up @@ -346,6 +444,7 @@ data class Configs(
val network: NetworkConfigs?,
val feeTiers: IList<FeeTier>?,
val feeDiscounts: IList<FeeDiscount>?,
val equityTiers: EquityTiers?,
val withdrawalGating: WithdrawalGating?,
val withdrawalCapacity: WithdrawalCapacity?,
) {
Expand All @@ -371,6 +470,11 @@ data class Configs(
parser.asList(data["feeDiscounts"]),
localizer,
)
val equityTiers = EquityTiers.create(
existing?.equityTiers,
parser,
parser.asMap(data["equityTiers"]),
)
var withdrawalGating = WithdrawalGating.create(
existing?.withdrawalGating,
parser,
Expand All @@ -383,12 +487,14 @@ data class Configs(
)
return if (existing?.network !== network ||
existing?.feeTiers != feeTiers ||
existing?.feeDiscounts != feeDiscounts
existing?.feeDiscounts != feeDiscounts ||
existing?.equityTiers != equityTiers
) {
Configs(
network,
feeTiers,
feeDiscounts,
equityTiers,
withdrawalGating,
withdrawalCapacity,
)
Expand All @@ -399,6 +505,7 @@ data class Configs(
null,
null,
null,
null,
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion v4_abacus.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'v4_abacus'
spec.version = '1.7.33'
spec.version = '1.7.34'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down

0 comments on commit 99e667e

Please sign in to comment.