Skip to content

Commit

Permalink
combined hourly and daily data (#725)
Browse files Browse the repository at this point in the history
Co-authored-by: Mike <[email protected]>
  • Loading branch information
mike-dydx and mike-dydx authored Oct 23, 2024
1 parent 0f9f73b commit 7743172
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.13.8"
version = "1.13.9"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ object VaultCalculator {
return parser.asTypedObject<IndexerMegavaultPositionResponse>(apiResponse)
}

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

if (combinedPnls.isNullOrEmpty()) {
return null
}

val vaultOfVaultsPnl =
historical!!.megavaultPnl!!.sortedByDescending { parser.asDatetime(it.createdAt)?.toEpochMilliseconds() ?: 0 }
combinedPnls.sortedByDescending { parser.asDatetime(it.createdAt)?.toEpochMilliseconds() ?: 0 }

val history = vaultOfVaultsPnl.mapNotNull { entry ->
parser.asDatetime(entry.createdAt)?.toEpochMilliseconds()?.toDouble()?.let { createdAt ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class VaultProcessor(

fun processMegaVaultsHistoricalPnl(
existing: InternalVaultState?,
payload: IndexerMegavaultHistoricalPnlResponse?,
payload: List<IndexerMegavaultHistoricalPnlResponse>?,
): InternalVaultState? {
if (payload == null) {
return existing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import indexer.models.chain.OnChainAccountVaultResponse
import kollections.iListOf

internal fun TradingStateMachine.onMegaVaultPnl(
payload: String
payloads: List<String>
): StateChanges {
val pnlResponse = parser.asTypedObject<IndexerMegavaultHistoricalPnlResponse>(payload)
val newState = vaultProcessor.processMegaVaultsHistoricalPnl(internalState.vault, pnlResponse)
val responses = payloads.mapNotNull { parser.asTypedObject<IndexerMegavaultHistoricalPnlResponse>(it) }
val newState = vaultProcessor.processMegaVaultsHistoricalPnl(internalState.vault, responses)
return updateVaultState(internalState, newState)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import exchange.dydx.abacus.utils.CoroutineTimer
import exchange.dydx.abacus.utils.Logger
import indexer.models.chain.OnChainAccountVaultResponse
import indexer.models.chain.OnChainNumShares
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

internal class VaultSupervisor(
stateMachine: TradingStateMachine,
Expand Down Expand Up @@ -132,18 +136,25 @@ internal class VaultSupervisor(
}

private fun retrieveMegaVaultPnl() {
val url = helper.configs.publicApiUrl("vaultHistoricalPnl")
if (url != null) {
helper.get(
url = url,
params = mapOf("resolution" to "day"),
headers = null,
) { _, response, httpCode, _ ->
if (helper.success(httpCode) && response != null) {
stateMachine.onMegaVaultPnl(response)
} else {
val scope = CoroutineScope(Dispatchers.Unconfined)
scope.launch {
val url = helper.configs.publicApiUrl("vaultHistoricalPnl")
if (url != null) {
val deferredDaily = async { helper.getAsync(url, params = mapOf("resolution" to "day"), headers = null) }
val deferredHourly = async { helper.getAsync(url, params = mapOf("resolution" to "hour"), headers = null) }

val dailyResponse = deferredDaily.await()
val hourlyResponse = deferredHourly.await()

if (dailyResponse.response != null || hourlyResponse.response != null) {
stateMachine.onMegaVaultPnl(listOfNotNull(dailyResponse.response, hourlyResponse.response))
} else if (dailyResponse.error != null) {
Logger.e {
"Failed to retrieve day mega vault pnl: ${dailyResponse.error}"
}
} else if (hourlyResponse.error != null) {
Logger.e {
"Failed to retrieve mega vault pnl: $httpCode, $response"
"Failed to retrieve hourly mega vault pnl: ${hourlyResponse.error}"
}
}
}
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
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.13.8'
spec.version = '1.13.9'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down

0 comments on commit 7743172

Please sign in to comment.