SecureSwapX is a privacy-preserving ETH <-> cUSDT fixed-rate swap powered by Zama FHEVM. Users can swap ETH for confidential cUSDT at a deterministic 1 ETH = 3000 cUSDT rate, hold balances in encrypted form, and decrypt balances on demand in the frontend.
This repository contains a complete full-stack implementation of a confidential swap:
- A confidential ERC7984-based token (cUSDT) with encrypted balances.
- A swap contract that mints/burns cUSDT at a fixed price and handles ETH liquidity.
- A React + Vite frontend that reads encrypted balances, submits encrypted swaps, and reveals decrypted balances through the Zama relayer flow.
- Public stablecoin balances leak sensitive financial information on-chain.
- Traditional swaps require price oracles, introduce price risk, and create complex UX.
- Users need simple, deterministic swaps that preserve privacy without relying on off-chain trust for balance privacy.
SecureSwapX solves these issues by:
- Using FHEVM to store and transfer encrypted balances for cUSDT.
- Enforcing a fixed exchange rate of 1 ETH = 3000 cUSDT in the swap contract.
- Providing a frontend that shows encrypted balances by default and decrypts them on user action.
- Fixed-rate swaps:
- 1 ETH = 3000 cUSDT (cUSDT has 6 decimals).
- ETH -> cUSDT minting and cUSDT -> ETH redemption at deterministic prices.
- Confidential balances:
- cUSDT balances are stored as encrypted values on-chain via ERC7984.
- Encrypted transfers for redemption:
- Users submit encrypted cUSDT amounts and proofs to redeem ETH.
- On-chain liquidity accounting:
- The swap contract holds ETH liquidity and enforces balance checks.
- Frontend privacy UX:
- Encrypted balance display by default.
- Explicit user action to decrypt and reveal actual balance.
- Privacy-first UX while keeping on-chain verification of swaps.
- Deterministic pricing removes oracle dependencies and slippage complexity.
- Clear separation of roles (admin/minter) for token security.
- Straightforward contract logic that is easy to audit and reason about.
- Smart contracts:
- Solidity 0.8.27
- Zama FHEVM libraries
- ERC7984 confidential token standard
- Hardhat + hardhat-deploy
- Frontend:
- React + Vite
- viem for reads, ethers for writes
- RainbowKit wallet integration
- No Tailwind CSS
- No frontend environment variables
- No localhost network usage
- No localStorage usage
- No JSON assets in frontend source
- Tooling:
- TypeScript
- npm
- ConfidentialUSDT (cUSDT)
- ERC7984-based confidential token.
- Roles:
- admin: manages the minter role.
- minter: allowed to mint and burn.
- Encrypted balances are stored on-chain; transfers and burns operate on encrypted values.
- SecureSwap
- Holds ETH liquidity.
- Fixed-rate math:
- cUSDT amount (uint64) = (weiAmount * 3000 * 10^6) / 1e18
- ETH amount (wei) = (cusdtAmount * 1e18) / (3000 * 10^6)
- Swaps:
- swapEthForCusdt(): mints cUSDT to user based on ETH input.
- swapCusdtForEth(): transfers encrypted cUSDT from user, burns it, and sends ETH.
- Owner controls:
- withdraw ETH liquidity.
- update owner.
- Connect wallet and load encrypted cUSDT balance via viem.
- Request encryption/decryption using the Zama relayer flow.
- Submit swaps with ethers (write operations) and viem (read operations).
- Decrypt balance only when user explicitly requests it.
contracts/ Smart contracts
ConfidentialUSDT.sol Confidential cUSDT token
SecureSwap.sol Fixed-rate swap
FHECounter.sol Template example (unused)
deploy/ Deployment scripts (hardhat-deploy)
deployments/ Deployed artifacts and ABI per network
tasks/ Hardhat tasks
test/ Contract tests
home/ Frontend (React + Vite)
- Node.js 20+
- npm
npm installFrontend dependencies live in home and are installed separately:
cd home
npm installnpm run compile
npm run testStart a local FHEVM-ready node and deploy contracts locally:
npx hardhat node
npx hardhat deploy --network localhostNote: The frontend is designed to use public test networks only (no localhost network usage).
Deployment uses a private key (no mnemonic). Configure your .env at the repository root:
- PRIVATE_KEY
- INFURA_API_KEY
- ETHERSCAN_API_KEY (optional for verification)
Then deploy:
npx hardhat deploy --network sepolia- The frontend ABI must be sourced from
deployments/sepolia. - The frontend does not use environment variables or local storage.
- The frontend reads with viem and writes with ethers.
docs/zama_llm.mdfor Zama FHEVM contract patterns and guidance.docs/zama_doc_relayer.mdfor relayer integration and frontend decryption flow.
- Connect wallet in the frontend.
- Deposit ETH to receive cUSDT at a fixed 1 ETH = 3000 cUSDT rate.
- View encrypted cUSDT balance by default.
- Click decrypt to reveal actual balance.
- Redeem cUSDT for ETH by submitting an encrypted amount and proof.
- Fixed-rate swaps are deterministic and do not use price oracles.
- The swap contract must hold sufficient ETH liquidity for redemptions.
- The owner can withdraw liquidity; users should trust the owner for liquidity availability.
- FHE privacy depends on correct proof generation and relayer integration.
- This code has not been audited; use on testnets or in controlled environments.
- Fixed-rate pricing does not respond to market prices.
- Liquidity is centralized in the swap contract and controlled by the owner.
- FHE operations add computational overhead.
- Add on-chain liquidity provider shares and accounting.
- Add dynamic pricing or external oracle integration (optional mode).
- Support multiple confidential stable assets.
- Improve UI for transaction status and proof lifecycle.
- Add comprehensive audits and formal verification.
- Optimize gas for encrypted transfers and burns.
- Expand deployment to additional testnets or L2s.
BSD-3-Clause-Clear. See LICENSE.