Skip to content

Commit

Permalink
reduce usage of !!
Browse files Browse the repository at this point in the history
  • Loading branch information
aforaleka committed May 21, 2024
1 parent 7d600d0 commit 603ad00
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand All @@ -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",
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 603ad00

Please sign in to comment.