Skip to content

Commit

Permalink
Merge branch 'main' into jared/tra-238-add-function-to-reclaim-unutil…
Browse files Browse the repository at this point in the history
…ized-funds-from
  • Loading branch information
jaredvu committed May 17, 2024
2 parents 9b1ffec + c533592 commit 45911ef
Show file tree
Hide file tree
Showing 15 changed files with 425 additions and 79 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.7.19"
version = "1.7.23"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class AccountCalculator(val parser: ParserProtocol, private val useParentSubacco

val modifiedPendingPosition = mutableMapOf<String, Any>()
modifiedPendingPosition.safeSet("assetId", assetId)
modifiedPendingPosition.safeSet("marketId", marketId)
modifiedPendingPosition.safeSet(
"firstOrderId",
parser.value(pending, "firstOrderId"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class AdjustIsolatedMarginInputCalculator(val parser: ParserProtocol) {
IsolatedMarginAdjustmentType.valueOf(it)
} ?: IsolatedMarginAdjustmentType.Add

return if (wallet != null && isolatedMarginAdjustment != null && type != null) {
return if (wallet != null && isolatedMarginAdjustment != null) {
val modified = state.mutable()
val parentTransferDelta = getModifiedTransferDelta(isolatedMarginAdjustment, true)
val childTransferDelta = getModifiedTransferDelta(isolatedMarginAdjustment, false)
Expand All @@ -44,8 +44,8 @@ internal class AdjustIsolatedMarginInputCalculator(val parser: ParserProtocol) {
"postOrder",
)

val modifiedParentSubaccount = parser.asNativeMap(parser.value(walletPostChildSubaccountTransfer, "accounts.subaccounts.$parentSubaccountNumber"))
val modifiedChildSubaccount = parser.asNativeMap(parser.value(walletPostChildSubaccountTransfer, "accounts.subaccounts.$childSubaccountNumber"))
val modifiedParentSubaccount = parser.asNativeMap(parser.value(walletPostChildSubaccountTransfer, "account.subaccounts.$parentSubaccountNumber"))
val modifiedChildSubaccount = parser.asNativeMap(parser.value(walletPostChildSubaccountTransfer, "account.subaccounts.$childSubaccountNumber"))
val modifiedIsolatedMarginAdjustment = finalize(isolatedMarginAdjustment, modifiedParentSubaccount, modifiedChildSubaccount, type)

modified["adjustIsolatedMargin"] = modifiedIsolatedMarginAdjustment
Expand Down Expand Up @@ -94,13 +94,13 @@ internal class AdjustIsolatedMarginInputCalculator(val parser: ParserProtocol) {
type: IsolatedMarginAdjustmentType,
): Map<String, Any> {
val summary = mutableMapOf<String, Any>()
val crossCollateral = parser.asDouble(parser.value(parentSubaccount, "freeCollateral.postOrder"))
val crossMarginUsage = parser.asDouble(parser.value(parentSubaccount, "marginUsage.postOrder"))
val crossCollateral = parentSubaccount?.get("freeCollateral")
val crossMarginUsage = parentSubaccount?.get("marginUsage")
val openPositions = parser.asNativeMap(childSubaccount?.get("openPositions"))
val marketId = openPositions?.keys?.firstOrNull()
val positionMargin = parser.asDouble(parser.value(childSubaccount, "freeCollateral.postOrder"))
val positionLeverage = parser.asDouble(parser.value(childSubaccount, "openPositions.$marketId.leverage.postOrder"))
val liquidationPrice = parser.asDouble(parser.value(childSubaccount, "openPositions.$marketId.liquidationPrice.postOrder"))
val positionMargin = childSubaccount?.get("freeCollateral")
val positionLeverage = parser.value(childSubaccount, "openPositions.$marketId.leverage")
val liquidationPrice = parser.value(childSubaccount, "openPositions.$marketId.liquidationPrice")

when (type) {
IsolatedMarginAdjustmentType.Add -> {
Expand All @@ -123,13 +123,51 @@ internal class AdjustIsolatedMarginInputCalculator(val parser: ParserProtocol) {
return summary
}

private fun amountField(): Map<String, Any> {
return mapOf(
"field" to "amount",
"type" to "double",
)
}

private fun requiredFields(): List<Any> {
return listOf(
amountField(),
)
}

private fun calculatedOptionsFromField(fields: List<Any>?): Map<String, Any>? {
fields?.let {
val options = mutableMapOf<String, Any>(
"needsSize" to false,
)

for (item in fields) {
parser.asNativeMap(item)?.let { field ->
when (parser.asString(field["field"])) {
"amount" -> {
options["needsSize"] = true
}
}
}
}

return options
}

return null
}

private fun finalize(
isolatedMarginAdjustment: Map<String, Any>,
parentSubaccount: Map<String, Any>?,
childSubaccount: Map<String, Any>?,
type: IsolatedMarginAdjustmentType,
): Map<String, Any> {
val modified = isolatedMarginAdjustment.mutable()
val fields = requiredFields()
modified.safeSet("fields", fields)
modified.safeSet("options", calculatedOptionsFromField(fields))
modified.safeSet("summary", summaryForType(parentSubaccount, childSubaccount, type))
return modified
}
Expand Down
4 changes: 4 additions & 0 deletions src/commonMain/kotlin/exchange.dydx.abacus/output/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ data class SubaccountPosition(
@Serializable
data class SubaccountPendingPosition(
val assetId: String,
val marketId: String,
val firstOrderId: String,
val orderCount: Int,
val freeCollateral: TradeStatesWithDoubleValues?,
Expand All @@ -492,6 +493,7 @@ data class SubaccountPendingPosition(
Logger.d { "creating Account Pending Position\n" }
data?.let {
val assetId = parser.asString(data["assetId"]) ?: return null
val marketId = parser.asString(data["marketId"]) ?: return null
val firstOrderId = parser.asString(data["firstOrderId"]) ?: return null
val orderCount = parser.asInt(data["orderCount"]) ?: return null
val freeCollateral = TradeStatesWithDoubleValues.create(
Expand All @@ -511,6 +513,7 @@ data class SubaccountPendingPosition(
)

return if (existing?.assetId != assetId ||
existing.marketId != marketId ||
existing.firstOrderId != firstOrderId ||
existing.orderCount != orderCount ||
existing.freeCollateral !== freeCollateral ||
Expand All @@ -519,6 +522,7 @@ data class SubaccountPendingPosition(
) {
SubaccountPendingPosition(
assetId,
marketId,
firstOrderId,
orderCount,
freeCollateral,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ data class AdjustIsolatedMarginInputOptions(
@Serializable
data class AdjustIsolatedMarginInputSummary(
val crossFreeCollateral: Double?,
val crossFreeCollateralUpdated: Double?,
val crossMarginUsage: Double?,
val crossMarginUsageUpdated: Double?,
val positionMargin: Double?,
val positionMarginUpdated: Double?,
val positionLeverage: Double?,
val positionLeverageUpdated: Double?,
val liquidationPrice: Double?,
val liquidationPriceUpdated: Double?,
) {
companion object {
internal fun create(
Expand All @@ -52,25 +57,40 @@ data class AdjustIsolatedMarginInputSummary(
Logger.d { "creating Adjust Isolated Margin Input Summary\n" }

data?.let {
val crossFreeCollateral = parser.asDouble(data["crossFreeCollateral"])
val crossMarginUsage = parser.asDouble(data["crossMarginUsage"])
val positionMargin = parser.asDouble(data["positionMargin"])
val positionLeverage = parser.asDouble(data["positionLeverage"])
val liquidationPrice = parser.asDouble(data["liquidationPrice"])
val crossFreeCollateral = parser.asDouble(parser.value(data, "crossFreeCollateral.current"))
val crossFreeCollateralUpdated = parser.asDouble(parser.value(data, "crossFreeCollateral.postOrder"))
val crossMarginUsage = parser.asDouble(parser.value(data, "crossMarginUsage.current"))
val crossMarginUsageUpdated = parser.asDouble(parser.value(data, "crossMarginUsage.postOrder"))
val positionMargin = parser.asDouble(parser.value(data, "positionMargin.current"))
val positionMarginUpdated = parser.asDouble(parser.value(data, "positionMargin.postOrder"))
val positionLeverage = parser.asDouble(parser.value(data, "positionLeverage.current"))
val positionLeverageUpdated = parser.asDouble(parser.value(data, "positionLeverage.postOrder"))
val liquidationPrice = parser.asDouble(parser.value(data, "liquidationPrice.current"))
val liquidationPriceUpdated = parser.asDouble(parser.value(data, "liquidationPrice.postOrder"))

return if (
existing?.crossFreeCollateral != crossFreeCollateral ||
existing?.crossFreeCollateralUpdated != crossFreeCollateralUpdated ||
existing?.crossMarginUsage != crossMarginUsage ||
existing?.crossMarginUsageUpdated != crossMarginUsageUpdated ||
existing?.positionMargin != positionMargin ||
existing?.positionMarginUpdated != positionMarginUpdated ||
existing?.positionLeverage != positionLeverage ||
existing?.liquidationPrice != liquidationPrice
existing?.positionLeverageUpdated != positionLeverageUpdated ||
existing?.liquidationPrice != liquidationPrice ||
existing?.liquidationPriceUpdated != liquidationPriceUpdated
) {
AdjustIsolatedMarginInputSummary(
crossFreeCollateral,
crossFreeCollateralUpdated,
crossMarginUsage,
crossMarginUsageUpdated,
positionMargin,
positionMarginUpdated,
positionLeverage,
positionLeverageUpdated,
liquidationPrice,
liquidationPriceUpdated,
)
} else {
existing
Expand All @@ -94,12 +114,10 @@ enum class IsolatedMarginAdjustmentType {
data class AdjustIsolatedMarginInput(
val type: IsolatedMarginAdjustmentType,
val amount: String?,
val amountPercent: String?,
val childSubaccountNumber: Int?,
val adjustIsolatedMarginInputOptions: AdjustIsolatedMarginInputOptions?,
val summary: AdjustIsolatedMarginInputSummary?,
val errors: String?,
val errorMessage: String?,
val fee: Double?,
val summary: AdjustIsolatedMarginInputSummary?
) {
companion object {
internal fun create(
Expand All @@ -116,48 +134,34 @@ data class AdjustIsolatedMarginInput(

val childSubaccountNumber = parser.asInt(data["ChildSubaccountNumber"])
val amount = parser.asString(data["Amount"])
val fee = parser.asDouble(data["fee"])
val amountPercent = parser.asString(data["AmountPercent"])

val adjustIsolatedMarginInputOptions = AdjustIsolatedMarginInputOptions.create(
existing?.adjustIsolatedMarginInputOptions,
parser,
parser.asMap(data["adjustIsolatedMarginInputOptions"]),
parser.asMap(data["options"]),
)
val summary = AdjustIsolatedMarginInputSummary.create(
existing?.summary,
parser,
parser.asMap(data["summary"]),
)

val errors = parser.asString(data["errors"])

val errorMessage: String? =
if (errors != null) {
val errorArray = parser.decodeJsonArray(errors)
val firstError = parser.asMap(errorArray?.first())
parser.asString(firstError?.get("message"))
} else {
null
}

return if (
existing?.type != type ||
existing.amount != amount ||
existing.amountPercent != amountPercent ||
existing.childSubaccountNumber != childSubaccountNumber ||
existing.adjustIsolatedMarginInputOptions != adjustIsolatedMarginInputOptions ||
existing.summary !== summary ||
existing.errors !== errors ||
existing.errorMessage != errorMessage ||
existing.fee != fee
existing.summary !== summary
) {
AdjustIsolatedMarginInput(
type,
amount,
amountPercent,
childSubaccountNumber,
adjustIsolatedMarginInputOptions,
summary,
errors,
errorMessage,
fee,
)
} else {
existing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ data class TradeInput(
val postOnly: Boolean,
val fee: Double?,
val marginMode: MarginMode,
val targetLeverage: Double?,
val targetLeverage: Double,
val bracket: TradeInputBracket?,
val marketOrder: TradeInputMarketOrder?,
val options: TradeInputOptions?,
Expand Down Expand Up @@ -818,7 +818,7 @@ data class TradeInput(
MarginMode.invoke(it)
} ?: MarginMode.cross

val targetLeverage = parser.asDouble(data["targetLeverage"])
val targetLeverage = parser.asDouble(data["targetLeverage"]) ?: 1.0

val goodTil = TradeInputGoodUntil.create(
existing?.goodTil,
Expand Down Expand Up @@ -859,7 +859,7 @@ data class TradeInput(
existing.postOnly != postOnly ||
existing.fee != fee ||
existing.marginMode != marginMode ||
existing?.targetLeverage != targetLeverage ||
existing.targetLeverage != targetLeverage ||
existing.bracket != bracket ||
existing.marketOrder != marketOrder ||
existing.options != options ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2244,7 +2244,7 @@ open class StateManagerAdaptor(
val walletAddress = wallet.walletAddress ?: error("walletAddress is null")
val isolatedMarginAdjustment = stateMachine.state?.input?.adjustIsolatedMargin
?: error("isolatedMarginAdjustment is null")
val amount = isolatedMarginAdjustment.amount ?: error("amount is null")
val amount = parser.asString(isolatedMarginAdjustment.amount) ?: error("amount is null")
val childSubaccountNumber = isolatedMarginAdjustment.childSubaccountNumber
?: error("childSubaccountNumber is null")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class V4StateManagerConfigs(
"historical-pnl":"/v4/historical-pnl",
"transfers":"/v4/transfers",
"historicalTradingRewardAggregations":"/v4/historicalTradingRewardAggregations",
"parent-fills":"/v4/fills/parentSubaccount",
"parent-transfers": "/v4/transfers/parentSubaccount"
"parent-fills":"/v4/fills/parentSubaccountNumber",
"parent-transfers": "/v4/transfers/parentSubaccountNumber"
},
"faucet":{
"faucet":"/faucet/tokens"
Expand Down
Loading

0 comments on commit 45911ef

Please sign in to comment.