Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.6.53: add analytics for SL/TP #325

Merged
merged 14 commits into from
May 1, 2024
Merged
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.6.52"
version = "1.6.53"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import exchange.dydx.abacus.utils.mutable
import exchange.dydx.abacus.utils.safeSet
import kotlin.math.abs

internal object TriggerOrdersConstants {
const val TRIGGER_ORDER_DEFAULT_DURATION_DAYS = 28.0
}

@Suppress("UNCHECKED_CAST")
internal class TriggerOrdersInputCalculator(val parser: ParserProtocol) {
internal fun calculate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ enum class AnalyticsEvent(val rawValue: String) {
// App
NetworkStatus("NetworkStatus"),

// Trade Events
// Trade
TradePlaceOrderClick("TradePlaceOrderClick"),
TradeCancelOrderClick("TradeCancelOrderClick"),
TradePlaceOrder("TradePlaceOrder"),
Expand All @@ -221,6 +221,17 @@ enum class AnalyticsEvent(val rawValue: String) {
TradeCancelOrderConfirmed("TradeCancelOrderConfirmed"),
TradePlaceOrderConfirmed("TradePlaceOrderConfirmed"),

// Trigger Order
TriggerOrderClick("TriggerOrderClick"),
TriggerPlaceOrder("TriggerPlaceOrder"),
TriggerCancelOrder("TriggerCancelOrder"),
TriggerPlaceOrderSubmissionConfirmed("TriggerPlaceOrderSubmissionConfirmed"),
TriggerCancelOrderSubmissionConfirmed("TriggerCancelOrderSubmissionConfirmed"),
TriggerPlaceOrderSubmissionFailed("TriggerPlaceOrderSubmissionFailed"),
TriggerCancelOrderSubmissionFailed("TriggerCancelOrderSubmissionFailed"),
TriggerCancelOrderConfirmed("TriggerCancelOrderConfirmed"),
TriggerPlaceOrderConfirmed("TriggerPlaceOrderConfirmed"),

// Transfers
TransferFaucetConfirmed("TransferFaucetConfirmed");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package exchange.dydx.abacus.state.manager

import exchange.dydx.abacus.calculator.TriggerOrdersConstants.TRIGGER_ORDER_DEFAULT_DURATION_DAYS
import exchange.dydx.abacus.output.Compliance
import exchange.dydx.abacus.output.ComplianceAction
import exchange.dydx.abacus.output.ComplianceStatus
Expand Down Expand Up @@ -120,9 +121,6 @@ open class StateManagerAdaptor(
var stateNotification: StateNotificationProtocol?,
var dataNotification: DataNotificationProtocol?,
) {
@Suppress("LocalVariableName", "PropertyName")
private val TRIGGER_ORDER_DEFAULT_DURATION_DAYS = 28.0

var stateMachine: TradingStateMachine = PerpTradingStateMachine(
environment,
uiImplementations.localizer,
Expand Down Expand Up @@ -1880,10 +1878,15 @@ open class StateManagerAdaptor(
val cancelOrderPayloads = mutableListOf<HumanReadableCancelOrderPayload>()
val triggerOrders = stateMachine.state?.input?.triggerOrders

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kotlin tip: we can instead enforce triggerOrders is non-null by doing:
val triggerOrders = requireNotNull(stateMachine.state?.input?.triggerOrders) { "triggerOrders input was null" }

then we don't need to do null checks on it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

part of me wants to write a codemod to go through the whole repo to replace all of the throwExceptions


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 = subaccount.openPositions?.find { it.id == marketId } ?: throw ParsingException(
ParsingErrorType.MissingRequiredData,
"no open position for $marketId",
)
val positionSize = position.size?.current

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

return HumanReadableTriggerOrdersPayload(
marketId,
positionSize,
placeOrderPayloads,
cancelOrderPayloads,
)
Expand Down Expand Up @@ -2152,6 +2157,7 @@ open class StateManagerAdaptor(
?: throw Exception("subaccount is null")
val order = subaccount.orders?.firstOrNull { it.id == orderId }
?: throw Exception("order is null")
val type = order.type.rawValue
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")
Expand All @@ -2160,6 +2166,7 @@ open class StateManagerAdaptor(

return HumanReadableCancelOrderPayload(
subaccountNumber,
type,
orderId,
clientId,
orderFlags,
Expand Down Expand Up @@ -2327,7 +2334,11 @@ open class StateManagerAdaptor(
val interval = Clock.System.now().toEpochMilliseconds()
.toDouble() - placeOrderRecord.timestampInMilliseconds
tracking(
AnalyticsEvent.TradePlaceOrderConfirmed.rawValue,
if (placeOrderRecord.isTriggerOrder) {
AnalyticsEvent.TriggerPlaceOrderConfirmed.rawValue
} else {
AnalyticsEvent.TradePlaceOrderConfirmed.rawValue
},
ParsingHelper.merge(
trackingParams(interval),
orderAnalyticsPayload,
Expand All @@ -2343,7 +2354,11 @@ open class StateManagerAdaptor(
val interval = Clock.System.now().toEpochMilliseconds()
.toDouble() - cancelOrderRecord.timestampInMilliseconds
tracking(
AnalyticsEvent.TradeCancelOrderConfirmed.rawValue,
if (cancelOrderRecord.isTriggerOrder) {
AnalyticsEvent.TriggerCancelOrderConfirmed.rawValue
} else {
AnalyticsEvent.TradeCancelOrderConfirmed.rawValue
},
ParsingHelper.merge(
trackingParams(interval),
orderAnalyticsPayload,
Expand Down
Loading
Loading