Skip to content

Commit

Permalink
feat: Make walletConnect uri as global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 committed Aug 28, 2024
1 parent 4d116a4 commit 5b4aebb
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 123 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-dragons-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@node-real/walletkit': patch
---

Make walletConnect uri as global variable
8 changes: 3 additions & 5 deletions packages/walletkit/src/core/modals/ConnectModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Modal } from '@/core/base/components/Modal';
import { useEventConfig, useEvmConfig } from '@/core/providers/WalletKitProvider/context';
import { useEventConfig } from '@/core/providers/WalletKitProvider/context';
import { useRouter } from './RouteProvider/context';
import { useConnectModal } from './context';
import { Navbar } from '@/core/components/Navbar';
import { EvmWalletConnectUriGenerator } from '@/evm/components/EvmWalletConnectUriProvider';
import { EvmConnectorUriProvider } from '@/evm/components/EvmConnectorUriProvider';

export function ConnectModal() {
const eventConfig = useEventConfig();
Expand All @@ -12,8 +12,6 @@ export function ConnectModal() {

const showBack = history.length > 1;

const evmConfig = useEvmConfig();

return (
<>
<Modal
Expand All @@ -27,7 +25,7 @@ export function ConnectModal() {
{view}
</Modal>

{evmConfig && <EvmWalletConnectUriGenerator />}
<EvmConnectorUriProvider />
</>
);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TemplateConnectWithQRCodeView } from '@/core/modals/ConnectModal/TemplateConnectWithQRCodeView';
import { useSelectedWallet } from '@/core/providers/WalletKitProvider/context';
import { useEvmIsConnected } from '@/evm/hooks/useEvmIsConnected';
import { useEvmWalletConnectUri } from '@/evm/hooks/useEvmWalletConnectUri';
import { useWalletConnectModal } from '@/evm/hooks/useWalletConnectModal';
import { EvmWallet, isWalletConnect } from '@/evm/wallets';
import { useEvmWalletConnectUri } from '../EvmWalletConnectUriProvider';

export function EvmConnectWithQRCodeView() {
const { selectedWallet } = useSelectedWallet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { useSelectedWallet } from '@/core/providers/WalletKitProvider/context';
import { useEvmIsConnected } from '@/evm/hooks/useEvmIsConnected';
import { useCallback, useEffect, useState } from 'react';
import { EventEmitter } from '@/core/utils/eventEmitter';
import { useEvmWalletConnectUri } from '../EvmWalletConnectUriProvider';
import { EvmWallet } from '@/evm/wallets';
import { openUri } from '@/core/utils/common';
import { getEvmGlobalData } from '@/evm/globalData';

export function EvmConnectWithWalletConnectView() {
const { selectedWallet } = useSelectedWallet();

const [status, setStatus] = useState(CONNECT_STATUS.CONNECTING);
const { wcUri } = useEvmWalletConnectUri();

useEffect(() => {
const onError = (error: any) => {
Expand Down Expand Up @@ -55,9 +54,11 @@ export function EvmConnectWithWalletConnectView() {
const onTryAgain = useCallback(() => {
setStatus(CONNECT_STATUS.CONNECTING);

const walletUri = (selectedWallet as EvmWallet).getUri(wcUri);
const wcUri = getEvmGlobalData().walletConnectUri;

const walletUri = (selectedWallet as EvmWallet).getUri(wcUri!);
openUri(walletUri);
}, [selectedWallet, wcUri]);
}, [selectedWallet]);

return (
<TemplateConnectingView
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { isMobile, isTMA } from '@/core/base/utils/mobile';
import { useEvmConfig } from '@/core/providers/WalletKitProvider/context';
import { setEvmGlobalData } from '@/evm/globalData';
import { useEvmWalletConnectUri } from '@/evm/hooks/useEvmWalletConnectUri';

export function EvmConnectorUriProvider() {
const { wallets } = useEvmConfig();

const isWcUriEnabled = (!!wallets.find((item) => item.useWalletConnect) || isTMA()) && isMobile();
const { wcUri } = useEvmWalletConnectUri({
enabled: isWcUriEnabled,
});

setEvmGlobalData({
walletConnectUri: wcUri,
});

return null;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { WagmiProvider } from 'wagmi';
import { useEvmConfig } from '@/core/providers/WalletKitProvider/context';
import { EvmWalletConnectUriProvider } from '../EvmWalletConnectUriProvider';

export interface EvmWalletProviderProps {
children: React.ReactNode;
Expand All @@ -17,7 +16,7 @@ export function EvmWalletProvider(props: EvmWalletProviderProps) {

return (
<WagmiProvider config={evmConfig.wagmiConfig} reconnectOnMount={evmConfig.autoConnect}>
<EvmWalletConnectUriProvider>{children}</EvmWalletConnectUriProvider>
{children}
</WagmiProvider>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
useSelectedWallet,
} from '@/core/providers/WalletKitProvider/context';
import { openUri } from '@/core/utils/common';
import { getEvmGlobalData } from '@/evm/globalData';
import { useWalletConnectModal } from '@/evm/hooks/useWalletConnectModal';
import { EvmWallet, isWalletConnect } from '@/evm/wallets';
import { useRef } from 'react';
import { useConnectors, useDisconnect } from 'wagmi';
import { useEvmWalletConnectUri } from '../EvmWalletConnectUriProvider';

interface SetEvmWalletClickRefProps {
clickRef: UseWalletRenderProps['clickRef'];
Expand All @@ -30,7 +30,6 @@ export function SetEvmWalletClickRef(props: SetEvmWalletClickRefProps) {
const connectors = useConnectors();
const { disconnect } = useDisconnect();
const wcModal = useWalletConnectModal();
const { wcUri } = useEvmWalletConnectUri();

const connectModal = useConnectModal();
const router = useRouter();
Expand Down Expand Up @@ -70,10 +69,11 @@ export function SetEvmWalletClickRef(props: SetEvmWalletClickRefProps) {
};

const jumpToWalletConnectView = () => {
if (!wcUri) return;

openUri(wallet.getUri(wcUri));
jumpTo(ViewRoutes.EVM_CONNECT_WITH_WALLET_CONNECT);
const wcUri = getEvmGlobalData().walletConnectUri;
if (wcUri) {
openUri(wallet.getUri(wcUri));
jumpTo(ViewRoutes.EVM_CONNECT_WITH_WALLET_CONNECT);
}
};

disconnect();
Expand Down
2 changes: 1 addition & 1 deletion packages/walletkit/src/evm/globalData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Metadata } from '@/core/providers/WalletKitProvider/context';
interface EvmGlobalData {
metadata?: Metadata;
walletConnectProjectId?: string;
walletConnectModalIsOpen?: boolean;
walletConnectUri?: string;
}

let evmGlobalData: EvmGlobalData = {};
Expand Down
81 changes: 81 additions & 0 deletions packages/walletkit/src/evm/hooks/useEvmWalletConnectUri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
useEvmConfig,
useEventConfig,
useLogger,
} from '@/core/providers/WalletKitProvider/context';
import { EventEmitter } from '@/core/utils/eventEmitter';
import { useEffect, useState } from 'react';
import { useConnect } from 'wagmi';
import { evmCommonErrorHandler } from '../utils/evmCommonErrorHandler';
import { useEvmIsConnected } from './useEvmIsConnected';
import { useWalletConnectConnector } from './useWalletConnectConnector';

interface UseEvmWalletConnectUriProps {
enabled?: boolean;
}

let timer: any;

export function useEvmWalletConnectUri(props: UseEvmWalletConnectUriProps = {}) {
const { enabled = true } = props;

const { connectAsync } = useConnect();
const { initialChainId } = useEvmConfig();
const eventConfig = useEventConfig();
const log = useLogger();

const connector = useWalletConnectConnector();
const isConnected = useEvmIsConnected();

const [wcUri, setWcUri] = useState<string>();

useEffect(() => {
if (isConnected || !connector || !enabled) return;

const onUpdateWcUri = ({ type, data }: any) => {
if (type === 'display_uri') {
setWcUri(data);
}
};

const connectWallet = async () => {
try {
log('[qrcode uri]', 'connecting');
const provider: any = await connector?.getProvider();

provider.rpc.showQrModal = false;

await connectAsync({ connector, chainId: initialChainId });
} catch (error: any) {
clearTimeout(timer);

timer = setTimeout(() => {
// props?.onError?.(error);

EventEmitter.emit(EventEmitter.EVM_WC_URI_ERROR, error);

if (error?.code === 4001) {
evmCommonErrorHandler({
log,
error,
handler: eventConfig.onError,
});
connectWallet(); // refresh qr code
}
}, 100);
}
};

connectWallet();

connector.emitter.on('message', onUpdateWcUri);
return () => {
connector?.emitter.off('message', onUpdateWcUri);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isConnected, enabled]);

return {
wcUri,
};
}
5 changes: 0 additions & 5 deletions packages/walletkit/src/evm/hooks/useWalletConnectModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useWalletConnectConnector } from './useWalletConnectConnector';
import { useEvmConnect } from './useEvmConnect';
import { useConnectModal } from '@/core/modals/ConnectModal/context';
import { useLogger } from '@/core/providers/WalletKitProvider/context';
import { setEvmGlobalData } from '../globalData';

export function useWalletConnectModal() {
const { connectAsync } = useEvmConnect();
Expand All @@ -22,10 +21,6 @@ export function useWalletConnectModal() {
clearTimeout(timer);
};
}

setEvmGlobalData({
walletConnectModalIsOpen: isOpen,
});
}, [connectModal, isOpen]);

const connector = useWalletConnectConnector();
Expand Down
1 change: 0 additions & 1 deletion packages/walletkit/src/evm/utils/evmConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function evmConfig(params: CustomizedEvmConfig) {
setEvmGlobalData({
metadata,
walletConnectProjectId,
walletConnectModalIsOpen: false,
});

const transports: CreateConfigParameters['transports'] =
Expand Down

0 comments on commit 5b4aebb

Please sign in to comment.