diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine.kt index 7073d8257..c934531fa 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine.kt @@ -1365,11 +1365,15 @@ open class TradingStateMachine( } } if (changes.changes.contains(Changes.vault) || changes.changes.contains(Changes.markets)) { - val positions = VaultCalculator.calculateVaultPositionsInternal( - vault = internalState.vault, - markets = marketsSummary?.markets, - ) - vault = Vault(details = internalState.vault?.details, positions = positions) + if (internalState.vault != null) { + val positions = VaultCalculator.calculateVaultPositionsInternal( + vault = internalState.vault, + markets = marketsSummary?.markets, + ) + vault = Vault(details = internalState.vault?.details, positions = positions) + } else { + vault = null + } } return PerpetualState( assets = assets, diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/manager/StateManagerAdaptorV2.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/manager/StateManagerAdaptorV2.kt index c14eba75e..aa7dbd7f4 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/manager/StateManagerAdaptorV2.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/manager/StateManagerAdaptorV2.kt @@ -358,6 +358,7 @@ internal class StateManagerAdaptorV2( onboarding.readyToConnect = readyToConnect markets.readyToConnect = readyToConnect accounts.readyToConnect = readyToConnect + vault.readyToConnect = readyToConnect if (readyToConnect) { pollGeo() } @@ -368,6 +369,7 @@ internal class StateManagerAdaptorV2( onboarding.indexerConnected = indexerConnected markets.indexerConnected = indexerConnected accounts.indexerConnected = indexerConnected + vault.indexerConnected = indexerConnected } private fun didSetSocketConnected(socketConnected: Boolean) { @@ -376,6 +378,7 @@ internal class StateManagerAdaptorV2( onboarding.socketConnected = socketConnected markets.socketConnected = socketConnected accounts.socketConnected = socketConnected + vault.socketConnected = socketConnected } private fun didSetValidatorConnected(validatorConnected: Boolean) { @@ -383,6 +386,7 @@ internal class StateManagerAdaptorV2( onboarding.validatorConnected = validatorConnected markets.validatorConnected = validatorConnected accounts.validatorConnected = validatorConnected + vault.validatorConnected = validatorConnected } internal fun dispose() { diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/Configs.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/Configs.kt index d7bc413a9..50dde4b11 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/Configs.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/Configs.kt @@ -189,6 +189,7 @@ data class OnboardingConfigs( @JsExport data class VaultConfigs( val retrieveVault: Boolean, + var useMocks: Boolean = true, ) { companion object { val forApp = VaultConfigs( diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/VaultSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/VaultSupervisor.kt index 9a22be818..db859df78 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/VaultSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/VaultSupervisor.kt @@ -7,6 +7,20 @@ import exchange.dydx.abacus.state.model.onVaultMarketPnls import exchange.dydx.abacus.state.model.onVaultMarketPositions import exchange.dydx.abacus.utils.AnalyticsUtils import exchange.dydx.abacus.utils.CoroutineTimer +import exchange.dydx.abacus.utils.NUM_PARENT_SUBACCOUNTS +import indexer.codegen.IndexerAssetPositionResponseObject +import indexer.codegen.IndexerMegavaultHistoricalPnlResponse +import indexer.codegen.IndexerMegavaultPositionResponse +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 indexer.codegen.IndexerVaultsHistoricalPnlResponse +import kotlinx.datetime.Instant +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json internal class VaultSupervisor( stateMachine: TradingStateMachine, @@ -47,45 +61,127 @@ internal class VaultSupervisor( } private fun retrieveMegaVaultPnl() { - val url = helper.configs.publicApiUrl("vault/megavault/historicalPnl") - if (url != null) { - helper.get( - url = url, - params = null, - headers = null, - ) { _, response, httpCode, _ -> - if (helper.success(httpCode) && response != null) { - stateMachine.onMegaVaultPnl(response) + if (configs.useMocks) { + val mock = IndexerMegavaultHistoricalPnlResponse( + megavaultPnl = arrayOf( + IndexerPnlTicksResponseObject( + equity = "10000.0", + totalPnl = "1000.0", + netTransfers = "0.0", + createdAt = Instant.fromEpochMilliseconds(1659465600000).toString(), + ), + IndexerPnlTicksResponseObject( + equity = "5000.0", + totalPnl = "500", + netTransfers = "0.0", + createdAt = Instant.fromEpochMilliseconds(1659379200000).toString(), + ), + ), + ) + stateMachine.onMegaVaultPnl(Json.encodeToString(mock)) + } else { + val url = helper.configs.publicApiUrl("vault/megavault/historicalPnl") + if (url != null) { + helper.get( + url = url, + params = null, + headers = null, + ) { _, response, httpCode, _ -> + if (helper.success(httpCode) && response != null) { + stateMachine.onMegaVaultPnl(response) + } } } } } private fun retrieveVaultMarketPnls() { - val url = helper.configs.publicApiUrl("vault/vaults/historicalPnl") - if (url != null) { - helper.get( - url = url, - params = null, - headers = null, - ) { _, response, httpCode, _ -> - if (helper.success(httpCode) && response != null) { - stateMachine.onVaultMarketPnls(response) + if (configs.useMocks) { + val btcHistory = IndexerVaultHistoricalPnl( + ticker = "BTC-USD", + historicalPnl = arrayOf( + IndexerPnlTicksResponseObject( + id = "1", + equity = "10500.0", + totalPnl = "500.0", + netTransfers = "0.0", + createdAt = Instant.fromEpochMilliseconds(1659465600000).toString(), + ), + IndexerPnlTicksResponseObject( + id = "2", + equity = "10000.0", + totalPnl = "0.0", + netTransfers = "0.0", + createdAt = Instant.fromEpochMilliseconds(1659379200000).toString(), + ), + ), + ) + val marketPnls = IndexerVaultsHistoricalPnlResponse( + vaultsPnl = arrayOf(btcHistory), + ) + stateMachine.onVaultMarketPnls(Json.encodeToString(marketPnls)) + } else { + val url = helper.configs.publicApiUrl("vault/vaults/historicalPnl") + if (url != null) { + helper.get( + url = url, + params = null, + headers = null, + ) { _, response, httpCode, _ -> + if (helper.success(httpCode) && response != null) { + stateMachine.onVaultMarketPnls(response) + } } } } } private fun retrieveVaultMarketPositions() { - val url = helper.configs.publicApiUrl("vault/positions") - if (url != null) { - helper.get( - url = url, - params = null, - headers = null, - ) { _, response, httpCode, _ -> - if (helper.success(httpCode) && response != null) { - stateMachine.onVaultMarketPositions(response) + if (configs.useMocks) { + val btcPosition = IndexerVaultPosition( + ticker = "BTC-USD", + assetPosition = IndexerAssetPositionResponseObject( + symbol = "USDC", + side = IndexerPositionSide.SHORT, + size = "40000.0", + assetId = "0", + subaccountNumber = NUM_PARENT_SUBACCOUNTS, + ), + perpetualPosition = IndexerPerpetualPositionResponseObject( + market = "BTC-USD", + status = IndexerPerpetualPositionStatus.OPEN, + side = IndexerPositionSide.LONG, + size = "1.0", + maxSize = null, + entryPrice = "50000.0", + realizedPnl = null, + createdAt = "2023-08-01T00:00:00Z", + createdAtHeight = "1000", + sumOpen = null, + sumClose = null, + netFunding = null, + unrealizedPnl = "5000.0", + closedAt = null, + exitPrice = null, + subaccountNumber = NUM_PARENT_SUBACCOUNTS, + ), + equity = "15000.0", + ) + val megaVaultPosition = IndexerMegavaultPositionResponse( + positions = arrayOf(btcPosition), + ) + stateMachine.onVaultMarketPositions(Json.encodeToString(megaVaultPosition)) + } else { + val url = helper.configs.publicApiUrl("vault/positions") + if (url != null) { + helper.get( + url = url, + params = null, + headers = null, + ) { _, response, httpCode, _ -> + if (helper.success(httpCode) && response != null) { + stateMachine.onVaultMarketPositions(response) + } } } }