2. Writing contracts

This document explains how to write confidential smart contract using fhEVM in Hardhat projects.

Prerequisites

Before proceeding, ensure you have:

  • A working Hardhat environment set up (see previous section).

  • Basic knowledge of Solidity.

  • An understanding of ERC20 tokens.

Understanding the example contract

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.

Step 1. Importing required libraries and contracts.

pragma solidity ^0.8.24;

import "fhevm/lib/TFHE.sol";
import "fhevm/config/ZamaFHEVMConfig.sol";
import "fhevm-contracts/contracts/token/ERC20/extensions/ConfidentialERC20Mintable.sol";
  • 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.

Step 2. Contract construction

contract MyConfidentialERC20 is SepoliaZamaFHEVMConfig, ConfidentialERC20Mintable {
  constructor(string memory name_, string memory symbol_) ConfidentialERC20Mintable(name_, symbol_, msg.sender) {}
}
  • This contract inherits SepoliaZamaFHEVMConfig and ConfidentialERC20Mintable.

  • The constructor initializes the ERC20 token with a name and symbol, setting the deployer as the initial owner.

Going further

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.

Last updated

Was this helpful?