Skip to content

Commit c8e6c93

Browse files
authored
refactor(bg/utils): keep single helper for getExchangeRates (#1090)
1 parent e638eae commit c8e6c93

2 files changed

Lines changed: 19 additions & 21 deletions

File tree

src/background/services/paymentSession.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
bigIntMax,
2020
convert,
2121
convertWithExchangeRate,
22-
getExchangeRatesMemoized,
22+
getExchangeRates,
2323
getNextSendableAmount,
2424
} from '@/background/utils';
2525
import type {
@@ -129,7 +129,7 @@ export class PaymentSession {
129129

130130
if (isCrossCurrency) {
131131
try {
132-
const exchangeRates = await getExchangeRatesMemoized();
132+
const exchangeRates = await getExchangeRates();
133133
amountToSend = convertWithExchangeRate(
134134
amountToSend,
135135
this.receiver,

src/background/utils.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,23 @@ interface ExchangeRates {
7575
rates: Record<string, number>;
7676
}
7777

78-
export const getExchangeRates = async (): Promise<ExchangeRates> => {
79-
const response = await fetch(EXCHANGE_RATES_URL);
80-
if (!response.ok) {
81-
throw new Error(
82-
`Could not fetch exchange rates. [Status code: ${response.status}]`,
83-
);
84-
}
85-
const rates = await response.json();
86-
if (!rates.base || !rates.rates) {
87-
throw new Error('Invalid rates format');
88-
}
89-
90-
return rates;
91-
};
92-
93-
export const getExchangeRatesMemoized = memoize(getExchangeRates, {
94-
maxAge: 15 * 60 * 1000,
95-
mechanism: 'stale-while-revalidate',
96-
});
78+
export const getExchangeRates = memoize(
79+
async (): Promise<ExchangeRates> => {
80+
const response = await fetch(EXCHANGE_RATES_URL);
81+
if (!response.ok) {
82+
throw new Error(
83+
`Could not fetch exchange rates. [Status code: ${response.status}]`,
84+
);
85+
}
86+
const rates = await response.json();
87+
if (!rates.base || !rates.rates) {
88+
throw new Error('Invalid rates format');
89+
}
90+
91+
return rates;
92+
},
93+
{ maxAge: 15 * 60 * 1000, mechanism: 'stale-while-revalidate' },
94+
);
9795

9896
export const getExchangeRate = (
9997
rates: ExchangeRates,

0 commit comments

Comments
 (0)