Skip to content

Commit b587054

Browse files
committed
chore: fiat store updateExchange accept fiat currency as input
1 parent b62b14d commit b587054

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async function start() {
6060
function queueExchangeRateUpdate() {
6161
setTimeout(async () => {
6262
exchangeRateUpdateStart = Date.now(); // in contrast to fiatStore.timestamp set before the update
63-
await updateExchangeRates(/* failGracefully */ true); // silently ignores errors
63+
await updateExchangeRates({ failGracefully: true }); // silently ignores errors
6464
queueExchangeRateUpdate();
6565
// Also add 2 minutes as upper bound to be immune to the user's system clock being wrong.
6666
}, Math.max(0, Math.min(exchangeRateUpdateStart + TWO_MINUTES - Date.now(), TWO_MINUTES)));

src/stores/Fiat.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ export function guessUserCurrency(regionOverwrite?: string) {
6666
return FiatCurrency.USD;
6767
}
6868

69+
export type UpdateExchangeRatesOptions = {
70+
// The currency to load
71+
// @default this.state.currency
72+
fiatCurrency?: FiatCurrencyOffered,
73+
74+
// If true, the function will not throw an error if the exchange rate update fails.
75+
// @default true
76+
failGracefully?: boolean,
77+
}
78+
6979
export const useFiatStore = createStore({
7080
id: 'fiat',
7181
state: (): FiatState => ({
@@ -86,21 +96,21 @@ export const useFiatStore = createStore({
8696
useBtcTransactionsStore().calculateFiatAmounts();
8797
useUsdcTransactionsStore().calculateFiatAmounts();
8898
},
89-
async updateExchangeRates(failGracefully = true) {
99+
async updateExchangeRates(options: UpdateExchangeRatesOptions = {}) {
100+
const { failGracefully = true, fiatCurrency = this.state.currency } = options;
90101
try {
91-
const currentCurrency = this.state.currency;
92102
// If a currency is bridged, but not by a bridge that supports historic rates, it's bridged via CPL Api.
93103
const isCplBridgedFiatCurrency = (currency: FiatCurrency) => isBridgedFiatCurrency(
94104
currency,
95105
FIAT_API_PROVIDER_CURRENT_PRICES,
96106
RateType.CURRENT,
97107
) && !isHistorySupportedFiatCurrency(currency, FIAT_API_PROVIDER_CURRENT_PRICES);
98-
const isCurrentCurrencyCplBridged = isCplBridgedFiatCurrency(currentCurrency);
108+
const isCurrentCurrencyCplBridged = isCplBridgedFiatCurrency(fiatCurrency);
99109
const prioritizedFiatCurrenciesOffered: Array<FiatCurrencyOffered> = [...new Set<FiatCurrencyOffered>([
100110
// As we limit the currencies we fetch for CryptoCompare to 25, prioritize a few currencies, we'd
101111
// prefer to be included, roughly the highest market cap currencies according to fiatmarketcap.com,
102112
// plus some smaller currencies for which Nimiq has strong communities.
103-
currentCurrency,
113+
fiatCurrency,
104114
...(['USD', 'CNY', 'EUR', 'JPY', 'GBP', 'KRW', 'INR', 'CAD', 'HKD', 'BRL', 'AUD', 'CRC', 'GMD',
105115
'XOF'] as const).map((ticker) => FiatCurrency[ticker]),
106116
// After that, prefer to include currencies CryptoCompare supports historic rates for, because it is
@@ -116,7 +126,7 @@ export const useFiatStore = createStore({
116126
])];
117127
const currenciesToUpdate: Array<FiatCurrencyOffered> = [];
118128
for (const currency of prioritizedFiatCurrenciesOffered) {
119-
if (currency !== currentCurrency
129+
if (currency !== fiatCurrency
120130
// Include all provider supported currencies, as at least one always has to be fetched via the
121131
// provider api, either because it's a directly supported currency, or because it's a currency
122132
// bridged via USD, also fetched from the provider, and fetching multiple currencies vs. only

0 commit comments

Comments
 (0)