Configure your widget for seamless wallet management
The widget has a built-in wallet management UI, so you can connect the wallet and use the widget as a standalone dApp out of the box. However, when embedding the widget into the dApp, reusing the existing wallet management UI of that dApp makes the most sense.
There are several ecosystems and types of chains (EVM, SVM, UTXO) supported by the widget, and therefore, there are several different libraries used to manage wallet connections to these chains.
EVM wallet connection
To manage wallet connection to EVM (Ethereum Virtual Machine) chains, switching chains, etc., the widget uses the Wagmi library internally and also provides first-class support for all Wagmi-based libraries such as RainbowKit.
If you already manage wallets using Wagmi or Wagmi-based library in your dApp and the Widget detects that it is wrapped in WagmiProvider it will start re-using your wallet management without any additional configuration.
The example below shows how to preconfigure a basic wallet management using Wagmi.
import { LiFiWidget } from'@lifi/widget';import { createClient } from'viem';import { WagmiProvider, createConfig, http } from'wagmi';import { mainnet, arbitrum, optimism, scroll } from'wagmi/chains';import { injected } from'wagmi/connectors';constwagmiConfig=createConfig({// Make sure to provide the full list of chains// you would like to support in the Widget// and keep them in sync, so all functionality// like switching chains can work correctly. chains: [mainnet, arbitrum, optimism, scroll], connectors: [injected()],client({ chain }) {returncreateClient({ chain, transport:http() }); },});exportconstWidgetPage= () => {return ( <WagmiProviderconfig={wagmiConfig} reconnectOnMount> <LiFiWidgetintegrator="wagmi-example" /> </WagmiProvider> );};
Keep chains in sync
It is important to keep the Wagmi chains configuration in sync with the Widget chain list so all functionality, like switching chains, can keep working correctly.
There are two approaches to this:
Manually update the Widget and Wagmi chains configuration to specify all chains you would like to support in your dApp and the Widget. See Configure Widget page to know more about the Widget's allow/deny chains configuration.
Get all available chains from LI.FI API and dynamically update Wagmi configuration. The Widget provides hooks to ease this approach.
Here is an example of how to support all available LI.FI chains dynamically using Wagmi, RainbowKit, and additional hooks from @lifi/widget package.
import { convertExtendedChain } from'@lifi/wallet-management';import { useAvailableChains } from'@lifi/widget';import { RainbowKitProvider, getDefaultConfig } from'@rainbow-me/rainbowkit';import { useMemo,type FC,type PropsWithChildren } from'react';importtype { Chain } from'viem';import { WagmiProvider } from'wagmi';import { mainnet } from'wagmi/chains';exportconstWalletProvider:FC<PropsWithChildren> = ({ children }) => {const { chains } =useAvailableChains();constwagmiConfig=useMemo(() => {const_chains: [Chain,...Chain[]] =chains?.length? (chains.map(convertExtendedChain) as [Chain,...Chain[]]): [mainnet];// Wagmi currently doesn't support updating the config after its creation,// so in order to keep the dynamic chains list updated, we need to // re-create a config every time the chains list changes.constwagmiConfig=getDefaultConfig({ appName:'LI.FI Widget Example', chains: _chains, projectId:'Your WalletConnect ProjectId', ssr:!chains?.length, });return wagmiConfig; }, [chains]);return ( <WagmiProviderconfig={wagmiConfig}reconnectOnMount={Boolean(chains?.length)} > <RainbowKitProvider>{children}</RainbowKitProvider> </WagmiProvider> );};
Please check out our complete RainbowKit example in the widget repository here.
Support for Ethers.js and other alternatives
Developers can still use Ethers.js or any other alternative library in their project and convert Signer/Provider objects to Wagmi's injected connector before wrapping the Widget with WagmiProvider.
SVM wallet connection
To manage wallet connection to SVM (Solana Virtual Machine) chains the widget uses the Solana Wallet Standard library.
If you already manage wallets using Solana Wallet Standard library in your dApp and the Widget detects that it is wrapped in ConnectionProvider and WalletProvider it will start re-using your wallet management without any additional configuration.
The example below shows how to preconfigure a basic wallet management for SVM.
importtype { Adapter } from'@solana/wallet-adapter-base';import { WalletAdapterNetwork } from'@solana/wallet-adapter-base';import { ConnectionProvider, WalletProvider,} from'@solana/wallet-adapter-react';import { clusterApiUrl } from'@solana/web3.js';importtype { FC, PropsWithChildren } from'react';constendpoint=clusterApiUrl(WalletAdapterNetwork.Mainnet);/** * Wallets that implement either of these standards will be available automatically. * * - Solana Mobile Stack Mobile Wallet Adapter Protocol * (https://github.com/solana-mobile/mobile-wallet-adapter) * - Solana Wallet Standard * (https://github.com/solana-labs/wallet-standard) * * If you wish to support a wallet that supports neither of those standards, * instantiate its legacy wallet adapter here. Common legacy adapters can be found * in the npm package `@solana/wallet-adapter-wallets`. */constwallets:Adapter[] = [];exportconstSolanaWalletProvider:FC<PropsWithChildren> = ({ children }) => {return ( <ConnectionProviderendpoint={endpoint}> <WalletProviderwallets={wallets} autoConnect> {children} </WalletProvider> </ConnectionProvider> );};
There are additional configurations to smooth integration for external wallet management or in case of internal one provide options for WalletConnect and Coinbase Wallet.
Using internal wallet management clicking the Connect wallet button triggers the opening of an internal wallet menu. In cases where external wallet management is used we provide onConnect configuration option. This option allows developers to specify a callback function that will be executed when the Connect wallet button is clicked.
Please see modified RainbowKit example below. Here we use openConnectModal function provided by useConnectModal hook to open RainbowKit wallet menu when the Connect wallet button is clicked.
import { formatChain, useAvailableChains } from'@lifi/widget';import { RainbowKitProvider, getDefaultConfig } from'@rainbow-me/rainbowkit';import { useMemo,type FC,type PropsWithChildren } from'react';importtype { Chain } from'viem';import { WagmiProvider } from'wagmi';import { mainnet } from'wagmi/chains';exportconstWalletProvider:FC<PropsWithChildren> = ({ children }) => {const { chains } =useAvailableChains();constwagmiConfig=useMemo(() => {const_chains: [Chain,...Chain[]] =chains?.length? (chains.map(formatChain) as [Chain,...Chain[]]): [mainnet];// Wagmi currently doesn't support updating the config after its creation,// so in order to keep the dynamic chains list updated, we need to // re-create a config every time the chains list changes.constwagmiConfig=getDefaultConfig({ appName:'LI.FI Widget Example', chains: _chains, projectId:'Your WalletConnect ProjectId', ssr:!chains?.length, });return wagmiConfig; }, [chains]);return ( <WagmiProviderconfig={wagmiConfig}reconnectOnMount={Boolean(chains?.length)} > <RainbowKitProvider>{children}</RainbowKitProvider> </WagmiProvider> );};
WalletConnect and Coinbase Wallet
We provide additional configuration for WalletConnect and Coinbase Wallet Wagmi connectors so when using built-in wallet management in the widget you can set WalletConnect's projectId or Coinbase Wallet's appName parameters.