Skip to content

Commit

Permalink
use new models
Browse files Browse the repository at this point in the history
  • Loading branch information
tyleroooo committed Sep 19, 2024
1 parent 1615fb5 commit bdc9629
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import exchange.dydx.abacus.state.internalstate.InternalSubaccountState
import exchange.dydx.abacus.utils.IList
import exchange.dydx.abacus.utils.IMap
import exchange.dydx.abacus.utils.Parser
import indexer.codegen.IndexerMegavaultHistoricalPnlResponse
import indexer.codegen.IndexerMegavaultPositionResponse
import indexer.codegen.IndexerVaultHistoricalPnl
import indexer.codegen.IndexerVaultPosition
import indexer.codegen.IndexerVaultsHistoricalPnlResponse
import kollections.toIList
import kotlinx.datetime.Clock
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -74,24 +79,24 @@ object VaultCalculator {
private val assetPositionProcessor = AssetPositionProcessor(parser)
private val subaccountCalculator = SubaccountCalculatorV2(parser)

fun getVaultHistoricalPnlResponse(apiResponse: String): IndexerVaultHistoricalPnlResponse? {
return parser.asTypedObject<IndexerVaultHistoricalPnlResponse>(apiResponse)
fun getVaultHistoricalPnlResponse(apiResponse: String): IndexerMegavaultHistoricalPnlResponse? {
return parser.asTypedObject<IndexerMegavaultHistoricalPnlResponse>(apiResponse)
}

fun getSubvaultHistoricalPnlResponse(apiResponse: String): IndexerSubvaultHistoricalPnlResponse? {
return parser.asTypedObject<IndexerSubvaultHistoricalPnlResponse>(apiResponse)
fun getSubvaultHistoricalPnlResponse(apiResponse: String): IndexerVaultsHistoricalPnlResponse? {
return parser.asTypedObject<IndexerVaultsHistoricalPnlResponse>(apiResponse)
}

fun getVaultPositionsResponse(apiResponse: String): IndexerVaultPositionResponse? {
return parser.asTypedObject<IndexerVaultPositionResponse>(apiResponse)
fun getVaultPositionsResponse(apiResponse: String): IndexerMegavaultPositionResponse? {
return parser.asTypedObject<IndexerMegavaultPositionResponse>(apiResponse)
}

fun calculateVaultSummary(historical: IndexerVaultHistoricalPnlResponse?): VaultDetails? {
if (historical?.vaultOfVaultsPnl.isNullOrEmpty()) {
fun calculateVaultSummary(historical: IndexerMegavaultHistoricalPnlResponse?): VaultDetails? {
if (historical?.megavaultPnl.isNullOrEmpty()) {
return null
}

val vaultOfVaultsPnl = historical!!.vaultOfVaultsPnl!!.sortedByDescending { parser.asDouble(it.createdAt) }
val vaultOfVaultsPnl = historical!!.megavaultPnl!!.sortedByDescending { parser.asDouble(it.createdAt) }

val history = vaultOfVaultsPnl.mapNotNull { entry ->
parser.asDouble(entry.createdAt)?.let { createdAt ->
Expand Down Expand Up @@ -132,21 +137,21 @@ object VaultCalculator {
}

fun calculateVaultPositions(
positions: IndexerVaultPositionResponse?,
histories: IndexerSubvaultHistoricalPnlResponse?,
positions: IndexerMegavaultPositionResponse?,
histories: IndexerVaultsHistoricalPnlResponse?,
markets: IMap<String, PerpetualMarket>?
): VaultPositions? {
if (positions?.positions == null) {
return null
}

val historiesMap = histories?.vaultsPnl?.associateBy { it.marketId }
val historiesMap = histories?.vaultsPnl?.associateBy { it.ticker }

return VaultPositions(positions = positions.positions.mapNotNull { calculateVaultPosition(it, historiesMap?.get(it.market), markets?.get(it.market)) }.toIList())
return VaultPositions(positions = positions.positions.mapNotNull { calculateVaultPosition(it, historiesMap?.get(it.ticker), markets?.get(it.ticker)) }.toIList())
}

fun calculateVaultPosition(position: IndexerVaultPosition, history: IndexerVaultHistoricalPnl?, perpetualMarket: PerpetualMarket?): VaultPosition? {
if (position.market == null) {
if (position.ticker == null) {
return null
}
val perpetualPosition = perpetualPositionProcessor.process(null, position.perpetualPosition)
Expand All @@ -165,15 +170,15 @@ object VaultCalculator {
),

),
marketsSummary = InternalMarketSummaryState(markets = mutableMapOf(position.market to InternalMarketState(perpetualMarket = perpetualMarket))),
marketsSummary = InternalMarketSummaryState(markets = mutableMapOf(position.ticker to InternalMarketState(perpetualMarket = perpetualMarket))),
periods = setOf(CalculationPeriod.current),
price = null,
configs = null,
)
val calculated = subaccount?.calculated?.get(CalculationPeriod.current)
val perpCalculated = perpetualPosition?.calculated?.get(CalculationPeriod.current)
return VaultPosition(
marketId = position.market,
marketId = position.ticker,
marginUsdc = calculated?.equity,
currentLeverageMultiple = perpCalculated?.leverage,
currentPosition = CurrentPosition(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import exchange.dydx.abacus.functional.vault.VaultCalculator.calculateVaultSumma
import exchange.dydx.abacus.output.PerpetualMarket
import exchange.dydx.abacus.utils.NUM_PARENT_SUBACCOUNTS
import indexer.codegen.IndexerAssetPositionResponseObject
import indexer.codegen.IndexerMegavaultHistoricalPnlResponse
import indexer.codegen.IndexerPerpetualPositionResponseObject
import indexer.codegen.IndexerPerpetualPositionStatus
import indexer.codegen.IndexerPnlTicksResponseObject
import indexer.codegen.IndexerPositionSide
import indexer.codegen.IndexerVaultHistoricalPnl
import indexer.codegen.IndexerVaultPosition
import kollections.iListOf
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -19,8 +22,8 @@ class VaultTests {

@Test
fun calculateVaultSummary_basic() {
val historicalPnl = IndexerVaultHistoricalPnlResponse(
vaultOfVaultsPnl = iListOf(
val historicalPnl = IndexerMegavaultHistoricalPnlResponse(
megavaultPnl = arrayOf(
IndexerPnlTicksResponseObject(
equity = "10000.0",
totalPnl = "1000.0",
Expand Down Expand Up @@ -60,8 +63,8 @@ class VaultTests {

@Test
fun shouldReturnNullForNullOrEmptyHistoricalPnl() {
val nullHistoricalPnl = IndexerVaultHistoricalPnlResponse(vaultOfVaultsPnl = null)
val emptyHistoricalPnl = IndexerVaultHistoricalPnlResponse(vaultOfVaultsPnl = iListOf())
val nullHistoricalPnl = IndexerMegavaultHistoricalPnlResponse(megavaultPnl = null)
val emptyHistoricalPnl = IndexerMegavaultHistoricalPnlResponse(megavaultPnl = arrayOf())

val nullVaultDetails = calculateVaultSummary(nullHistoricalPnl)
val emptyVaultDetails = calculateVaultSummary(emptyHistoricalPnl)
Expand All @@ -77,8 +80,8 @@ class VaultTests {
val thirtyDaysAgoTimestamp = latestTimestamp - 30.days.inWholeMilliseconds
val twentyNineDaysAgoTimestamp = latestTimestamp - 29.days.inWholeMilliseconds

val historicalPnl = IndexerVaultHistoricalPnlResponse(
vaultOfVaultsPnl = iListOf(
val historicalPnl = IndexerMegavaultHistoricalPnlResponse(
megavaultPnl = arrayOf(
IndexerPnlTicksResponseObject(
equity = "10000.0",
totalPnl = "1000.0",
Expand Down Expand Up @@ -121,7 +124,7 @@ class VaultTests {
@Test
fun shouldCalculateVaultPositionCorrectly() {
val position = IndexerVaultPosition(
market = "BTC-USD",
ticker = "BTC-USD",
assetPosition = IndexerAssetPositionResponseObject(
symbol = "USDC",
side = IndexerPositionSide.SHORT,
Expand Down Expand Up @@ -151,8 +154,8 @@ class VaultTests {
)

val history = IndexerVaultHistoricalPnl(
marketId = "BTC-USD",
historicalPnl = iListOf(
ticker = "BTC-USD",
historicalPnl = arrayOf(
IndexerPnlTicksResponseObject(
id = "1",
equity = "10500.0",
Expand Down

0 comments on commit bdc9629

Please sign in to comment.