diff --git a/src/services/swap.ts b/src/services/swap.ts index b38a783d8..d8278ca30 100644 --- a/src/services/swap.ts +++ b/src/services/swap.ts @@ -48,7 +48,7 @@ import { StablePool, } from './pool'; -import { cacheAllDCLPools } from './swapV3'; +import { cacheAllDCLPools, checkCacheDCLPools } from './swapV3'; import { createSmartRouteLogicWorker, transformWorkerResult, @@ -364,7 +364,8 @@ export const estimateSwap = async ({ let poolsMap = {}; let tokensMap = {}; try { - if (!localStorage.getItem(REF_DCL_POOL_CACHE_KEY)) { + const isValidCached = checkCacheDCLPools(); + if (!isValidCached) { await cacheAllDCLPools(); } } catch (error) {} diff --git a/src/services/swapV3.ts b/src/services/swapV3.ts index 6c5367252..c336f7dfd 100644 --- a/src/services/swapV3.ts +++ b/src/services/swapV3.ts @@ -1,5 +1,5 @@ import { TokenMetadata, ftGetStorageBalance } from './ft-contract'; - +import moment from 'moment'; import { utils } from 'near-api-js'; import { @@ -1623,8 +1623,23 @@ export const listPools = async () => { export const cacheAllDCLPools = async () => { const pools = await listPools(); - - localStorage.setItem(REF_DCL_POOL_CACHE_KEY, JSON.stringify(pools)); + const pools_latest = pools.map((p) => { + return { + ...p, + update_time: moment().unix(), + }; + }); + localStorage.setItem(REF_DCL_POOL_CACHE_KEY, JSON.stringify(pools_latest)); +}; +export const checkCacheDCLPools = () => { + const cachedPools = localStorage.getItem(REF_DCL_POOL_CACHE_KEY); + if (cachedPools) { + const pools = JSON.parse(cachedPools); + return ( + Number(pools[0]?.update_time || 0) > Number(moment().unix() - Number(60)) + ); + } + return false; }; export interface UserStorageDetail { diff --git a/src/state/swap.tsx b/src/state/swap.tsx index dce42419d..05dac6c3e 100644 --- a/src/state/swap.tsx +++ b/src/state/swap.tsx @@ -48,6 +48,7 @@ import { } from '../services/swap'; import { EstimateSwapView, swap, swapFromServer } from '../services/swap'; import { + cacheAllDCLPools, get_pool, PoolInfo, quote, @@ -810,6 +811,9 @@ export const useSwapV3 = ({ const bestFee = bestEstimate?.tag?.split('|')?.[1] ? Number(bestEstimate?.tag?.split('|')?.[1]) : null; + useEffect(() => { + cacheAllDCLPools(); + }, []); useEffect(() => { if (!bestFee || wrapOperation) return;