From 9c3199a24e3c4fb9cd3254b2cece9c99d6d0f161 Mon Sep 17 00:00:00 2001 From: Rui <102453770+ruixhuang@users.noreply.github.com> Date: Mon, 17 Jun 2024 16:16:32 -0700 Subject: [PATCH] Fix toInt() crash (#448) Co-authored-by: mobile-build-bot-git --- detekt.yml | 2 ++ .../calculator/AccountCalculator.kt | 8 +++++++- .../calculator/MarginModeCalculator.kt | 7 ++++++- .../wallet/account/AccountProcessor.kt | 6 ++++++ .../state/model/TradingStateMachine.kt | 17 ----------------- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/detekt.yml b/detekt.yml index e5142244b..498db2ddb 100644 --- a/detekt.yml +++ b/detekt.yml @@ -7,6 +7,8 @@ naming: MatchingDeclarationName: # Affects a lot of the TradingStateMachine+_.kt files active: false + EnumNaming: + active: false complexity: CognitiveComplexMethod: diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/AccountCalculator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/AccountCalculator.kt index 8cd18a1e1..47bf2bd59 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/AccountCalculator.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/AccountCalculator.kt @@ -2,6 +2,7 @@ package exchange.dydx.abacus.calculator import abs import exchange.dydx.abacus.protocols.ParserProtocol +import exchange.dydx.abacus.utils.Logger import exchange.dydx.abacus.utils.NUM_PARENT_SUBACCOUNTS import exchange.dydx.abacus.utils.ParsingHelper import exchange.dydx.abacus.utils.mutable @@ -22,7 +23,12 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco val subaccounts = parser.asMap(account["subaccounts"]) ?: return account var modified = account.mutable() for ((subaccountNumber, subaccount) in subaccounts) { - val parentSubaccountNumber = subaccountNumber.toInt() % NUM_PARENT_SUBACCOUNTS + val subaccountNumberInt = parser.asInt(subaccountNumber) + if (subaccountNumberInt == null) { + Logger.e { "Invalid subaccount number: $subaccountNumber" } + continue + } + val parentSubaccountNumber = subaccountNumberInt % NUM_PARENT_SUBACCOUNTS if (parentSubaccountNumber in subaccountNumbers) { val key = "subaccounts.$subaccountNumber" modified.safeSet( diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/MarginModeCalculator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/MarginModeCalculator.kt index bdaeab2ed..89e6f8327 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/calculator/MarginModeCalculator.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/calculator/MarginModeCalculator.kt @@ -2,6 +2,7 @@ package exchange.dydx.abacus.calculator import abs import exchange.dydx.abacus.protocols.ParserProtocol +import exchange.dydx.abacus.utils.Logger import exchange.dydx.abacus.utils.MAX_LEVERAGE_BUFFER_PERCENT import exchange.dydx.abacus.utils.MAX_SUBACCOUNT_NUMBER import exchange.dydx.abacus.utils.NUM_PARENT_SUBACCOUNTS @@ -162,7 +163,11 @@ internal object MarginModeCalculator { // Check if an existing childSubaccount is available to use for Isolated Margin Trade var availableSubaccountNumber = subaccountNumber utilizedSubaccountsMarketIdMap.forEach { (key, marketIds) -> - val subaccountNumberToCheck = key.toInt() + val subaccountNumberToCheck = parser.asInt(key) + if (subaccountNumberToCheck == null) { + Logger.e { "Invalid subaccount number: $key" } + return@forEach + } if (subaccountNumberToCheck != subaccountNumber) { if (marketIds.contains(marketId) && marketIds.size <= 1) { return subaccountNumberToCheck diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/processor/wallet/account/AccountProcessor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/processor/wallet/account/AccountProcessor.kt index 465252fc6..c04d7e046 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/processor/wallet/account/AccountProcessor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/processor/wallet/account/AccountProcessor.kt @@ -9,6 +9,7 @@ import exchange.dydx.abacus.protocols.ParserProtocol import exchange.dydx.abacus.responses.SocketInfo import exchange.dydx.abacus.state.manager.BlockAndTime import exchange.dydx.abacus.utils.IMutableList +import exchange.dydx.abacus.utils.Logger import exchange.dydx.abacus.utils.Numeric import exchange.dydx.abacus.utils.mutable import exchange.dydx.abacus.utils.safeSet @@ -739,6 +740,11 @@ internal class V4SubaccountsProcessor(parser: ParserProtocol) : SubaccountProces val modifiedSubaccounts = existing.mutable() val modifiedSubaccountIds = mutableListOf() for ((key, value) in existing) { + val keyInt = parser.asInt(key) + if (keyInt == null) { + Logger.e { "Invalid subaccount key: $key" } + continue + } val subaccount = parser.asNativeMap(value) if (subaccount != null) { val (modifiedSubaccount, subaccountUpdated) = subaccountProcessor.updateHeight( 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 f7b38fcdf..16fd8d82e 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine.kt @@ -1366,23 +1366,6 @@ open class TradingStateMachine( ) } - private fun calculateAccount(subaccountNumbers: IList, period: CalculationPeriod) { - this.account?.let { - this.marketsSummary?.let { marketsSummary -> - parser.asNativeMap(marketsSummary["markets"])?.let { markets -> - this.account = accountCalculator.calculate( - it, - subaccountNumbers, - null, - markets, - priceOverwrite(markets), - setOf(period), - ) - } - } - } - } - private fun priceOverwrite(markets: Map): Map? { // TODO(@aforaleka): Uncomment when protocol can match collateralization check at limit price // if (parser.asString(input?.get("current")) == "trade") {