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

Mike/add margin type #373

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import exchange.dydx.abacus.utils.NUM_PARENT_SUBACCOUNTS
import exchange.dydx.abacus.utils.ParsingHelper
import exchange.dydx.abacus.utils.mutable
import exchange.dydx.abacus.utils.safeSet
import exchange.dydx.abacus.output.MarginMode

class AccountCalculator(val parser: ParserProtocol, private val useParentSubaccount: Boolean) {
private val subaccountCalculator = SubaccountCalculator(parser)
Expand Down Expand Up @@ -55,6 +56,7 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco
if (subaccountNumbers != null) {
val groupedSubaccounts = mutableMapOf<String, Any>()
for (subaccountNumber in subaccountNumbers) {
val marginMode = if(subaccountNumber >= NUM_PARENT_SUBACCOUNTS) MarginMode.ISOLATED else MarginMode.CROSS
val subaccount =
parser.asNativeMap(parser.value(subaccounts, "$subaccountNumber")) ?: break
if (subaccountNumber < NUM_PARENT_SUBACCOUNTS) {
Expand All @@ -76,6 +78,7 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco
mergeChildOpenPositions(
parentSubaccount,
subaccountNumber,
marginMode,
subaccount,
childOpenPositions,
)
Expand All @@ -85,6 +88,7 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco
mergeChildPendingPositions(
parentSubaccount,
subaccountNumber,
marginMode,
subaccount,
orders,
markets,
Expand All @@ -93,7 +97,7 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco
parentSubaccount
}
}
parentSubaccount = mergeOrders(parentSubaccount, subaccount)
parentSubaccount = mergeOrders(parentSubaccount, subaccount, marginMode)
parentSubaccount = sumEquity(parentSubaccount, subaccount)
groupedSubaccounts["$parentSubaccountNumber"] = parentSubaccount
}
Expand All @@ -108,6 +112,7 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco
private fun mergeChildOpenPositions(
parentSubaccount: Map<String, Any>,
childSubaccountNumber: Int,
marginMode: MarginMode,
childSubaccount: Map<String, Any>,
childOpenPositions: Map<String, Any>,
): Map<String, Any> {
Expand All @@ -121,6 +126,10 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco
"childSubaccountNumber",
childSubaccountNumber,
)
modifiedChildOpenPosition?.safeSet(
"marginMode",
marginMode,
)
modifiedChildOpenPosition?.safeSet(
"quoteBalance",
childSubaccount["quoteBalance"],
Expand All @@ -145,6 +154,7 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco
private fun mergeChildPendingPositions(
parentSubaccount: Map<String, Any>,
childSubaccountNumber: Int,
marginMode: MarginMode,
childSubaccount: Map<String, Any>,
childOrders: Map<String, Any>,
markets: Map<String, Any>?,
Expand Down Expand Up @@ -189,6 +199,14 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco
"orderCount",
parser.value(pending, "orderCount"),
)
modifiedPendingPosition?.safeSet(
"childSubaccountNumber",
childSubaccountNumber,
)
modifiedPendingPosition?.safeSet(
"marginMode",
marginMode,
)
modifiedPendingPosition.safeSet(
"quoteBalance",
childSubaccount["quoteBalance"],
Expand Down Expand Up @@ -219,12 +237,13 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco
private fun mergeOrders(
parentSubaccount: Map<String, Any>,
childSubaccount: Map<String, Any>,
marginMode: MarginMode,
): Map<String, Any> {
// Each empty subaccount should have order for one market only
// Just in case it has more than one market, we will create
// two separate pending positions.

val mergedOrders = ParsingHelper.merge(
var mergedOrders = ParsingHelper.merge(
parser.asNativeMap(
parser.value(parentSubaccount, "orders"),
),
Expand All @@ -233,6 +252,15 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco
),
)

mergedOrders = mergedOrders?.mapValues { (_, value) ->
var modifiedOrder = parser.asMap(value)?.toMutableMap() ?: mutableMapOf()
modifiedOrder?.safeSet(
"marginMode",
marginMode,
)
modifiedOrder
}

val modifiedParentSubaccount = parentSubaccount.toMutableMap()
modifiedParentSubaccount.safeSet(
"orders",
Expand Down
53 changes: 40 additions & 13 deletions src/commonMain/kotlin/exchange.dydx.abacus/output/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ enum class PositionSide(val rawValue: String) {
}
}

@JsExport
@Serializable
enum class MarginMode(val rawValue: String) {
ISOLATED("ISOLATED"),
CROSS("CROSS");

companion object {
operator fun invoke(rawValue: String) =
MarginMode.values().firstOrNull { it.rawValue == rawValue }
}
}

@JsExport
@Serializable
data class TradeStatesWithPositionSides(
Expand Down Expand Up @@ -249,10 +261,14 @@ data class SubaccountPosition(
val liquidationPrice: TradeStatesWithDoubleValues,
val resources: SubaccountPositionResources,
val childSubaccountNumber: Int?,
val marginMode: MarginMode?,
val freeCollateral: TradeStatesWithDoubleValues,
val marginUsage: TradeStatesWithDoubleValues,
val quoteBalance: TradeStatesWithDoubleValues, // available for isolated market position
val equity: TradeStatesWithDoubleValues, // available for isolated market position
/** available for isolated market position */
val quoteBalance: TradeStatesWithDoubleValues,
/** available for isolated market position */
val equity: TradeStatesWithDoubleValues,
/** Margin type of the position */
) {
companion object {
internal fun create(
Expand Down Expand Up @@ -361,6 +377,7 @@ data class SubaccountPosition(
parser.asMap(data["liquidationPrice"]),
)
val childSubaccountNumber = parser.asInt(data["childSubaccountNumber"])
val marginMode = parser.asString("marginType")?.let{ MarginMode.invoke(it) }
val freeCollateral = TradeStatesWithDoubleValues.create(
null,
parser,
Expand Down Expand Up @@ -408,7 +425,8 @@ data class SubaccountPosition(
existing.freeCollateral !== freeCollateral ||
existing.marginUsage !== marginUsage ||
existing.quoteBalance !== quoteBalance ||
existing.equity !== equity
existing.equity !== equity ||
existing.marginMode !== marginMode
) {
val side = positionSide(size)
SubaccountPosition(
Expand Down Expand Up @@ -436,6 +454,7 @@ data class SubaccountPosition(
liquidationPrice,
resources,
childSubaccountNumber,
marginMode,
freeCollateral,
marginUsage,
quoteBalance,
Expand Down Expand Up @@ -633,7 +652,6 @@ data class SubaccountOrderResources(
@JsExport
@Serializable
data class SubaccountOrder(
val subaccountNumber: Int?,
val id: String,
val clientId: Int?,
val type: OrderType,
Expand All @@ -660,7 +678,9 @@ data class SubaccountOrder(
val reduceOnly: Boolean,
val cancelReason: String?,
val resources: SubaccountOrderResources,
) {
val subaccountNumber: Int?,
val marginMode: MarginMode?,
) {
companion object {
internal fun create(
existing: SubaccountOrder?,
Expand All @@ -670,8 +690,6 @@ data class SubaccountOrder(
): SubaccountOrder? {
Logger.d { "creating Account Order\n" }
data?.let {
// TODO: Remove default to 0 for subaccountNumber once new indexer response is consumed. Prevents breaking change
val subaccountNumber = parser.asInt(data["subaccountNumber"]) ?: 0
val id = parser.asString(data["id"])
val clientId = parser.asInt(data["clientId"])
val marketId = parser.asString(data["marketId"])
Expand All @@ -693,6 +711,9 @@ data class SubaccountOrder(
val resources = parser.asMap(data["resources"])?.let {
SubaccountOrderResources.create(existing?.resources, parser, it, localizer)
}
// TODO: Remove default to 0 for subaccountNumber once new indexer response is consumed. Prevents breaking change
val subaccountNumber = parser.asInt(data["subaccountNumber"]) ?: 0
val marginMode = parser.asString(data["marginMode"])?.let { MarginMode.invoke(it) }
if (id != null && marketId != null && type != null && side != null && status != null && price != null && size != null &&
resources != null
) {
Expand All @@ -717,8 +738,7 @@ data class SubaccountOrder(
val cancelReason = parser.asString(data["cancelReason"])

return if (
existing?.subaccountNumber != subaccountNumber ||
existing.id != id ||
existing?.id != id ||
existing.clientId != clientId ||
existing.type !== type ||
existing.side !== side ||
Expand All @@ -742,10 +762,11 @@ data class SubaccountOrder(
existing.postOnly != postOnly ||
existing.reduceOnly != reduceOnly ||
existing.cancelReason != cancelReason ||
existing.resources !== resources
existing.resources !== resources ||
existing.subaccountNumber != subaccountNumber ||
existing.marginMode !== marginMode
) {
SubaccountOrder(
subaccountNumber,
id,
clientId,
type,
Expand All @@ -772,7 +793,9 @@ data class SubaccountOrder(
reduceOnly,
cancelReason,
resources,
)
subaccountNumber,
marginMode,
)
} else {
existing
}
Expand Down Expand Up @@ -1338,7 +1361,11 @@ data class Subaccount(
parser.asList(data["pendingPositions"]),
)
val orders =
orders(parser, existing?.orders, parser.asMap(data["orders"]), localizer)
orders(parser,
existing?.orders,
parser.asMap(data["orders"]),
localizer
)

/*
val transfers = AccountTransfers.fromArray(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ class V4ParentSubaccountTests : V4BaseTests(true) {
"current": 7962.44
},
"childSubaccountNumber": 128,
"marginMode": "ISOLATED",
"equity": {
"current": 829.16
},
Expand Down Expand Up @@ -533,6 +534,7 @@ class V4ParentSubaccountTests : V4BaseTests(true) {
"current": 1410.69
},
"childSubaccountNumber": 128,
"marginMode": "ISOLATED",
"quoteBalance": {
"current": 267.89
},
Expand Down Expand Up @@ -603,6 +605,7 @@ class V4ParentSubaccountTests : V4BaseTests(true) {
"current": 1949.55
},
"childSubaccountNumber": 128,
"marginMode": "ISOLATED",
"quoteBalance": {
"current": 367.89
},
Expand Down
Loading