Skip to content

buffedgecko/fhe-otc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FHE-OTC: Private Over-the-Counter Token Swap

Privacy-First P2P Token Exchange powered by Fully Homomorphic Encryption (FHE)

Fhenix Solidity License

🎯 Overview

FHE-OTC is a decentralized over-the-counter trading platform that enables private peer-to-peer token swaps using Fully Homomorphic Encryption. All sensitive trading dataβ€”prices, amounts, and order detailsβ€”remain encrypted on-chain, providing unprecedented privacy for institutional and retail traders.

Key Features

  • πŸ”’ Encrypted Order Book - Prices and amounts never exposed on-chain
  • πŸ›‘οΈ MEV Protection - No front-running possible without price visibility
  • 🀝 P2P Matching - Direct counterparty settlement without intermediaries
  • 🌐 EVM Compatible - Works on Arbitrum, Base, and any EVM chain with CoFHE

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Frontend (React)                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”             β”‚
β”‚  β”‚   Wallet    β”‚  β”‚   Order     β”‚  β”‚   Trade     β”‚             β”‚
β”‚  β”‚ Connection  β”‚  β”‚   Form      β”‚  β”‚   History   β”‚             β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β”‚
β”‚                         β”‚                                        β”‚
β”‚                   cofhejs SDK                                    β”‚
β”‚              (Client-side Encryption)                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
                          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Smart Contracts (Solidity)                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚                    FHE-OTC.sol                            β”‚   β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”‚   β”‚
β”‚  β”‚  β”‚ Encrypted   β”‚  β”‚   Order     β”‚  β”‚ Settlement  β”‚       β”‚   β”‚
β”‚  β”‚  β”‚ Order Book  β”‚  β”‚  Matching   β”‚  β”‚   Engine    β”‚       β”‚   β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                         β”‚                                        β”‚
β”‚                   CoFHE Coprocessor                              β”‚
β”‚              (FHE Operations on Encrypted Data)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
                          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Blockchain Layer                              β”‚
β”‚         Arbitrum / Base / Ethereum (with CoFHE support)         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ” Privacy Guarantees

Data Type Traditional DEX FHE-OTC
Order Price Visible to all Encrypted
Order Amount Visible to all Encrypted
Order Direction Visible to all Encrypted
Counterparty Identity Visible after match Optional anonymity
MEV Exposure High risk Protected

πŸš€ Quick Start

Prerequisites

  • Node.js 20+
  • Bun or npm
  • Foundry (for contract development)
  • MetaMask or compatible wallet

Installation

# Clone the repository
git clone https://github.com/buffedgecko/fhe-otc.git
cd fhe-otc

# Install dependencies
bun install

# Setup environment
cp .env.example .env

Deploy Contracts (Testnet)

# Deploy to Arbitrum Sepolia
forge script script/Deploy.s.sol --rpc-url $ARBITRUM_SEPOLIA_RPC --broadcast

# Or deploy to Base Sepolia
forge script script/Deploy.s.sol --rpc-url $BASE_SEPOLIA_RPC --broadcast

Run Frontend

cd frontend
bun install
bun run dev

πŸ“ Project Structure

fhe-otc/
β”œβ”€β”€ contracts/
β”‚   └── src/
β”‚       β”œβ”€β”€ FHEOTC.sol          # Main OTC contract
β”‚       β”œβ”€β”€ EncryptedOrder.sol  # Order data structures
β”‚       └── interfaces/         # Contract interfaces
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/         # React components
β”‚   β”‚   β”œβ”€β”€ hooks/              # Custom hooks for FHE
β”‚   β”‚   β”œβ”€β”€ pages/              # Next.js pages
β”‚   β”‚   └── lib/                # Utilities
β”‚   └── package.json
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ ARCHITECTURE.md         # Detailed architecture
β”‚   β”œβ”€β”€ SECURITY.md             # Security considerations
β”‚   └── TUTORIAL.md             # Step-by-step guide
β”œβ”€β”€ test/
β”‚   └── FHEOTC.t.sol            # Foundry tests
β”œβ”€β”€ foundry.toml
β”œβ”€β”€ package.json
└── README.md

πŸ§ͺ Testing

# Run all tests
forge test

# Run with verbosity
forge test -vvv

# Run specific test
forge test --match-test test_CreateOrder

πŸ“Š Use Cases

1. Institutional Trading

Large token swaps without market impact or front-running risk.

2. Private Settlements

OTC deals between parties who want to keep terms confidential.

3. MEV-Sensitive Trades

Execute trades without exposing your intentions to searchers.

4. Whale Protection

Trade large amounts without triggering price manipulation.

πŸ”§ Smart Contract API

Create Order

function createOrder(
    address tokenA,
    address tokenB,
    InEuint64 calldata encryptedAmountA,
    InEuint64 calldata encryptedAmountB,
    bytes calldata encryptedPrice
) external returns (bytes32 orderId);

Match Orders

function matchOrders(
    bytes32 orderA,
    bytes32 orderB,
    Permission calldata permission
) external;

Cancel Order

function cancelOrder(
    bytes32 orderId,
    Permission calldata permission
) external;

πŸ”‘ FHE Operations

FHE-OTC uses the following encrypted operations:

Operation Description
FHE.add() Add encrypted amounts
FHE.sub() Subtract encrypted amounts
FHE.mul() Multiply encrypted values
FHE.eq() Check equality (encrypted result)
FHE.lt() Less than comparison
FHE.gt() Greater than comparison
FHE.select() Conditional selection

🌐 Network Configuration

Network Chain ID RPC Faucet
Arbitrum Sepolia 421614 https://sepolia-rollup.arbitrum.io/rpc Alchemy Faucet
Base Sepolia 84532 https://sepolia.base.org Alchemy Faucet
Ethereum Sepolia 11155111 https://rpc.sepolia.org Alchemy Faucet

πŸ“œ License

MIT License - See LICENSE for details.

πŸ™ Acknowledgments

πŸ“ž Community


Built for Fhenix Private By Design dApp Buildathon Wave 2 πŸš€

About

Private Over-the-Counter Token Swap using Fully Homomorphic Encryption (FHE) - Fhenix Buildathon

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors