-
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.
feat: Make walletConnect uri as global variable
- Loading branch information
Showing
12 changed files
with
122 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@node-real/walletkit': patch | ||
--- | ||
|
||
Make walletConnect uri as global variable |
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
2 changes: 1 addition & 1 deletion
2
packages/walletkit/src/evm/components/EvmConnectWithQRCodeView/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
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
19 changes: 19 additions & 0 deletions
19
packages/walletkit/src/evm/components/EvmConnectorUriProvider/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,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; | ||
} |
98 changes: 0 additions & 98 deletions
98
packages/walletkit/src/evm/components/EvmWalletConnectUriProvider/index.tsx
This file was deleted.
Oops, something went wrong.
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
81 changes: 81 additions & 0 deletions
81
packages/walletkit/src/evm/hooks/useEvmWalletConnectUri.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,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, | ||
}; | ||
} |
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