Changeset View
Changeset View
Standalone View
Standalone View
apps/marlin-wallet/web/src/active-asset.ts
| // Copyright (c) 2026 The Bitcoin developers | // Copyright (c) 2026 The Bitcoin developers | ||||
| // Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
| import { CryptoTicker } from 'ecash-price'; | import { | ||||
| CryptoTicker, | |||||
| formatPrice, | |||||
| type PriceFormatterConfig, | |||||
| } from 'ecash-price'; | |||||
| import { XEC_ASSET, type AssetDefinition } from './supported-assets'; | import { XEC_ASSET, type AssetDefinition } from './supported-assets'; | ||||
| type ActiveAsset = { def: AssetDefinition }; | type ActiveAsset = { def: AssetDefinition }; | ||||
| let current: ActiveAsset = { def: XEC_ASSET }; | let current: ActiveAsset = { def: XEC_ASSET }; | ||||
| export function setActiveAsset(asset: ActiveAsset): void { | export function setActiveAsset(asset: ActiveAsset): void { | ||||
| current = asset; | current = asset; | ||||
| Show All 21 Lines | |||||
| export function activeQuoteCurrency(): CryptoTicker | undefined { | export function activeQuoteCurrency(): CryptoTicker | undefined { | ||||
| return current.def.quoteCurrency ?? undefined; | return current.def.quoteCurrency ?? undefined; | ||||
| } | } | ||||
| export function allowFiatForActiveAsset(): boolean { | export function allowFiatForActiveAsset(): boolean { | ||||
| return current.def.quoteCurrency !== undefined; | return current.def.quoteCurrency !== undefined; | ||||
| } | } | ||||
| /** formatPrice uppercases crypto tickers; swap in the asset's display ticker. */ | |||||
| function withDisplayTickerSuffix( | |||||
| formatted: string, | |||||
| displayTicker: string, | |||||
| cryptoTicker: CryptoTicker, | |||||
| ): string { | |||||
| const upperSuffix = ` ${cryptoTicker.toString().toUpperCase()}`; | |||||
| const displaySuffix = ` ${displayTicker}`; | |||||
| if (formatted.endsWith(upperSuffix)) { | |||||
| return formatted.slice(0, -upperSuffix.length) + displaySuffix; | |||||
| } | |||||
| return formatted; | |||||
| } | |||||
| /** Format an amount in the active asset using its display ticker (preserves case). */ | |||||
| export function formatActiveAssetAmount( | |||||
| amount: number, | |||||
| config?: PriceFormatterConfig, | |||||
| ): string { | |||||
| const cryptoTicker = activeCryptoTicker(); | |||||
| const formatted = formatPrice(amount, cryptoTicker, config); | |||||
| return withDisplayTickerSuffix( | |||||
| formatted, | |||||
| activeAssetTicker(), | |||||
| cryptoTicker, | |||||
| ); | |||||
| } | |||||