Back to top
The CurrentWallet feature enables dApps to support multiple wallets while keeping only one wallet active at a time. This built-in feature eliminates the need to handle the current wallet state in your application code.
While typically you only have one active wallet, there are scenarios where you may require multiple active wallets. In such cases, you can create multiple CurrentWallet instances.
type CreateCurrentWalletOptions = { defaultCurrentWallet?: WalletName; persistKey?: string; }; type CreateCurrentWallet = ( connectorsOrWallets: (Connector | Wallet)[], options: CreateCurrentWalletOptions, ) => CurrentWallet;
The createCurrentWallet function returns the currentWallet interface.
import { MetaMask } from '@react-web3-wallet/metamask'; import { DeFiWallet } from '@react-web3-wallet/defiwallet'; import { createCurrentWallet } from '@react-web3-wallet/react'; export const currentWallet = createCurrentWallet([ new MetaMask(), new DeFiWallet(), ]);
The defaultCurrentWallet option allows you to specify the default wallet for currentWallet. If not provided, the first wallet in the list will be set as the default.
The persistKey option allows you to specify the key used for persisting the currentWallet state in local storage. By default, the key is set to @react-web3-wallet/currentWallet
.
Most wallet APIs are available on the currentWallet object. The following additional APIs are specific to currentWallet. See also the Wallet documentation for other available APIs.
export interface CurrentWallet extends Wallet { useWalletName: () => WalletName; switchCurrentWallet: (name: WalletName) => void; connectAsCurrentWallet: ( name: WalletName, chain?: number | AddEthereumChainParameter, ) => Promise<void>; }
interface CurrentWallet { useWalletName: () => WalletName; }
Returns the currentWallet name.
export interface CurrentWallet { switchCurrentWallet: (name: WalletName) => void; }
Set the specified wallet as the currentWallet.
export interface CurrentWallet { connectAsCurrentWallet: ( name: WalletName, chain?: number | AddEthereumChainParameter, ) => Promise<void>; }
The connectAsCurrentWallet function connects to the specified wallet and sets it as the current wallet if the connection is successful.