Run FHEVM tests in Hardhat
In this section, you'll find everything you need to test your FHEVM smart contracts in your Hardhat project.
FHEVM Runtime Modes
The FHEVM Hardhat plugin provides three FHEVM runtime modes tailored for different stages of contract development and testing. Each mode offers a trade-off between speed, encryption, and persistence.
The Hardhat (In-Memory) default network: 🧪 Uses mock encryption. Ideal for regular tests, CI test coverage, and fast feedback during early contract development. No real encryption is used.
The Hardhat Node (Local Server) network: 🧪 Uses mock encryption. Ideal when you need persistent state - for example, when testing frontend interactions, simulating user flows, or validating deployments in a realistic local environment. Still uses mock encryption.
The Sepolia Testnet network: 🔐 Uses real encryption. Use this mode once your contract logic is stable and validated locally. This is the only mode that runs on the full FHEVM stack with real encrypted values. It simulates real-world production conditions but is slower and requires Sepolia ETH.
Summary
Hardhat (default)
🧪 Mock
❌ No
In-Memory
⚡⚡ Very Fast
Fast local testing and coverage
Hardhat Node
🧪 Mock
✅ Yes
Server
⚡ Fast
Frontend integration and local persistent testing
Sepolia Testnet
🔐 Real Encryption
✅ Yes
Server
🐢 Slow
Full-stack validation with real encrypted data
The FHEVM Hardhat Template
To demonstrate the three available testing modes, we'll use the fhevm-hardhat-template, which comes with the FHEVM Hardhat Plugin pre-installed, a basic FHECounter
smart contract, and ready-to-use tasks for interacting with a deployed instance of this contract.
Run on Hardhat (default)
To run your tests in-memory using FHEVM mock values, simply run the following:
npx hardhat test --network hardhat
Run on Hardhat Node
You can also run your tests against a local Hardhat node, allowing you to deploy contract instances and interact with them in a persistent environment.
Launch the Hardhat Node server:
Open a new terminal window.
From the root project directory, run the following:
npx hardhat node
Run your test suite (optional):
From the root project directory:
npx hardhat test --network localhost
Deploy the FHECounter
smart contract on Hardhat Node
From the root project directory:
npx hardhat deploy --network localhost
Check the deployed contract FHEVM configuration:
npx hardhat fhevm check-fhevm-compatibility --network localhost --address <deployed contract address>
Interact with the deployed FHECounter
smart contract
From the root project directory:
Decrypt the current counter value:
npx hardhat --network localhost task:decrypt-count
Increment the counter by 1:
npx hardhat --network localhost task:increment --value 1
Decrypt the new counter value:
npx hardhat --network localhost task:decrypt-count
Run on Sepolia Ethereum Testnet
To test your FHEVM smart contract using real encrypted values, you can run your tests on the Sepolia Testnet.
Rebuild the project for Sepolia
From the root project directory:
npx hardhat clean
npx hardhat compile --network sepolia
Deploy the FHECounter
smart contract on Sepolia
npx hardhat deploy --network sepolia
Check the deployed FHECounter
contract FHEVM configuration
From the root project directory:
npx hardhat fhevm check-fhevm-compatibility --network sepolia --address <deployed contract address>
If an internal exception is raised, it likely means the contract was not properly compiled for the Sepolia network.
Interact with the deployed FHECounter
contract
From the root project directory:
Decrypt the current counter value (⏳ wait...):
npx hardhat --network sepolia task:decrypt-count
Increment the counter by 1 (⏳ wait...):
npx hardhat --network sepolia task:increment --value 1
Decrypt the new counter value (⏳ wait...):
npx hardhat --network sepolia task:decrypt-count
Last updated