Changeset View
Changeset View
Standalone View
Standalone View
apps/marlin-wallet/web/src/price.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, PriceFetcher, PricePair } from 'ecash-price'; | import { CryptoTicker, Fiat, PriceFetcher, PricePair } from 'ecash-price'; | ||||
| import { activeAssetTicker, activeQuoteCurrency } from './active-asset'; | |||||
| export class MarlinPriceFetcher { | export class MarlinPriceFetcher { | ||||
| private priceFetcher: PriceFetcher; | private priceFetcher: PriceFetcher; | ||||
| private sourceCurrencies: CryptoTicker[]; | private sourceCurrencies: CryptoTicker[]; | ||||
| constructor(priceFetcher: PriceFetcher, sourceCurrencies: CryptoTicker[]) { | constructor(priceFetcher: PriceFetcher, sourceCurrencies: CryptoTicker[]) { | ||||
| this.priceFetcher = priceFetcher; | this.priceFetcher = priceFetcher; | ||||
| this.sourceCurrencies = sourceCurrencies; | this.sourceCurrencies = sourceCurrencies; | ||||
| } | } | ||||
| /** | /** | ||||
| * Keep the ecash-price PriceFetcher.current() API but always refresh all | * Keep the ecash-price PriceFetcher.current() API but always refresh all | ||||
| * the prices for all the source currencies. | * the prices for all the source currencies. | ||||
| * | * | ||||
| * We assume the user won't switch fiat often but might switch source often. | * We assume the user won't switch fiat often but might switch source often. | ||||
| * In order to improve the experience this wrapper keeps the cache hot for | * In order to improve the experience this wrapper keeps the cache hot for | ||||
| * all the source currencies and thus avoid reaching rate limits as much as | * all the source currencies and thus avoid reaching rate limits as much as | ||||
| * possible. | * possible. | ||||
| */ | */ | ||||
| async current(pair: PricePair): Promise<number | null> { | async current(pair: PricePair): Promise<number | null> { | ||||
| // Special case for Firma: this is a stablecoin pegged to USD, so we | |||||
| // can simply return 1.0 for the price if the user is using USD as the | |||||
| // quote currency. | |||||
| if ( | |||||
| activeAssetTicker() === 'FIRMA' && | |||||
| pair.source.toString() === activeQuoteCurrency().toString() && | |||||
| pair.quote.toString() === Fiat.USD.toString() | |||||
| ) { | |||||
| return 1.0; | |||||
| } | |||||
| await this.priceFetcher.currentPairs( | await this.priceFetcher.currentPairs( | ||||
| this.sourceCurrencies.map(source => ({ | this.sourceCurrencies.map(source => ({ | ||||
| source, | source, | ||||
| quote: pair.quote, | quote: pair.quote, | ||||
| })), | })), | ||||
| ); | ); | ||||
| return await this.priceFetcher.current(pair); | return await this.priceFetcher.current(pair); | ||||
| } | } | ||||
| } | } | ||||