Skip to main content

EPOOL Contract

Overview

The EPOOL (Encrypted Pool) is the core AMM contract. It implements a Uniswap V2-style constant product formula with fully encrypted reserves, obfuscated public state, and atomic swap execution.

Each EPOOL holds two CERC20 (ERC7984) tokens and issues encrypted LP shares as a CERC20 token itself.

Key Features

  • Full Privacy: Reserves stored as encrypted euint64
  • Obfuscated Public State: Price discovery via ~3x magnitude uncertainty
  • Atomic Swaps: Single-transaction execution with proof verification
  • 4-Term Taylor Pricing: Efficient FHE-compatible constant-product approximation
  • Uniswap V2 Fees: Implicit in reserves, realized at withdrawal

State Variables

IERC7984 public immutable assetA;
IERC7984 public immutable assetB;
uint24 public swapFeeBps; // e.g. 3000 = 0.3%
euint64 private reserveA; // Encrypted
euint64 private reserveB; // Encrypted
euint64 private minObfuscationFactor; // e.g. 1,000,000
euint64 private obfuscationFactorStep; // e.g. 30
uint32 public lastUpdate;
bool public reservesInitialized;
EPoolTypes.obfuscatedStatesStruct public obfuscatedStates;

Core Functions

bootstrap

function bootstrap(
externalEuint64 amountAExt,
externalEuint64 amountBExt,
bytes calldata inputProof
) external

Owner-only, one-time initialization. Mints LP = (amountA >> 1) + (amountB >> 1).

atomicSwapAForB

function atomicSwapAForB(
externalEuint64 amountInExt,
externalEuint64 minAmountOutExt,
bytes calldata proofIn,
address recipient,
uint128 decryptedORA,
uint128 decryptedORB,
bytes calldata reserveProof
) external

Atomic swap from assetA to assetB. Validates proof, computes output, checks slippage, executes or refunds.

atomicSwapBForA

Same as above but reversed direction.

contributeLiquidity

function contributeLiquidity(
externalEuint64 amountAExt,
externalEuint64 amountBExt,
bytes calldata amountProof,
uint128 decryptedORA,
uint128 decryptedORB,
uint128 decryptedOL,
bytes calldata OProof
) external

Proportional LP minting: mint = min(amountA * obfSupply / obfReserveA, amountB * obfSupply / obfReserveB).

removeLiquidity

function removeLiquidity(
externalEuint64 sharesToRemoveExt,
bytes calldata sharesProof,
uint128 decryptedORA,
uint128 decryptedORB,
uint128 decryptedOL,
bytes calldata OProof
) external

Proportional withdrawal with 0.05% fee.

Events

event AtomicSwapExecuted(
address indexed caller,
address indexed recipient,
bool aForB,
bytes32 amountOutHandle
);
event AtomicSwapRefunded(address indexed caller, bool aForB, string reason);
event SwapExecuted(address indexed sender, address indexed recipient, bool aForB);
event LiquiditySeeded(address indexed provider);
event LiquidityAdjusted(address indexed caller, bool add);
event WithdrawalFeeCollected(uint256 indexed tokenId, bytes32 feeAHandle, bytes32 feeBHandle);

Constants

NameValueDescription
BPS_DENOMINATOR1,000,000Fee basis points scale
WITHDRAWAL_FEE_BPS5000.05% withdrawal fee

Errors

error InvalidRecipient();
error PositionNFTNotConfigured();
error NotPositionOwner(uint256 tokenId, address caller);
error PositionTokenMismatch(uint256 tokenId);
error DeadlineExpired();

Security

  • ReentrancyGuard: All critical functions
  • Proof verification: FHE signatures on obfuscated state
  • Taylor bounds: Max swap ~6.25% of reserve lower bound
  • Slippage check: Encrypted minAmountOut
  • Atomic refund: Input returned if swap fails