@web3auth/modal
Version:
Multi chain wallet aggregator for web3Auth
65 lines (62 loc) • 2 kB
JavaScript
import { Connection } from '@solana/web3.js';
import { CHAIN_NAMESPACES, SolanaWallet } from '@web3auth/no-modal';
import { useState, useMemo, useEffect } from 'react';
import { useChain } from '../../hooks/useChain.js';
import '@babel/runtime/helpers/objectSpread2';
import '@babel/runtime/helpers/defineProperty';
import '@web3auth/auth';
import 'deepmerge';
import '../../../config.js';
import '../../../ui/config.js';
import '../../../ui/css/index.css.js';
import 'bowser';
import 'react-dom/client';
import '@toruslabs/http-helpers';
import 'clsx';
import 'tailwind-merge';
import 'react/jsx-runtime';
import 'react-i18next';
import '../../../ui/localeImport.js';
import 'classnames';
import 'react-qrcode-logo';
import '../../../ui/components/Login/Login.js';
import { useWeb3Auth } from '../../hooks/useWeb3Auth.js';
const useSolanaWallet = () => {
const {
provider,
web3Auth
} = useWeb3Auth();
const {
chainNamespace
} = useChain();
const [accounts, setAccounts] = useState(null);
const solanaWallet = useMemo(() => {
if (!provider) return null;
if (chainNamespace !== CHAIN_NAMESPACES.SOLANA) return null;
return new SolanaWallet(provider);
}, [provider, chainNamespace]);
const connection = useMemo(() => {
if (!web3Auth || !provider || chainNamespace !== CHAIN_NAMESPACES.SOLANA) return null;
return new Connection(web3Auth.currentChain.rpcTarget);
}, [web3Auth, provider, chainNamespace]);
useEffect(() => {
const init = async () => {
if (chainNamespace !== CHAIN_NAMESPACES.SOLANA) {
setAccounts(null);
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, chainNamespace]);
return {
solanaWallet,
accounts,
connection
};
};
export { useSolanaWallet };