UNPKG

@web3auth/modal

Version:

Multi chain wallet aggregator for web3Auth

42 lines (39 loc) 1.42 kB
import { Connection } from '@solana/web3.js'; import { SolanaWallet, CHAIN_NAMESPACES } from '@web3auth/no-modal'; import { useState, useMemo, useEffect } from 'react'; import { useWeb3Auth } from '../../hooks/useWeb3Auth.js'; const useSolanaWallet = () => { const { provider, web3Auth } = useWeb3Auth(); const [accounts, setAccounts] = useState(null); const solanaWallet = useMemo(() => { if (!provider) return null; return new SolanaWallet(provider); }, [provider]); const connection = useMemo(() => { if (!web3Auth || !provider) return null; return new Connection(web3Auth.currentChain.rpcTarget); }, [web3Auth, provider]); useEffect(() => { const init = async () => { var _web3Auth$currentChai; if (!(web3Auth !== null && web3Auth !== void 0 && (_web3Auth$currentChai = web3Auth.currentChain) !== null && _web3Auth$currentChai !== void 0 && _web3Auth$currentChai.chainNamespace) || web3Auth.currentChain.chainNamespace !== CHAIN_NAMESPACES.SOLANA) { return; } if (!solanaWallet) return; const accounts = await solanaWallet.getAccounts(); if ((accounts === null || accounts === void 0 ? void 0 : accounts.length) > 0) { setAccounts(accounts); } }; if (solanaWallet) init(); }, [solanaWallet, web3Auth]); return { solanaWallet, accounts, connection }; }; export { useSolanaWallet };