Commit 87389a4e authored by tom goriunov's avatar tom goriunov Committed by GitHub

Migrate from `web3modal` to `reown` (#2321)

* migrate from `web3modal` to `reown`

Fixes #2316

* bump up package versions
parent 023c4400
import { useWeb3Modal, useWeb3ModalState } from '@web3modal/wagmi/react'; import { useAppKit, useAppKitState } from '@reown/appkit/react';
import React from 'react'; import React from 'react';
import { useDisconnect, useAccountEffect } from 'wagmi'; import { useDisconnect, useAccountEffect } from 'wagmi';
...@@ -10,8 +10,8 @@ interface Params { ...@@ -10,8 +10,8 @@ interface Params {
} }
export default function useWeb3Wallet({ source }: Params) { export default function useWeb3Wallet({ source }: Params) {
const { open: openModal } = useWeb3Modal(); const { open: openModal } = useAppKit();
const { open: isOpen } = useWeb3ModalState(); const { open: isOpen } = useAppKitState();
const { disconnect } = useDisconnect(); const { disconnect } = useDisconnect();
const [ isOpening, setIsOpening ] = React.useState(false); const [ isOpening, setIsOpening ] = React.useState(false);
const [ isClientLoaded, setIsClientLoaded ] = React.useState(false); const [ isClientLoaded, setIsClientLoaded ] = React.useState(false);
......
import { defaultWagmiConfig } from '@web3modal/wagmi/react/config'; import { WagmiAdapter } from '@reown/appkit-adapter-wagmi';
import { http } from 'viem'; import { http } from 'viem';
import { createConfig, type CreateConfigParameters } from 'wagmi'; import { createConfig } from 'wagmi';
import config from 'configs/app'; import config from 'configs/app';
import currentChain from 'lib/web3/currentChain'; import currentChain from 'lib/web3/currentChain';
const feature = config.features.blockchainInteraction; const feature = config.features.blockchainInteraction;
const wagmiConfig = (() => { const wagmi = (() => {
const chains: CreateConfigParameters['chains'] = [ currentChain ]; const chains = [ currentChain ];
if (!feature.isEnabled) { if (!feature.isEnabled) {
const wagmiConfig = createConfig({ const wagmiConfig = createConfig({
...@@ -19,31 +19,21 @@ const wagmiConfig = (() => { ...@@ -19,31 +19,21 @@ const wagmiConfig = (() => {
batch: { multicall: { wait: 100 } }, batch: { multicall: { wait: 100 } },
}); });
return wagmiConfig; return { config: wagmiConfig, adapter: null };
} }
const wagmiConfig = defaultWagmiConfig({ const wagmiAdapter = new WagmiAdapter({
chains, networks: chains,
multiInjectedProviderDiscovery: true, multiInjectedProviderDiscovery: true,
transports: { transports: {
[currentChain.id]: http(), [currentChain.id]: http(),
}, },
projectId: feature.walletConnect.projectId, projectId: feature.walletConnect.projectId,
metadata: {
name: `${ config.chain.name } explorer`,
description: `${ config.chain.name } explorer`,
url: config.app.baseUrl,
icons: [ config.UI.navigation.icon.default ].filter(Boolean),
},
auth: {
email: true,
socials: [],
},
ssr: true, ssr: true,
batch: { multicall: { wait: 100 } }, batch: { multicall: { wait: 100 } },
}); });
return wagmiConfig; return { config: wagmiAdapter.wagmiConfig, adapter: wagmiAdapter };
})(); })();
export default wagmiConfig; export default wagmi;
...@@ -16,6 +16,7 @@ export function walletConnect(): CspDev.DirectiveDescriptor { ...@@ -16,6 +16,7 @@ export function walletConnect(): CspDev.DirectiveDescriptor {
'*.walletconnect.com', '*.walletconnect.com',
'*.walletconnect.org', '*.walletconnect.org',
'wss://relay.walletconnect.com', 'wss://relay.walletconnect.com',
'wss://relay.walletconnect.org',
'wss://www.walletlink.org', 'wss://www.walletlink.org',
], ],
'frame-ancestors': [ 'frame-ancestors': [
......
...@@ -84,7 +84,7 @@ const config: PlaywrightTestConfig = defineConfig({ ...@@ -84,7 +84,7 @@ const config: PlaywrightTestConfig = defineConfig({
// The createWeb3Modal() function from web3modal/wagmi/react somehow pollutes the global styles which causes the tests to fail // The createWeb3Modal() function from web3modal/wagmi/react somehow pollutes the global styles which causes the tests to fail
// We don't call this function in TestApp and since we use useWeb3Modal() and useWeb3ModalState() hooks in the code, we have to mock the module // We don't call this function in TestApp and since we use useWeb3Modal() and useWeb3ModalState() hooks in the code, we have to mock the module
// Otherwise it will complain that createWeb3Modal() is no called before the hooks are used // Otherwise it will complain that createWeb3Modal() is no called before the hooks are used
{ find: /^@web3modal\/wagmi\/react$/, replacement: './playwright/mocks/modules/@web3modal/wagmi/react.js' }, { find: /^@reown\/appkit\/react$/, replacement: './playwright/mocks/modules/@reown/appkit/react.js' },
{ find: '/playwright/index.ts', replacement: './playwright/index.ts' }, { find: '/playwright/index.ts', replacement: './playwright/index.ts' },
{ find: '/playwright/envs.js', replacement: './playwright/envs.js' }, { find: '/playwright/envs.js', replacement: './playwright/envs.js' },
......
function useWeb3Modal() { function useAppKit() {
return { return {
open: () => {}, open: () => {},
}; };
} }
function useWeb3ModalState() { function useAppKitState() {
return { return {
isOpen: false, isOpen: false,
}; };
} }
function useWeb3ModalTheme() { function useAppKitTheme() {
return { return {
setThemeMode: () => {}, setThemeMode: () => {},
}; };
} }
function createWeb3Modal() {} function createAppKit() {}
export { export {
createWeb3Modal, createAppKit,
useWeb3Modal, useAppKit,
useWeb3ModalState, useAppKitState,
useWeb3ModalTheme, useAppKitTheme,
}; };
import { Alert, Box, Button, chakra, Flex, Link, Radio, RadioGroup } from '@chakra-ui/react'; import { Alert, Box, Button, chakra, Flex, Link, Radio, RadioGroup } from '@chakra-ui/react';
import { useWeb3Modal } from '@web3modal/wagmi/react'; import { useAppKit } from '@reown/appkit/react';
import React from 'react'; import React from 'react';
import type { SubmitHandler } from 'react-hook-form'; import type { SubmitHandler } from 'react-hook-form';
import { FormProvider, useForm } from 'react-hook-form'; import { FormProvider, useForm } from 'react-hook-form';
...@@ -35,7 +35,7 @@ interface Props extends AddressVerificationFormFirstStepFields, AddressCheckStat ...@@ -35,7 +35,7 @@ interface Props extends AddressVerificationFormFirstStepFields, AddressCheckStat
const AddressVerificationStepSignature = ({ address, signingMessage, contractCreator, contractOwner, onContinue, noWeb3Provider }: Props) => { const AddressVerificationStepSignature = ({ address, signingMessage, contractCreator, contractOwner, onContinue, noWeb3Provider }: Props) => {
const [ signMethod, setSignMethod ] = React.useState<SignMethod>(noWeb3Provider ? 'manual' : 'wallet'); const [ signMethod, setSignMethod ] = React.useState<SignMethod>(noWeb3Provider ? 'manual' : 'wallet');
const { open: openWeb3Modal } = useWeb3Modal(); const { open: openWeb3Modal } = useAppKit();
const { isConnected } = useAccount(); const { isConnected } = useAccount();
const formApi = useForm<Fields>({ const formApi = useForm<Fields>({
......
import { useColorMode } from '@chakra-ui/react'; import { useColorMode } from '@chakra-ui/react';
import { createWeb3Modal, useWeb3ModalTheme } from '@web3modal/wagmi/react'; import { createAppKit, useAppKitTheme } from '@reown/appkit/react';
import React from 'react'; import React from 'react';
import { WagmiProvider } from 'wagmi'; import { WagmiProvider } from 'wagmi';
import config from 'configs/app'; import config from 'configs/app';
import currentChain from 'lib/web3/currentChain';
import wagmiConfig from 'lib/web3/wagmiConfig'; import wagmiConfig from 'lib/web3/wagmiConfig';
import colors from 'theme/foundations/colors'; import colors from 'theme/foundations/colors';
import { BODY_TYPEFACE } from 'theme/foundations/typography'; import { BODY_TYPEFACE } from 'theme/foundations/typography';
...@@ -13,13 +14,27 @@ const feature = config.features.blockchainInteraction; ...@@ -13,13 +14,27 @@ const feature = config.features.blockchainInteraction;
const init = () => { const init = () => {
try { try {
if (!feature.isEnabled) { if (!feature.isEnabled || !wagmiConfig.adapter) {
return; return;
} }
createWeb3Modal({ createAppKit({
wagmiConfig, adapters: [ wagmiConfig.adapter ],
networks: [ currentChain ],
metadata: {
name: `${ config.chain.name } explorer`,
description: `${ config.chain.name } explorer`,
url: config.app.baseUrl,
icons: [ config.UI.navigation.icon.default ].filter(Boolean),
},
projectId: feature.walletConnect.projectId, projectId: feature.walletConnect.projectId,
features: {
analytics: false,
email: true,
socials: [],
onramp: false,
swaps: false,
},
themeVariables: { themeVariables: {
'--w3m-font-family': `${ BODY_TYPEFACE }, sans-serif`, '--w3m-font-family': `${ BODY_TYPEFACE }, sans-serif`,
'--w3m-accent': colors.blue[600], '--w3m-accent': colors.blue[600],
...@@ -28,8 +43,6 @@ const init = () => { ...@@ -28,8 +43,6 @@ const init = () => {
}, },
featuredWalletIds: [], featuredWalletIds: [],
allowUnsupportedChain: true, allowUnsupportedChain: true,
enableOnramp: false,
enableSwaps: false,
}); });
} catch (error) {} } catch (error) {}
}; };
...@@ -42,7 +55,7 @@ interface Props { ...@@ -42,7 +55,7 @@ interface Props {
const DefaultProvider = ({ children }: Props) => { const DefaultProvider = ({ children }: Props) => {
return ( return (
<WagmiProvider config={ wagmiConfig }> <WagmiProvider config={ wagmiConfig.config }>
{ children } { children }
</WagmiProvider> </WagmiProvider>
); );
...@@ -50,7 +63,7 @@ const DefaultProvider = ({ children }: Props) => { ...@@ -50,7 +63,7 @@ const DefaultProvider = ({ children }: Props) => {
const Web3ModalProvider = ({ children }: Props) => { const Web3ModalProvider = ({ children }: Props) => {
const { colorMode } = useColorMode(); const { colorMode } = useColorMode();
const { setThemeMode } = useWeb3ModalTheme(); const { setThemeMode } = useAppKitTheme();
React.useEffect(() => { React.useEffect(() => {
setThemeMode(colorMode); setThemeMode(colorMode);
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment