CompliantERC20

CompliantERC20

Category: Identity | Difficulty: Advanced | Chapters: Compliance | Concept: FHE.select() for branch-free compliant transfers

ERC20-like token with encrypted balances and compliance checks

Why this example

This example focuses on FHE.select() for branch-free compliant transfers. It is designed to be self-contained and easy to run locally.

Quick start

npm install
npm run test:mocked -- test/identity/FullFlow.test.ts

Dependencies

Deployment plan

Step
Contract
Args
Saves As

1

IdentityRegistry

-

registry

2

ComplianceRules

@registry, 1

complianceRules

3

CompliantERC20

"Compliant Token", "CPL", @complianceRules

token

Contract and test

Pitfalls to avoid

No pitfalls are highlighted in the tests for this example.

API Reference

Overview

ERC20-like token with encrypted balances and compliance checks

Developer Notes

Example for fhEVM Examples - Identity Category

name

Token name

symbol

Token symbol

DECIMALS

Token decimals

totalSupply

Total supply (public for transparency)

complianceChecker

Compliance checker interface (can be ComplianceRules or custom)

owner

Owner/admin

pendingOwner

Pending owner for two-step ownership transfer

Transfer

Emitted on token transfers (indexed for efficient filtering)

Parameters

Name
Type
Description

from

address

Address tokens are transferred from

to

address

Address tokens are transferred to

Approval

Emitted when spending allowance is set

Parameters

Name
Type
Description

owner

address

Address of the token owner

spender

address

Address authorized to spend

Mint

Emitted when new tokens are minted

Parameters

Name
Type
Description

to

address

Address receiving the minted tokens

amount

uint256

Number of tokens minted

ComplianceCheckerUpdated

Emitted when the compliance checker contract is updated

Parameters

Name
Type
Description

newChecker

address

Address of the new compliance checker

OwnershipTransferStarted

Emitted when ownership transfer is initiated

Parameters

Name
Type
Description

currentOwner

address

Current owner address

pendingOwner

address

Address that can accept ownership

OwnershipTransferred

Emitted when ownership transfer is completed

Parameters

Name
Type
Description

previousOwner

address

Previous owner address

newOwner

address

New owner address

OnlyOwner

Thrown when caller is not the contract owner

OnlyPendingOwner

Thrown when caller is not the pending owner

InvalidOwner

Thrown when new owner is the zero address

ComplianceCheckerNotSet

Thrown when compliance checker is required but not set

UnauthorizedCiphertext

Thrown when caller supplies an unauthorized ciphertext handle

TotalSupplyOverflow

Thrown when mint amount would exceed uint64 accounting bounds

constructor

Initialize the token

Parameters

Name
Type
Description

tokenName

string

Token name

tokenSymbol

string

Token symbol

checker

address

Address of the compliance checker contract

setComplianceChecker

Set the compliance checker contract

Parameters

Name
Type
Description

checker

address

Address of the compliance checker

transferOwnership

Initiate transfer of contract ownership

Parameters

Name
Type
Description

newOwner

address

Address that can accept ownership

acceptOwnership

Accept ownership transfer

mint

Mint tokens to an address

Only owner can mint. Compliance is NOT checked on mint.

Parameters

Name
Type
Description

to

address

Recipient address

amount

uint256

Amount to mint (plaintext)

transfer

Transfer tokens with encrypted amount

Branch-free transfer with compliance checks

Parameters

Name
Type
Description

to

address

Recipient address

encryptedAmount

externalEuint64

Encrypted amount to transfer

inputProof

bytes

Proof for encrypted input

Return Values

Name
Type
Description

success

bool

Always returns true (actual transfer amount may be 0) Key insight: We never revert on failed compliance. Instead: - If compliant: transfer the requested amount - If not compliant: transfer 0 (no state change, no info leak)

transfer

Transfer with euint64 amount (for approved callers)

Parameters

Name
Type
Description

to

address

Recipient

amount

euint64

Encrypted amount

Return Values

Name
Type
Description

success

bool

Always true

approve

Approve spender to transfer tokens

Parameters

Name
Type
Description

spender

address

Address to approve

encryptedAmount

externalEuint64

Encrypted allowance amount

inputProof

bytes

Proof for encrypted input

Return Values

Name
Type
Description

success

bool

Always true

transferFrom

Transfer from another account (requires approval)

Parameters

Name
Type
Description

from

address

Source address

to

address

Destination address

encryptedAmount

externalEuint64

Encrypted amount

inputProof

bytes

Proof for encrypted input

Return Values

Name
Type
Description

success

bool

Always true

balanceOf

Get encrypted balance

Parameters

Name
Type
Description

account

address

Address to query

Return Values

Name
Type
Description

[0]

euint64

Encrypted balance

allowance

Get encrypted allowance

Parameters

Name
Type
Description

account

address

Owner address

spender

address

Spender address

Return Values

Name
Type
Description

[0]

euint64

Encrypted allowance

decimals

Get decimals

Return Values

Name
Type
Description

[0]

uint8

Token decimals

_transfer

Internal transfer implementation

_The heart of branch-free compliance

Logic flow:

  1. Check sender compliance (if checker is set)

  2. Check recipient compliance (if checker is set)

  3. Check sender has sufficient balance

  4. Combine all checks with FHE.and()

  5. Use FHE.select() to set transfer amount:

    • If all checks pass: transfer requested amount

    • If any check fails: transfer 0

  6. Update balances (even if amount is 0)_

Parameters

Name
Type
Description

from

address

Source address

to

address

Destination address

amount

euint64

Encrypted amount to transfer

Return Values

Name
Type
Description

success

bool

Always returns true (actual transfer may be 0)

IComplianceChecker

Overview

Interface for compliance checking contracts

checkCompliance

Check if a user passes compliance requirements

Parameters

Name
Type
Description

user

address

Address to check compliance for

Return Values

Name
Type
Description

[0]

ebool

Encrypted boolean indicating compliance status

Last updated