Skip to content

Commit

Permalink
fix: update local tokens cache to keep priceUSD in sync (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov authored Mar 1, 2023
1 parent 66c52c0 commit d3f4d87
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const useChainSelect = (formType: SwapFormType) => {
}
const selectedChains = chainOrder
.map((chainId) => chains.find((chain) => chain.id === chainId))
.filter((chain) => chain) as EVMChain[];
.filter(Boolean) as EVMChain[];

return selectedChains;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/src/hooks/useRouteExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const useRouteExecution = ({
if (!routeExecution?.route) {
throw Error('Execution route not found.');
}
queryClient.removeQueries(['routes']);
queryClient.removeQueries(['routes'], { exact: false });
return lifi.executeRoute(account.signer, routeExecution.route, {
updateCallback,
switchChainHook,
Expand Down
28 changes: 26 additions & 2 deletions packages/widget/src/hooks/useSwapRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAddress } from '@ethersproject/address';
import type { Route } from '@lifi/sdk';
import type { Route, RoutesResponse, Token } from '@lifi/sdk';
import { LifiErrorCode } from '@lifi/sdk';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import Big from 'big.js';
Expand Down Expand Up @@ -176,7 +176,7 @@ export const useSwapRoutes = () => {
steps: [contractCallQuote],
};

return { routes: [route] };
return { routes: [route] } as RoutesResponse;
}
return lifi.getRoutes(
{
Expand Down Expand Up @@ -213,6 +213,30 @@ export const useSwapRoutes = () => {
}
return true;
},
onSuccess(data) {
if (data.routes[0]) {
// Update local tokens cache to keep priceUSD in sync
const { fromToken, toToken } = data.routes[0];
[fromToken, toToken].forEach((token) => {
queryClient.setQueriesData<Token[]>(
['token-balances', account.address, token.chainId],
(data) => {
if (data) {
const clonedData = [...data];
const index = clonedData.findIndex(
(dataToken) => dataToken.address === token.address,
);
clonedData[index] = {
...clonedData[index],
...token,
};
return clonedData;
}
},
);
});
}
},
},
);

Expand Down
1 change: 1 addition & 0 deletions packages/widget/src/hooks/useTokenAddressBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const useTokenAddressBalance = (
useTokenBalances(chainId);

const token = useMemo(() => {
console.log('useTokenAddressBalance');
const token = (tokensWithBalance ?? tokens)?.find(
(token) => token.address === tokenAddress && token.chainId === chainId,
);
Expand Down
20 changes: 10 additions & 10 deletions packages/widget/src/hooks/useTokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,20 @@ export const useTokenBalance = (token?: Token, accountAddress?: string) => {
});
}

queryClient.setQueryData<TokenAmount[]>(
queryClient.setQueriesData<TokenAmount[]>(
['token-balances', accountAddress, token!.chainId],
(data) => {
if (data) {
const index = data.findIndex(
const clonedData = [...data];
const index = clonedData.findIndex(
(dataToken) => dataToken.address === token!.address,
);
data[index] = {
...data[index],
clonedData[index] = {
...clonedData[index],
amount: formattedAmount,
};
return clonedData;
}
return data;
},
);

Expand All @@ -112,11 +113,10 @@ export const useTokenBalance = (token?: Token, accountAddress?: string) => {
);

const refetchAllBalances = () => {
queryClient.refetchQueries([
'token-balances',
token?.chainId,
accountAddress,
]);
queryClient.refetchQueries(
['token-balances', accountAddress, token?.chainId],
{ exact: false },
);
};

const refetchNewBalance = useCallback(() => {
Expand Down
26 changes: 12 additions & 14 deletions packages/widget/src/hooks/useTokenSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ export const useTokenSearch = (
},
);
if (token) {
queryClient.setQueriesData(
['tokens'],
(data?: TokensResponse) => {
if (
!data?.tokens[chainId as number].some(
(t) => t.address === token.address,
)
) {
data?.tokens[chainId as number].push(token as Token);
}
return data;
},
{ updatedAt: Date.now() },
);
queryClient.setQueriesData<TokensResponse>(['tokens'], (data) => {
if (
data &&
!data.tokens[chainId as number].some(
(t) => t.address === token.address,
)
) {
const clonedData = { ...data };
clonedData.tokens[chainId as number].push(token as Token);
return clonedData;
}
});
}
return token as Token;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const EnabledToolsButton: React.FC<{
const navigate = useNavigate();
const [enabledTools, tools] = useSettingsStore((state) => {
const enabledTools = Object.values(state[`_enabled${type}`] ?? {});
return [enabledTools.filter((tool) => tool).length, enabledTools.length];
return [enabledTools.filter(Boolean).length, enabledTools.length];
}, shallow);

const handleClick = () => {
Expand Down

0 comments on commit d3f4d87

Please sign in to comment.