Smart Contract Examples
Production-ready FHE smart contract templates. Copy, modify, and deploy these examples to build your own confidential applications.
Contract Examples
Choose an example to view the complete smart contract implementation
Basic Confidential Voting
A simple voting contract where votes remain encrypted until revealed
beginner
Voting
Solidity
Gas: ~120Kbasic-voting.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "fhevm/lib/TFHE.sol";
import "fhevm/abstracts/EIP712WithModifier.sol";
contract ConfidentialVoting is EIP712WithModifier {
struct Proposal {
string description;
euint32 yesVotes;
euint32 noVotes;
bool ended;
}
mapping(uint256 => Proposal) public proposals;
mapping(uint256 => mapping(address => bool)) public hasVoted;
uint256 public proposalCount;
address public admin;
event ProposalCreated(uint256 proposalId, string description);
event VoteCast(uint256 proposalId, address voter);
event ProposalEnded(uint256 proposalId, uint32 yesVotes, uint32 noVotes);
constructor() EIP712WithModifier("ConfidentialVoting", "1") {
admin = msg.sender;
}
function createProposal(string memory _description) external {
require(msg.sender == admin, "Only admin can create proposals");
proposals[proposalCount] = Proposal({
description: _description,
yesVotes: TFHE.asEuint32(0),
noVotes: TFHE.asEuint32(0),
ended: false
});
emit ProposalCreated(proposalCount, _description);
proposalCount++;
}
function vote(
uint256 _proposalId,
bytes calldata encryptedVote
) external onlySignedPublicKey {
require(_proposalId < proposalCount, "Invalid proposal");
require(!proposals[_proposalId].ended, "Voting ended");
require(!hasVoted[_proposalId][msg.sender], "Already voted");
euint32 vote = TFHE.asEuint32(encryptedVote);
euint32 one = TFHE.asEuint32(1);
// vote is 1 for yes, 0 for no
proposals[_proposalId].yesVotes = TFHE.add(
proposals[_proposalId].yesVotes,
TFHE.mul(vote, one)
);
proposals[_proposalId].noVotes = TFHE.add(
proposals[_proposalId].noVotes,
TFHE.mul(TFHE.sub(one, vote), one)
);
hasVoted[_proposalId][msg.sender] = true;
emit VoteCast(_proposalId, msg.sender);
}
function endVoting(uint256 _proposalId) external {
require(msg.sender == admin, "Only admin can end voting");
require(!proposals[_proposalId].ended, "Already ended");
proposals[_proposalId].ended = true;
// Reveal results
uint32 yesCount = TFHE.decrypt(proposals[_proposalId].yesVotes);
uint32 noCount = TFHE.decrypt(proposals[_proposalId].noVotes);
emit ProposalEnded(_proposalId, yesCount, noCount);
}
}
Key Features
Encrypted Votes
Votes remain private until reveal
Admin Controls
Secure proposal management
Quick Deploy
# Install dependencies
npm install @fhenixjs/fhenix-hardhat-plugin
# Deploy to Fhenix testnet
npx hardhat deploy --network fhenix-testnet
Documentation
Comprehensive guides for FHE smart contract development
Templates
Starter templates for common FHE use cases
Live Examples
Test these contracts on the Fhenix testnet