Skip to content

Commit

Permalink
chore: fiat store updateExchange accept fiat currency as input
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Jun 17, 2024
1 parent b62b14d commit b587054
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
20 changes: 15 additions & 5 deletions src/stores/Fiat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ({
Expand All @@ -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<FiatCurrencyOffered> = [...new Set<FiatCurrencyOffered>([
// 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
Expand All @@ -116,7 +126,7 @@ export const useFiatStore = createStore({
])];
const currenciesToUpdate: Array<FiatCurrencyOffered> = [];
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
Expand Down

0 comments on commit b587054

Please sign in to comment.