-
Notifications
You must be signed in to change notification settings - Fork 1
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
handle short position case for displaying liquidation price, live-update position row items #205
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,17 +113,19 @@ private class dydxAdjustMarginInputViewPresenter: HostedViewPresenter<dydxAdjust | |
AbacusStateManager.shared.adjustIsolatedMargin(input: childSubaccountNumber, type: .childsubaccountnumber) | ||
|
||
Publishers | ||
.CombineLatest3( | ||
.CombineLatest4( | ||
AbacusStateManager.shared.state.market(of: marketId).compactMap { $0 }, | ||
AbacusStateManager.shared.state.assetMap, | ||
AbacusStateManager.shared.state.adjustIsolatedMarginInput.compactMap { $0 } | ||
AbacusStateManager.shared.state.adjustIsolatedMarginInput.compactMap { $0 }, | ||
AbacusStateManager.shared.state.selectedSubaccountPositions.compactMap { $0.first(where: { $0.id == marketId }) } | ||
) | ||
.sink { [weak self] market, assetMap, input in | ||
.sink { [weak self] market, assetMap, input, position in | ||
self?.updateState(market: market, assetMap: assetMap) | ||
self?.updateFields(input: input) | ||
self?.updateForMarginDirection(input: input) | ||
self?.updatePrePostValues(input: input, market: market) | ||
self?.updateLiquidationPrice(input: input, market: market) | ||
guard let side = position.side.current else { return } | ||
self?.updateLiquidationPrice(input: input, side: side, market: market) | ||
} | ||
.store(in: &subscriptions) | ||
} | ||
|
@@ -267,24 +269,32 @@ private class dydxAdjustMarginInputViewPresenter: HostedViewPresenter<dydxAdjust | |
} | ||
} | ||
|
||
private func updateLiquidationPrice(input: AdjustIsolatedMarginInput, market: PerpetualMarket) { | ||
private func updateLiquidationPrice(input: AdjustIsolatedMarginInput, side: Abacus.PositionSide, market: PerpetualMarket) { | ||
if let displayTickSizeDecimals = market.configs?.displayTickSizeDecimals?.intValue { | ||
let curLiquidationPrice = input.summary?.liquidationPrice ?? 0 | ||
let postLiquidationPrice = input.summary?.liquidationPriceUpdated ?? 0 | ||
let curLiquidationPrice = input.summary?.liquidationPrice | ||
let postLiquidationPrice = input.summary?.liquidationPriceUpdated | ||
let currentLeverage = input.summary?.positionLeverage?.doubleValue ?? 0 | ||
let postLeverage = input.summary?.positionLeverageUpdated?.doubleValue ?? 0 | ||
|
||
viewModel?.liquidationPrice = dydxAdjustMarginLiquidationPriceViewModel() | ||
|
||
if input.summary?.positionLeverageUpdated == nil { | ||
let hasNoInput = input.summary?.positionLeverageUpdated == nil | ||
|
||
if hasNoInput { | ||
// no input, no change, update accordingly | ||
viewModel?.liquidationPrice?.direction = .none | ||
if currentLeverage <= 1 { | ||
if currentLeverage <= 1 && side == Abacus.PositionSide.long_ { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. up to you on whether you want to simplify this if else - on Android i didn't end up doing any complex checks on leverage or side. i just used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How did u decide to display "None" or not then? |
||
viewModel?.liquidationPrice?.before = DataLocalizer.shared?.localize(path: "APP.GENERAL.NONE", params: nil) | ||
} else { | ||
viewModel?.liquidationPrice?.before = dydxFormatter.shared.dollar(number: curLiquidationPrice, digits: displayTickSizeDecimals) | ||
} | ||
viewModel?.liquidationPrice?.after = nil | ||
} else if side == Abacus.PositionSide.short_ { | ||
// else there is input, handle short positions which always have a liquidation price | ||
viewModel?.liquidationPrice?.before = dydxFormatter.shared.dollar(number: curLiquidationPrice, digits: displayTickSizeDecimals) | ||
viewModel?.liquidationPrice?.after = dydxFormatter.shared.dollar(number: postLiquidationPrice, digits: displayTickSizeDecimals) | ||
} else { | ||
// else there is input, handle long positions which sometimes have a liquidation price | ||
switch (currentLeverage <= 1, postLeverage <= 1) { | ||
case (true, true): | ||
viewModel?.liquidationPrice?.before = DataLocalizer.shared?.localize(path: "APP.GENERAL.NONE", params: nil) | ||
|
@@ -299,17 +309,17 @@ private class dydxAdjustMarginInputViewPresenter: HostedViewPresenter<dydxAdjust | |
viewModel?.liquidationPrice?.before = dydxFormatter.shared.dollar(number: curLiquidationPrice, digits: displayTickSizeDecimals) | ||
viewModel?.liquidationPrice?.after = dydxFormatter.shared.dollar(number: postLiquidationPrice, digits: displayTickSizeDecimals) | ||
} | ||
} | ||
|
||
switch input.type { | ||
case .add: | ||
// liquidation price is moving further from oracle price with less leverage | ||
viewModel?.liquidationPrice?.direction = currentLeverage <= 1 && postLeverage <= 1 ? .none : .safer | ||
case .remove: | ||
// liquidation price is moving closer to oracle price with more leverage | ||
viewModel?.liquidationPrice?.direction = currentLeverage <= 1 && postLeverage <= 1 ? .none : .riskier | ||
default: | ||
viewModel?.liquidationPrice?.direction = .none | ||
} | ||
switch input.type { | ||
case .add: | ||
// liquidation price is moving further from oracle price with less leverage | ||
viewModel?.liquidationPrice?.direction = hasNoInput ? .none : .safer | ||
case .remove: | ||
// liquidation price is moving closer to oracle price with more leverage | ||
viewModel?.liquidationPrice?.direction = hasNoInput ? .none : .riskier | ||
default: | ||
viewModel?.liquidationPrice?.direction = .none | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i've seen positionLeverageUpdated sometimes be a weird value. on android i'm just checking
input.amount == nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
input.amount
is "0" though or "000" etc, i wanted to avoid that