Documentation Index
Fetch the complete documentation index at: https://fhenix-mintlify-fix-broken-nav-1776644086.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Building with FHERC20 requires understanding both traditional smart contract best practices and the unique considerations that come with fully homomorphic encryption. This guide provides actionable recommendations for secure, efficient, and privacy-preserving implementations.Security Best Practices
Operator Permissions - Full Access Risk
Operator Permissions - Full Access Risk
Risk: Operators have unlimited access to a user’s balance until expiration.Recommendation:Best practices:
- Grant operators only for the minimum necessary time
- Use specific timeframes: 5-10 minutes for single transactions, 1 day for recurring operations
- Document operator requirements clearly in your UI
- Provide easy revocation by calling
setOperator(address, block.timestamp)
Zero-Replacement Handling
Zero-Replacement Handling
Risk: Insufficient balance transfers zero tokens instead of reverting, which can lead to unexpected behavior.Recommendation:Best practices:
- Always work with the returned
euint64 transferredvalue - Implement balance checks when transfer success is critical
- Use the transferred amount in subsequent operations, not the requested amount
- Consider using transfer callbacks for atomic operations
Access Control Management
Access Control Management
Risk: Improper FHE access control can prevent users from accessing their own balances or expose data to unauthorized parties.Recommendation:Best practices:
- Always call
FHE.allowThis()for values the contract needs to use - Always call
FHE.allow(value, user)for values users need to access - Grant access immediately after creating or modifying encrypted values
- Review access control on all encrypted state changes
Reentrancy in Transfer Callbacks
Reentrancy in Transfer Callbacks
Risk: The Best practices:
onConfidentialTransferReceived callback is executed during the transfer, creating reentrancy opportunities.Recommendation:- Use OpenZeppelin’s ReentrancyGuard for all receiver implementations
- Follow checks-effects-interactions pattern
- Minimize external calls in callbacks
- Keep callback logic simple and gas-efficient
Input Validation
Input Validation
Risk: Unvalidated inputs can lead to unexpected behavior or security vulnerabilities.Recommendation:Best practices:
- Validate token source (
msg.sender) - Validate transfer initiator (
operator) and source (from) when needed - Check data length before decoding
- Validate decoded parameters for reasonable bounds
- Return
FHE.asEbool(false)to reject invalid transfers
Operators Best Practices
When to Use Operators
Standard User Interactions
Standard User Interactions
Use operators when:Advantages:
- User directly approves via wallet transaction
- Simple on-chain permission grants
- Simple, direct approach
- No signature complexity
- Immediate effect
Long-Lived Permissions
Long-Lived Permissions
Use operators when:
- Granting recurring permissions
Comparison Table
| Factor | Operator (setOperator) |
|---|---|
| Gas Cost | User pays gas |
| Transaction Count | 2 (approve + action) |
| Complexity | Simple |
| User Experience | Standard wallet flow |
| Use Case | General permissions |
| Implementation | Direct on-chain |
Common Pitfalls and Solutions
Pitfall: Forgetting to Grant FHE Access
Pitfall: Forgetting to Grant FHE Access
Problem:Solution:
Pitfall: Not Handling Zero Transfers
Pitfall: Not Handling Zero Transfers
Problem:Solution:
Pitfall: Incorrect Claim Management
Pitfall: Incorrect Claim Management
Problem:Solution:
Pitfall: Operator Expiration Timing
Pitfall: Operator Expiration Timing
Problem:Solution:
Pitfall: Not Validating Callback Returns
Pitfall: Not Validating Callback Returns
Problem:Solution:
Related Topics
- Review Core Features for fundamental concepts
- Learn about Operators for permission management
- Explore Transfer Callbacks for safe transfers