From a629757d53f5f07fb37e2828dea4c931838bc6c9 Mon Sep 17 00:00:00 2001
From: Rui <rui@dydx.exchange>
Date: Mon, 14 Oct 2024 10:04:26 -0700
Subject: [PATCH 1/2] Wait for the position values before calculating
 subaccount

---
 .../calculator/SubaccountCalculator.kt        | 24 +++++++++++++------
 .../calculator/V2/SubaccountCalculatorV2.kt   | 18 ++++++++++----
 2 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/SubaccountCalculator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/SubaccountCalculator.kt
index 05e2f828f..b48dc9c6c 100644
--- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/SubaccountCalculator.kt
+++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/SubaccountCalculator.kt
@@ -245,7 +245,20 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
     ) {
         for (period in periods) {
             val quoteBalance = parser.asDouble(value(subaccount, "quoteBalance", period))
-            if (quoteBalance != null) {
+
+            var hasPositionCalculated = false
+            positions?.let {
+                for ((key, position) in positions) {
+                    val valueTotal = parser.asDouble(value(position, "valueTotal", period))
+                    if (valueTotal != null) {
+                        hasPositionCalculated = true
+                        break
+                    }
+                }
+            }
+            val positionsReady = positions.isNullOrEmpty() || hasPositionCalculated
+
+            if (quoteBalance != null && positionsReady) {
                 var notionalTotal = Numeric.double.ZERO
                 var valueTotal = Numeric.double.ZERO
                 var initialRiskTotal = Numeric.double.ZERO
@@ -464,12 +477,9 @@ internal class SubaccountCalculator(val parser: ParserProtocol) {
     ) {
         for (period in periods) {
             val quoteBalance = parser.asDouble(value(subaccount, "quoteBalance", period))
-            if (quoteBalance != null) {
-                val equity =
-                    parser.asDouble(value(subaccount, "equity", period)) ?: Numeric.double.ZERO
-                val initialRiskTotal =
-                    parser.asDouble(value(subaccount, "initialRiskTotal", period))
-                        ?: Numeric.double.ZERO
+            val equity = parser.asDouble(value(subaccount, "equity", period))
+            val initialRiskTotal = parser.asDouble(value(subaccount, "initialRiskTotal", period))
+            if (quoteBalance != null && equity != null && initialRiskTotal != null) {
                 val imf =
                     parser.asDouble(configs?.get("initialMarginFraction"))
                         ?: parser.asDouble(0.05)!!
diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/SubaccountCalculatorV2.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/SubaccountCalculatorV2.kt
index cceb4b4b3..970230c6b 100644
--- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/SubaccountCalculatorV2.kt
+++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/V2/SubaccountCalculatorV2.kt
@@ -80,13 +80,21 @@ internal class SubaccountCalculatorV2(
             val calculated = subaccount.calculated[period] ?: InternalSubaccountCalculated()
             subaccount.calculated[period] = calculated
 
+            var hasPositionCalculated = false
+            for (position in positions?.values ?: emptyList()) {
+                if (position.calculated[period] != null) {
+                    hasPositionCalculated = true
+                }
+            }
+            val positionsReady = positions.isNullOrEmpty() || hasPositionCalculated
+
             val quoteBalance = calculated.quoteBalance
-            if (quoteBalance != null) {
+            if (quoteBalance != null && positionsReady) {
                 var notionalTotal = Numeric.double.ZERO
                 var valueTotal = Numeric.double.ZERO
                 var initialRiskTotal = Numeric.double.ZERO
 
-                for ((key, position) in positions ?: emptyMap()) {
+                for (position in positions?.values ?: emptyList()) {
                     val positionCalculated = position.calculated[period]
                     notionalTotal += positionCalculated?.notionalTotal ?: Numeric.double.ZERO
                     valueTotal += positionCalculated?.valueTotal ?: Numeric.double.ZERO
@@ -169,9 +177,9 @@ internal class SubaccountCalculatorV2(
         for (period in periods) {
             val calculated = subaccount?.calculated?.get(period)
             val quoteBalance = calculated?.quoteBalance
-            if (quoteBalance != null) {
-                val equity = calculated.equity ?: Numeric.double.ZERO
-                val initialRiskTotal = calculated.initialRiskTotal ?: Numeric.double.ZERO
+            val equity = calculated?.equity
+            val initialRiskTotal = calculated?.initialRiskTotal
+            if (quoteBalance != null && equity != null && initialRiskTotal != null) {
                 val imf = configs?.initialMarginFraction ?: 0.05
 
                 calculated.buyingPower = calculateBuyingPower(

From 93868c6e8050b7433345f89f7233d43c690e0506 Mon Sep 17 00:00:00 2001
From: Rui <rui@dydx.exchange>
Date: Mon, 14 Oct 2024 10:08:58 -0700
Subject: [PATCH 2/2] Bump version

---
 build.gradle.kts  | 2 +-
 v4_abacus.podspec | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/build.gradle.kts b/build.gradle.kts
index dba1fedaa..298c835b5 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -52,7 +52,7 @@ allprojects {
 }
 
 group = "exchange.dydx.abacus"
-version = "1.12.21"
+version = "1.12.22"
 
 repositories {
     google()
diff --git a/v4_abacus.podspec b/v4_abacus.podspec
index 2538916b5..775815611 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.12.21'
+    spec.version                  = '1.12.22'
     spec.homepage                 = 'https://github.com/dydxprotocol/v4-abacus'
     spec.source                   = { :http=> ''}
     spec.authors                  = ''