Skip to content

Commit

Permalink
wip clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
moo-onthelawn committed Apr 30, 2024
1 parent ca98ba4 commit 4fa7d8e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ class V4StateManagerAdaptor(
callback,
if (isTriggerOrder) {
HumanReadableTriggerOrdersPayload(
payload.marketId,
listOf(payload),
emptyList(),
)
Expand All @@ -1056,6 +1057,7 @@ class V4StateManagerAdaptor(

private fun submitCancelOrder(
orderId: String,
marketId: String?,
callback: TransactionCallback,
payload: HumanReadableCancelOrderPayload,
analyticsPayload: IMap<String, Any>?,
Expand Down Expand Up @@ -1101,6 +1103,7 @@ class V4StateManagerAdaptor(
callback,
if (isTriggerOrder) {
HumanReadableTriggerOrdersPayload(
marketId,
emptyList(),
listOf(payload),
)
Expand Down Expand Up @@ -1191,13 +1194,14 @@ class V4StateManagerAdaptor(
val payload = cancelOrderPayload(orderId)
val subaccount = stateMachine.state?.subaccount(subaccountNumber)
val existingOrder = subaccount?.orders?.firstOrNull { it.id == orderId }
val marketId = existingOrder?.marketId
val analyticsPayload = analyticsUtils.cancelOrderAnalyticsPayload(
payload,
existingOrder,
)
val uiClickTimeMs = trackOrderClick(analyticsPayload, AnalyticsEvent.TradeCancelOrderClick)

submitCancelOrder(orderId, callback, payload, analyticsPayload, uiClickTimeMs)
submitCancelOrder(orderId, marketId, callback, payload, analyticsPayload, uiClickTimeMs)
}

override fun commitTriggerOrders(callback: TransactionCallback): HumanReadableTriggerOrdersPayload {
Expand All @@ -1210,11 +1214,12 @@ class V4StateManagerAdaptor(
payload.cancelOrderPayloads.forEach { cancelPayload ->
val subaccount = stateMachine.state?.subaccount(subaccountNumber)
val existingOrder = subaccount?.orders?.firstOrNull { it.id == cancelPayload.orderId }
val marketId = existingOrder?.marketId
val cancelOrderAnalyticsPayload = analyticsUtils.cancelTriggerOrderAnalyticsPayload(
cancelPayload,
existingOrder,
)
submitCancelOrder(cancelPayload.orderId, callback, cancelPayload, cancelOrderAnalyticsPayload, uiClickTimeMs, true)
submitCancelOrder(cancelPayload.orderId, marketId, callback, cancelPayload, cancelOrderAnalyticsPayload, uiClickTimeMs, true)
}

payload.placeOrderPayloads.forEach { placePayload ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ data class HumanReadableCancelOrderPayload(
@JsExport
@Serializable
data class HumanReadableTriggerOrdersPayload(
val marketId: String,
val marketId: String?,
val placeOrderPayloads: List<HumanReadablePlaceOrderPayload>,
val cancelOrderPayloads: List<HumanReadableCancelOrderPayload>,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ internal class SubaccountSupervisor(
callback,
if (isTriggerOrder) {
HumanReadableTriggerOrdersPayload(
payload.marketId,
listOf(payload),
emptyList(),
)
Expand Down Expand Up @@ -685,17 +686,16 @@ internal class SubaccountSupervisor(

private fun submitCancelOrder(
orderId: String,
marketId: String?,
callback: TransactionCallback,
payload: HumanReadableCancelOrderPayload,
analyticsPayload: IMap<String, Any>?,
uiClickTimeMs: Double,
isTriggerOrder: Boolean = false,
): HumanReadableCancelOrderPayload {
val clientId = payload.clientId
val string = Json.encodeToString(payload)

val uiClickTimeMs = Clock.System.now().toEpochMilliseconds().toDouble()
tracking(AnalyticsEvent.TradeCancelOrderClick.rawValue, analyticsPayload)

stopWatchingLastOrder()

val isShortTermOrder = payload.orderFlags == 0
Expand Down Expand Up @@ -731,6 +731,7 @@ internal class SubaccountSupervisor(
callback,
if (isTriggerOrder) {
HumanReadableTriggerOrdersPayload(
marketId,
emptyList(),
listOf(payload),
)
Expand Down Expand Up @@ -777,9 +778,11 @@ internal class SubaccountSupervisor(
val payload = cancelOrderPayload(orderId)
val subaccount = stateMachine.state?.subaccount(subaccountNumber)
val existingOrder = subaccount?.orders?.firstOrNull { it.id == orderId }
val marketId = existingOrder?.marketId
val analyticsPayload = analyticsUtils.cancelOrderAnalyticsPayload(payload, existingOrder)
val uiClickTimeMs = trackOrderClick(analyticsPayload, AnalyticsEvent.TradeCancelOrderClick)

return submitCancelOrder(orderId, callback, payload, analyticsPayload)
return submitCancelOrder(orderId, marketId, callback, payload, analyticsPayload, uiClickTimeMs)
}

internal fun commitTriggerOrders(
Expand All @@ -795,11 +798,12 @@ internal class SubaccountSupervisor(
payload.cancelOrderPayloads.forEach { cancelPayload ->
val subaccount = stateMachine.state?.subaccount(subaccountNumber)
val existingOrder = subaccount?.orders?.firstOrNull { it.id == cancelPayload.orderId }
val marketId = existingOrder?.marketId
val cancelOrderAnalyticsPayload = analyticsUtils.cancelTriggerOrderAnalyticsPayload(
cancelPayload,
existingOrder,
)
submitCancelOrder(cancelPayload.orderId, callback, cancelPayload, cancelOrderAnalyticsPayload, true)
submitCancelOrder(cancelPayload.orderId, marketId, callback, cancelPayload, cancelOrderAnalyticsPayload, uiClickTimeMs, true)
}

payload.placeOrderPayloads.forEach { placePayload ->
Expand Down
16 changes: 4 additions & 12 deletions src/commonMain/kotlin/exchange.dydx.abacus/utils/AnalyticsUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class AnalyticsUtils {
payload: HumanReadableCancelOrderPayload,
existingOrder: SubaccountOrder?,
): IMap<String, Any>? {
return cancelOrderAnalyticsPayload(payload, existingOrder, true)
return cancelOrderAnalyticsPayload(payload, existingOrder)
}

/**
Expand All @@ -79,24 +79,22 @@ class AnalyticsUtils {
payload: HumanReadablePlaceOrderPayload,
midMarketPrice: Double?,
): IMap<String, Any>? {
return placeOrderAnalyticsPayload(payload, midMarketPrice, false, true)
return placeOrderAnalyticsPayload(payload, midMarketPrice, false)
}

/**
* Format Place Order Payload and add additional details for `TradePlaceOrder` Analytic Events
* @param payload HumanReadablePlaceOrderPayload
* @param midMarketPrice Double?
* @param isClosePosition Boolean?
* @param fromSlTpDialog Boolean?
*/
fun placeOrderAnalyticsPayload(
payload: HumanReadablePlaceOrderPayload,
midMarketPrice: Double?,
isClosePosition: Boolean? = false,
fromSlTpDialog: Boolean? = false, // xcxc remove
): IMap<String, Any>? {
return ParsingHelper.merge(
formatPlaceOrderPayload(payload, isClosePosition, fromSlTpDialog),
formatPlaceOrderPayload(payload, isClosePosition),
iMapOf(
"inferredTimeInForce" to calculateOrderTimeInForce(payload),
"midMarketPrice" to midMarketPrice,
Expand All @@ -112,7 +110,6 @@ class AnalyticsUtils {
private fun formatPlaceOrderPayload(
payload: HumanReadablePlaceOrderPayload,
isClosePosition: Boolean? = false,
fromSlTpDialog: Boolean? = false,
): IMap<String, Any>? {
return iMapOf(
"clientId" to payload.clientId,
Expand All @@ -121,7 +118,6 @@ class AnalyticsUtils {
"goodTilTimeInSeconds" to payload.goodTilTimeInSeconds,
"goodTilBlock" to payload.goodTilBlock,
"isClosePosition" to isClosePosition,
"fromSlTpDialog" to fromSlTpDialog,
"marketId" to payload.marketId,
"postOnly" to payload.postOnly,
"price" to payload.price,
Expand Down Expand Up @@ -167,25 +163,21 @@ class AnalyticsUtils {
* Format Cancel Order Payload and add order details for `TradeCancelOrder` Analytic Events
* @param payload HumanReadableCancelOrderPayload
* @param existingOrder SubaccountOrder?
* @param fromSlTpDialog Boolean
*/
fun cancelOrderAnalyticsPayload(
payload: HumanReadableCancelOrderPayload,
existingOrder: SubaccountOrder?,
fromSlTpDialog: Boolean? = false,
): IMap<String, Any>? {
return ParsingHelper.merge(
formatCancelOrderPayload(payload, fromSlTpDialog),
formatCancelOrderPayload(payload),
if (existingOrder != null) formatOrder(existingOrder) else mapOf(),
)?.toIMap()
}

private fun formatCancelOrderPayload(
payload: HumanReadableCancelOrderPayload,
fromSlTpDialog: Boolean? = false,
): IMap<String, Any>? {
return iMapOf(
"fromSlTpDialog" to fromSlTpDialog, // xcxc remove
"subaccountNumber" to payload.subaccountNumber,
"clientId" to payload.clientId,
"orderId" to payload.orderId,
Expand Down

0 comments on commit 4fa7d8e

Please sign in to comment.