Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Oct 23, 2024
1 parent e5f6ee8 commit 084b5b3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ object VaultCalculator {
}

fun calculateVaultSummary(historicals: List<IndexerMegavaultHistoricalPnlResponse>?): VaultDetails? {
val combinedPnls = historicals?.flatMap { it.megavaultPnl?.toList() ?: emptyList() } // Convert Array to List
val combinedPnls = historicals?.flatMap { it.megavaultPnl?.toList() ?: emptyList() } // Convert Array to List

if (combinedPnls.isNullOrEmpty()) {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext

internal class VaultSupervisor(
stateMachine: TradingStateMachine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class VaultTests {

@Test
fun calculateVaultSummary_basic() {
val historicalPnl = IndexerMegavaultHistoricalPnlResponse(
val historicalPnl1 = IndexerMegavaultHistoricalPnlResponse(
megavaultPnl = arrayOf(
IndexerPnlTicksResponseObject(
equity = "10000.0",
Expand All @@ -44,7 +44,24 @@ class VaultTests {
),
)

val vaultDetails = calculateVaultSummary(historicalPnl)
val historicalPnl2 = IndexerMegavaultHistoricalPnlResponse(
megavaultPnl = arrayOf(
IndexerPnlTicksResponseObject(
equity = "10000.0",
totalPnl = "1000.0",
netTransfers = "0.0",
createdAt = Instant.fromEpochMilliseconds(1659465500000).toString(),
),
IndexerPnlTicksResponseObject(
equity = "5000.0",
totalPnl = "500",
netTransfers = "0.0",
createdAt = Instant.fromEpochMilliseconds(1659379200001).toString(),
),
),
)

val vaultDetails = calculateVaultSummary(listOf(historicalPnl1, historicalPnl2))

val expectedVaultDetails = VaultDetails(
totalValue = 10000.0,
Expand All @@ -55,6 +72,16 @@ class VaultTests {
equity = 10000.0,
totalPnl = 1000.0,
),
VaultHistoryEntry(
date = 1659465500000.0,
equity = 10000.0,
totalPnl = 1000.0,
),
VaultHistoryEntry(
date = 1659379200001.0,
equity = 5000.0,
totalPnl = 500.0,
),
VaultHistoryEntry(
date = 1659379200000.0,
equity = 5000.0,
Expand All @@ -71,8 +98,8 @@ class VaultTests {
val nullHistoricalPnl = IndexerMegavaultHistoricalPnlResponse(megavaultPnl = null)
val emptyHistoricalPnl = IndexerMegavaultHistoricalPnlResponse(megavaultPnl = arrayOf())

val nullVaultDetails = calculateVaultSummary(nullHistoricalPnl)
val emptyVaultDetails = calculateVaultSummary(emptyHistoricalPnl)
val nullVaultDetails = calculateVaultSummary(listOf(nullHistoricalPnl))
val emptyVaultDetails = calculateVaultSummary(listOf(emptyHistoricalPnl))

assertEquals(null, nullVaultDetails)
assertEquals(null, emptyVaultDetails)
Expand Down Expand Up @@ -120,7 +147,7 @@ class VaultTests {
),
)

val vaultDetails = calculateVaultSummary(historicalPnl)
val vaultDetails = calculateVaultSummary(listOf(historicalPnl))

assertNotNull(vaultDetails)
assertEquals(0.6403508771929824, vaultDetails.thirtyDayReturnPercent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ fun TradingStateMachine.rest(
}

"/v4/vault/v1/megavault/historicalPnl" -> {
changes = onMegaVaultPnl(payload)
changes = onMegaVaultPnl(listOf(payload))
}

"/v4/vault/v1/megavault/positions" -> {
Expand Down

0 comments on commit 084b5b3

Please sign in to comment.