From 3dd9c4226a504e4a9fcd6ac872a3deec92b34693 Mon Sep 17 00:00:00 2001 From: John Huang Date: Wed, 22 May 2024 11:37:26 -0700 Subject: [PATCH] Feature/tra 257 margin type according to market (#372) --- build.gradle.kts | 2 +- .../processor/markets/MarketProcessor.kt | 2 +- .../model/TradingStateMachine+TradeInput.kt | 23 +++++++++---- .../payload/v4/V4ParentSubaccountTests.kt | 32 +++++++++++++++++++ .../tests/payloads/MarketsChannelMock.kt | 3 +- v4_abacus.podspec | 2 +- 6 files changed, 54 insertions(+), 10 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 051d07a9b..d2c14deb7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -51,7 +51,7 @@ allprojects { } group = "exchange.dydx.abacus" -version = "1.7.25" +version = "1.7.26" repositories { google() diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/processor/markets/MarketProcessor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/processor/markets/MarketProcessor.kt index 7128af89d..ddbddb758 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/processor/markets/MarketProcessor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/processor/markets/MarketProcessor.kt @@ -70,7 +70,7 @@ internal class MarketProcessor(parser: ParserProtocol, private val calculateSpar private val configsKeyMap = mapOf( "string" to mapOf( - "perpetualMarketType" to "perpetualMarketType", + "marketType" to "perpetualMarketType", ), "double" to mapOf( "maintenanceMarginFraction" to "maintenanceMarginFraction", diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+TradeInput.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+TradeInput.kt index f97211031..15f915912 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+TradeInput.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+TradeInput.kt @@ -123,6 +123,7 @@ internal fun TradingStateMachine.updateTradeInputFromMarket( val marketId = parser.asString(trade["marketId"]) if (marketId != null && account != null) { val existingMarginMode = findExistingMarginMode(account, marketId, subaccountNumber) + ?: findMarketMarginMode(this.marketsSummary, marketId) if (existingMarginMode != null) { modified["marginMode"] = existingMarginMode if (existingMarginMode == "ISOLATED" && parser.asDouble(trade["targetLeverage"]) == null) { @@ -154,12 +155,14 @@ private fun TradingStateMachine.findExistingMarginMode( parser.asString(parser.value(it, "marketId")) == marketId } if (order != null) { - return if (parser.asInt( - parser.value( - order, - "subaccountNumber", - ), - ) ?: subaccountNumber != subaccountNumber + return if (( + parser.asInt( + parser.value( + order, + "subaccountNumber", + ), + ) ?: subaccountNumber + ) != subaccountNumber ) { "ISOLATED" } else { @@ -169,6 +172,14 @@ private fun TradingStateMachine.findExistingMarginMode( return null } +private fun TradingStateMachine.findMarketMarginMode( + marketsSummary: Map?, + marketId: String +): String? { + val marginMode = parser.asString(parser.value(marketsSummary, "markets.$marketId.configs.perpetualMarketType")) + return marginMode ?: "CROSS" +} + internal fun TradingStateMachine.initiateTrade( marketId: String?, subaccountNumber: Int, diff --git a/src/commonTest/kotlin/exchange.dydx.abacus/payload/v4/V4ParentSubaccountTests.kt b/src/commonTest/kotlin/exchange.dydx.abacus/payload/v4/V4ParentSubaccountTests.kt index b23fb799a..d44b9a88e 100644 --- a/src/commonTest/kotlin/exchange.dydx.abacus/payload/v4/V4ParentSubaccountTests.kt +++ b/src/commonTest/kotlin/exchange.dydx.abacus/payload/v4/V4ParentSubaccountTests.kt @@ -321,6 +321,38 @@ class V4ParentSubaccountTests : V4BaseTests(true) { } """.trimIndent(), ) + + test( + { + perp.tradeInMarket("AVAX-USD", 0) + }, + """ + { + "input": { + "current": "trade", + "trade": { + "marginMode": "ISOLATED" + } + } + } + """.trimIndent(), + ) + + test( + { + perp.tradeInMarket("LINK-USD", 0) + }, + """ + { + "input": { + "current": "trade", + "trade": { + "marginMode": "CROSS" + } + } + } + """.trimIndent(), + ) } private fun testSubaccountChannelData() { diff --git a/src/commonTest/kotlin/exchange.dydx.abacus/tests/payloads/MarketsChannelMock.kt b/src/commonTest/kotlin/exchange.dydx.abacus/tests/payloads/MarketsChannelMock.kt index 701b5e7d2..a00bbd6bf 100644 --- a/src/commonTest/kotlin/exchange.dydx.abacus/tests/payloads/MarketsChannelMock.kt +++ b/src/commonTest/kotlin/exchange.dydx.abacus/tests/payloads/MarketsChannelMock.kt @@ -77,7 +77,8 @@ internal class MarketsChannelMock { "atomicResolution":-10, "quantumConversionExponent":-9, "stepBaseQuantums":1000000, - "subticksPerTick":100000 + "subticksPerTick":100000, + "marketType":"ISOLATED" }, "SUSHI-USD": { "market": "SUSHI-USD", diff --git a/v4_abacus.podspec b/v4_abacus.podspec index 7899f6f72..4db5b9cb7 100644 --- a/v4_abacus.podspec +++ b/v4_abacus.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = 'v4_abacus' - spec.version = '1.7.25' + spec.version = '1.7.26' spec.homepage = 'https://github.com/dydxprotocol/v4-abacus' spec.source = { :http=> ''} spec.authors = ''