SwapERC7984ToERC7984

Category: Identity | Difficulty: Intermediate | Chapters: Erc7984, Swaps | Concept: ERC7984 → ERC7984 swap using FHE.allowTransient across token contracts (KYC is public)

Swap one confidential ERC7984 token for another using transient permissions

Why this example

This example focuses on ERC7984 → ERC7984 swap using FHE.allowTransient across token contracts (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/SwapERC7984ToERC7984.test.ts

Dependencies

  • SimpleKycRegistry

  • MintableConfidentialToken

Deployment plan

Step
Contract
Args
Saves As

1

SimpleKycRegistry

$deployer

kyc

2

MintableConfidentialToken

$deployer, "Token A", "TKA", "ipfs://token-a"

tokenA

3

MintableConfidentialToken

$deployer, "Token B", "TKB", "ipfs://token-b"

tokenB

4

SwapERC7984ToERC7984

@kyc

swap

Contract and test

Pitfalls to avoid

  • should revert if operator approval is missing

  • should revert if omitting allowTransient for the toToken

  • should revert if omitting allowTransient for the fromToken

API Reference

Overview

Swap one confidential ERC7984 token for another using transient permissions

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

NotOperator

Error thrown when caller is not an approved operator for the token

Parameters

Name
Type
Description

caller

address

The address attempting the operation

token

address

The token that requires operator approval

kyc

The KYC registry for compliance checks

constructor

Initializes the swap contract with KYC registry

Parameters

Name
Type
Description

kyc_

contract SimpleKycRegistry

The KYC registry contract

swapConfidentialForConfidential

Swap confidential amount from one ERC7984 token to another

Requires operator approval on the fromToken for this contract.

Parameters

Name
Type
Description

fromToken

contract IERC7984

ERC7984 token to transfer from the user into the swap

toToken

contract IERC7984

ERC7984 token to transfer from the swap to the user

encryptedAmount

externalEuint64

Encrypted amount input

inputProof

bytes

Proof for the encrypted input

swapWithoutAllowingToToken

Anti-pattern: omit FHE.allowTransient(amountTransferred, address(toToken)) (will revert)

Included to demonstrate a common integration pitfall.

Parameters

Name
Type
Description

fromToken

contract IERC7984

ERC7984 token to transfer from the user

toToken

contract IERC7984

ERC7984 token to transfer to the user

encryptedAmount

externalEuint64

Encrypted amount input

inputProof

bytes

Proof for the encrypted input

swapWithoutAllowingFromToken

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

Included to demonstrate why token contracts need permission to consume ciphertext inputs.

Parameters

Name
Type
Description

fromToken

contract IERC7984

ERC7984 token to transfer from the user

toToken

contract IERC7984

ERC7984 token to transfer to the user

encryptedAmount

externalEuint64

Encrypted amount input

inputProof

bytes

Proof for the encrypted input

Last updated