Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: walletconnect prefetch #2124

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ jobs:
run: pnpm build
- name: Lint and format
run: pnpm lint
- name: Install playwright browser
run: pnpm exec playwright install
- name: Run tests
run: pnpm test && pnpm test:cli
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@types/react-dom": "^18.3.0",
"@vanilla-extract/esbuild-plugin": "^2.3.5",
"@vanilla-extract/vite-plugin": "^4.0.9",
"@vitest/browser": "2.0.4",
"autoprefixer": "^10.4.16",
"dotenv": "^16.4.5",
"esbuild": "^0.20.2",
Expand All @@ -65,6 +66,7 @@
"lokijs": "^1.5.12",
"next": "^14.2.3",
"next-auth": "4.24.5",
"playwright": "1.45.3",
"postcss": "^8.4.32",
"postcss-prefix-selector": "^1.16.0",
"react": "^18.3.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/rainbowkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
"postcss": "^8.4.32",
"react": "^18.3.0",
"vitest": "2.0.4",
"dotenv": "^16.4.5"
"dotenv": "^16.4.5",
"@vitest/browser": "2.0.4",
"playwright": "1.45.3"
},
"dependencies": {
"@vanilla-extract/css": "1.14.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useConnectionStatus } from '../../hooks/useConnectionStatus';
import ConnectOptions from '../ConnectOptions/ConnectOptions';
import { Dialog } from '../Dialog/Dialog';
import { DialogContent } from '../Dialog/DialogContent';
import { useWalletConnectStore } from '../RainbowKitProvider/WalletConnectStoreProvider';
import { SignIn } from '../SignIn/SignIn';

export interface ConnectModalProps {
Expand All @@ -15,9 +16,10 @@ export function ConnectModal({ onClose, open }: ConnectModalProps) {
const titleId = 'rk_connect_title';
const connectionStatus = useConnectionStatus();

const { resetWalletConnectUri } = useWalletConnectStore();

const { disconnect } = useDisconnect();
const { isConnecting } = useAccount();

// when a user cancels or dismisses the SignIn modal for SIWE, disconnect and call onClose
const onAuthCancel = React.useCallback(() => {
onClose();
Expand All @@ -29,9 +31,9 @@ export function ConnectModal({ onClose, open }: ConnectModalProps) {
// the user closes it, we need to know the wallet isn't connecting anymore.
// So if it's connecting, we disconnect it.
if (isConnecting) disconnect();

resetWalletConnectUri();
onClose();
}, [onClose, disconnect, isConnecting]);
}, [onClose, disconnect, resetWalletConnectUri, isConnecting]);

if (connectionStatus === 'disconnected') {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BrowserType, getBrowser, isSafari } from '../../utils/browsers';
import { getGradientRGBAs } from '../../utils/colors';
import { PlatformType, getPlatform } from '../../utils/platforms';
import { InstructionStepName } from '../../wallets/Wallet';
import { useWalletConnectModal } from '../../wallets/useWalletConnectModal';
import {
WalletConnector,
useWalletConnectors,
Expand Down Expand Up @@ -212,12 +213,15 @@ export function ConnectDetail({
name,
qrCode,
ready,
showWalletConnectModal,
getDesktopUri,
} = wallet;
const isDesktopDeepLinkAvailable = !!getDesktopUri;
const safari = isSafari();

const isWalletConnectWallet = wallet.id === 'walletConnect';

const { openWalletConnectModal } = useWalletConnectModal();

const { i18n } = useContext(I18nContext);

const hasExtension = !!wallet.extensionDownloadUrl;
Expand All @@ -236,15 +240,15 @@ export function ConnectDetail({
label: string;
onClick?: () => void;
href?: string;
} | null = showWalletConnectModal
} | null = isWalletConnectWallet
? {
description: !compactModeEnabled
? i18n.t('connect.walletconnect.description.full')
: i18n.t('connect.walletconnect.description.compact'),
label: i18n.t('connect.walletconnect.open.label'),
onClick: () => {
onClose();
showWalletConnectModal();
openWalletConnectModal();
},
}
: hasQrCode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useContext } from 'react';
import { useRequestWalletConnectUri } from '../../hooks/useRequestWalletConnectUri';
import { isMobile } from '../../utils/isMobile';
import { WalletButtonContext } from '../RainbowKitProvider/WalletButtonContext';
import { DesktopOptions } from './DesktopOptions';
Expand All @@ -7,6 +8,8 @@ import { MobileStatus } from './MobileStatus';

export default function ConnectOptions({ onClose }: { onClose: () => void }) {
const { connector } = useContext(WalletButtonContext);
// Prefetch WalletConnect URI
useRequestWalletConnectUri();

return isMobile() ? (
connector ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { WalletButtonContext } from '../RainbowKitProvider/WalletButtonContext';
import { Text } from '../Text/Text';

import { addLatestWalletId } from '../../wallets/latestWalletId';
import { useWalletConnectStore } from '../RainbowKitProvider/WalletConnectStoreProvider';
import {
ConnectDetail,
DownloadDetail,
Expand Down Expand Up @@ -75,6 +76,7 @@ export function DesktopOptions({ onClose }: { onClose: () => void }) {
const initialized = useRef(false);

const { connector } = useContext(WalletButtonContext);
const { onWalletConnectUri } = useWalletConnectStore();

// The `WalletButton` component made the connect modal appear empty when trying to connect.
// This happened because of a mix up between EIP-6963 and RainbowKit connectors.
Expand All @@ -87,6 +89,8 @@ export function DesktopOptions({ onClose }: { onClose: () => void }) {
.filter((wallet) => wallet.ready || !!wallet.extensionDownloadUrl)
.sort((a, b) => a.groupIndex - b.groupIndex);

useEffect(() => onWalletConnectUri(setQrCodeUri), [onWalletConnectUri]);

const groupedWallets = groupBy(wallets, (wallet) => wallet.groupName);

const supportedI18nGroupNames = [
Expand All @@ -111,7 +115,7 @@ export function DesktopOptions({ onClose }: { onClose: () => void }) {
const connectToWallet = (wallet: WalletConnector) => {
setConnectionError(false);
if (wallet.ready) {
wallet?.connect?.()?.catch(() => {
wallet?.connectWallet?.()?.catch(() => {
setConnectionError(true);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
} from 'react';
import { touchableStyles } from '../../css/touchableStyles';
import { isIOS } from '../../utils/isMobile';
import { useWalletConnectModal } from '../../wallets/useWalletConnectModal';
import {
WalletConnector,
useWalletConnectors,
Expand Down Expand Up @@ -65,16 +66,19 @@ export function WalletButton({
onClose: () => void;
connecting?: boolean;
}) {
const isWalletConnectWallet = wallet.id === 'walletConnect';

const { openWalletConnectModal } = useWalletConnectModal();

const {
connect,
connectWallet,
iconBackground,
iconUrl,
id,
name,
getMobileUri,
ready,
shortName,
showWalletConnectModal,
} = wallet;

const coolModeRef = useCoolMode(iconUrl);
Expand Down Expand Up @@ -112,17 +116,21 @@ export function WalletButton({
}
};

if (id !== 'walletConnect') onMobileUri();

// If the id is "walletConnect" then "showWalletConnectModal" will always be true
if (showWalletConnectModal) {
showWalletConnectModal();
onClose?.();
return;
if (isWalletConnectWallet) {
onClose();
openWalletConnectModal();
} else {
onMobileUri();
connectWallet();
}

connect?.();
}, [connect, getMobileUri, showWalletConnectModal, onClose, name, id]);
}, [
connectWallet,
isWalletConnectWallet,
openWalletConnectModal,
getMobileUri,
onClose,
name,
]);

useEffect(() => {
// When using `reactStrictMode: true` in development mode the useEffect hook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { RainbowKitChainProvider } from './RainbowKitChainContext';
import { ShowBalanceProvider } from './ShowBalanceContext';
import { ShowRecentTransactionsContext } from './ShowRecentTransactionsContext';
import { WalletButtonProvider } from './WalletButtonContext';
import { WalletConnectStoreProvider } from './WalletConnectStoreProvider';
import { useFingerprint } from './useFingerprint';
import { usePreloadImages } from './usePreloadImages';
import { clearWalletConnectDeepLink } from './walletConnectDeepLink';
Expand Down Expand Up @@ -103,60 +104,62 @@ export function RainbowKitProvider({

return (
<RainbowKitChainProvider initialChain={initialChain}>
<WalletButtonProvider>
<I18nProvider locale={locale}>
<CoolModeContext.Provider value={coolMode}>
<ModalSizeProvider modalSize={modalSize}>
<ShowRecentTransactionsContext.Provider
value={showRecentTransactions}
>
<TransactionStoreProvider>
<AvatarContext.Provider value={avatarContext}>
<AppContext.Provider value={appContext}>
<ThemeIdContext.Provider value={id}>
<ShowBalanceProvider>
<ModalProvider>
{theme ? (
<div {...createThemeRootProps(id)}>
<style
// biome-ignore lint/security/noDangerouslySetInnerHtml: TODO
dangerouslySetInnerHTML={{
// Selectors are sanitized to only contain alphanumeric
// and underscore characters. Theme values generated by
// cssStringFromTheme are sanitized, removing
// characters that terminate values / HTML tags.
__html: [
`${selector}{${cssStringFromTheme(
'lightMode' in theme
? theme.lightMode
: theme,
)}}`,

'darkMode' in theme
? `@media(prefers-color-scheme:dark){${selector}{${cssStringFromTheme(
theme.darkMode,
{ extends: theme.lightMode },
)}}}`
: null,
].join(''),
}}
/>
{children}
</div>
) : (
children
)}
</ModalProvider>
</ShowBalanceProvider>
</ThemeIdContext.Provider>
</AppContext.Provider>
</AvatarContext.Provider>
</TransactionStoreProvider>
</ShowRecentTransactionsContext.Provider>
</ModalSizeProvider>
</CoolModeContext.Provider>
</I18nProvider>
</WalletButtonProvider>
<WalletConnectStoreProvider>
<WalletButtonProvider>
<I18nProvider locale={locale}>
<CoolModeContext.Provider value={coolMode}>
<ModalSizeProvider modalSize={modalSize}>
<ShowRecentTransactionsContext.Provider
value={showRecentTransactions}
>
<TransactionStoreProvider>
<AvatarContext.Provider value={avatarContext}>
<AppContext.Provider value={appContext}>
<ThemeIdContext.Provider value={id}>
<ShowBalanceProvider>
<ModalProvider>
{theme ? (
<div {...createThemeRootProps(id)}>
<style
// biome-ignore lint/security/noDangerouslySetInnerHtml: TODO
dangerouslySetInnerHTML={{
// Selectors are sanitized to only contain alphanumeric
// and underscore characters. Theme values generated by
// cssStringFromTheme are sanitized, removing
// characters that terminate values / HTML tags.
__html: [
`${selector}{${cssStringFromTheme(
'lightMode' in theme
? theme.lightMode
: theme,
)}}`,

'darkMode' in theme
? `@media(prefers-color-scheme:dark){${selector}{${cssStringFromTheme(
theme.darkMode,
{ extends: theme.lightMode },
)}}}`
: null,
].join(''),
}}
/>
{children}
</div>
) : (
children
)}
</ModalProvider>
</ShowBalanceProvider>
</ThemeIdContext.Provider>
</AppContext.Provider>
</AvatarContext.Provider>
</TransactionStoreProvider>
</ShowRecentTransactionsContext.Provider>
</ModalSizeProvider>
</CoolModeContext.Provider>
</I18nProvider>
</WalletButtonProvider>
</WalletConnectStoreProvider>
</RainbowKitChainProvider>
);
}
Loading