@@ -10,7 +10,7 @@ import type { Browser, Runtime } from 'webextension-polyfill';
1010import { BACKGROUND_TO_POPUP_CONNECTION_NAME } from '@/shared/messages' ;
1111import { EXCHANGE_RATES_URL } from './config' ;
1212import { INTERNAL_PAGE_URL_PROTOCOLS , NEW_TAB_PAGES } from './constants' ;
13- import { notNullOrUndef } from '@/shared/helpers' ;
13+ import { memoize , notNullOrUndef } from '@/shared/helpers' ;
1414import type { WalletAddress } from '@interledger/open-payments' ;
1515
1616type OnConnectCallback = Parameters <
@@ -90,6 +90,11 @@ export const getExchangeRates = async (): Promise<ExchangeRates> => {
9090 return rates ;
9191} ;
9292
93+ export const getExchangeRatesMemoized = memoize ( getExchangeRates , {
94+ maxAge : 15 * 60 * 1000 ,
95+ mechanism : 'stale-while-revalidate' ,
96+ } ) ;
97+
9398export const getExchangeRate = (
9499 rates : ExchangeRates ,
95100 forAssetCode : string ,
@@ -121,7 +126,7 @@ export const convertWithExchangeRate = <T extends AmountValue | bigint>(
121126 const scaleDiff = from . assetScale - to . assetScale ;
122127 const scaledExchangeRate = exchangeRate * 10 ** scaleDiff ;
123128
124- const converted = BigInt ( Math . ceil ( Number ( amount ) / scaledExchangeRate ) ) ;
129+ const converted = BigInt ( Math . round ( Number ( amount ) / scaledExchangeRate ) ) ;
125130
126131 return typeof amount === 'string'
127132 ? ( converted . toString ( ) as T )
0 commit comments