-
Notifications
You must be signed in to change notification settings - Fork 362
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
14 changed files
with
342 additions
and
313 deletions.
There are no files selected for viewing
177 changes: 80 additions & 97 deletions
177
advanced/wallets/reown-web-wallet/src/hooks/useAppKitProviderEventsManager.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 |
---|---|---|
@@ -1,111 +1,94 @@ | ||
import { W3mFrameProvider } from "@reown/appkit-wallet" | ||
import { Provider, useAppKitAccount, useAppKitProvider } from '@reown/appkit/react' | ||
import { Provider, useAppKit, useAppKitAccount, useAppKitProvider, useAppKitState } from '@reown/appkit/react' | ||
|
||
import { useCallback, useEffect } from "react" | ||
import { walletKit } from '@/utils/WalletConnectUtil' | ||
import ModalStore from "@/store/ModalStore" | ||
import { useRouter } from "next/router" | ||
|
||
export default function useAppKitProviderEventsManager() { | ||
const { walletProvider } = useAppKitProvider<W3mFrameProvider>('eip155') | ||
const { address, isConnected, caipAddress, status } = useAppKitAccount() | ||
|
||
const onConnect = useCallback(async () => { | ||
console.log('onConnect') | ||
|
||
|
||
|
||
await new Promise(resolve => setTimeout(resolve, 1000)) | ||
|
||
let pendingSessionProposals = Object.values(await walletKit.getPendingSessionProposals()) | ||
let pendingSessionRequests = await walletKit.getPendingSessionRequests() | ||
|
||
console.log('pendingSessionProposals', pendingSessionProposals) | ||
console.log('pendingSessionRequests', pendingSessionRequests) | ||
|
||
// get pending session proposals from local storage | ||
const pendingSessionProposal = localStorage.getItem('pendingSessionProposal') | ||
console.log('pendingSessionProposal', pendingSessionProposal) | ||
|
||
|
||
|
||
if (pendingSessionProposal) { | ||
const proposal = JSON.parse(pendingSessionProposal) | ||
|
||
console.log('opening session proposal modal', proposal) | ||
ModalStore.open('SessionProposalModal', { proposal }) | ||
} else { | ||
const latestProposal = pendingSessionProposals[pendingSessionProposals.length - 1] | ||
ModalStore.open('SessionProposalModal', { | ||
proposal: { | ||
id: latestProposal.id, | ||
params: latestProposal, | ||
verifyContext: { | ||
verified: { | ||
isScam: false, | ||
origin: '', | ||
validation: 'VALID', | ||
verifyUrl: '' | ||
} | ||
} | ||
} | ||
}) | ||
const router = useRouter() | ||
const { open } = useAppKitState() | ||
const { walletProvider } = useAppKitProvider<W3mFrameProvider>('eip155') | ||
const { address, isConnected, caipAddress, status } = useAppKitAccount() | ||
const appkit= useAppKit() | ||
|
||
useEffect(() => { | ||
if (open) { | ||
console.log('closing modal because open is true') | ||
ModalStore.close() | ||
} | ||
}, [open]) | ||
|
||
|
||
const onConnect = useCallback(async () => { | ||
console.log('onConnect') | ||
|
||
const uri = router.query.uri as string | ||
|
||
if (uri) { | ||
console.log('pairing with uri', uri) | ||
try { | ||
await walletKit.pair({ uri }) | ||
} catch (error) { | ||
console.error('pairing error', error) | ||
} | ||
} | ||
}, [walletKit]) | ||
|
||
const handleSetPreferredAccount = async (account: any) => { | ||
console.log('onSetPreferredAccount', account.address) | ||
|
||
const {chainId} = await walletProvider.getChainId(); | ||
|
||
const caip10Address = `eip155:${chainId}:${account.address}` | ||
|
||
const sessions = Object.values(walletKit.getActiveSessions()) | ||
console.log('sessions', sessions) | ||
|
||
sessions.forEach(async session => { | ||
const currentAccounts = session.namespaces.eip155.accounts; | ||
const updatedAccounts = [ | ||
caip10Address, | ||
...currentAccounts.filter(acc => acc !== caip10Address) | ||
]; | ||
|
||
console.log('accounts', currentAccounts, updatedAccounts) | ||
|
||
await walletKit.updateSession({ | ||
topic: session.topic, | ||
namespaces: { | ||
...session.namespaces, | ||
eip155: { | ||
...session.namespaces.eip155, | ||
accounts: updatedAccounts | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
|
||
// await new Promise(resolve => setTimeout(resolve, 5000)) | ||
|
||
// pendingSessionProposals = Object.values(await walletKit.getPendingSessionProposals()) | ||
// pendingSessionRequests = await walletKit.getPendingSessionRequests() | ||
|
||
// console.log('pendingSessionProposals 2', pendingSessionProposals) | ||
// console.log('pendingSessionRequests 2', pendingSessionRequests) | ||
|
||
// if (pendingSessionProposals.length > 0) { | ||
// const latestProposal = pendingSessionProposals[pendingSessionProposals.length - 1] | ||
// console.log('latestProposal', latestProposal) | ||
|
||
// // this is a hack to get the proposal to the modal | ||
// // walletkit doesn't include the verifyContext in the pendinng proposal object | ||
// // a better workaround is to manually store verifyContext in the local storage when session_proposal event is received | ||
// ModalStore.open('SessionProposalModal', { | ||
// proposal: { | ||
// id: latestProposal.id, | ||
// params: latestProposal, | ||
// verifyContext: { | ||
// verified: { | ||
// isScam: false, | ||
// origin: '', | ||
// validation: 'VALID', | ||
// verifyUrl: '' | ||
// } | ||
// } | ||
// } | ||
// }) | ||
// } else if (pendingSessionRequests.length > 0) { | ||
// const latestRequest = pendingSessionRequests[pendingSessionRequests.length - 1] | ||
|
||
// try { | ||
// console.log('resolving request', latestRequest) | ||
// const response = await walletProvider.request(latestRequest as any) | ||
// } catch (e) { | ||
// console.error('error', e) | ||
// } | ||
// } | ||
|
||
}, [walletProvider]) | ||
|
||
|
||
useEffect(() => { | ||
console.log('appkit hook', isConnected, walletProvider) | ||
if (!isConnected || !walletProvider) { | ||
return | ||
} | ||
useEffect(() => { | ||
if (status === 'reconnecting' || status === 'connecting' || status === undefined) { | ||
ModalStore.open('AppKitConnectionModal', undefined) | ||
return | ||
} | ||
|
||
if (status === 'disconnected') { | ||
appkit.open() | ||
return | ||
} | ||
|
||
if (isConnected) { | ||
onConnect() | ||
} | ||
if (walletProvider) { | ||
ModalStore.close() | ||
appkit.close() | ||
|
||
onConnect() | ||
// walletProvider.onConnect(onConnect) | ||
|
||
walletProvider.onConnect(onConnect) | ||
walletProvider.onSetPreferredAccount(handleSetPreferredAccount) | ||
} | ||
|
||
}, [isConnected, walletProvider]) | ||
}, [isConnected, walletProvider, status]) | ||
} |
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
Oops, something went wrong.