Skip to content

Commit

Permalink
adjust what is displayed in positions list
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Oct 22, 2024
1 parent ce9b2dc commit b6aedd4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
12 changes: 12 additions & 0 deletions dydx/dydxFormatter/dydxFormatter/dydxFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ public final class dydxFormatter: NSObject, SingletonProtocol {
return nil
}

public func condensedDollar(number: Double?, digits: Int = 2) -> String? {
if let number = number {
let text = condensed(number: NSNumber(value: number), digits: digits)
if text?.first == "-" {
return "-$\(text?.dropFirst() ?? "")"
} else {
return "$\(text ?? "")"
}
}
return nil
}

/// formats the number as "$" or "-$" of "+$" prefixed
/// - Parameters:
/// - number: the number to format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class dydxPortfolioPositionsViewPresenter: HostedViewPresenter<dydxPortfolioPosi
if onboarded {
let vaultBalance = vault?.account?.balanceUsdc?.doubleValue ?? 0
let vaultApy = vault?.details?.thirtyDayReturnPercent?.doubleValue
self.viewModel?.vaultBalance = dydxFormatter.shared.dollar(number: vaultBalance, digits: 2)
self.viewModel?.vaultBalance = dydxFormatter.shared.dollar(number: vaultBalance, digits: 0)
self.viewModel?.vaultApy = vaultApy
} else {
self.viewModel?.vaultBalance = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ private class dydxVaultViewBuilderPresenter: HostedViewPresenter<dydxVaultViewMo

viewModel?.positions = vault?.positions?.positions?.map { (position) -> dydxVaultPositionViewModel? in
guard let leverage = position.currentLeverageMultiple?.doubleValue,
let notionalValue = position.currentPosition?.usdc?.doubleValue,
let positionSize = position.currentPosition?.asset?.doubleValue,
let marketId = position.marketId,
// special case for fake USDC market to show unused margin
let assetId = marketId == "USDC-USD" ? "USDC" : marketMap[marketId]?.assetId,
let displayId = assetMap[assetId]?.id ?? marketMap[marketId]?.displayId
else { return nil }
let equity = position.marginUsdc?.doubleValue ?? 0
let notionalValue = position.currentPosition?.usdc?.doubleValue ?? 0
let positionSize = position.currentPosition?.asset?.doubleValue ?? 0
let iconType: PlatformIconViewModel.IconType
let tokenUnitPrecision: Int
if marketId == "USDC-USD" {
Expand All @@ -113,6 +114,7 @@ private class dydxVaultViewBuilderPresenter: HostedViewPresenter<dydxVaultViewMo
iconType: iconType,
side: positionSize > 0 ? .long : .short,
leverage: leverage,
equity: equity,
notionalValue: notionalValue,
positionSize: positionSize.magnitude,
tokenUnitPrecision: tokenUnitPrecision,
Expand All @@ -121,7 +123,7 @@ private class dydxVaultViewBuilderPresenter: HostedViewPresenter<dydxVaultViewMo
sparklineValues: position.thirtyDayPnl?.sparklinePoints?.map({ $0.doubleValue }))
}
.compactMap { $0 }
.sorted(by: { $0.notionalValue > $1.notionalValue })
.sorted(by: { $0.equity > $1.equity })
}

private func updateChartState(vault: Abacus.Vault?, valueType: dydxVaultChartViewModel.ValueTypeOption, timeType: dydxVaultChartViewModel.ValueTimeOption) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class dydxVaultPositionViewModel: PlatformViewModel {
@Published public var iconType: PlatformIconViewModel.IconType = .init(url: nil, placeholderText: nil)
@Published public var side: SideTextViewModel.Side
@Published public var leverage: Double
@Published public var equity: Double
@Published public var notionalValue: Double
@Published public var positionSize: Double
@Published public var tokenUnitPrecision: Int
Expand All @@ -36,11 +37,13 @@ public class dydxVaultPositionViewModel: PlatformViewModel {
}

fileprivate var notionalValueText: String {
dydxFormatter.shared.dollar(number: notionalValue) ?? "--"
let size = dydxFormatter.shared.condensedDollar(number: notionalValue) ?? "--"
let equity = dydxFormatter.shared.condensedDollar(number: equity) ?? "--"
return "\(size) / \(equity)"
}

fileprivate var positionSizeText: String {
dydxFormatter.shared.localFormatted(number: positionSize, digits: tokenUnitPrecision) ?? "--"
dydxFormatter.shared.condensed(number: positionSize, digits: tokenUnitPrecision) ?? "--"
}

fileprivate var pnlColor: ThemeColor.SemanticColor {
Expand All @@ -52,7 +55,7 @@ public class dydxVaultPositionViewModel: PlatformViewModel {
}

fileprivate var pnlAmountText: String {
dydxFormatter.shared.dollar(number: pnlAmount) ?? "--"
dydxFormatter.shared.dollar(number: pnlAmount, digits: 0) ?? "--"
}

fileprivate var pnlPercentageText: String {
Expand All @@ -64,6 +67,7 @@ public class dydxVaultPositionViewModel: PlatformViewModel {
iconType: PlatformIconViewModel.IconType,
side: SideTextViewModel.Side,
leverage: Double,
equity: Double,
notionalValue: Double,
positionSize: Double,
tokenUnitPrecision: Int,
Expand All @@ -74,6 +78,7 @@ public class dydxVaultPositionViewModel: PlatformViewModel {
self.iconType = iconType
self.side = side
self.leverage = leverage
self.equity = equity
self.notionalValue = notionalValue
self.positionSize = positionSize
self.tokenUnitPrecision = tokenUnitPrecision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ private struct dydxVaultView: View {
.leftAligned()
.frame(width: VaultPositionView.marketSectionWidth)
.lineLimit(1)
Text(DataLocalizer.shared?.localize(path: "APP.GENERAL.SIZE", params: nil) ?? "")
let sizeText = DataLocalizer.shared?.localize(path: "APP.GENERAL.SIZE", params: nil) ?? ""
let equityText = DataLocalizer.shared?.localize(path: "APP.GENERAL.EQUITY", params: nil) ?? ""
Text(sizeText + " / " + equityText)
.themeColor(foreground: .textTertiary)
.themeFont(fontType: .base, fontSize: .small)
.lineLimit(1)
Expand Down

0 comments on commit b6aedd4

Please sign in to comment.