diff --git a/.changeset/late-birds-compare.md b/.changeset/late-birds-compare.md deleted file mode 100644 index 22664bbd..00000000 --- a/.changeset/late-birds-compare.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@totejs/walletkit': patch ---- - -Fixed `WalletConnect` automatic connection issue in the follow scenario: connect the WalletConnect diff --git a/.changeset/lovely-months-argue.md b/.changeset/lovely-months-argue.md new file mode 100644 index 00000000..8674cc05 --- /dev/null +++ b/.changeset/lovely-months-argue.md @@ -0,0 +1,5 @@ +--- +'@totejs/walletkit': patch +--- + +Fixed trustwallet losing account status after refreshing diff --git a/.changeset/pre.json b/.changeset/pre.json index 86820e1a..7f8f1fa2 100644 --- a/.changeset/pre.json +++ b/.changeset/pre.json @@ -4,7 +4,7 @@ "initialVersions": { "example-nextjs": "0.0.1", "example-vite": "0.0.1", - "@totejs/walletkit": "1.0.7-alpha.1", + "@totejs/walletkit": "1.0.7-alpha.2", "website": "0.0.1" }, "changesets": [ diff --git a/.eslintignore b/.eslintignore index 3859ffd8..9aaceae7 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,4 @@ .next dist CHANGELOG.md -build.cjs \ No newline at end of file +.changeset/*.md \ No newline at end of file diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index aae16a82..7815ae84 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -6,7 +6,6 @@ on: - alpha - 0.x - 0.x-alpha - - dev jobs: release: diff --git a/.prettierignore b/.prettierignore index 9262527b..9aaceae7 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,4 @@ .next dist -CHANGELOG.md \ No newline at end of file +CHANGELOG.md +.changeset/*.md \ No newline at end of file diff --git a/packages/walletkit/dev/App.tsx b/packages/walletkit/dev/App.tsx index df07e60e..b712b404 100644 --- a/packages/walletkit/dev/App.tsx +++ b/packages/walletkit/dev/App.tsx @@ -1,5 +1,6 @@ import { chains } from './chains'; import { WagmiConfig, createConfig } from 'wagmi'; +import VConsole from 'vconsole'; import { SwitchNetworkModal, ThemeMode, @@ -23,6 +24,8 @@ import { } from '../src/wallets'; import React from 'react'; +new VConsole(); + const config = createConfig( getDefaultConfig({ autoConnect: true, diff --git a/packages/walletkit/package.json b/packages/walletkit/package.json index c2c4e2a5..98031493 100644 --- a/packages/walletkit/package.json +++ b/packages/walletkit/package.json @@ -52,6 +52,7 @@ "react-dom": "^18.2.0", "rollup-plugin-peer-deps-external": "^2.2.4", "typescript": "^5.3.2", + "vconsole": "^3.15.1", "viem": "^1.19.9", "vite": "^4.5.0", "vite-plugin-dts": "^3.6.3", diff --git a/packages/walletkit/src/globalData/index.ts b/packages/walletkit/src/globalData/index.ts index 4971f77f..84b12cdc 100644 --- a/packages/walletkit/src/globalData/index.ts +++ b/packages/walletkit/src/globalData/index.ts @@ -7,6 +7,7 @@ export interface GlobalDataType { appUrl?: string; walletConnectProjectId?: string; walletConnectConnector?: Connector; + walletConnectModalIsOpen?: boolean; } let globalData: GlobalDataType = { diff --git a/packages/walletkit/src/hooks/useQRCodeUri.ts b/packages/walletkit/src/hooks/useQRCodeUri.ts index 1caec7b0..10c78a9c 100644 --- a/packages/walletkit/src/hooks/useQRCodeUri.ts +++ b/packages/walletkit/src/hooks/useQRCodeUri.ts @@ -14,11 +14,11 @@ export function useQRCodeUri() { const [wcUri, setWcUri] = useState(''); useEffect(() => { - const { walletConnectConnector: connector } = getGlobalData(); + const connector = getGlobalData().walletConnectConnector; if (isConnected || !connector) return; const onUpdateWcUri = ({ type, data }: any) => { - if (type === 'display_uri') { + if (type === 'display_uri' && !getGlobalData().walletConnectModalIsOpen) { setWcUri(data); } }; diff --git a/packages/walletkit/src/hooks/useWalletConnectModal.ts b/packages/walletkit/src/hooks/useWalletConnectModal.ts index 7260bd34..c343d5f8 100644 --- a/packages/walletkit/src/hooks/useWalletConnectModal.ts +++ b/packages/walletkit/src/hooks/useWalletConnectModal.ts @@ -2,7 +2,7 @@ import { MODAL_AUTO_CLOSE_DELAY } from '@/constants/common'; import { useEffect, useState } from 'react'; import { useModal, useWalletKitContext } from '..'; import { useWalletKitConnect } from './useWalletKitConnect'; -import { getGlobalData } from '@/globalData'; +import { getGlobalData, setGlobalData } from '@/globalData'; export function useWalletConnectModal() { const { connectAsync } = useWalletKitConnect(); @@ -21,6 +21,10 @@ export function useWalletConnectModal() { clearTimeout(timer); }; } + + setGlobalData({ + walletConnectModalIsOpen: isOpen, + }); }, [isOpen, onClose]); return { @@ -28,7 +32,7 @@ export function useWalletConnectModal() { onOpenWcModal: async () => { document.body.style.setProperty('--wcm-z-index', '2147483647'); - const { walletConnectConnector: connector } = getGlobalData(); + const connector = getGlobalData().walletConnectConnector; const provider = await connector?.getProvider(); provider.rpc.showQrModal = true; diff --git a/packages/walletkit/src/pages/Connecting/index.tsx b/packages/walletkit/src/pages/Connecting/index.tsx index 4e88342e..f0057e24 100644 --- a/packages/walletkit/src/pages/Connecting/index.tsx +++ b/packages/walletkit/src/pages/Connecting/index.tsx @@ -8,7 +8,7 @@ import { useWalletConfig } from '@/hooks/useWalletConfig'; import { useWalletDownloadUrl } from '@/hooks/useWalletDownloadUrl'; import { useWalletKitConnect } from '@/hooks/useWalletKitConnect'; import { useWalletLogos } from '@/hooks/useWalletLogos'; -import { useWalletKitContext } from '@/index'; +import { cx, useWalletKitContext } from '@/index'; import { useState, useCallback, useEffect } from 'react'; import { ConnectSpinner } from './ConnectSpinner'; import { Content } from './Content'; @@ -167,7 +167,7 @@ export function ConnectingPage() { {(status === states.FAILED || status === states.REJECTED) && ( - @@ -175,7 +175,13 @@ export function ConnectingPage() { {status === states.UNAVAILABLE && ( - diff --git a/packages/walletkit/src/utils/common.ts b/packages/walletkit/src/utils/common.ts index 18541857..420d4acc 100644 --- a/packages/walletkit/src/utils/common.ts +++ b/packages/walletkit/src/utils/common.ts @@ -47,3 +47,11 @@ export function commonErrorHandler(props: { log: any; handler: any; error: any } handler?.(error, description); } } + +export async function sleep(duration = 1000) { + return new Promise((resolve) => { + setTimeout(() => { + resolve(null); + }, duration); + }); +} diff --git a/packages/walletkit/src/wallets/trustWallet/connector.ts b/packages/walletkit/src/wallets/trustWallet/connector.ts index 902d4599..c1f73cfd 100644 --- a/packages/walletkit/src/wallets/trustWallet/connector.ts +++ b/packages/walletkit/src/wallets/trustWallet/connector.ts @@ -2,6 +2,7 @@ import { Chain } from 'wagmi'; import { MetaMaskConnector } from 'wagmi/connectors/metaMask'; import { TRUST_WALLET_ID } from '.'; import { getInjectedProvider } from '../utils'; +import { sleep } from '@/utils/common'; export type TrustWalletConnectorOptions = { shimDisconnect?: boolean; @@ -30,6 +31,13 @@ export class TrustWalletConnector extends MetaMaskConnector { options, }); } + + public async getProvider() { + if (typeof window !== 'undefined' && !window.trustwallet?.request) { + await sleep(); + } + return this.options.getProvider(); + } } function getProvider() { @@ -37,7 +45,7 @@ function getProvider() { const provider = getInjectedProvider('isTrust') ?? window.trustwallet ?? window.trustWallet; - if (provider && provider.removeListener === undefined) { + if (provider && !provider.removeListener === undefined) { provider.removeListener = provider.off; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cc2bbd97..4606b13b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -152,6 +152,9 @@ importers: typescript: specifier: ^5.3.2 version: 5.3.2 + vconsole: + specifier: ^3.15.1 + version: 3.15.1 viem: specifier: ^1.19.9 version: 1.19.9(typescript@5.3.2)