First smart contract
This document introduces the fundamentals of writing confidential smart contracts using the fhEVM. You'll learn how to create contracts that can perform computations on encrypted data while maintaining data privacy.
In this guide, we'll walk through creating a basic smart contract that demonstrates core fhEVM concepts and encrypted operations.
Your first smart contract
Let’s build a simple Encrypted Counter smart contract to demonstrate the configuration process and the use of encrypted state variables.
Writing the contract
Create a new file called EncryptedCounter.sol
in your contracts/
folder and add the following code:
How it works
Configuring fhEVM: The contract inherits from
SepoliaZamaFHEVMConfig
which provides the necessary configuration for local development and testing. This configuration includes the addresses of the TFHE library and Gateway contracts.When deploying to different networks, you can use the appropriate configuration:
The configuration handles setting up:
TFHE library address for encrypted operations
Network-specific parameters
Initializing encrypted variables:
The
counter
variable is set to an encrypted0
usingTFHE.asEuint8(0)
.Permissions are granted to the contract itself for the
counter
ciphertext usingTFHE.allowThis(counter)
.A constant
CONST_ONE
is initialized as an encrypted value to represent the number1
.
Encrypted operations: The
increment()
function adds the encrypted constantCONST_ONE
to thecounter
usingTFHE.add
.
Limitations:
There are two notable issues with this contract:
Counter value visibility: Since the counter is incremented by a fixed value, observers could deduce its value by analyzing blockchain events. To address this, see the documentation on:
Access control for
counter
: The counter is encrypted, but no access is granted to decrypt or view its value. Without proper ACL permissions, the counter remains inaccessible to users. To resolve this, refer to:
Testing
With any contracts that you write you will need to write tests as well. You can start by using something like this as a template:
How the tests work
The test file demonstrates key concepts for testing fhEVM smart contracts:
Test setup:
before
: Initializes test signers (users) that will interact with the contractbeforeEach
: Deploys a fresh instance of the contract before each testCreates FHE instances for each signer to handle encryption/decryption
Test structure:
Key components:
createInstance()
: Sets up FHE instances for each signer to handle encrypted operationsgetSigners()
: Provides test accounts to interact with the contractcontractFactory.deploy()
: Creates a new contract instance for testingtx.wait()
: Ensures transactions are mined before continuing
Best practices
General best practices
Deploy fresh contract instances for each test to ensure isolation
Use descriptive test names that explain the expected behavior
Handle asynchronous operations properly with async/await
Set up proper encryption instances for testing encrypted values
Next steps
Congratulations! You’ve configured and written your first confidential smart contract. Here are some ideas to expand your knowledge:
Explore advanced configurations: Customize the
FHEVMConfig
to suit specific encryption requirements.Add functionalities: Extend the contract by adding decrement functionality or resetting the counter.
Integrate frontend: Learn how to decrypt and display encrypted data in a dApp using the
fhevmjs
library.
Last updated