Skip to content

Commit

Permalink
add "none" behavior for adjust margin
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Jun 28, 2024
1 parent 7de65b5 commit 632af75
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private class dydxAdjustMarginInputViewPresenter: HostedViewPresenter<dydxAdjust
self?.viewModel?.inlineAlert = InlineAlertViewModel(.init(title: nil, body: error.localizedMessage, level: .error))
return
} else {
self?.resetInputAmount()
Router.shared?.navigate(to: RoutingRequest(path: "/action/dismiss"), animated: true, completion: nil)
}
}
Expand All @@ -101,6 +102,10 @@ private class dydxAdjustMarginInputViewPresenter: HostedViewPresenter<dydxAdjust
attachChildren(workers: childPresenters)
}

private func resetInputAmount() {
AbacusStateManager.shared.adjustIsolatedMargin(input: nil, type: .amount)
}

override func start() {
super.start()
guard let marketId, let childSubaccountNumber else { return }
Expand Down Expand Up @@ -198,7 +203,15 @@ private class dydxAdjustMarginInputViewPresenter: HostedViewPresenter<dydxAdjust
ctaButtonPresenter.viewModel?.ctaButtonState = .disabled()
return
} else {
ctaButtonPresenter.viewModel?.ctaButtonState = .enabled()
switch input.type {
case .add:
ctaButtonPresenter.viewModel?.ctaButtonState = .enabled(DataLocalizer.shared?.localize(path: "APP.TRADE.ADD_MARGIN", params: nil) ?? "")
case .remove:
ctaButtonPresenter.viewModel?.ctaButtonState = .enabled(DataLocalizer.shared?.localize(path: "APP.TRADE.REMOVE_MARGIN", params: nil) ?? "")
default:
assertionFailure("no margin direction")
break
}
viewModel?.inlineAlert = nil
}

Expand Down Expand Up @@ -256,21 +269,44 @@ private class dydxAdjustMarginInputViewPresenter: HostedViewPresenter<dydxAdjust

private func updateLiquidationPrice(input: AdjustIsolatedMarginInput, market: PerpetualMarket) {
if let displayTickSizeDecimals = market.configs?.displayTickSizeDecimals?.intValue {
let curLiquidationPrice = input.summary?.liquidationPrice
let postLiquidationPrice = input.summary?.liquidationPriceUpdated
let curLiquidationPrice = input.summary?.liquidationPrice ?? 0
let postLiquidationPrice = input.summary?.liquidationPriceUpdated ?? 0
let currentLeverage = input.summary?.positionLeverage?.doubleValue ?? 0
let postLeverage = input.summary?.positionLeverageUpdated?.doubleValue ?? 0

viewModel?.liquidationPrice = dydxAdjustMarginLiquidationPriceViewModel()
viewModel?.liquidationPrice?.before = dydxFormatter.shared.dollar(number: curLiquidationPrice, digits: displayTickSizeDecimals)
viewModel?.liquidationPrice?.after = input.summary?.positionLeverageUpdated == nil ? nil : dydxFormatter.shared.dollar(number: postLiquidationPrice, digits: displayTickSizeDecimals)

if input.summary?.positionLeverageUpdated == nil {
viewModel?.liquidationPrice?.direction = .none
if currentLeverage <= 1 {
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 {
switch (currentLeverage <= 1, postLeverage <= 1) {
case (true, true):
viewModel?.liquidationPrice?.before = DataLocalizer.shared?.localize(path: "APP.GENERAL.NONE", params: nil)
viewModel?.liquidationPrice?.after = nil
case (true, false):
viewModel?.liquidationPrice?.before = DataLocalizer.shared?.localize(path: "APP.GENERAL.NONE", params: nil)
viewModel?.liquidationPrice?.after = dydxFormatter.shared.dollar(number: postLiquidationPrice, digits: displayTickSizeDecimals)
case (false, true):
viewModel?.liquidationPrice?.before = dydxFormatter.shared.dollar(number: curLiquidationPrice, digits: displayTickSizeDecimals)
viewModel?.liquidationPrice?.after = DataLocalizer.shared?.localize(path: "APP.GENERAL.NONE", params: nil)
case (false, false):
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 = .safer
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 = .riskier
viewModel?.liquidationPrice?.direction = currentLeverage <= 1 && postLeverage <= 1 ? .none : .riskier
default:
viewModel?.liquidationPrice?.direction = .none
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Utilities

public class dydxAdjustMarginCtaButtonViewModel: PlatformViewModel {
public enum State {
case enabled(String? = nil)
case enabled(String)
case disabled(String? = nil)
case thinking
}
Expand Down Expand Up @@ -43,7 +43,7 @@ public class dydxAdjustMarginCtaButtonViewModel: PlatformViewModel {
let state: PlatformButtonState
switch ctaButtonState {
case .enabled(let title):
buttonTitle = title ?? DataLocalizer.localize(path: "APP.TRADE.ADD_MARGIN")
buttonTitle = title
state = .primary
case .disabled(let title):
buttonTitle = title ?? DataLocalizer.localize(path: "APP.TRADE.ENTER_AMOUNT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class dydxAdjustMarginLiquidationPriceViewModel: PlatformViewModel {
if self.after == nil {
Text(self.before ?? "")
.themeFont(fontSize: .large)
.themeColor(foreground: .textPrimary)
.themeColor(foreground: .textSecondary)
} else {
Text(self.before ?? "")
.themeFont(fontSize: .small)
Expand Down

0 comments on commit 632af75

Please sign in to comment.