Metamask Integration with Wagmi: Waiting for Transactions to Complete
As developers, we are constantly looking for ways to improve our experience and increase security. Two popular tools that are making waves in the web3 space are Metamask and Wagmi. In this article, we will explore how to use Wagmi’s useWaitForTransaction
combination with Metamask running on a local Hardhat node.
What is Metamask?
Metamask is a popular Ethereum wallet that allows users to manage their accounts, balances, and transactions. It provides an API to interact with the blockchain, allowing developers to access various features such as wallets, token management, and more.
Wagmi’s useWaitForTransaction
Wagmi is a web3 connection management library that provides an easy way to interact with wallets, oracles, and other services. Its “useWaitForTransaction” hook allows you to wait for transactions to complete on a specific wallet instance before processing the result.
Setting up Metamask on Hardhat
To use Metamask on your local Hardhat node, follow these steps:
- Install Metamask as a dependency in your project:
npm install metamask
- Create a new file calledmetamask.js
and add the following code to initialize Metamask:
import { createClient } from '@metamask/dApp';
const metamask = createClient({
address: '0x...YourMetamaskAddress...', // replace with your Metamask wallet address
network: 'mainnet', // select your network (e.g. mainnet, devnet)
});
export default metamask;
Integrating useWaitForTransactionwith Wagmi
Now that you've initialized Metamask, let's integrate it with Wagmi usinguseWaitForTransaction. Add the following code to your
metamask.jsfile:
import { useWaitForTransaction } from 'wagmi';
const wagmi = useWaitForTransaction({
hook: 'useMetamask',
args: {
client: metamask,
},
});
export default wagmi;
What does it do?
The useWaitForTransactionhook has a
hookparameter that specifies the library to wait for. In this case, we're using
useMetamask, which is Wagmi's native hook for interacting with Metamask.
We pass anargsobject containing an instance of
clientfrom Metamask, which is obtained using the
createClient()method.
Waiting for transactions on Metamask
When you callwagmi, it will wait for transactions to complete on your local Hardhat node using Metamask. This means that when a transaction is initiated,
wagmi:
- Wait for the transaction to be mined or confirmed.
- Once the transaction is confirmed (e.g. sent to a wallet),wagmi
extracts the result and sends it back to your application.
UsinguseWaitForTransaction, you can decouple your application's business logic from the underlying blockchain, making it easier to manage complex workflows and reducing errors.
Usage Example
Here is an example of how you can usewagmiwith Metamask to send a transaction:
import { wagmi } from './metamask';
const { chainId, accounts } = wait wagmi.createTransaction({
enter: 'eth_send_transaction',
data: {
// your transaction data here,
},
});
const result = wait chainId.toBuffer();
In this example, wagmicreates a new transaction using the Metamask API and sends it to the Ethereum mainnet. The variable
result
is then extracted from the transaction, which can be used by your application.
Conclusion
In this article, we have shown how to use Wagmi’s useWaitForTransaction
combination with Metamask running on a local Hardhat node. By decoupling your application’s business logic from the blockchain, you can create more complex and reliable applications that process transactions in a straightforward manner.