Creating the contract

Import Required Libraries/Contracts

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "fhevm/lib/TFHE.sol";
import "fhevm/gateway/GatewayCaller.sol";
import "@openzeppelin/contracts/access/Ownable2Step.sol";

Defining the Confidential ERC20 Contract

Our confidential ERC20 contract will implement the Ownable2Step contract from OpenZeppelin and the GatewayCaller library to enable decryption calls via the Gateway. The Ownable2Step contract sets the owner in the constructor, allowing only the owner to view all users’ balances.

contract ConfidentialERC20 is Ownable2Step, GatewayCaller {
    // Remaining Logic
}

State variables:

We want our balances to be encrypted so we define a mapping of the address to an euint64 data type. Initialize state variables for total supply, balances, and allowances using encrypted integers (e.g., euint64 for balances)

uint64 public _totalSupply;
string public _name;
string public _symbol;
uint8 public constant decimals = 6;
mapping(address => euint64) internal balances;
mapping(address => mapping(address => euint64)) internal allowances;

Events:

The events are the same as in a standard ERC20 contract.

event Transfer(address indexed from, address indexed to);
event Approval(address indexed owner, address indexed spender);
event Mint(address indexed to, uint64 amount);
event DecryptedBalance(address indexed userAddress, uint64 amount);

Last updated