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

Remove position leverage validation #449

Merged
Merged
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
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.75"
version = "1.7.76"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ internal class InputValidator(
"MARKET_ORDER_NOT_ENOUGH_LIQUIDITY" to 1030,
"MARKET_ORDER_ERROR_INDEX_PRICE_SLIPPAGE" to 1031,
"MARKET_ORDER_ERROR_ORDERBOOK_SLIPPAGE" to 1032,
"MARKET_ORDER_PRICE_IMPACT_AT_MAX_LEVERAGE" to 1033,
aforaleka marked this conversation as resolved.
Show resolved Hide resolved
"ORDER_WOULD_FLIP_POSITION" to 1034,
"TRIGGER_MUST_ABOVE_INDEX_PRICE" to 1040,
"TRIGGER_MUST_BELOW_INDEX_PRICE" to 1041,
"LIMIT_MUST_ABOVE_TRIGGER_PRICE" to 1042,
"LIMIT_MUST_BELOW_TRIGGER_PRICE" to 1043,
"PRICE_MUST_POSITIVE" to 1044,

"INVALID_LARGE_POSITION_LEVERAGE" to 1051,
"INVALID_NEW_POSITION_LEVERAGE" to 1052,
"INVALID_NEW_ACCOUNT_MARGIN_USAGE" to 1053,

"BUY_TRIGGER_TOO_CLOSE_TO_LIQUIDATION_PRICE" to 1060,
Expand All @@ -58,7 +55,6 @@ internal class InputValidator(

"WOULD_NOT_REDUCE_UNCHECK" to 1100,

"MARKET_ORDER_CLOSE_TO_MAX_LEVERAGE" to 1200,
"MARKET_ORDER_WARNING_INDEX_PRICE_SLIPPAGE" to 1201,
"MARKET_ORDER_WARNING_ORDERBOOK_SLIPPAGE" to 1202,
"LIMIT_PRICE_TRIGGER_PRICE_SLIPPAGE_LOWER" to 1203,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package exchange.dydx.abacus.validator.trade

import abs
import exchange.dydx.abacus.protocols.LocalizerProtocol
import exchange.dydx.abacus.protocols.ParserProtocol
import exchange.dydx.abacus.state.app.helper.Formatter
Expand Down Expand Up @@ -43,15 +42,6 @@ internal class TradePositionStateValidator(
change,
)
if (position != null) {
val leverageError = validatePositionLeverage(
position,
trade,
change,
restricted,
)
if (leverageError != null) {
errors.add(leverageError)
}
val positionSizeError = validatePositionSize(
position,
market,
Expand Down Expand Up @@ -105,107 +95,6 @@ internal class TradePositionStateValidator(
}
}

private fun validatePositionLeverage(
position: Map<String, Any>,
trade: Map<String, Any>,
change: PositionChange,
restricted: Boolean,
): Map<String, Any>? {
/*
MARKET_ORDER_CLOSE_TO_MAX_LEVERAGE
MARKET_ORDER_PRICE_IMPACT_AT_MAX_LEVERAGE

INVALID_NEW_POSITION_LEVERAGE
INVALID_LARGE_POSITION_LEVERAGE
*/
return if (overMaxLeverage(
position,
)
) {
if (parser.asString(trade["type"]) == "MARKET") {
val increasingPosition = when (change) {
PositionChange.NEW, PositionChange.CROSSING, PositionChange.INCREASING -> true
else -> false
}

error(
if (increasingPosition) "ERROR" else "WARNING",
"MARKET_ORDER_PRICE_IMPACT_AT_MAX_LEVERAGE",
if (increasingPosition) listOf("size.size") else null,
if (increasingPosition) "APP.TRADE.MODIFY_SIZE_FIELD" else null,
"ERRORS.TRADE_BOX_TITLE.MARKET_ORDER_PRICE_IMPACT_AT_MAX_LEVERAGE",
"ERRORS.TRADE_BOX.MARKET_ORDER_PRICE_IMPACT_AT_MAX_LEVERAGE",
)
} else {
if (change == PositionChange.NEW) {
error(
"ERROR",
"INVALID_NEW_POSITION_LEVERAGE",
listOf("size.size"),
"APP.TRADE.MODIFY_SIZE_FIELD",
"ERRORS.TRADE_BOX_TITLE.INVALID_NEW_POSITION_LEVERAGE",
"ERRORS.TRADE_BOX.INVALID_NEW_POSITION_LEVERAGE",
)
} else {
error(
"ERROR",
"INVALID_LARGE_POSITION_LEVERAGE",
listOf("size.size"),
"APP.TRADE.MODIFY_SIZE_FIELD",
"ERRORS.TRADE_BOX_TITLE.INVALID_LARGE_POSITION_LEVERAGE",
"ERRORS.TRADE_BOX.INVALID_LARGE_POSITION_LEVERAGE",
)
}
}
} else {
if (parser.asString(trade["type"]) == "MARKET") {
val increasingPosition = when (change) {
PositionChange.NEW, PositionChange.CROSSING, PositionChange.INCREASING -> true
else -> false
}

if (increasingPosition && orderOverMaxLeverage(position)) {
error(
"WARNING",
"MARKET_ORDER_CLOSE_TO_MAX_LEVERAGE",
listOf("size.size"),
"APP.TRADE.MODIFY_SIZE_FIELD",
"WARNINGS.TRADE_BOX_TITLE.MARKET_ORDER_CLOSE_TO_MAX_LEVERAGE",
"WARNINGS.TRADE_BOX.MARKET_ORDER_CLOSE_TO_MAX_LEVERAGE",
)
} else {
null
}
} else {
null
}
}
}

private fun overMaxLeverage(
position: Map<String, Any>,
): Boolean {
val leverage = parser.asDouble(parser.value(position, "leverage.postOrder"))?.abs()
val adjustedImf = parser.asDouble(parser.value(position, "adjustedImf.current"))
return overLeverage(leverage, adjustedImf)
}

private fun overLeverage(leverage: Double?, adjustedImf: Double?): Boolean {
return if (leverage != null && adjustedImf != null && adjustedImf > Numeric.double.ZERO) {
leverage > (Numeric.double.ONE / adjustedImf) || leverage < Numeric.double.ZERO
} else {
false
}
}

private fun orderOverMaxLeverage(
position: Map<String, Any>,
): Boolean {
val leverage = parser.asDouble(parser.value(position, "leverage.postOrder"))?.abs()
val adjustedImf = parser.asDouble(parser.value(position, "adjustedImf.postOrder"))
return overLeverage(leverage, adjustedImf)
}

private fun validatePositionSize(
position: Map<String, Any>?,
market: Map<String, Any>?,
Expand Down
Loading