/ src / components / transactions / Bridge / useGetBridgeLimits.tsx
useGetBridgeLimits.tsx
 1  import { useQuery } from '@tanstack/react-query';
 2  import { BigNumber, Contract } from 'ethers';
 3  import { getProvider } from 'src/utils/marketsAndNetworksConfig';
 4  
 5  import { getChainSelectorFor, laneConfig } from './BridgeConfig';
 6  // NOTE: lightweight ABI
 7  import TokenPoolAbi from './Tokenpool-abi.json';
 8  
 9  export const useGetBridgeLimit = (sourceChainId: number) => {
10    return useQuery({
11      queryFn: async () => {
12        const sourceLaneConfig = laneConfig.find((config) => config.sourceChainId === sourceChainId);
13        if (!sourceLaneConfig) {
14          throw Error('No sourceLaneConfig found');
15        }
16  
17        const provider = getProvider(sourceChainId);
18        const tokenPoolAddress = sourceLaneConfig.lockReleaseTokenPool;
19  
20        if (!tokenPoolAddress) {
21          // only applies to the lock release token pool on Ethereum
22          return {
23            bridgeLimit: '-1',
24            currentBridgedAmount: '-1',
25            remainingAmount: '-1',
26          };
27        }
28  
29        const tokenPool = new Contract(tokenPoolAddress, TokenPoolAbi, provider);
30  
31        const [bridgeLimit, currentBridgedAmount]: [BigNumber, BigNumber] = await Promise.all([
32          tokenPool.getBridgeLimit(),
33          tokenPool.getCurrentBridgedAmount(),
34        ]);
35  
36        return {
37          bridgeLimit: bridgeLimit.toString(),
38          currentBridgedAmount: currentBridgedAmount.toString(),
39          remainingAmount: bridgeLimit.sub(currentBridgedAmount).toString(),
40        };
41      },
42      queryKey: ['getBridgeLimit', sourceChainId],
43      // always fetch data to get most recent bridged amounts
44      gcTime: 0,
45      staleTime: 0,
46    });
47  };
48  
49  interface RateLimitProps {
50    destinationChainId: number;
51    sourceChainId: number;
52  }
53  
54  export const useGetRateLimit = ({ destinationChainId, sourceChainId }: RateLimitProps) => {
55    return useQuery({
56      queryFn: async () => {
57        const sourceLaneConfig = laneConfig.find((config) => config.sourceChainId === sourceChainId);
58        if (!sourceLaneConfig) {
59          throw Error('No sourceLaneConfig found');
60        }
61  
62        const tokenPoolAddress =
63          sourceLaneConfig.lockReleaseTokenPool ?? sourceLaneConfig.burnMintTokenPool;
64  
65        if (!tokenPoolAddress) {
66          return {
67            tokens: '0',
68            capacity: '0',
69            rate: '0',
70          };
71        }
72  
73        const provider = getProvider(sourceChainId);
74        const tokenPool = new Contract(tokenPoolAddress, TokenPoolAbi, provider);
75        const destinationChainSelector = getChainSelectorFor(destinationChainId);
76  
77        const [tokens, , isEnabled, capacity, rate]: [
78          BigNumber,
79          number,
80          boolean,
81          BigNumber,
82          BigNumber
83        ] = await tokenPool.getCurrentOutboundRateLimiterState(destinationChainSelector);
84  
85        return {
86          tokens: isEnabled ? tokens.toString() : '0',
87          capacity: capacity.toString(),
88          rate: rate.toString(),
89        };
90      },
91      queryKey: ['getRateLimit', destinationChainId, sourceChainId],
92      staleTime: 0,
93      refetchInterval: 5000, // fetch ever 5 seconds to get the latest rate limits
94    });
95  };