Skip to content

Commit

Permalink
feat: add walletConnected event
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Oct 30, 2023
1 parent 417b0bd commit f916940
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/widget/src/providers/WalletProvider/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -44,6 +46,7 @@ const WalletContext = createContext<WalletContextProps>(initialContext);
export const useWallet = (): WalletContextProps => useContext(WalletContext);

export const WalletProvider: FC<PropsWithChildren> = ({ children }) => {
const emitter = useWidgetEvents();
const { walletManagement } = useWidgetConfig();
const [account, setAccount] = useState<WalletAccount>({});
const [currentWallet, setCurrentWallet] = useState<Wallet | undefined>();
Expand All @@ -52,6 +55,7 @@ export const WalletProvider: FC<PropsWithChildren> = ({ children }) => {
setCurrentWallet(wallet);
const account = await extractAccountFromSigner(wallet?.account?.signer);
setAccount(account);
return account;
};

const connect = useCallback(
Expand All @@ -60,13 +64,21 @@ export const WalletProvider: FC<PropsWithChildren> = ({ 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 () => {
Expand All @@ -78,7 +90,7 @@ export const WalletProvider: FC<PropsWithChildren> = ({ children }) => {
if (currentWallet) {
await liFiWalletManagement.disconnect(currentWallet);
currentWallet.removeAllListeners();
handleWalletUpdate(undefined);
await handleWalletUpdate(undefined);
}
}, [currentWallet, walletManagement]);

Expand All @@ -98,7 +110,7 @@ export const WalletProvider: FC<PropsWithChildren> = ({ 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;
Expand All @@ -122,7 +134,7 @@ export const WalletProvider: FC<PropsWithChildren> = ({ children }) => {
await walletAgnosticAddChain(provider, chainId);
} else {
await currentWallet?.addChain(chainId);
handleWalletUpdate(currentWallet);
await handleWalletUpdate(currentWallet);
}

return true;
Expand All @@ -146,7 +158,7 @@ export const WalletProvider: FC<PropsWithChildren> = ({ children }) => {
await walletAgnosticAddToken(provider, chainId, token);
} else {
await currentWallet?.addToken(chainId, token);
handleWalletUpdate(currentWallet);
await handleWalletUpdate(currentWallet);
}
} catch {}
},
Expand All @@ -166,7 +178,7 @@ export const WalletProvider: FC<PropsWithChildren> = ({ children }) => {
}
await liFiWalletManagement.autoConnect(activeWallets);
activeWallets[0].on('walletAccountChanged', handleWalletUpdate);
handleWalletUpdate(activeWallets[0]);
await handleWalletUpdate(activeWallets[0]);
};
autoConnect();
}, []);
Expand Down
7 changes: 7 additions & 0 deletions packages/widget/src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum WidgetEvent {
DestinationChainTokenSelected = 'destinationChainTokenSelected',
SendToWalletToggled = 'sendToWalletToggled',
ReviewTransactionPageEntered = 'reviewTransactionPageEntered',
WalletConnected = 'walletConnected',
}

export type WidgetEvents = {
Expand All @@ -24,6 +25,7 @@ export type WidgetEvents = {
destinationChainTokenSelected: ChainTokenSelected;
sendToWalletToggled: boolean;
reviewTransactionPageEntered?: Route;
walletConnected: WalletConnected;
};

export interface RouteContactSupport {
Expand All @@ -46,3 +48,8 @@ export interface ChainTokenSelected {
chainId: ChainId;
tokenAddress: string;
}

export interface WalletConnected {
chainId?: number;
address?: string;
}

0 comments on commit f916940

Please sign in to comment.