Integrating with web3 libraries
Using web3 libraries to interact with accounts.
Openfort's accounts object is fully compatible with popular web3 libraries for interfacing wallets, including ethers
, and wagmi
.
Read below to learn how to best integrate Openfort alongside these libraries.
Ethers#
Ethers represents connected wallets as a provider, which can be used to take read-only actions with the wallet, and a signer, which can be used to take write actions (signatures and transactions).
Call the wallet's getEvmProvider
method to get a provider:
_10const provider = getEvmProvider();_10_10const web3Provider = new ethers.providers.Web3Provider(provider);
Then, call the provider's getSigner
method to get the corresponding signer:
_10const signer = web3Provider.getSigner();
Wagmi#
Wagmi is a set of React hooks for interfacing with Ethereum wallets, allowing you read wallet state, request signatures or transactions, and take read and write actions on the blockchain.
1. Setup dependencies#
First, ensure you have the required dependencies installed:
_10npm i wagmi @tanstack/react-query @openfort/openfort-js
2. Setup TanStack Query#
To start, set up your app with the TanStack Query's React Provider. Wagmi uses TanStack Query under the hood to power its data fetching and caching of wallet and blockchain data.
_10import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
Next, create a new instance of the QueryClient
:
_10const queryClient = new QueryClient();
Then, wrap your app's components with the QueryClientProvider
. This must be rendered inside the WagmiProvider
component.
_10<WagmiProvider config={config}>_10 <QueryClientProvider client={queryClient}>_10 <Connect />_10 </QueryClientProvider>_10 </WagmiProvider>
For the client
property of the QueryClientProvider
, pass the queryClient
instance you created.
3. Setup Wagmi#
To build your wagmi
config, import the createConfig
method. Next, import your app's required chains from viem/chains
and the http
transport from wagmi
. Your app's required chains should match whatever you configure as supportedChains
for Openfort.
_14import { createConfig, http } from 'wagmi';_14import { sepolia } from 'wagmi/chains';_14import { injected } from 'wagmi/connectors';_14import { openfortConnector } from './openfortConnector'; // You'll need to create this_14_14export const config = createConfig({_14 chains: [sepolia],_14 connectors: [injected(), openfortConnector],_14 transports: {_14 [sepolia.id]: http(),_14 // For each of your required chains, add an entry to `transports` with_14 // a key of the chain's `id` and a value of `http()`_14 },_14});
4. Create Openfort Connector#
You'll need to create a custom connector for Openfort. Create a new file openfortConnector.ts
:
_25import {QueryClient, QueryClientProvider} from '@tanstack/react-query';_25import {WagmiProvider} from 'wagmi';_25_25import {Connect} from './components/Connect';_25import {config} from './wagmi';_25import {useEffect} from 'react';_25import {openfortInstance} from './main';_25_25const queryClient = new QueryClient();_25_25export default function App() {_25 useEffect(() => {_25 if (!openfortInstance) return;_25 openfortInstance.getEmbeddedState();_25 openfortInstance.getEthereumProvider(); // EIP-6963_25 }, [openfortInstance]);_25_25 return (_25 <WagmiProvider config={config}>_25 <QueryClientProvider client={queryClient}>_25 <Connect />_25 </QueryClientProvider>_25 </WagmiProvider>_25 );_25}
Complete example Altogether, this should look like:
That's it! You've successfully integrated Openfort alongside wagmi
in your app! 🎉
5. Use Wagmi hooks in your components#
Once you've completed the setup above, you can use wagmi's React hooks throughout your app to interface with wallets and take read and write actions on the blockchain.
To use wagmi hooks, like useAccount
, in your components, import the hook directly from wagmi
and call it as usual:
_10import {useAccount} from 'wagmi';_10_10export default const WalletAddress = () => {_10 const {address} = useAccount();_10 return <p>Wallet address: {address}</p>;_10}
Demo app#
Check out our wagmi demo app to see the hooks listed above in action.
Feel free to take a look at the app's source code to see an end-to-end implementation of Openfort with wagmi.