Back to top
The createWallet function is used to create a wallet interface with a specified connector.
type CreateWallet = (connector: Connector) => Wallet;
Example usage:
import { MetaMask } from '@react-web3-wallet/MetaMask'; import { createWallet } from '@react-web3-wallet/react'; const metaMask = createWallet(new MetaMask());
interface Wallet { getWalletName: () => WalletName; connect: (chain?: number | AddEthereumChainParameter) => Promise<void>; autoConnect: () => Promise<boolean>; disconnect: (force?: boolean) => Promise<void>; watchAsset: (asset: WatchAssetParameter) => Promise<void>; useIsConnecting: () => boolean; useIsConnected: () => boolean; useChainId: () => number | undefined; useAccount: () => string | undefined; useBalance: () => bigint | undefined; useProvider: (network?: Networkish) => BrowserProvider | undefined; useHasProvider: ( providerFilter?: (provider: Provider) => boolean, detectProviderOptions?: DetectProviderOptions, ) => boolean; }
interface Wallet { getWalletName: () => WalletName; }
Returns the name of the wallet. A dApp can support multiple wallets, and the wallet name serves as a unique identifier for each wallet.
export interface AddEthereumChainParameter { chainId: number; chainName: string; nativeCurrency: { name: string; symbol: string; decimals: 18; }; rpcUrls: string[]; blockExplorerUrls?: string[]; /** * Currently ignored. */ iconUrls?: string[]; } type ChainId = number interface Wallet { connect(chain?: ChainId | AddEthereumChainParameter) => Promise<void> }
Initiates a connection to the wallet. You can pass the chain
parameter to specify the desired chain to connect to. If the user is already connected to this chain, no additional steps will be taken. Otherwise, the user will be prompted to switch to the chain, if one of two conditions is met:
interface Wallet { autoConnect() => Promise<boolean> }
Attempts to connect to the wallet automatically. The autoConnect function never rejects and always resolves. It only attempts to establish a connection without requiring further user interaction during the connecting process.
the returned promise will:
true
if the connection succeeded.false
if the connection failed.interface Wallet { disconnect(force?: boolean) => Promise<void> }
Disconnects the wallet. Wallet connector implementations should override this method if the wallet supports forceful disconnection.
What is force disconnection?
For certain wallets like MetaMask, there is no way to forcefully disconnect from dApps.
For other wallets like WalletConnect, users have the option to forcefully disconnect from dApps.
This method is specified by EIP-747
interface WatchAssetParameter { address: string; symbol: string; decimals: number; image: string; } interface Wallet { watchAsset: (asset: WatchAssetParameter) => Promise<void>; }
Adds an asset to the wallet's list of tracked assets.
interface Wallet { useIsConnecting: () => boolean; }
Indicates whether there's a pending connection. A connection can be initiated by calling either Wallet.connect
or Wallet.autoConnect
.
interface Wallet { useAccount: () => string | undefined; }
Returns the active user wallet account(the account that the user current selected in the wallet extension/app).
interface Wallet { useBalance: () => bigint | undefined; }
Returns the active user wallet account balance.
Returns the active user wallet chainId.
interface Wallet { useChainId: () => number| undefined; }
interface Wallet { useIsConnected: () => boolean; }
Indicates whether user is connected to the wallet. If isConnected
is true, we can assume that:
useIsConnected is a convenient hook:
const useIsConnected = (): boolean => { return !!useAccount() && !!useChainId(); };
interface Wallet { useProvider: (network?: Networkish) => BrowserProvider | undefined; }
Returns a BrowserProvider
instance that wraps the underling wallet provider.
useHasProvider
can be used to detect whether a wallet is available(installed) on user's device(browser).
const hasMetaMask = metaMask.useHasProvider(); if (!hasMetaMask) { // redirect user to the wallet install page }