From 603ad00fe7340ba6c8eecf9004998784974a3826 Mon Sep 17 00:00:00 2001 From: aforaleka Date: Tue, 21 May 2024 11:06:20 -0400 Subject: [PATCH] reduce usage of !! --- .../state/app/helper/TriggerOrderToastGenerator.kt | 8 ++++---- .../state/manager/StateManagerAdaptor.kt | 14 ++++++-------- .../state/manager/V4StateManagerAdaptor.kt | 6 ++++-- .../providers/FillsNotificationProvider.kt | 4 ++-- .../state/v2/supervisor/SubaccountSupervisor.kt | 8 +++++--- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/app/helper/TriggerOrderToastGenerator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/app/helper/TriggerOrderToastGenerator.kt index 63772bc52..e2688e725 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/app/helper/TriggerOrderToastGenerator.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/app/helper/TriggerOrderToastGenerator.kt @@ -119,14 +119,14 @@ class TriggerOrderToastGenerator( } val takeProfitOrders = triggerOrders?.filter { order -> selectedSubaccountPosition?.side?.current?.let { currentSide -> - (order.type == OrderType.takeProfitMarket || order.type == OrderType.takeProfitLimit) && - order.side!!.isOppositeOf(currentSide) + (order.type == OrderType.takeProfitMarket || order.type == OrderType.takeProfitLimit) && order.side != null && + order.side.isOppositeOf(currentSide) } ?: false } val stopLossOrders = triggerOrders?.filter { order -> selectedSubaccountPosition?.side?.current?.let { currentSide -> - (order.type == OrderType.stopMarket || order.type == OrderType.stopLimit) && - order.side!!.isOppositeOf(currentSide) + (order.type == OrderType.stopMarket || order.type == OrderType.stopLimit) && order.side != null && + order.side.isOppositeOf(currentSide) } ?: false } diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/StateManagerAdaptor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/StateManagerAdaptor.kt index e42c9b7f3..5fe19126f 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/StateManagerAdaptor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/StateManagerAdaptor.kt @@ -2195,14 +2195,12 @@ open class StateManagerAdaptor( @Throws(Exception::class) fun cancelOrderPayload(orderId: String): HumanReadableCancelOrderPayload { - val subaccount = stateMachine.state?.subaccount(subaccountNumber) - ?: throw Exception("subaccount is null") - val order = subaccount.orders?.firstOrNull { it.id == orderId } - ?: throw Exception("order is null") - val type = order.type?.rawValue ?: throw Exception("order type is null") - val clientId = order.clientId ?: throw Exception("clientId is null") - val orderFlags = order.orderFlags ?: throw Exception("orderFlags is null") - val clobPairId = order.clobPairId ?: throw Exception("clobPairId is null") + val subaccount = requireNotNull(stateMachine.state?.subaccount(subaccountNumber)) {"subaccount is null"} + val order = requireNotNull(subaccount.orders?.firstOrNull { it.id == orderId }) {"order is null"} + val type = requireNotNull(order.type?.rawValue) {"order type is null"} + val clientId = requireNotNull(order.clientId) {"clientId is null"} + val orderFlags = requireNotNull(order.orderFlags) {"orderFlags is null"} + val clobPairId = requireNotNull(order.clobPairId) {"clobPairId is null"} val goodTilBlock = order.goodTilBlock val goodTilBlockTime = order.goodTilBlockTime diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/V4StateManagerAdaptor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/V4StateManagerAdaptor.kt index 48d47f5d6..23203f2ef 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/V4StateManagerAdaptor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/V4StateManagerAdaptor.kt @@ -1216,8 +1216,9 @@ class V4StateManagerAdaptor( override fun cancelOrder(orderId: String, callback: TransactionCallback) { val payload = cancelOrderPayload(orderId) val subaccount = stateMachine.state?.subaccount(subaccountNumber) + // orders should have marketId unless it has already been canceled val existingOrder = - subaccount?.orders?.firstOrNull { it.id == orderId } ?: throw ParsingException( + subaccount?.orders?.firstOrNull { it.id == orderId && it.marketId != null } ?: throw ParsingException( ParsingErrorType.MissingRequiredData, "no existing order to be cancelled for $orderId", ) @@ -1241,7 +1242,8 @@ class V4StateManagerAdaptor( payload.cancelOrderPayloads.forEach { cancelPayload -> val subaccount = stateMachine.state?.subaccount(subaccountNumber) - val existingOrder = subaccount?.orders?.firstOrNull { it.id == cancelPayload.orderId } + // orders should have marketId unless it has already been canceled + val existingOrder = subaccount?.orders?.firstOrNull { it.id == cancelPayload.orderId && it.marketId != null } ?: throw ParsingException( ParsingErrorType.MissingRequiredData, "no existing order to be cancelled for $cancelPayload.orderId", diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/notification/providers/FillsNotificationProvider.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/notification/providers/FillsNotificationProvider.kt index 63b9a1afa..d439e4663 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/notification/providers/FillsNotificationProvider.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/manager/notification/providers/FillsNotificationProvider.kt @@ -111,8 +111,8 @@ class FillsNotificationProvider( val filledAmountText = parser.asString(order.totalFilled) val priceText = priceText(fill.price, tickSize) val averagePriceText = parser.asString(averagePrice(fillsForOrder)) - val orderType = order.type!!.rawValue - val orderTypeText = text(orderType) + val orderType = order.type?.rawValue + val orderTypeText = orderType?.let { text(it) } val params = ( iMapOf( "MARKET" to marketId, diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountSupervisor.kt index c95823665..504dd3fa0 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountSupervisor.kt @@ -854,7 +854,8 @@ internal class SubaccountSupervisor( internal fun cancelOrder(orderId: String, callback: TransactionCallback): HumanReadableCancelOrderPayload { val payload = cancelOrderPayload(orderId) val subaccount = stateMachine.state?.subaccount(subaccountNumber) - val existingOrder = subaccount?.orders?.firstOrNull { it.id == orderId } ?: throw ParsingException( + // orders should have marketId unless it has already been canceled + val existingOrder = subaccount?.orders?.firstOrNull { it.id == orderId && it.marketId != null } ?: throw ParsingException( ParsingErrorType.MissingRequiredData, "no existing order to be cancelled for $orderId", ) @@ -877,7 +878,8 @@ internal class SubaccountSupervisor( payload.cancelOrderPayloads.forEach { cancelPayload -> val subaccount = stateMachine.state?.subaccount(subaccountNumber) - val existingOrder = subaccount?.orders?.firstOrNull { it.id == cancelPayload.orderId } + // orders should have marketId unless it has already been canceled + val existingOrder = subaccount?.orders?.firstOrNull { it.id == cancelPayload.orderId && it.marketId != null } ?: throw ParsingException( ParsingErrorType.MissingRequiredData, "no existing order to be cancelled for $cancelPayload.orderId", @@ -1205,7 +1207,7 @@ internal class SubaccountSupervisor( ?: throw Exception("subaccount is null") val order = subaccount.orders?.firstOrNull { it.id == orderId } ?: throw Exception("order is null") - val type = order.type!!.rawValue + val type = order.type?.rawValue ?: error("order type is null") val clientId = order.clientId ?: error("clientId is null") val orderFlags = order.orderFlags ?: error("orderFlags is null") val clobPairId = order.clobPairId ?: error("clobPairId is null")