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 72873a3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ enum class AnalyticsEvent(val rawValue: String) {
TradePlaceOrderClick("TradePlaceOrderClick"),
TradeCancelOrderClick("TradeCancelOrderClick"),
TradeTriggerOrderClick("TradeTriggerOrderClick"),
TradePlaceOrder("TradePlaceOrder"), // analytics here
TradeCancelOrder("TradeCancelOrder"), // analytics here
TradePlaceOrder("TradePlaceOrder"),
TradeCancelOrder("TradeCancelOrder"),
TradeTriggerPlaceOrder("TradeTriggerPlaceOrder"),
TradeTriggerCancelOrder("TradeTriggerCancelOrder"),
TradePlaceOrderSubmissionConfirmed("TradePlaceOrderSubmissionConfirmed"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1880,10 +1880,12 @@ open class StateManagerAdaptor(
val cancelOrderPayloads = mutableListOf<HumanReadableCancelOrderPayload>()
val triggerOrders = stateMachine.state?.input?.triggerOrders

val marketId = triggerOrders?.marketId ?: throw Exception("marketId is null")

val subaccountNumber = connectedSubaccountNumber ?: throw Exception("subaccountNumber is null")
val subaccount = stateMachine.state?.subaccount(subaccountNumber) ?: throw Exception("subaccount is null")

val marketId = triggerOrders?.marketId ?: throw Exception("marketId is null")
val position = parser.asNativeMap(parser.value(subaccount, "openPositions.$marketId"))
val positionSize = parser.asDouble(parser.value(position, "size.current"))

fun updateTriggerOrder(triggerOrder: TriggerOrder) {
// Cases
Expand Down Expand Up @@ -1924,6 +1926,7 @@ open class StateManagerAdaptor(

return HumanReadableTriggerOrdersPayload(
marketId,
positionSize,
placeOrderPayloads,
cancelOrderPayloads,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1005,14 +1005,17 @@ class V4StateManagerAdaptor(
): HumanReadablePlaceOrderPayload {
val clientId = payload.clientId
val string = Json.encodeToString(payload)
val marketId = payload.marketId
val position = stateMachine.state?.subaccount(subaccountNumber)?.openPositions?.find { it.id == marketId }
val positionSize = position?.size?.current

stopWatchingLastOrder()

submitTransaction(
TransactionType.PlaceOrder,
string,
onSubmitTransaction = {
val submitTimeMs = trackOrderSubmit(uiClickTimeMs, analyticsPayload)
val submitTimeMs = trackOrderSubmit(uiClickTimeMs, analyticsPayload, isTriggerOrder)
ioImplementations.threading?.async(ThreadingType.abacus) {
this.placeOrderRecords.add(
PlaceOrderRecord(
Expand Down Expand Up @@ -1040,6 +1043,8 @@ class V4StateManagerAdaptor(
callback,
if (isTriggerOrder) {
HumanReadableTriggerOrdersPayload(
marketId,
positionSize,
listOf(payload),
emptyList(),
)
Expand All @@ -1056,6 +1061,7 @@ class V4StateManagerAdaptor(

private fun submitCancelOrder(
orderId: String,
marketId: String?,
callback: TransactionCallback,
payload: HumanReadableCancelOrderPayload,
analyticsPayload: IMap<String, Any>?,
Expand All @@ -1064,6 +1070,8 @@ class V4StateManagerAdaptor(
) {
val clientId = payload.clientId
val string = Json.encodeToString(payload)
val position = stateMachine.state?.subaccount(subaccountNumber)?.openPositions?.find { it.id == marketId }
val positionSize = position?.size?.current

val isShortTermOrder = payload.orderFlags == 0

Expand All @@ -1073,7 +1081,7 @@ class V4StateManagerAdaptor(
TransactionType.CancelOrder,
string,
onSubmitTransaction = {
val submitTimeMs = trackOrderSubmit(uiClickTimeMs, analyticsPayload, true)
val submitTimeMs = trackOrderSubmit(uiClickTimeMs, analyticsPayload, isTriggerOrder, true)
ioImplementations.threading?.async(ThreadingType.abacus) {
this.cancelOrderRecords.add(
CancelOrderRecord(
Expand Down Expand Up @@ -1101,6 +1109,8 @@ class V4StateManagerAdaptor(
callback,
if (isTriggerOrder) {
HumanReadableTriggerOrdersPayload(
marketId,
positionSize,
emptyList(),
listOf(payload),
)
Expand Down Expand Up @@ -1128,13 +1138,18 @@ class V4StateManagerAdaptor(
private fun trackOrderSubmit(
uiClickTimeMs: Double,
analyticsPayload: IMap<String, Any>?,
isTriggerOrder: Boolean,
isCancel: Boolean = false
): Double {
val submitTimeMs = Clock.System.now().toEpochMilliseconds().toDouble()
val uiDelayTimeMs = submitTimeMs - uiClickTimeMs

tracking(
if (isCancel) AnalyticsEvent.TradeCancelOrder.rawValue else AnalyticsEvent.TradePlaceOrder.rawValue,
if (isCancel) {
if (isTriggerOrder) AnalyticsEvent.TradeTriggerCancelOrder.rawValue else AnalyticsEvent.TradeCancelOrder.rawValue
} else {
if (isTriggerOrder) AnalyticsEvent.TradeTriggerPlaceOrder.rawValue else AnalyticsEvent.TradePlaceOrder.rawValue
},
ParsingHelper.merge(uiTrackingParams(uiDelayTimeMs), analyticsPayload)
?.toIMap(),
)
Expand Down Expand Up @@ -1191,13 +1206,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 +1226,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,8 @@ data class HumanReadableCancelOrderPayload(
@JsExport
@Serializable
data class HumanReadableTriggerOrdersPayload(
val marketId: String,
val marketId: String?,
val positionSize: Double?,
val placeOrderPayloads: List<HumanReadablePlaceOrderPayload>,
val cancelOrderPayloads: List<HumanReadableCancelOrderPayload>,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ internal class SubaccountSupervisor(
val transferPayloadString =
if (transferPayload != null) Json.encodeToString(transferPayload) else null

val marketId = payload.marketId
val position = stateMachine.state?.subaccount(subaccountNumber)?.openPositions?.find { it.id == marketId }
val positionSize = position?.size?.current

val isShortTermOrder = when (payload.type) {
"MARKET" -> true
"LIMIT" -> {
Expand Down Expand Up @@ -568,7 +572,7 @@ internal class SubaccountSupervisor(

val orderTransactionCallback = { response: String? ->
val error = parseTransactionResponse(response)
trackOrderSubmitted(error, analyticsPayload)
trackOrderSubmitted(error, analyticsPayload, isTriggerOrder)
if (error == null) {
lastOrderClientId = clientId
} else {
Expand All @@ -582,6 +586,8 @@ internal class SubaccountSupervisor(
callback,
if (isTriggerOrder) {
HumanReadableTriggerOrdersPayload(
marketId,
positionSize,
listOf(payload),
emptyList(),
)
Expand Down Expand Up @@ -668,33 +674,44 @@ internal class SubaccountSupervisor(
private fun trackOrderSubmitted(
error: ParsingError?,
analyticsPayload: IMap<String, Any>?,
isTriggerOrder: Boolean,
isCancel: Boolean = false,
) {
if (error != null) {
tracking(
if (isCancel) AnalyticsEvent.TradeCancelOrderSubmissionFailed.rawValue else AnalyticsEvent.TradePlaceOrderSubmissionFailed.rawValue,
if (isCancel) {
if (isTriggerOrder) AnalyticsEvent.TradeTriggerCancelOrderSubmissionFailed.rawValue else AnalyticsEvent.TradeCancelOrderSubmissionFailed.rawValue
} else {
if (isTriggerOrder) AnalyticsEvent.TradeTriggerPlaceOrderSubmissionFailed.rawValue else AnalyticsEvent.TradePlaceOrderSubmissionFailed.rawValue
},
ParsingHelper.merge(errorTrackingParams(error), analyticsPayload)?.toIMap(),
)
} else {
tracking(
if (isCancel) AnalyticsEvent.TradeCancelOrderSubmissionConfirmed.rawValue else AnalyticsEvent.TradePlaceOrderSubmissionConfirmed.rawValue,
if (isCancel) {
if (isTriggerOrder) AnalyticsEvent.TradeTriggerCancelOrderSubmissionConfirmed.rawValue else AnalyticsEvent.TradeCancelOrderSubmissionConfirmed.rawValue
} else {
if (isTriggerOrder) AnalyticsEvent.TradeTriggerPlaceOrderSubmissionConfirmed.rawValue else AnalyticsEvent.TradePlaceOrderSubmissionConfirmed.rawValue
},
analyticsPayload,
)
}
}

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)
val position = stateMachine.state?.subaccount(subaccountNumber)?.openPositions?.find { it.id == marketId }
val positionSize = position?.size?.current

stopWatchingLastOrder()

Expand All @@ -717,7 +734,7 @@ internal class SubaccountSupervisor(
},
transactionCallback = { response: String? ->
val error = parseTransactionResponse(response)
trackOrderSubmitted(error, analyticsPayload, true)
trackOrderSubmitted(error, analyticsPayload, isTriggerOrder, true)
if (error == null) {
this.orderCanceled(orderId)
} else {
Expand All @@ -731,6 +748,8 @@ internal class SubaccountSupervisor(
callback,
if (isTriggerOrder) {
HumanReadableTriggerOrdersPayload(
marketId,
positionSize,
emptyList(),
listOf(payload),
)
Expand Down Expand Up @@ -777,9 +796,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 +816,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 Expand Up @@ -982,10 +1004,12 @@ internal class SubaccountSupervisor(
val cancelOrderPayloads = mutableListOf<HumanReadableCancelOrderPayload>()
val triggerOrders = stateMachine.state?.input?.triggerOrders

val subaccount = stateMachine.state?.subaccount(subaccountNumber) ?: throw Exception("subaccount is null")

val marketId = triggerOrders?.marketId ?: throw Exception("marketId is null")

val subaccount = stateMachine.state?.subaccount(subaccountNumber) ?: throw Exception("subaccount is null")
val position = helper.parser.asNativeMap(helper.parser.value(subaccount, "openPositions.$marketId"))
val positionSize = helper.parser.asDouble(helper.parser.value(position, "size.current"))

fun updateTriggerOrder(triggerOrder: TriggerOrder) {
// Cases
// 1. Existing order -> update
Expand Down Expand Up @@ -1023,7 +1047,7 @@ internal class SubaccountSupervisor(
updateTriggerOrder(triggerOrders.takeProfitOrder)
}

return HumanReadableTriggerOrdersPayload(marketId, placeOrderPayloads, cancelOrderPayloads)
return HumanReadableTriggerOrdersPayload(marketId, positionSize, placeOrderPayloads, cancelOrderPayloads)
}

@Throws(Exception::class)
Expand Down
21 changes: 7 additions & 14 deletions src/commonMain/kotlin/exchange.dydx.abacus/utils/AnalyticsUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class AnalyticsUtils {

return iMapOf(
"marketId" to payload.marketId,
"stopLossOrderAction" to stopLossOrderAction,
"takeProfitOrderAction" to takeProfitOrderAction,
"positionSize" to payload.positionSize,
"stopLossOrderAction" to stopLossOrderAction?.rawValue,
"takeProfitOrderAction" to takeProfitOrderAction?.rawValue,
) as IMap<String, Any>?
}

Expand All @@ -68,7 +69,7 @@ class AnalyticsUtils {
payload: HumanReadableCancelOrderPayload,
existingOrder: SubaccountOrder?,
): IMap<String, Any>? {
return cancelOrderAnalyticsPayload(payload, existingOrder, true)
return cancelOrderAnalyticsPayload(payload, existingOrder)
}

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

/**
* 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 +111,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 +119,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 +164,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 72873a3

Please sign in to comment.