UNPKG

@lifi/wallet-management

Version:

LI.FI Wallet Management solution.

62 lines 2.83 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { ChainType } from '@lifi/sdk'; import { useConfig } from 'wagmi'; import { connect, disconnect, getAccount } from 'wagmi/actions'; import { useLastConnectedAccount } from '../hooks/useAccount.js'; import { useWalletManagementEvents } from '../hooks/useWalletManagementEvents.js'; import { getChainTypeIcon } from '../icons.js'; import { WalletManagementEvent } from '../types/events.js'; import { WalletTagType } from '../types/walletTagType.js'; import { createWalletConnectElement } from '../utils/elements.js'; import { getConnectorIcon } from '../utils/getConnectorIcon.js'; import { isWalletInstalled } from '../utils/isWalletInstalled.js'; import { CardListItemButton } from './CardListItemButton.js'; export const EVMListItemButton = ({ ecosystemSelection, connector, tagType, onNotInstalled, onConnected, onConnecting, onError, }) => { const emitter = useWalletManagementEvents(); const config = useConfig(); const { setLastConnectedAccount } = useLastConnectedAccount(); const connectorName = connector.displayName || connector.name; const connectorDisplayName = ecosystemSelection ? 'Ethereum' : connectorName; const handleEVMConnect = async () => { if (tagType === WalletTagType.Connected) { onConnected?.(); return; } try { const identityCheckPassed = isWalletInstalled(connector.id); if (!identityCheckPassed) { onNotInstalled?.(connector); return; } if (connector.id === 'walletConnect') { createWalletConnectElement(); } const connectedAccount = getAccount(config); onConnecting?.(); const data = await connect(config, { connector }); if (connectedAccount.connector) { await disconnect(config, { connector: connectedAccount.connector }); } setLastConnectedAccount(connector); emitter.emit(WalletManagementEvent.WalletConnected, { address: data.accounts[0], chainId: data.chainId, chainType: ChainType.EVM, connectorId: connector.id, connectorName: connectorName, }); onConnected?.(); } catch (error) { onError?.(error); } }; return (_jsx(CardListItemButton, { icon: ecosystemSelection ? getChainTypeIcon(ChainType.EVM) : (getConnectorIcon(connector) ?? ''), onClick: handleEVMConnect, title: connectorDisplayName, tagType: ecosystemSelection && tagType !== WalletTagType.Connected ? undefined : tagType }, connector.id)); }; //# sourceMappingURL=EVMListItemButton.js.map