Skip to content

Commit

Permalink
Use mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixhuang committed Sep 25, 2024
1 parent 79d6a04 commit 2de3ff6
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ internal class StateManagerAdaptorV2(
onboarding.readyToConnect = readyToConnect
markets.readyToConnect = readyToConnect
accounts.readyToConnect = readyToConnect
vault.readyToConnect = readyToConnect
if (readyToConnect) {
pollGeo()
}
Expand All @@ -368,6 +369,7 @@ internal class StateManagerAdaptorV2(
onboarding.indexerConnected = indexerConnected
markets.indexerConnected = indexerConnected
accounts.indexerConnected = indexerConnected
vault.indexerConnected = indexerConnected
}

private fun didSetSocketConnected(socketConnected: Boolean) {
Expand All @@ -376,13 +378,15 @@ internal class StateManagerAdaptorV2(
onboarding.socketConnected = socketConnected
markets.socketConnected = socketConnected
accounts.socketConnected = socketConnected
vault.socketConnected = socketConnected
}

private fun didSetValidatorConnected(validatorConnected: Boolean) {
system.validatorConnected = validatorConnected
onboarding.validatorConnected = validatorConnected
markets.validatorConnected = validatorConnected
accounts.validatorConnected = validatorConnected
vault.validatorConnected = validatorConnected
}

internal fun dispose() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ data class OnboardingConfigs(
@JsExport
data class VaultConfigs(
val retrieveVault: Boolean,
var useMocks: Boolean = true,
) {
companion object {
val forApp = VaultConfigs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down

0 comments on commit 2de3ff6

Please sign in to comment.