-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
134 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/walletkit/src/components/WalletConnectUriProvider/context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React, { useContext } from 'react'; | ||
|
||
export interface WalletConnectUriContextProps { | ||
wcUri: string; | ||
} | ||
|
||
export const WalletConnectUriContext = React.createContext({} as WalletConnectUriContextProps); | ||
|
||
export function useWalletConnectUri() { | ||
const context = useContext(WalletConnectUriContext); | ||
return context; | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/walletkit/src/components/WalletConnectUriProvider/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import { WalletConnectUriContext } from './context'; | ||
import { useWalletKitContext } from '../WalletKitProvider/context'; | ||
import { WALLET_CONNECT_ID } from '../../wallets'; | ||
import { useConnector } from '../../hooks/useConnectors'; | ||
import { useAccount, useConnect } from 'wagmi'; | ||
import { commonErrorHandler } from '../../utils/common'; | ||
|
||
/** | ||
* Don't use `useWalletKitConnect` | ||
* otherwise when user repeats go and back between the wallet list and QR code page | ||
* due to the following `connectAsync` logic, multiple errors will be thrown | ||
*/ | ||
|
||
export interface WalletConnectUriProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
let timer: any = 0; | ||
|
||
export function WalletConnectUriProvider(props: WalletConnectUriProviderProps) { | ||
const { children } = props; | ||
|
||
const { log, options } = useWalletKitContext(); | ||
|
||
const [wcUri, setWcUri] = useState<string>(''); | ||
const connector = useConnector(WALLET_CONNECT_ID); | ||
|
||
const { connectAsync } = useConnect(); // don't use `useWalletKitConnect` | ||
const { isConnected } = useAccount(); | ||
|
||
useEffect(() => { | ||
if (!connector || isConnected || connector?.options.showQrModal) return; | ||
|
||
let provider: any; | ||
const updateWcUri = async () => { | ||
provider = await connector.getProvider(); | ||
provider.on('display_uri', setWcUri); | ||
}; | ||
updateWcUri(); | ||
|
||
const connectWallet = async () => { | ||
try { | ||
await connectAsync({ connector }); | ||
} catch (error: any) { | ||
clearTimeout(timer); | ||
|
||
timer = setTimeout(() => { | ||
commonErrorHandler({ | ||
log, | ||
error, | ||
handler: options.onError, | ||
}); | ||
|
||
if (error?.code === 4001) { | ||
connectWallet(); | ||
} | ||
}, 100); | ||
} | ||
}; | ||
|
||
connectWallet(); | ||
|
||
return () => { | ||
provider?.off?.('display_uri', setWcUri); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [connector, isConnected]); | ||
|
||
const value = useMemo(() => { | ||
return { | ||
wcUri, | ||
}; | ||
}, [wcUri]); | ||
|
||
return ( | ||
<WalletConnectUriContext.Provider value={value}>{children}</WalletConnectUriContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters