SwapERC7984ToERC20

Category: Identity | Difficulty: Advanced | Chapters: Erc7984, Swaps | Concept: ERC7984 → ERC20 swap using public decryption + FHE.checkSignatures (KYC is public)

Swap a confidential ERC7984 token amount to a public ERC20 via public decryption finalization

Why this example

This example focuses on ERC7984 → ERC20 swap using public decryption + FHE.checkSignatures (KYC is public). It is designed to be self-contained and easy to run locally.

Quick start

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

Dependencies

  • SimpleKycRegistry

  • MintableConfidentialToken

  • MockERC20

Deployment plan

Step
Contract
Args
Saves As

1

SimpleKycRegistry

$deployer

kyc

2

MintableConfidentialToken

$deployer, "Mock Confidential Token", "mCONF", "ipfs://mintable-erc7984"

confidentialToken

3

MockERC20

$deployer, 0

erc20

4

SwapERC7984ToERC20

$deployer, @confidentialToken, @erc20, @kyc

swap

Contract and test

Pitfalls to avoid

  • should revert swap if operator approval is missing

  • should revert when omitting allowTransient for the token

API Reference

Overview

Swap a confidential ERC7984 token amount to a public ERC20 via public decryption finalization

Developer Notes

Example for fhEVM Examples - OpenZeppelin Confidential Contracts

NotKycApproved

Error thrown when a non-KYC-approved address attempts an operation

Parameters

Name
Type
Description

account

address

The address that was not KYC-approved

InvalidFinalization

Error thrown when finalization is called with an invalid amount handle

Parameters

Name
Type
Description

amount

euint64

The invalid encrypted amount handle

fromToken

The confidential ERC7984 token to swap from

toToken

The public ERC20 token to swap to

kyc

The KYC registry for compliance checks

constructor

Initializes the swap contract with token addresses and KYC registry

Parameters

Name
Type
Description

initialOwner

address

The address that will own the contract

fromToken_

contract IERC7984

The ERC7984 token to swap from

toToken_

contract IERC20

The ERC20 token to swap to

kyc_

contract SimpleKycRegistry

The KYC registry contract

swapConfidentialToERC20

Swap confidential token amount to public ERC20 (two-step: request + finalize)

Requires operator approval on the fromToken for this contract.

Parameters

Name
Type
Description

encryptedAmount

externalEuint64

Encrypted amount input (requested swap amount)

inputProof

bytes

Proof for the encrypted input Emits ConfidentialTransfer on the ERC7984 token; the transferred handle must be publicly decrypted off-chain, then finalized with finalizeSwap.

swapWithoutAllowingToken

Anti-pattern: omit FHE.allowTransient(amount, address(fromToken)) (will revert)

Parameters

Name
Type
Description

encryptedAmount

externalEuint64

Encrypted amount input

inputProof

bytes

Proof for the encrypted input

swapWithoutPublishing

Anti-pattern: omit FHE.makePubliclyDecryptable(amountTransferred) (finalization becomes impossible)

Parameters

Name
Type
Description

encryptedAmount

externalEuint64

Encrypted amount input

inputProof

bytes

Proof for the encrypted input

finalizeSwap

Finalize a swap using the public decryption proof from FHE.publicDecrypt

Parameters

Name
Type
Description

amount

euint64

The encrypted handle that was published during swapConfidentialToERC20

cleartextAmount

uint64

Decrypted cleartext amount matching amount

decryptionProof

bytes

KMS signature proof returned by the public decryption endpoint

Last updated