Skip to content

Commit

Permalink
Vault PNL cut off (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixhuang authored Nov 14, 2024
1 parent a8099e8 commit 4d4e1fb
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 13 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.27"
version = "1.13.28"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ object VaultCalculator {
}
}.filter { entry -> entry.date != null && entry.date >= dataCutoffMs }

val latestEntry = history.first()
val latestEntry = history.firstOrNull() ?: return null
val latestTime = latestEntry.date ?: Clock.System.now().toEpochMilliseconds().toDouble()
val thirtyDaysAgoTime = latestTime - 30.days.inWholeMilliseconds

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ package exchange.dydx.abacus.processor.vault

import exchange.dydx.abacus.functional.vault.ThirtyDayPnl
import exchange.dydx.abacus.functional.vault.VaultCalculator
import exchange.dydx.abacus.processor.base.BaseProcessor
import exchange.dydx.abacus.processor.wallet.account.AssetPositionProcessor
import exchange.dydx.abacus.processor.wallet.account.PerpetualPositionProcessor
import exchange.dydx.abacus.protocols.LocalizerProtocol
import exchange.dydx.abacus.protocols.ParserProtocol
import exchange.dydx.abacus.state.internalstate.InternalVaultPositionState
import exchange.dydx.abacus.state.internalstate.InternalVaultState
import exchange.dydx.abacus.state.manager.V4Environment
import indexer.codegen.IndexerMegavaultHistoricalPnlResponse
import indexer.codegen.IndexerMegavaultPositionResponse
import indexer.codegen.IndexerTransferBetweenResponse
import indexer.codegen.IndexerVaultsHistoricalPnlResponse
import indexer.models.chain.OnChainAccountVaultResponse

internal class VaultProcessor(
parser: ParserProtocol,
private val parser: ParserProtocol,
private val environment: V4Environment?,
localizer: LocalizerProtocol?,
) : BaseProcessor(parser) {
) {

private val perpetualPositionProcessor = PerpetualPositionProcessor(parser, localizer)
private val assetPositionProcessor = AssetPositionProcessor(parser)
Expand All @@ -31,7 +32,10 @@ internal class VaultProcessor(
return existing
}

val newValue = VaultCalculator.calculateVaultSummary(payloads.toTypedArray())
val newValue = VaultCalculator.calculateVaultSummary(
historicals = payloads.toTypedArray(),
dataCutoffMs = environment?.megavaultHistoryStartDateMs ?: 0.0,
)
return if (newValue != existing?.details) {
existing?.copy(details = newValue) ?: InternalVaultState(details = newValue)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ class V4Environment(
val chainName: String?,
val chainLogo: String?,
rewardsHistoryStartDateMs: String,
val megavaultHistoryStartDateMs: Double?,
val megavaultOperatorName: String,
isMainNet: Boolean,
endpoints: EnvironmentEndpoints,
Expand Down Expand Up @@ -471,6 +472,7 @@ class V4Environment(
val chainName = parser.asString(data["chainName"])
val chainLogo = parser.asString(data["chainLogo"])
val rewardsHistoryStartDateMs = parser.asString(data["rewardsHistoryStartDateMs"]) ?: ServerTime.now().minus(180.days).toEpochMilliseconds().toString()
val megavaultHistoryStartDateMs = parser.asDouble(data["megavaultHistoryStartDateMs"])
val megavaultOperatorName = parser.asString(data["megavaultOperatorName"]) ?: return null
val isMainNet = parser.asBool(data["isMainNet"]) ?: return null
val endpoints =
Expand Down Expand Up @@ -503,6 +505,7 @@ class V4Environment(
chainName = chainName,
chainLogo = "$deploymentUri$chainLogo",
rewardsHistoryStartDateMs = rewardsHistoryStartDateMs,
megavaultHistoryStartDateMs = megavaultHistoryStartDateMs,
megavaultOperatorName = megavaultOperatorName,
isMainNet = isMainNet,
endpoints = endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ open class TradingStateMachine(
processor
}
internal val walletProcessor = WalletProcessor(parser, localizer)
internal val vaultProcessor = VaultProcessor(parser, localizer)
internal val vaultProcessor = VaultProcessor(parser, environment, localizer)
internal val configsProcessor = ConfigsProcessor(parser, localizer)
internal val routerProcessor = SkipProcessor(
parser = parser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class AbacusMockData {
chainName = "dYdX Chain",
chainLogo = "dYdX-logo.png",
rewardsHistoryStartDateMs = "1704844800000",
megavaultHistoryStartDateMs = 1704844800000.0,
megavaultOperatorName = "test",
isMainNet = false,
endpoints = EnvironmentEndpoints(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@ import indexer.codegen.IndexerPositionSide
import indexer.codegen.IndexerVaultHistoricalPnl
import indexer.codegen.IndexerVaultPosition
import indexer.codegen.IndexerVaultsHistoricalPnlResponse
import kotlinx.datetime.Instant
import kotlinx.datetime.Clock
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours

internal class VaultMocks {

val now = Clock.System.now()
private val megaVaultPnl = IndexerMegavaultHistoricalPnlResponse(
megavaultPnl = arrayOf(
IndexerPnlTicksResponseObject(
equity = "10000.0",
totalPnl = "1000.0",
netTransfers = "0.0",
createdAt = Instant.fromEpochMilliseconds(1659465600000).toString(),
createdAt = now.minus(1.hours).toString(),
),
IndexerPnlTicksResponseObject(
equity = "5000.0",
totalPnl = "500",
netTransfers = "0.0",
createdAt = Instant.fromEpochMilliseconds(1659379200000).toString(),
createdAt = now.minus(1.hours).minus(1.days).toString(),
),
),
)
Expand All @@ -43,14 +46,14 @@ internal class VaultMocks {
equity = "10500.0",
totalPnl = "500.0",
netTransfers = "0.0",
createdAt = Instant.fromEpochMilliseconds(1659465600000).toString(),
createdAt = now.minus(1.hours).toString(),
),
IndexerPnlTicksResponseObject(
id = "2",
equity = "10000.0",
totalPnl = "0.0",
netTransfers = "0.0",
createdAt = Instant.fromEpochMilliseconds(1659379200000).toString(),
createdAt = now.minus(1.hours).minus(1.days).toString(),
),
),
)
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.27'
spec.version = '1.13.28'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down

0 comments on commit 4d4e1fb

Please sign in to comment.