Skip to content

Commit

Permalink
combined hourly and daily data
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Oct 23, 2024
1 parent 0f9f73b commit e5f6ee8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 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,11 @@ 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
import kotlin.coroutines.CoroutineContext

internal class VaultSupervisor(
stateMachine: TradingStateMachine,
Expand Down Expand Up @@ -132,18 +137,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(listOf(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
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 e5f6ee8

Please sign in to comment.