Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
This tutorial walks you through performing tests in the mocked mode provided by the fhEVM Hardhat template.
Before proceeding, ensure you have:
A configured Hardhat project using the fhEVM Hardhat template. (See the previous section)
Basic knowledge of Solidity and Hardhat testing. (See the Hardhat testing documentation)
fhEVM provides a mocked mode in Hardhat that allows for:
Faster testing on a local Hardhat network.
The ability to analyze code coverage.
A simulated version of encrypted types (they are not truly encrypted).
Access to Hardhat features such as snapshots (evm_snapshot
), time manipulation (evm_increaseTime
), and debugging (console.log
).
To learn more about the fhEVM mocked mode, refer to the README in the fhEVM Hardhat template repository.
To run tests in mocked mode, open a terminal in your project's root directory and execute:
This command runs all tests locally in mocked mode. You should see the test results in your console.
For most development and demonstration scenarios, mocked mode is sufficient. However, for production-ready development and a real testing environment, you need to run your tests on a real network where the coprocessor is deployed for example Sepolia. Refer to the next section on how to deploy your contract on Sepolia test network.
This guide will walk you through installing all necessary dependencies from scratch, preparing you for developing and deploying fhEVM smart contracts using Hardhat.
To use Hardhat to build and deploy confidential smart contract, you need to have the following:
Node.js (v20 or later)
A package manager: npm
, yarn
, or pnpm
Git for version control
Foundry for using the command cast
If you already have these installed, skip ahead to
To run the installation commands, open a terminal:
Use your system's built-in terminal (e.g., Terminal on macOS, Command Prompt or PowerShell on Windows, or a Linux shell).
Alternatively, use the integrated terminal in a IDE such as .
VS code provide the official for an enhanced development experience with Hardhat.
Instead of installing Node.js directly, we recommend using Node Version Manager (nvm
), which allows you to manage multiple versions of Node.js:
Install nvm
following the recommended tutorials:
Install Node.js using nvm. This installs the latest version of Node.js:
Install pnpm (optional but recommended for better speed and efficiency). After installing Node.js, you automatically have npm, so run:
Alternatively, you can continue using npm
or yarn
if you prefer.
Git is required for managing source code, cloning repositories, and handling version control. Install Git based on your operating system:
Linux: Use your distribution's package manager (e.g., sudo apt-get install git
on Ubuntu/Debian).
We will need the command cast
in our tutorial. This command is available with installing Foundry.
Install Foundry using the official installer:
Reload your terminal configuration:
Run the Foundry installer:
This will install all Foundry components, including cast
, forge
, anvil
, and chisel
.
To verify the installation, run:
This guide walks you through setting up your Hardhat environment using Zama's fhEVM Hardhat template.
Open the repository:
Click "Use this template" to create a new repository under your GitHub account.
Clone the repository you have created to your local environment
Copy the repository URL of your newly created repository.
Open a terminal and run:
Copy the environment configuration template:
Install project dependencies: Depending on your package manager, run one of the following:
contracts/
: Write your Solidity smart contracts here.
test/
: Place your test scripts for smart contract testing.
deploy/
: Deployment scripts for deploying your contracts.
hardhat.config.js
: The pre-configured Hardhat setup file for deploying on Sepolia.
.env
: The environment file that stores sensitive or environment-specific variables such as private keys and API keys.
You are now ready to start building your confidential smart contracts with fhEVM!
This tutorial covers the essential steps to quickly write and deploy a confidential ERC20 smart contract using the .
Hardhat is a flexible, extensible development environment for Ethereum, providing a robust toolkit for compiling, testing, and deploying smart contracts. With Zama’s fhEVM integration, you can develop confidential ERC20 contracts locally and then seamlessly deploy them to a real fhEVM node.
In this tutorial, you will learn to:
Set up a Hardhat project with the .
Write confidential contracts utilizing encrypted data types.
Test your contracts in mocked mode (for rapid feedback and coverage).
Deploy your confidential ERC20 contract to the Sepolia test network.
Interact with your deployed contract, including performing encrypted transfers.
By following these steps, you’ll get familiar with the complete development workflow using Hardhat, and be able to build, test and deploy confidential ERC20 smart contracts on Sepolia testnet.
Windows: .
macOS: Install via with brew install git
or .
You now have all the necessary dependencies installed. Continue to to begin creating and deploying your FHE smart contracts!
To learn more about Hardhat, refer to the .
This guide walks you through deploying your Confidential ERC20 smart contract on the Sepolia test network.
Before proceeding, ensure you have:
A configured Hardhat project using the fhEVM Hardhat Template (see previous sections).
A crypto wallet installed (e.g., Metamask).
Some Sepolia ETH available for testing. If you don’t have enough ETH, use a Sepolia faucet to request free SepoliaETH for testing:
Generate a mnemonic seed for accounts using cast
:
(cast
is a Foundry function. If you don't have Foundry installed, run curl -L https://foundry.paradigm.xyz | bash
first):
Obtain a Sepolia RPC URL:
Sign up for a node provider such as Alchemy - https://www.alchemy.com/
or Infura - https://infura.io/
. Copy your Sepolia RPC URL.
Open the .env
file:
Past your mnemonic in the code:
MNEMONIC=<Your mnemonic generated>
Paste a Sepolia RPC URL:
SEPOLIA_RPC_URL=<Your node provider URL>
Verify generated accounts:
Open your wallet (e.g, MetaMask)
Import the first 2 accounts (e.g., Alice and Bob) into your wallet with their private keys.
Fund these 2 accounts with some Sepolia ETH.
In the deploy/
directory, there is a preconfigured deploy.ts
file that handles the deployment process. You can customize it or add your own scripts.
To deploy the contracts to Sepolia, run:
Once deployment is successful, you should see a console output that includes the contract address such as:
You can verify this contract on Sepolia Etherscan by searching for the deployed address.
Congratulations! 🎉 You have deployed your first confidential ERC20 smart contract. Let's mint a few tokens and perform some encrypted transactions!
After deploying your first fhEVM contract using Hardhat, this guide shows you how to interact with it using Hardhat tasks.
Before interacting with your deployed contract, ensure the following:
Deployment completed: You have successfully deployed the MyConfidentialERC20
contract (see previous section)
Contract address: You have saved the deployed contract address
Sepolia ETH: You have some Sepolia ETH in your wallet for transaction fees
Environment setup: Your .env
file is properly configured with your private key
First, let's mint some confidential tokens to your account:
Once successful, you'll see a transaction confirmation in the terminal that looks like this:
You can check the total supply of tokens to confirm your mint was successful:
Once successful, you'll see a transaction confirmation in the terminal:
To verify your account balance:
Note to remind yourself what was are the private keys of the accounts of the MNEMONIC SEED you can always check it by running:
Once successful, you'll see a transaction confirmation in the terminal.
To transfer confidential tokens to another account:
Once successful, you'll see a transaction confirmation in the terminal.
After the transfer, you can check both accounts balances:
If both balances have changed accordingly the transaction was successful.
Here's a complete list of available Hardhat tasks for interacting with your contract:
mint
: Mint new encrypted tokens
transfer
: Transfer encrypted tokens to another address
balance
: Check encrypted balance of an account
totalSupply
: Get the total token supply
For detailed help on any task, run:
🎉 Congratulations on completing this tutorial! You’ve taken the first step in building confidential smart contracts using fhEVM. It's time now to take the next step and build your own confidential dApps!
To continue your journey and deepen your knowledge, explore the resources below.
Use out-of-box templates and frameworks designed for developers to build confidential dApps easily.
Smart contract development
Frontend development
Join the community to shape the future of blockchain together with us.
This document explains how to write confidential smart contract using fhEVM in Hardhat projects.
Before proceeding, ensure you have:
A working Hardhat environment set up (see previous section).
Basic knowledge of Solidity.
An understanding of ERC20 tokens.
The Hardhat template includes an example contract in the contracts/
folder - MyConfidentialERC20.sol
. This contract enables:
Private ERC20 token transfers.
Encrypted balances.
Minting functionality for authorized addresses.
Let's break down the contract.
TFHE.sol
: The core Solidity library of Zama's fhEVM. It enables encrypted data type like euint64
, secures encrypted operations, such as addition and comparison and allows access control.
SepoliaZamaFHEVMConfig
: A configuration contract that automatically sets up the required configurations for real-time encrypted operations on the Sepolia testnet.
ConfidentialERC20Mintable.sol
: The confidential smart contract that allows for full ERC20 compatibility with FHE encryption.
This contract inherits SepoliaZamaFHEVMConfig
and ConfidentialERC20Mintable
.
The constructor initializes the ERC20 token with a name and symbol, setting the deployer as the initial owner.
This is a simple basic contract that we will deploy and use in this tutorial. To write more complex confidential smart contracts or customize your own functions:
Explore the full range of fhEVM capabilities in the Smart Contract section.
Use the fhevm-contracts
library and extend from the basic contract templates.
The fhevm-contracts is a Solidity library designed for developers to easily develop confidential smart contracts using fhEVM. It provides:
Ready-to-use confidential contracts: Pre-built implementations of common token standards with FHE capabilities
Base contracts: Foundational building blocks for creating custom confidential smart contracts
Extensions: Additional features and utilities that can be added to base contracts
Testing utilities: Tools to help test FHE-enabled smart contracts
See more details in the fhEVM-contracts documentation.
Your contract is ready! Let's move on to testing and deployment.
: Understand the core technology behind fhEVM, including its cryptographic foundations and use cases.
: Expand your skills with hands-on demos and tutorials crafted to guide you through various real-world scenarios.
: If you have a ChatGPT plus account, try out our custom ChatGPT model tailored for Solidity and fhEVM developers.
: A developer-friendly starting point for building and testing smart contracts on fhEVM.
: Access standardized contracts for encrypted operations.
: Quickly develop FHE-compatible dApps using a clean React.js setup.
: Build scalable, server-rendered dApps with FHE integration.
: Develop responsive and modular dApps with FHE support in Vue.js.
: Join the community to get the latest update, have live discussion with fellow developers and Zama team.
: Get support on all technical questions related to fhEVM
: Participate to tackle challenges and earn rewards in cash.