Skip to content

Commit

Permalink
Don't remove wallet account if storageAddress is undefined (#595)
Browse files Browse the repository at this point in the history
* Don't remove wallet account if `storageAddress` is undefined
This is to fix a bug where the wallet account is sometimes deleted
when switching between tenants

* Change storage key for account so that it's not cached per tenant

* Add log statements

* Add more log statements

* Fix condition for initializing wallet

* Flip condition for initializing wallet

* Remove condition for account check

* Add log statement

* Add another log statement

* Add logs for talisman

* Add delay for initializing wallets

* Revert unnecessary changes

* Address comments

---------

Co-authored-by: Kacper Szarkiewicz <[email protected]>
  • Loading branch information
ebma and Sharqiewicz authored Nov 4, 2024
1 parent 457fd1a commit 6d23258
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/GlobalStateProvider/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const initTalisman = async (dAppName: string, selected?: string) => {
if (!wallet) return;
await wallet.enable(dAppName);
const accounts = await wallet.getAccounts();
const selectedWallet = accounts.find((a) => a.address === selected) || accounts[0];
return selectedWallet;
return accounts.find((a) => a.address === selected) || accounts[0];
};

const initWalletConnect = async (chainId: string) => {
Expand Down
28 changes: 15 additions & 13 deletions src/GlobalStateProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import { ThemeName } from '../models/Theme';
import { storageService } from '../services/storage/local';
import { handleWalletConnectDisconnect, initSelectedWallet } from './helpers';

const SECONDS_IN_A_DAY = 86400;
const EXPIRATION_PERIOD = 2 * SECONDS_IN_A_DAY; // 2 days

export interface GlobalState {
dAppName: string;
tenantName: TenantName;
Expand Down Expand Up @@ -48,8 +45,7 @@ const GlobalStateProvider = ({ children }: { children: JSX.Element }) => {
set,
clear,
} = useLocalStorage<string | undefined>({
key: `${storageKeys.ACCOUNT}-${tenantName}`,
expire: EXPIRATION_PERIOD,
key: `${storageKeys.ACCOUNT}`,
});

const clearLocalStorageWallets = () => {
Expand All @@ -71,21 +67,27 @@ const GlobalStateProvider = ({ children }: { children: JSX.Element }) => {
[set],
);

const accountAddress = walletAccount?.address;
useEffect(() => {
const run = async () => {
const delayWalletInitialization = async (storageAddress: string) => {
// Delay the wallet initialization to allow the wallet extension to be injected, otherwise the call to wallet.enable()
// might fail see https://github.com/TalismanSociety/talisman-connect/issues/25
setTimeout(async () => {
const selectedWallet = await initSelectedWallet(dAppName, tenantName, storageAddress);
if (selectedWallet) setWallet(selectedWallet);
}, 400);
};

const initializeWallet = () => {
if (!storageAddress) {
removeWalletAccount();
return;
}
// skip if tenant already initialized
if (tenantRef.current === tenantName || accountAddress) return;
if (tenantRef.current === tenantName) return;
tenantRef.current = tenantName;
const selectedWallet = await initSelectedWallet(dAppName, tenantName, storageAddress);
if (selectedWallet) setWallet(selectedWallet);
delayWalletInitialization(storageAddress).catch(console.error);
};
run();
}, [storageAddress, removeWalletAccount, dAppName, tenantName, accountAddress]);
initializeWallet();
}, [storageAddress, dAppName, tenantName]);

const providerValue = useMemo<GlobalState>(
() => ({
Expand Down

0 comments on commit 6d23258

Please sign in to comment.