diff --git a/packages/widget/src/providers/WalletProvider/WalletProvider.tsx b/packages/widget/src/providers/WalletProvider/WalletProvider.tsx index 3cbeb3288..04fcee8e2 100644 --- a/packages/widget/src/providers/WalletProvider/WalletProvider.tsx +++ b/packages/widget/src/providers/WalletProvider/WalletProvider.tsx @@ -19,6 +19,8 @@ import { useMemo, useState, } from 'react'; +import { useWidgetEvents } from '../../hooks'; +import { WidgetEvent } from '../../types'; import { useWidgetConfig } from '../WidgetProvider'; import type { WalletAccount, WalletContextProps } from './types'; @@ -44,6 +46,7 @@ const WalletContext = createContext(initialContext); export const useWallet = (): WalletContextProps => useContext(WalletContext); export const WalletProvider: FC = ({ children }) => { + const emitter = useWidgetEvents(); const { walletManagement } = useWidgetConfig(); const [account, setAccount] = useState({}); const [currentWallet, setCurrentWallet] = useState(); @@ -52,6 +55,7 @@ export const WalletProvider: FC = ({ children }) => { setCurrentWallet(wallet); const account = await extractAccountFromSigner(wallet?.account?.signer); setAccount(account); + return account; }; const connect = useCallback( @@ -60,13 +64,21 @@ export const WalletProvider: FC = ({ children }) => { const signer = await walletManagement.connect(); const account = await extractAccountFromSigner(signer); setAccount(account); + emitter.emit(WidgetEvent.WalletConnected, { + address: account.address, + chainId: account.chainId, + }); return; } await liFiWalletManagement.connect(wallet); wallet.on('walletAccountChanged', handleWalletUpdate); - handleWalletUpdate(wallet); + const account = await handleWalletUpdate(wallet); + emitter.emit(WidgetEvent.WalletConnected, { + address: account.address, + chainId: account.chainId, + }); }, - [walletManagement], + [emitter, walletManagement], ); const disconnect = useCallback(async () => { @@ -78,7 +90,7 @@ export const WalletProvider: FC = ({ children }) => { if (currentWallet) { await liFiWalletManagement.disconnect(currentWallet); currentWallet.removeAllListeners(); - handleWalletUpdate(undefined); + await handleWalletUpdate(undefined); } }, [currentWallet, walletManagement]); @@ -98,7 +110,7 @@ export const WalletProvider: FC = ({ children }) => { await walletAgnosticSwitchChain(provider, chainId); } else { await currentWallet?.switchChain(chainId); - handleWalletUpdate(currentWallet); + await handleWalletUpdate(currentWallet); } // TODO: this will fail if it's not created with ethers 'any' network, replace with the new signer when possible return account.signer; @@ -122,7 +134,7 @@ export const WalletProvider: FC = ({ children }) => { await walletAgnosticAddChain(provider, chainId); } else { await currentWallet?.addChain(chainId); - handleWalletUpdate(currentWallet); + await handleWalletUpdate(currentWallet); } return true; @@ -146,7 +158,7 @@ export const WalletProvider: FC = ({ children }) => { await walletAgnosticAddToken(provider, chainId, token); } else { await currentWallet?.addToken(chainId, token); - handleWalletUpdate(currentWallet); + await handleWalletUpdate(currentWallet); } } catch {} }, @@ -166,7 +178,7 @@ export const WalletProvider: FC = ({ children }) => { } await liFiWalletManagement.autoConnect(activeWallets); activeWallets[0].on('walletAccountChanged', handleWalletUpdate); - handleWalletUpdate(activeWallets[0]); + await handleWalletUpdate(activeWallets[0]); }; autoConnect(); }, []); diff --git a/packages/widget/src/types/events.ts b/packages/widget/src/types/events.ts index 40b368a7a..9795fa668 100644 --- a/packages/widget/src/types/events.ts +++ b/packages/widget/src/types/events.ts @@ -11,6 +11,7 @@ export enum WidgetEvent { DestinationChainTokenSelected = 'destinationChainTokenSelected', SendToWalletToggled = 'sendToWalletToggled', ReviewTransactionPageEntered = 'reviewTransactionPageEntered', + WalletConnected = 'walletConnected', } export type WidgetEvents = { @@ -24,6 +25,7 @@ export type WidgetEvents = { destinationChainTokenSelected: ChainTokenSelected; sendToWalletToggled: boolean; reviewTransactionPageEntered?: Route; + walletConnected: WalletConnected; }; export interface RouteContactSupport { @@ -46,3 +48,8 @@ export interface ChainTokenSelected { chainId: ChainId; tokenAddress: string; } + +export interface WalletConnected { + chainId?: number; + address?: string; +}