Skip to content

Commit

Permalink
Merge pull request #460 from lidofinance/fix/wc-rate-limit
Browse files Browse the repository at this point in the history
fix: higher block fetch interval
  • Loading branch information
itaven authored Sep 9, 2024
2 parents 5f0000c + 491b420 commit 768123d
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 85 deletions.
2 changes: 1 addition & 1 deletion config/groups/web3.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parseEther } from '@ethersproject/units';

// interval in ms for RPC event polling for token balance and tx updates
export const PROVIDER_POLLING_INTERVAL = 7_000;
export const PROVIDER_POLLING_INTERVAL = 12_000;
// how long in ms to wait for RPC batching(multicall and provider)
export const PROVIDER_BATCH_TIME = 150;

Expand Down
8 changes: 7 additions & 1 deletion features/withdrawals/hooks/contract/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export const useWithdrawalRequest = ({
needsApprove,
allowance,
isLoading: loadingUseApprove,
refetch: refetchAllowance,
} = useApprove(
valueBN,
tokenContract.address,
Expand Down Expand Up @@ -321,7 +322,11 @@ export const useWithdrawalRequest = ({
);
}

await onConfirm?.();
await Promise.all([
onConfirm?.(),
isApprovalFlow &&
refetchAllowance({ throwOnError: false, cancelRefetch: false }),
]);
txModalStages.success(amount, token, txHash);
return true;
} catch (error) {
Expand All @@ -341,6 +346,7 @@ export const useWithdrawalRequest = ({
needsApprove,
onConfirm,
onRetry,
refetchAllowance,
txModalStages,
waitForTx,
],
Expand Down
3 changes: 3 additions & 0 deletions features/wsteth/wrap/hooks/use-wrap-tx-approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const useWrapTxApprove = ({ amount, token }: UseWrapTxApproveArgs) => {
needsApprove,
allowance,
isLoading: isApprovalLoading,
refetch: refetchAllowance,
} = useApprove(
amount,
stethTokenAddress,
Expand All @@ -49,13 +50,15 @@ export const useWrapTxApprove = ({ amount, token }: UseWrapTxApproveArgs) => {
allowance,
isApprovalLoading,
isApprovalNeededBeforeWrap,
refetchAllowance,
}),
[
allowance,
isApprovalNeededBeforeWrap,
needsApprove,
isApprovalLoading,
processApproveTx,
refetchAllowance,
],
);
};
10 changes: 9 additions & 1 deletion features/wsteth/wrap/wrap-form-context/wrap-form-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useMemo,
createContext,
useContext,
useCallback,
} from 'react';
import { useForm, FormProvider } from 'react-hook-form';
import { useWrapTxApprove } from '../hooks/use-wrap-tx-approve';
Expand Down Expand Up @@ -72,9 +73,16 @@ export const WrapFormProvider: FC<PropsWithChildren> = ({ children }) => {
const approvalData = useWrapTxApprove({ amount: amount ?? Zero, token });
const isSteth = token === TOKENS_TO_WRAP.STETH;

const onConfirm = useCallback(async () => {
await Promise.allSettled([
networkData.revalidateWrapFormData(),
approvalData.refetchAllowance(),
]);
}, [networkData, approvalData]);

const processWrapFormFlow = useWrapFormProcessor({
approvalData,
onConfirm: networkData.revalidateWrapFormData,
onConfirm,
onRetry: retryFire,
});

Expand Down
8 changes: 7 additions & 1 deletion providers/web3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ type ChainsList = [wagmiChains.Chain, ...wagmiChains.Chain[]];

const wagmiChainsArray = Object.values(wagmiChains) as any as ChainsList;

const queryClient = new QueryClient();
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
},
},
});

const Web3Provider: FC<PropsWithChildren> = ({ children }) => {
const {
Expand Down
29 changes: 20 additions & 9 deletions shared/hooks/use-allowance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useQueryClient } from '@tanstack/react-query';
import { BigNumber } from 'ethers';
import { useCallback, useMemo } from 'react';
import { Address } from 'viem';
import { Address, WatchContractEventOnLogsFn } from 'viem';
import { useReadContract, useWatchContractEvent } from 'wagmi';

const nativeToBN = (data: bigint) => BigNumber.from(data.toString());
Expand Down Expand Up @@ -50,6 +50,12 @@ const Erc20AllowanceAbi = [
},
] as const;

type OnLogsFn = WatchContractEventOnLogsFn<
typeof Erc20AllowanceAbi,
'Transfer' | 'Approval',
true
>;

