Skip to content

Commit

Permalink
Fix toInt() crash (#448)
Browse files Browse the repository at this point in the history
Co-authored-by: mobile-build-bot-git <[email protected]>
  • Loading branch information
ruixhuang and mobile-build-bot authored Jun 17, 2024
1 parent 95aee0d commit 9c3199a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
2 changes: 2 additions & 0 deletions detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ naming:
MatchingDeclarationName:
# Affects a lot of the TradingStateMachine+_.kt files
active: false
EnumNaming:
active: false

complexity:
CognitiveComplexMethod:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -739,6 +740,11 @@ internal class V4SubaccountsProcessor(parser: ParserProtocol) : SubaccountProces
val modifiedSubaccounts = existing.mutable()
val modifiedSubaccountIds = mutableListOf<Int>()
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1366,23 +1366,6 @@ open class TradingStateMachine(
)
}

private fun calculateAccount(subaccountNumbers: IList<Int>, 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<String, Any>): Map<String, Any>? {
// TODO(@aforaleka): Uncomment when protocol can match collateralization check at limit price
// if (parser.asString(input?.get("current")) == "trade") {
Expand Down

0 comments on commit 9c3199a

Please sign in to comment.