The FHEVM React Template is an ultra-minimal React project for building and running an FHEVM-enabled dApp.
It works alongside the fhevm-hardhat-template
and provides a simple development frontend for interacting with the FHECounter.sol
contract.
This template also illustrates how to run your FHEVM-dApp on both Sepolia as well as a local Hardhat Node (much faster).
- @zama-fhe/relayer-sdk: Fully Homomorphic Encryption for Ethereum Virtual Machine
- React: Modern UI framework for building interactive interfaces
- Next.js: Next-generation frontend build tool
- Tailwind: Utility-first CSS framework for rapid UI development
- You need to have Metamask browser extension installed on your browser.
Follow the step-by-step guide in the Hardhat + MetaMask documentation to set up your local devnet using Hardhat and MetaMask.
- Name: Hardhat
- RPC URL: http://127.0.0.1:8545
- Chain ID: 31337
- Currency symbol: ETH
- Clone this repository.
- From the repo root, run:
npm install
- Setup your hardhat environment variables:
Follow the detailed instructions in the FHEVM documentation to setup MNEMONIC
+ INFURA_API_KEY
Hardhat environment variables
- Start a local Hardhat node (new terminal):
# Default RPC: http://127.0.0.1:8545 | chainId: 31337
npm run hardhat-node
- Launch the frontend in mock mode:
npm run dev:mock
-
Start your browser with the Metamask extension installed and open http://localhost:3000
-
Open the Metamask extension to connect to the local Hardhat node i. Select Add network. ii. Select Add a network manually. iii. Enter your Hardhat Network RPC URL, http://127.0.0.1:8545 (or http://localhost:8545). iv. Enter your Hardhat Network chain ID, 31337 (or 0x539 in hexadecimal format).
- Deploy your contract on Sepolia Testnet
npm run deploy:sepolia
-
In your browser open
http://localhost:3000
-
Open the Metamask extension to connect to the Sepolia network
When using MetaMask as a wallet provider with a development node like Hardhat, you may encounter two common types of errors:
MetaMask tracks wallet nonces (the number of transactions sent from a wallet). However, if you restart your Hardhat node, the nonce is reset on the dev node, but MetaMask does not update its internal nonce tracking. This discrepancy causes a nonce mismatch error.
MetaMask caches the results of view function calls. If you restart your Hardhat node, MetaMask may return outdated cached data corresponding to a previous instance of the node, leading to incorrect results.
To fix the nonce mismatch error, simply clear the MetaMask cache:
- Open the MetaMask browser extension.
- Select the Hardhat network.
- Go to Settings > Advanced.
- Click the "Clear Activity Tab" red button to reset the nonce tracking.
The correct way to do this is also explained here.
To fix the view function result mismatch:
- Restart the entire browser. MetaMask stores its cache in the extension's memory, which cannot be cleared by simply clearing the browser cache or using MetaMask's built-in cache cleaning options.
By following these steps, you can ensure that MetaMask syncs correctly with your Hardhat node and avoid potential issues related to nonces and cached view function results.
-
<root>/packages/site/fhevm
: This folder contains the essential hooks needed to interact with FHEVM-enabled smart contracts. It is meant to be easily copied and integrated into any FHEVM + React project. -
<root>/packages/site/hooks/useFHECounter.tsx
: A simple React custom hook that demonstrates how to use theuseFhevm
hook in a basic use case, serving as an example of integration.
<root>/packages/site/hooks/metamask
: This folder includes hooks designed to manage the MetaMask Wallet provider. These hooks can be easily adapted or replaced to support other wallet providers, following the EIP-6963 standard,- Additionally, the project is designed to be flexible, allowing developers to easily replace
ethers.js
with a more React-friendly library of their choice, such asWagmi
.
- Hardhat + MetaMask: Set up your local devnet step by step using Hardhat and MetaMask.
- FHEVM Documentation
- FHEVM Hardhat
- @zama-fhe/relayer-sdk Documentation
- Setting up MNEMONIC and INFURA_API_KEY
- React Documentation
- FHEVM Discord Community
- GitHub Issues
This project is licensed under the BSD-3-Clause-Clear License - see the LICENSE file for details.