type UseAllowanceProps = {
token: Address;
account: Address;
Expand All @@ -72,14 +78,19 @@ export const useAllowance = ({
query: { enabled, select: nativeToBN },
});

const onLogs = useCallback(() => {
void queryClient.invalidateQueries(
{
queryKey: allowanceQuery.queryKey,
},
{ cancelRefetch: false },
);
}, [allowanceQuery.queryKey, queryClient]);
const onLogs: OnLogsFn = useCallback(
() => {
void queryClient.invalidateQueries(
{
queryKey: allowanceQuery.queryKey,
},
{ cancelRefetch: false },
);
},
// queryKey is unstable
// eslint-disable-next-line react-hooks/exhaustive-deps
[account, spender, token],
);

useWatchContractEvent({
abi: Erc20AllowanceAbi,
Expand Down
22 changes: 19 additions & 3 deletions shared/hooks/use-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import type { AbstractLidoSDKErc20 } from '@lidofinance/lido-ethereum-sdk/erc20';
import type { GetBalanceData } from 'wagmi/query';
import type { Address, WatchContractEventOnLogsFn } from 'viem';
import { config } from 'config';

const nativeToBN = (data: bigint) => BigNumber.from(data.toString());

Expand All @@ -24,10 +25,23 @@ const balanceToBN = (data: GetBalanceData) => nativeToBN(data.value);
export const useEthereumBalance = () => {
const queryClient = useQueryClient();
const { address } = useAccount();
const { data: blockNumber } = useBlockNumber({ watch: !!address });
const { data: blockNumber } = useBlockNumber({
watch: {
poll: true,
pollingInterval: config.PROVIDER_POLLING_INTERVAL,
enabled: !!address,
},
cacheTime: config.PROVIDER_POLLING_INTERVAL,
});

const queryData = useBalance({
address,
query: { select: balanceToBN, staleTime: 7000, enabled: !!address },
query: {
select: balanceToBN,
// because we subscribe to block
staleTime: Infinity,
enabled: !!address,
},
});

useEffect(() => {
Expand All @@ -37,7 +51,9 @@ export const useEthereumBalance = () => {
// dedups rpc requests
{ cancelRefetch: false },
);
}, [blockNumber, queryClient, queryData.queryKey]);

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [blockNumber]);

return queryData;
};
Expand Down
69 changes: 0 additions & 69 deletions test/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,7 @@ export interface PostRequest {
schema: object;
}

const FLOAT_REGEX = /^\d+(\.\d+)?$/;

export const GET_REQUESTS: GetRequest[] = [
{
uri: '/api/oneinch-rate?token=ETH',
isDeprecated: true,
schema: {
type: 'object',
properties: {
rate: { type: 'number', min: 0 },
toReceive: { type: 'string' },
fromAmount: { type: 'string' },
},
required: ['rate', 'toReceive', 'fromAmount'],
additionalProperties: false,
},
},
{
uri: `/api/short-lido-stats?chainId=${CONFIG.STAND_CONFIG.chainId}`,
isDeprecated: true,
schema: {
type: 'object',
properties: {
uniqueAnytimeHolders: { type: 'string' },
uniqueHolders: { type: 'string' },
totalStaked: { type: 'string' },
marketCap: { type: 'number' },
},
required: [
'totalStaked',
'marketCap',
'uniqueAnytimeHolders',
'uniqueHolders',
],
additionalProperties: true,
},
},
{
uri: '/api/eth-apr',
isDeprecated: true,
schema: { type: 'string', pattern: FLOAT_REGEX },
},
{
uri: '/api/totalsupply',
isDeprecated: true,
schema: { type: 'string', pattern: FLOAT_REGEX },
},
{
uri: '/api/eth-price',
isDeprecated: true,
schema: {
type: 'object',
properties: {
price: {
type: 'number',
min: 0,
},
},
required: ['price'],
additionalProperties: true,
},
},
{
uri: '/api/rewards?address=0x87c0e047F4e4D3e289A56a36570D4CB957A37Ef1&currency=usd&onlyRewards=false&archiveRate=true&skip=0&limit=10',
skipTestnet: true, // api/rewards don't work on testnet
Expand Down Expand Up @@ -121,14 +60,6 @@ export const GET_REQUESTS: GetRequest[] = [
},
},
},
{
uri: '/api/sma-steth-apr',
isDeprecated: true,
schema: {
type: 'string',
pattern: FLOAT_REGEX,
},
},
];

export const POST_REQUESTS: PostRequest[] = [
Expand Down

0 comments on commit 768123d

Please sign in to comment.