Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/contexts/PoolzAppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Address } from "viem";
import type { UseSwitchChainReturnType } from "wagmi";
import { getWalletClient } from "wagmi/actions";
import { WalletClient } from "viem";
import { useMetaMaskLockState } from "../hooks/useMetaMaskLockState";

export const CUSTOMER_ACCOUNT_VARIABLE = "__CUSTOMER_ACCOUNT__";

Expand All @@ -21,6 +22,11 @@ export interface PoolzAppContextType {
chain?: Chain;
chainId: number;

// MetaMask lock state
isWalletLocked: boolean;
isWalletUnlocked: boolean;
lastLockChangeTimestamp: number | null;

switchChainAsync: UseSwitchChainReturnType['switchChainAsync'];
isSwitching: boolean;
watchAsset: ReturnType<typeof useWatchAsset>;
Expand All @@ -42,6 +48,7 @@ export const PoolzAppProvider: React.FC<PoolzAppProviderProps> = ({ children })
const { switchChainAsync, isPending } = useSwitchChain();
const watchAsset = useWatchAsset();
const publicClient = usePublicClient()
const { isLocked, isUnlocked, lastChangeTimestamp } = useMetaMaskLockState();

const [walletClient, setWalletClient] = useState<WalletClient | undefined>(undefined);
// Customer account management
Expand Down Expand Up @@ -91,6 +98,11 @@ export const PoolzAppProvider: React.FC<PoolzAppProviderProps> = ({ children })
chain,
chainId,

// MetaMask lock state
isWalletLocked: isLocked,
isWalletUnlocked: isUnlocked,
lastLockChangeTimestamp: lastChangeTimestamp,

switchChainAsync,
isSwitching: isPending,
watchAsset,
Expand Down
81 changes: 81 additions & 0 deletions src/hooks/useMetaMaskLockState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useEffect, useState } from "react";
import { useAccount } from "wagmi";

export interface MetaMaskLockState {
isLocked: boolean;
isUnlocked: boolean;
lastChangeTimestamp: number | null;
}

/**
* Hook to detect MetaMask lock/unlock state changes
*
* This hook listens to the `accountsChanged` event from MetaMask.
* When MetaMask is locked, it emits an empty accounts array.
* When unlocked, it emits the accounts array with addresses.
*
* @returns {MetaMaskLockState} Object containing lock state information
*
* @example
* ```tsx
* const { isLocked, isUnlocked, lastChangeTimestamp } = useMetaMaskLockState();
*
* useEffect(() => {
* if (isLocked) {
* console.log('MetaMask is locked');
* }
* }, [isLocked]);
* ```
*/
export const useMetaMaskLockState = (): MetaMaskLockState => {
const { isConnected, address } = useAccount();
const [isLocked, setIsLocked] = useState(false);
const [lastChangeTimestamp, setLastChangeTimestamp] = useState<number | null>(
null,
);

useEffect(() => {
// Only listen if ethereum provider exists (MetaMask installed)
if (
globalThis.window === undefined ||
!(globalThis as any).ethereum
) {
return;
}

const ethereum = (globalThis as any).ethereum;

const handleAccountsChanged = (accounts: string[]) => {
const timestamp = Date.now();
setLastChangeTimestamp(timestamp);

// If accounts array is empty and we were connected, MetaMask is locked
if (accounts.length === 0 && isConnected) {
setIsLocked(true);
} else if (accounts.length > 0) {
// MetaMask is unlocked and has accounts
setIsLocked(false);
}
};

// Listen to accounts changed event
ethereum.on("accountsChanged", handleAccountsChanged);

// Initial check
if (isConnected && address) {
setIsLocked(false);
}

return () => {
if (ethereum.removeListener) {
ethereum.removeListener("accountsChanged", handleAccountsChanged);
}
};
}, [isConnected, address]);

return {
isLocked,
isUnlocked: !isLocked && isConnected,
lastChangeTimestamp,
};
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export type { TokenBalance, NativeBalance } from "./contexts/BalanceContext";
export { useSidNameForAddress } from "./hooks/useSidNameForAddress";
export { useWalletConnection } from "./hooks/useWalletConnection";
export { useTheSiwe } from "./hooks/useTheSiwe";
export { useMetaMaskLockState } from "./hooks/useMetaMaskLockState";
export type { MetaMaskLockState } from "./hooks/useMetaMaskLockState";
export { contractsByChain } from "./contracts";

export type {
Expand Down