Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import java.math.BigInteger

private val usdFiatFormatter = CurrencyFormatter(type = CurrencyFormatter.Type.Fiat, currency = Currency.USD)

class GetTransactionsImpl(
private val transactionsRepository: TransactionRepository,
scope: CoroutineScope = CoroutineScope(Dispatchers.IO),
Expand Down Expand Up @@ -111,11 +113,11 @@ class TransactionDataAggregateImpl(
AmountSign.Incoming.format(formatter.string(value, asset))
} ?: ""
}
TransactionType.PerpetualOpenPosition -> CurrencyFormatter(type = CurrencyFormatter.Type.Fiat, currency = Currency.USD).string(
TransactionType.PerpetualOpenPosition -> usdFiatFormatter.string(
CryptoFiatConverter.toFiat(Crypto(data.transaction.value), HypercoreUSDC.decimals, price = 1.0).atomicValue,
)
TransactionType.PerpetualClosePosition -> pnl?.let {
PriceChangeFormatter(CurrencyFormatter(type = CurrencyFormatter.Type.Fiat, currency = Currency.USD)).string(it)
PriceChangeFormatter(usdFiatFormatter).string(it)
} ?: ""
TransactionType.StakeUndelegate,
TransactionType.StakeRewards,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import com.gemwallet.android.ui.theme.paddingMiddle
import com.wallet.core.primitives.Currency
import com.wallet.core.primitives.PerpetualMarginType

private val usdFormatter = CurrencyFormatter(currency = Currency.USD)

internal fun LazyListScope.positionProperties(
position: PerpetualPositionDetailsDataAggregate?,
onAutocloseClick: () -> Unit,
Expand Down Expand Up @@ -121,5 +123,5 @@ private fun PerpetualMarginType.title(): String {
}

private fun Double?.formatTriggerOrder(label: String): String? {
return this?.let { "$label: ${CurrencyFormatter(currency = Currency.USD).string(it)}" }
return this?.let { "$label: ${usdFormatter.string(it)}" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import com.wallet.core.primitives.TransactionDirection
import com.wallet.core.primitives.TransactionState
import com.wallet.core.primitives.TransactionType

private val usdFiatFormatter = CurrencyFormatter(type = CurrencyFormatter.Type.Fiat, currency = Currency.USD)

@Composable
fun TransactionDataAggregate.getTitle(): String =
perpetualTitle(type, perpetualDirection) ?: stringResource(type.getTitle(direction, state))
Expand Down Expand Up @@ -72,7 +74,7 @@ fun TransactionDataAggregate.formatAddress(): String? = when (type) {
TransactionType.PerpetualOpenPosition,
TransactionType.PerpetualClosePosition,
TransactionType.PerpetualModifyPosition -> perpetualPrice?.let {
"${stringResource(R.string.asset_price)}: ${CurrencyFormatter(type = CurrencyFormatter.Type.Fiat, currency = Currency.USD).string(it)}"
"${stringResource(R.string.asset_price)}: ${usdFiatFormatter.string(it)}"
}
TransactionType.StakeFreeze -> resourceType?.let {
"${stringResource(id = R.string.transfer_to)} ${stringResource(it.titleRes())}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ import SwiftUI
@Observable
@MainActor
public final class AutocloseSceneViewModel {
private let currencyFormatter = CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue)
private let currencyFormatter: CurrencyFormatter
private let percentFormatter = PercentFormatter.signed
private let perpetualFormatter = PerpetualFormatter(provider: .hypercore)
private let type: AutocloseType
private let estimator: AutocloseEstimator

var input: AutocloseInput

public init(type: AutocloseType) {
public init(type: AutocloseType, currencyFormatter: CurrencyFormatter = .usd) {
self.type = type
self.currencyFormatter = currencyFormatter
estimator = AutocloseEstimator(type: type)

input = AutocloseInput(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ struct OpenPositionItemViewModel: ListAssetItemViewable {

var action: ((ListAssetItemAction) -> Void)?

init(data: AutocloseOpenData) {
init(data: AutocloseOpenData, currencyFormatter: CurrencyFormatter = .usd) {
self.data = data
currencyFormatter = CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue)
self.currencyFormatter = currencyFormatter
}

var name: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ public struct PerpetualViewModel {
private let priceFormatter: CurrencyFormatter
private let percentFormatter = PercentFormatter.signed

public init(perpetual: Perpetual, currencyStyle: CurrencyFormatterType = .abbreviated) {
public init(
perpetual: Perpetual,
currencyStyle: CurrencyFormatterType = .abbreviated,
priceFormatter: CurrencyFormatter = .usd,
) {
self.perpetual = perpetual
self.priceFormatter = priceFormatter
marketValueFormatter = CurrencyFormatter(type: currencyStyle, currencyCode: Currency.usd.rawValue)
priceFormatter = CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue)
}

public var name: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import SwiftUI

struct TransactionPnlViewModel {
private let metadata: TransactionPerpetualMetadata?
private let currencyFormatter: CurrencyFormatter

init(metadata: TransactionPerpetualMetadata?) {
init(metadata: TransactionPerpetualMetadata?, currencyFormatter: CurrencyFormatter = .usd) {
self.metadata = metadata
self.currencyFormatter = currencyFormatter
}
}

Expand All @@ -22,9 +24,8 @@ extension TransactionPnlViewModel: ItemModelProvidable {
return .empty
}

let formatter = CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue)
let sign = metadata.pnl >= 0 ? "+" : ""
let pnlFormatted = formatter.string(metadata.pnl)
let pnlFormatted = currencyFormatter.string(metadata.pnl)
let color = metadata.pnl >= 0 ? Colors.green : Colors.red

return .pnl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import Primitives

struct TransactionPriceViewModel {
private let metadata: TransactionPerpetualMetadata?
private let currencyFormatter: CurrencyFormatter

init(metadata: TransactionPerpetualMetadata?) {
init(metadata: TransactionPerpetualMetadata?, currencyFormatter: CurrencyFormatter = .usd) {
self.metadata = metadata
self.currencyFormatter = currencyFormatter
}
}

Expand All @@ -20,8 +22,7 @@ extension TransactionPriceViewModel: ItemModelProvidable {
return .empty
}

let formatter = CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue)
let priceFormatted = formatter.string(metadata.price)
let priceFormatted = currencyFormatter.string(metadata.price)

return .price(
title: Localized.Asset.price,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Store
@Observable
@MainActor
final class PerpetualsPreviewViewModel {
private let currencyFormatter = CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue)
private let currencyFormatter: CurrencyFormatter

let positionsQuery: ObservableQuery<PerpetualPositionsRequest>
let walletBalanceQuery: ObservableQuery<PerpetualWalletBalanceRequest>
Expand All @@ -21,7 +21,8 @@ final class PerpetualsPreviewViewModel {
walletBalanceQuery.value
}

init(walletId: WalletId) {
init(walletId: WalletId, currencyFormatter: CurrencyFormatter = .usd) {
self.currencyFormatter = currencyFormatter
positionsQuery = ObservableQuery(PerpetualPositionsRequest(walletId: walletId), initialValue: [])
walletBalanceQuery = ObservableQuery(PerpetualWalletBalanceRequest(walletId: walletId), initialValue: .zero)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class PortfolioSceneViewModel: ChartListViewable {
private let currencyFormatter: CurrencyFormatter
private let priceFormatter: CurrencyFormatter
private let percentFormatter = PercentFormatter.signed
private let perpetualFormatter = CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue)
private let perpetualFormatter: CurrencyFormatter

var state: PortfolioState

Expand All @@ -38,10 +38,12 @@ public final class PortfolioSceneViewModel: ChartListViewable {
service: PortfolioDataService,
preferences: ObservablePreferences,
defaultType: PortfolioType = .wallet,
perpetualFormatter: CurrencyFormatter = .usd,
) {
self.wallet = wallet
self.service = service
self.preferences = preferences
self.perpetualFormatter = perpetualFormatter
let currencyCode = preferences.preferences.currency
currencyFormatter = CurrencyFormatter(type: .currency, currencyCode: currencyCode)
priceFormatter = CurrencyFormatter(currencyCode: currencyCode)
Expand Down
2 changes: 1 addition & 1 deletion ios/Packages/Formatters/Sources/AutocloseFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public struct AutocloseFormatter: Sendable {
private let stopLossLabel: String

public init(
currencyFormatter: CurrencyFormatter = CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue),
currencyFormatter: CurrencyFormatter = .usd,
takeProfitLabel: String,
stopLossLabel: String,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c). Gem Wallet. All rights reserved.

import Foundation
import Primitives

public extension CurrencyFormatter {
static let usd = CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct PerpetualDetailsViewModel: Sendable, Identifiable {
}

private let type: PerpetualDetailsType
private let currencyFormatter = CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue)
private let currencyFormatter: CurrencyFormatter
private let numericFormatter = NumericFormatter()
private let percentFormatter = PercentFormatter.signed
private let percentSignLessFormatter = PercentFormatter.unsigned
Expand All @@ -47,8 +47,9 @@ public struct PerpetualDetailsViewModel: Sendable, Identifiable {
stopLossLabel: Localized.Perpetual.stopLoss,
)

public init(type: PerpetualDetailsType) {
public init(type: PerpetualDetailsType, currencyFormatter: CurrencyFormatter = .usd) {
self.type = type
self.currencyFormatter = currencyFormatter
}

var data: PerpetualConfirmData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public extension PriceChangeViewModel {
static func mock(value: Double?) -> PriceChangeViewModel {
PriceChangeViewModel(
value: value,
currencyFormatter: CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue),
currencyFormatter: CurrencyFormatter.usd,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public extension TotalValueViewModel {
) -> TotalValueViewModel {
TotalValueViewModel(
totalValue: .mock(value: value, pnlAmount: pnlAmount, pnlPercentage: pnlPercentage),
currencyFormatter: CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue),
currencyFormatter: CurrencyFormatter.usd,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension PnLViewModel {
PnLViewModel(
pnl: pnl,
marginAmount: marginAmount,
currencyFormatter: CurrencyFormatter(type: .currency, currencyCode: Currency.usd.rawValue),
currencyFormatter: CurrencyFormatter.usd,
percentFormatter: PercentFormatter.signed,
)
}
Expand Down
Loading