-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathweb3.tsx
131 lines (114 loc) · 3.91 KB
/
web3.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {
FC,
PropsWithChildren,
createContext,
useContext,
useMemo,
useState,
} from 'react';
import { WalletsModalForEth } from 'reef-knot/connect-wallet-modal';
import { dynamics } from 'config';
import { WalletsListEthereum } from 'reef-knot/wallets';
import {
AutoConnect,
getWalletsDataList,
ReefKnot,
} from 'reef-knot/core-react';
import { createConfig, http, WagmiProvider } from 'wagmi';
import * as wagmiChains from 'wagmi/chains';
import invariant from 'tiny-invariant';
import { CHAINS } from '@lidofinance/lido-ethereum-sdk';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { useThemeToggle } from '@lidofinance/lido-ui';
type ChainsList = [wagmiChains.Chain, ...wagmiChains.Chain[]];
export const L2_CHAINS = [10, 11155420, 1946, 1301];
const wagmiChainsArray = Object.values(wagmiChains) as any as ChainsList;
const supportedChains = wagmiChainsArray.filter((chain) =>
dynamics.supportedChains.includes(chain.id),
) as ChainsList;
const defaultChain =
wagmiChainsArray.find((chain) => chain.id === dynamics.defaultChain) ||
supportedChains[0]; // first supported chain as fallback;
const queryClient = new QueryClient();
type CustomRpcContextValue = {
customRpc: Record<number, string | null>;
activeRpc: Record<number, string>;
setCustomRpcUrl: (chain: number, rpcUrl: string | null) => void;
};
const CustomRpcContext = createContext<CustomRpcContextValue | null>(null);
CustomRpcContext.displayName = 'CustomRpcContext';
export const useCustomRpc = () => {
const context = useContext(CustomRpcContext);
invariant(context);
return context;
};
const Web3Provider: FC<PropsWithChildren> = ({ children }) => {
const { themeName } = useThemeToggle();
const [customRpc, setCustomRpc] = useState<Record<number, string | null>>({});
const activeRpc: Record<number, string> = useMemo(() => {
const getRpc = (chain: number): string =>
customRpc[chain] ?? dynamics.rpcProviderUrls[chain];
return {
[CHAINS.Mainnet]: getRpc(CHAINS.Mainnet),
[CHAINS.Holesky]: getRpc(CHAINS.Holesky),
[CHAINS.Sepolia]: getRpc(CHAINS.Sepolia),
// OP sepolia
[CHAINS.OptimismSepolia]: getRpc(CHAINS.OptimismSepolia),
// Soneium Minato
[CHAINS.SoneiumMinato]: getRpc(CHAINS.SoneiumMinato),
// Unichain Sepolia
[CHAINS.UnichainSepolia]: getRpc(CHAINS.UnichainSepolia),
};
}, [customRpc]);
const customRpcContextValue = useMemo(
() => ({
customRpc,
activeRpc,
setCustomRpcUrl: (chain: number, rpcUrl: string | null) => {
setCustomRpc((old) => ({ ...old, [chain]: rpcUrl }));
},
}),
[customRpc, activeRpc],
);
const { walletsDataList } = useMemo(() => {
return getWalletsDataList({
walletsList: WalletsListEthereum,
rpc: activeRpc,
walletconnectProjectId: dynamics.walletconnectProjectId,
defaultChain: defaultChain,
});
}, [activeRpc]);
const config = useMemo(() => {
return createConfig({
chains: supportedChains,
ssr: true,
multiInjectedProviderDiscovery: false,
transports: supportedChains.reduce(
(res, curr) => ({
...res,
[curr.id]: http(activeRpc[curr.id], { batch: true }),
}),
{},
),
});
}, [activeRpc]);
return (
<CustomRpcContext.Provider value={customRpcContextValue}>
<WagmiProvider config={config} reconnectOnMount={false}>
<QueryClientProvider client={queryClient}>
<ReefKnot
rpc={activeRpc}
chains={supportedChains}
walletDataList={walletsDataList}
>
<AutoConnect autoConnect />
{children}
<WalletsModalForEth shouldInvertWalletIcon={themeName === 'dark'} />
</ReefKnot>
</QueryClientProvider>
</WagmiProvider>
</CustomRpcContext.Provider>
);
};
export default Web3Provider;