Skip to content

Commit

Permalink
Static Typing: Clean up market processor codepath and update tests (#642
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ruixhuang authored Sep 12, 2024
1 parent 7e13434 commit 1b9234a
Show file tree
Hide file tree
Showing 13 changed files with 737 additions and 350 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
package exchange.dydx.abacus.calculator

import exchange.dydx.abacus.protocols.ParserProtocol
import exchange.dydx.abacus.state.internalstate.InternalMarketSummaryState
import exchange.dydx.abacus.utils.Logger
import exchange.dydx.abacus.utils.Numeric
import exchange.dydx.abacus.utils.mutable
import exchange.dydx.abacus.utils.safeSet

@Suppress("UNCHECKED_CAST")
internal class MarketCalculator(val parser: ParserProtocol) {
internal fun calculate(
fun calculate(
marketsSummary: InternalMarketSummaryState,
): InternalMarketSummaryState {
val markets = marketsSummary.markets

var volume24HUSDC = Numeric.double.ZERO
var openInterestUSDC = Numeric.double.ZERO
var trades24H = Numeric.double.ZERO

for ((key, market) in markets) {
val perpetual = market.perpetualMarket?.perpetual
if (perpetual != null) {
volume24HUSDC += perpetual.volume24H ?: Numeric.double.ZERO
openInterestUSDC += perpetual.openInterestUSDC
trades24H += perpetual.trades24H ?: Numeric.double.ZERO
}
}

marketsSummary.volume24HUSDC = volume24HUSDC
marketsSummary.openInterestUSDC = openInterestUSDC
marketsSummary.trades24H = trades24H

return marketsSummary
}

internal fun calculateDeprecated(
marketsSummary: Map<String, Any>?,
assets: Map<String, Any>?,
keys: Set<String>? = null
Expand Down Expand Up @@ -36,7 +61,7 @@ internal class MarketCalculator(val parser: ParserProtocol) {
if (assets == null) {
Logger.d { "Expecting assets" }
} else {
val marketCaps = calculateMarketCaps(market, assets)
val marketCaps = calculateMarketCapsDeprecated(market, assets)
modifiedMarkets?.safeSet(key, marketCaps)
}
val perpetual = parser.asNativeMap(market["perpetual"])
Expand All @@ -56,7 +81,7 @@ internal class MarketCalculator(val parser: ParserProtocol) {
return modified
}

private fun calculateMarketCaps(
private fun calculateMarketCapsDeprecated(
market: Map<String, Any>,
assets: Map<String, Any>
): Map<String, Any> {
Expand Down
7 changes: 6 additions & 1 deletion src/commonMain/kotlin/exchange.dydx.abacus/output/Market.kt
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,12 @@ data class PerpetualMarketSummary(
markets[marketId] = it
}
}
return createPerpetualMarketSummary(existing, parser, data, markets)
return PerpetualMarketSummary(
volume24HUSDC = marketSummaryState.volume24HUSDC,
openInterestUSDC = marketSummaryState.openInterestUSDC,
trades24H = marketSummaryState.trades24H,
markets = markets.toIMap(),
)
} else {
val marketsData = parser.asMap(data["markets"]) ?: return null
val changedMarkets = changes.markets ?: marketsData.keys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,6 @@ internal class MarketProcessor(
val name = parser.asString(payload.ticker) ?: return null
val oraclePrice = parser.asDouble(cachedIndexerOraclePrice?.oraclePrice) ?: parser.asDouble(payload.oraclePrice)
val status = createStatus(payload.status)
if (status == null || !status.canDisplay) {
return null
}
try {
val newValue = PerpetualMarket(
id = name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ internal data class InternalTradeInputOptions(

internal data class InternalMarketSummaryState(
var markets: MutableMap<String, InternalMarketState> = mutableMapOf(),

var volume24HUSDC: Double? = null,
var openInterestUSDC: Double? = null,
var trades24H: Double? = null,
)

internal data class InternalMarketState(
Expand Down
Loading

0 comments on commit 1b9234a

Please sign in to comment.