From cef9816b4494c1d748e2120fb96ad34eccffe99f Mon Sep 17 00:00:00 2001 From: Prashan Dharmasena Date: Thu, 30 May 2024 10:26:29 -0400 Subject: [PATCH 1/2] Ensure all Json.parseToJsonElement calls are wrapped in try-catch with appropriately scoped exceptions. --- .../state/model/TradingStateMachine+Account.kt | 12 +++++++++++- .../model/TradingStateMachine+FeeDiscounts.kt | 13 ++++++++++++- .../state/model/TradingStateMachine+Rewards.kt | 14 ++++++++++++-- .../state/model/TradingStateMachine+Squid.kt | 12 ++++++++++-- .../state/model/TradingStateMachine+Wallet.kt | 8 +++++++- .../state/model/TradingStateMachine.kt | 3 ++- .../kotlin/exchange.dydx.abacus/utils/Parser.kt | 7 +++---- 7 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Account.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Account.kt index 8fd141649..5048a82a7 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Account.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Account.kt @@ -6,12 +6,22 @@ import exchange.dydx.abacus.responses.StateResponse import exchange.dydx.abacus.state.changes.Changes import exchange.dydx.abacus.state.changes.StateChanges import exchange.dydx.abacus.state.manager.BlockAndTime +import exchange.dydx.abacus.utils.Logger import kollections.iListOf import kollections.toIList +import kotlinx.serialization.SerializationException import kotlinx.serialization.json.Json internal fun TradingStateMachine.account(payload: String): StateChanges { - val json = parser.asMap(Json.parseToJsonElement(payload)) + val json = try { + parser.asMap(Json.parseToJsonElement(payload)) + } catch (exception: SerializationException) { + Logger.e { + "Failed to deserialize account: $payload \n" + + "Exception: $exception" + } + null + } return if (json != null) { receivedAccount(json) } else { diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+FeeDiscounts.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+FeeDiscounts.kt index 187fc09a1..26d848bc1 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+FeeDiscounts.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+FeeDiscounts.kt @@ -2,12 +2,23 @@ package exchange.dydx.abacus.state.model import exchange.dydx.abacus.state.changes.Changes import exchange.dydx.abacus.state.changes.StateChanges +import exchange.dydx.abacus.utils.Logger +import kollections.iEmptyList import kollections.iListOf +import kotlinx.serialization.SerializationException import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonArray internal fun TradingStateMachine.feeDiscounts(payload: String): StateChanges { - val json = Json.parseToJsonElement(payload).jsonArray.toList() + val json = try { + Json.parseToJsonElement(payload).jsonArray.toList() + } catch (exception: SerializationException) { + Logger.e { + "Failed to deserialize feeDiscounts: $payload \n" + + "Exception: $exception" + } + return StateChanges(iEmptyList()) + } return receivedFeeDiscounts(json) } diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Rewards.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Rewards.kt index f4c4693dd..3e4dc9138 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Rewards.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Rewards.kt @@ -1,7 +1,9 @@ package exchange.dydx.abacus.state.model import exchange.dydx.abacus.state.changes.StateChanges +import exchange.dydx.abacus.utils.Logger import kollections.iListOf +import kotlinx.serialization.SerializationException import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonObject @@ -17,8 +19,16 @@ internal fun TradingStateMachine.onChainRewardsParams(payload: String): StateCha return StateChanges(iListOf()) } -internal fun TradingStateMachine.onChainRewardTokenPrice(payload: String): StateChanges { - val json = Json.parseToJsonElement(payload).jsonObject.toMap() +internal fun TradingStateMachine.onChainRewardTokenPrice(payload: String): StateChanges? { + val json = try { + Json.parseToJsonElement(payload).jsonObject.toMap() + } catch (exception: SerializationException) { + Logger.e { + "Failed to deserialize onChainRewardTokenPrice: $payload \n" + + "Exception: $exception" + } + null + } val map = parser.asMap(json) val price = parser.asMap(map?.get("marketPrice")) rewardsParams = diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt index 943d5becc..ec0831841 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt @@ -2,7 +2,10 @@ package exchange.dydx.abacus.state.model import exchange.dydx.abacus.state.changes.Changes import exchange.dydx.abacus.state.changes.StateChanges +import exchange.dydx.abacus.utils.Logger +import kollections.iEmptyList import kollections.iListOf +import kotlinx.serialization.SerializationException import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonObject @@ -73,8 +76,13 @@ internal fun TradingStateMachine.squidRouteV2( internal fun TradingStateMachine.squidStatus( payload: String, transactionId: String? -): StateChanges? { - val json = Json.parseToJsonElement(payload).jsonObject.toMap() +): StateChanges { + val json = try { + Json.parseToJsonElement(payload).jsonObject.toMap() + } catch (exception: SerializationException) { + Logger.e { "Failed to deserialize squidStatus: $payload \nException: $exception" } + return StateChanges(iEmptyList()) + } transferStatuses = routerProcessor.receivedStatus(transferStatuses, json, transactionId) return StateChanges(iListOf(Changes.transferStatuses)) } diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Wallet.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Wallet.kt index fe496cab4..c57519466 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Wallet.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Wallet.kt @@ -5,9 +5,11 @@ import exchange.dydx.abacus.responses.SocketInfo import exchange.dydx.abacus.state.changes.Changes import exchange.dydx.abacus.state.changes.StateChanges import exchange.dydx.abacus.state.manager.BlockAndTime +import exchange.dydx.abacus.utils.Logger import kollections.iListOf import kollections.iMutableListOf import kollections.toIList +import kotlinx.serialization.SerializationException import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonArray @@ -226,7 +228,11 @@ internal fun TradingStateMachine.onChainAccountBalances(payload: String): StateC val account = json.jsonArray.toList() this.wallet = walletProcessor.receivedAccountBalances(wallet, account) return StateChanges(iListOf(Changes.accountBalances), null) - } catch (e: Exception) { + } catch (exception: SerializationException) { + Logger.e { + "Failed to deserialize onChainAccountBalances: $payload \n" + + "Exception: $exception" + } StateChanges(iListOf()) } } 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 c580314b4..3b1f49afb 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine.kt @@ -69,6 +69,7 @@ import kollections.iMutableListOf import kollections.iMutableMapOf import kollections.toIList import kollections.toIMutableMap +import kotlinx.serialization.SerializationException import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonObject import kotlin.math.max @@ -246,7 +247,7 @@ open class TradingStateMachine( val json = try { Json.parseToJsonElement(jsonString).jsonObject.toMap() - } catch (e: Exception) { + } catch (e: SerializationException) { errors.add( ParsingError( ParsingErrorType.ParsingError, diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/utils/Parser.kt b/src/commonMain/kotlin/exchange.dydx.abacus/utils/Parser.kt index 63a8b6d24..ff7b5015b 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/utils/Parser.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/utils/Parser.kt @@ -8,6 +8,7 @@ import kollections.toIList import kollections.toIMap import kollections.toIMutableList import kotlinx.datetime.Instant +import kotlinx.serialization.SerializationException import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.JsonNull @@ -373,9 +374,8 @@ class Parser : ParserProtocol { if (text == null) return null val map = try { Json.parseToJsonElement(text).jsonObject.toMap().toIMap() - } catch (e: Exception) { + } catch (e: SerializationException) { Logger.e { "Unable to decode json object: $text" } - Logger.e { "Exception: $e" } null } return map @@ -385,9 +385,8 @@ class Parser : ParserProtocol { if (text == null) return null val list = try { Json.parseToJsonElement(text).jsonArray.toIList() - } catch (e: Exception) { + } catch (e: SerializationException) { Logger.e { "Unable to decode json object: $text" } - Logger.e { "Exception: $e" } null } return list From f96d95575d44ebee8a9341012f7acdaa3787fb94 Mon Sep 17 00:00:00 2001 From: mobile-build-bot-git Date: Fri, 7 Jun 2024 19:09:18 +0000 Subject: [PATCH 2/2] Bump version --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index f0d6a63d1..bc6ce10b4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -51,7 +51,7 @@ allprojects { } group = "exchange.dydx.abacus" -version = "1.7.60" +version = "1.7.61" repositories { google()