From b587054efcce471a2f3ad8ad771ea4a456c3588a Mon Sep 17 00:00:00 2001 From: Maxi Date: Mon, 17 Jun 2024 12:34:29 +0300 Subject: [PATCH] chore: fiat store updateExchange accept fiat currency as input --- src/main.ts | 2 +- src/stores/Fiat.ts | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main.ts b/src/main.ts index 41642d47d..109fe6ff8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -60,7 +60,7 @@ async function start() { function queueExchangeRateUpdate() { setTimeout(async () => { exchangeRateUpdateStart = Date.now(); // in contrast to fiatStore.timestamp set before the update - await updateExchangeRates(/* failGracefully */ true); // silently ignores errors + await updateExchangeRates({ failGracefully: true }); // silently ignores errors queueExchangeRateUpdate(); // Also add 2 minutes as upper bound to be immune to the user's system clock being wrong. }, Math.max(0, Math.min(exchangeRateUpdateStart + TWO_MINUTES - Date.now(), TWO_MINUTES))); diff --git a/src/stores/Fiat.ts b/src/stores/Fiat.ts index f02a9ff3c..c01822643 100644 --- a/src/stores/Fiat.ts +++ b/src/stores/Fiat.ts @@ -66,6 +66,16 @@ export function guessUserCurrency(regionOverwrite?: string) { return FiatCurrency.USD; } +export type UpdateExchangeRatesOptions = { + // The currency to load + // @default this.state.currency + fiatCurrency?: FiatCurrencyOffered, + + // If true, the function will not throw an error if the exchange rate update fails. + // @default true + failGracefully?: boolean, +} + export const useFiatStore = createStore({ id: 'fiat', state: (): FiatState => ({ @@ -86,21 +96,21 @@ export const useFiatStore = createStore({ useBtcTransactionsStore().calculateFiatAmounts(); useUsdcTransactionsStore().calculateFiatAmounts(); }, - async updateExchangeRates(failGracefully = true) { + async updateExchangeRates(options: UpdateExchangeRatesOptions = {}) { + const { failGracefully = true, fiatCurrency = this.state.currency } = options; try { - const currentCurrency = this.state.currency; // If a currency is bridged, but not by a bridge that supports historic rates, it's bridged via CPL Api. const isCplBridgedFiatCurrency = (currency: FiatCurrency) => isBridgedFiatCurrency( currency, FIAT_API_PROVIDER_CURRENT_PRICES, RateType.CURRENT, ) && !isHistorySupportedFiatCurrency(currency, FIAT_API_PROVIDER_CURRENT_PRICES); - const isCurrentCurrencyCplBridged = isCplBridgedFiatCurrency(currentCurrency); + const isCurrentCurrencyCplBridged = isCplBridgedFiatCurrency(fiatCurrency); const prioritizedFiatCurrenciesOffered: Array = [...new Set([ // As we limit the currencies we fetch for CryptoCompare to 25, prioritize a few currencies, we'd // prefer to be included, roughly the highest market cap currencies according to fiatmarketcap.com, // plus some smaller currencies for which Nimiq has strong communities. - currentCurrency, + fiatCurrency, ...(['USD', 'CNY', 'EUR', 'JPY', 'GBP', 'KRW', 'INR', 'CAD', 'HKD', 'BRL', 'AUD', 'CRC', 'GMD', 'XOF'] as const).map((ticker) => FiatCurrency[ticker]), // After that, prefer to include currencies CryptoCompare supports historic rates for, because it is @@ -116,7 +126,7 @@ export const useFiatStore = createStore({ ])]; const currenciesToUpdate: Array = []; for (const currency of prioritizedFiatCurrenciesOffered) { - if (currency !== currentCurrency + if (currency !== fiatCurrency // Include all provider supported currencies, as at least one always has to be fetched via the // provider api, either because it's a directly supported currency, or because it's a currency // bridged via USD, also fetched from the provider, and fetching multiple currencies vs. only