Home

Core

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:


_10
const provider = getEvmProvider();
_10
_10
const web3Provider = new ethers.providers.Web3Provider(provider);

Then, call the provider's getSigner method to get the corresponding signer:


_10
const 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:


_10
npm 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.


_10
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

Next, create a new instance of the QueryClient:


_10
const 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.


_14
import { createConfig, http } from 'wagmi';
_14
import { sepolia } from 'wagmi/chains';
_14
import { injected } from 'wagmi/connectors';
_14
import { openfortConnector } from './openfortConnector'; // You'll need to create this
_14
_14
export 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:


_25
import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
_25
import {WagmiProvider} from 'wagmi';
_25
_25
import {Connect} from './components/Connect';
_25
import {config} from './wagmi';
_25
import {useEffect} from 'react';
_25
import {openfortInstance} from './main';
_25
_25
const queryClient = new QueryClient();
_25
_25
export 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:

app

_25
import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
_25
import {WagmiProvider} from 'wagmi';
_25
_25
import {Connect} from './components/Connect';
_25
import {config} from './wagmi';
_25
import {useEffect} from 'react';
_25
import {openfortInstance} from './main';
_25
_25
const queryClient = new QueryClient();
_25
_25
export 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
}

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:


_10
import {useAccount} from 'wagmi';
_10
_10
export 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.