Skip to content

Commit

Permalink
Hotfix price impact (#568)
Browse files Browse the repository at this point in the history
* add update dcl pools logic

* update
  • Loading branch information
xieqiancaosissi authored Jul 18, 2024
1 parent 295c4ba commit c2a6c1f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/services/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
StablePool,
} from './pool';

import { cacheAllDCLPools } from './swapV3';
import { cacheAllDCLPools, checkCacheDCLPools } from './swapV3';
import {
createSmartRouteLogicWorker,
transformWorkerResult,
Expand Down Expand Up @@ -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) {}
Expand Down
21 changes: 18 additions & 3 deletions src/services/swapV3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TokenMetadata, ftGetStorageBalance } from './ft-contract';

import moment from 'moment';
import { utils } from 'near-api-js';

import {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/state/swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
} from '../services/swap';
import { EstimateSwapView, swap, swapFromServer } from '../services/swap';
import {
cacheAllDCLPools,
get_pool,
PoolInfo,
quote,
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit c2a6c1f

Please sign in to comment.