Actions
Actions are functions that will help you control the modal, subscribe to wallet events and interact with them and smart contracts.
Open and close the modalβ
const modal = createWeb3Modal({ wagmiConfig, projectId })
modal.open()
modal.close()
You can also select the modal's view when calling the open
function
modal.open({ view: 'Account' })
List of views you can select
Variable | Description |
---|---|
Connect | Principal view of the modal - default view when disconnected |
Account | User profile - default view when connected |
AllWallets | Shows the list of all available wallets |
Networks | List of available networks - you can select and target a specific network before connecting |
WhatIsANetwork | "What is a network" onboarding view |
WhatIsAWallet | "What is a wallet" onboarding view |
OnRampProviders | "OnRamp main view |
Disconnectβ
modal.disconnect()
WalletInfoβ
Metadata information from the connected wallet
function handler({ name, icon }){
console.log(name, icon)
}
modal.subscribeWalletInfo(handler)
//or
const { name, icon } = modal.getWalletInfo()
Ethereum Libraryβ
- Wagmi
- Ethers
You can use Wagmi actions to sign messages, interact with smart contracts, and much more.
getAccountβ
Action for accessing account data and connection status.
import { getAccount } from '@wagmi/core'
import { wagmiConfig } from './main'
const account = getAccount(wagmiConfig)
signMessageβ
Action for signing messages with connected account.
import { signMessage } from '@wagmi/core'
import { wagmiConfig } from './main'
await signMessage(wagmiConfig, { message: 'hello world' })
You can use the following methods to get data and subscribe to changes:
getAddressβ
const address = modal.getAddress()
getErrorβ
const error = modal.getError()
getChainIdβ
const chainId = modal.getChainId()
switchNetworkβ
const chainId = 137
modal.switchNetwork(chainId)
getIsConnectedβ
const isConnected = modal.getIsConnected()
getWalletProviderβ
The wallet provider.
const walletProvider = modal.getWalletProvider()
getWalletProviderTypeβ
const walletProviderType = modal.getWalletProviderType()
subscribeProviderβ
function handleChange({ provider, providerType, address, error, chainId, isConnected }) {
//...
}
modal.subscribeProvider(handleChange)
Modal Stateβ
Get the current value of the modal's state
const modal = createWeb3Modal({ wagmiConfig, projectId })
const { open, selectedNetworkId } = modal.getState()
The modal state is an object of two properties:
Property | Description | Type |
---|---|---|
open | Open state will be true when the modal is open and false when closed. | boolean |
selectedNetworkId | The current chain id selected by the user | number |
You can also subscribe to the modal's state changes.
const modal = createWeb3Modal({ wagmiConfig, projectId })
modal.subscribeState(newState => console.log(newState))
ThemeModeβ
Set the themeMode
after creating the modal
const modal = createWeb3Modal({ wagmiConfig, projectId })
modal.setThemeMode('dark')
Get the current themeMode
value by calling the getThemeMode
function
const modal = createWeb3Modal({ wagmiConfig, projectId })
const themeMode = modal.getThemeMode()
themeVariablesβ
Set the themeVariables
after creating the modal
const modal = createWeb3Modal({ wagmiConfig, projectId })
modal.setThemeVariables({ ... })
Get the current themeVariables
value by calling the getThemeVariables
function
const modal = createWeb3Modal({ wagmiConfig, projectId })
const themeMode = modal.getThemeVariables()
Subscribe to theme changesβ
const unsubscribe = modal.subscribeTheme(newTheme => console.log(newTheme))
Track modal eventsβ
modal.getEvent() // get last event
modal.subscribeEvents(event => console.log(event)) // subscribe to events
Was this helpful?