Privacy-Preserving Limit Orders on Uniswap V4 Using Inco Lightning (TEE)
Built with: Uniswap V4 Hooks + Inco Lightning TEE + Base Sepolia
Shadow Orders enables users to place encrypted limit orders on Uniswap V4 without revealing their trading intentions. By leveraging Inco Network's Lightning SDK and Uniswap V4's hook architecture, Shadow Orders protects traders from front-running and MEV attacks while maintaining full decentralization.
Traditional limit orders on DEXs expose traders to several vulnerabilities:
- Front-Running: Malicious actors can see pending limit orders in the mempool and execute their trades first
- MEV Extraction: Sophisticated bots extract value by observing order parameters (direction, size, limit price)
- Privacy Loss: All trading strategies are visible on-chain, allowing competitors to exploit this information
- Market Manipulation: Large limit orders can be used to manipulate market prices when visible
Imagine Alice wants to buy 1000 USDC worth of ETH when the price drops to $2,900. With traditional DEXs:
- Alice submits a limit order with her parameters visible in the mempool
- Bob (a MEV bot) sees this large order and the target price
- Bob front-runs Alice's order, buying ETH just before $2,900
- The price moves up due to Bob's purchase
- Alice's order executes at a worse price (or doesn't execute at all)
- Bob sells his ETH for a profit, having extracted value from Alice
Shadow Orders solves this: Alice's limit price, order size, and buy/sell direction are all encrypted using TEE. Bob can see there's an order, but has no idea what the parameters are, eliminating front-running opportunities.
Shadow Orders uses Inco Lightning with Trusted Execution Environment (TEE) to encrypt order parameters client-side before submitting them on-chain. The smart contract can verify these encrypted values against market conditions while preserving privacy throughout the entire lifecycle.
- Client-Side TEE Encryption: Order parameters are encrypted in the browser using Inco's Lightning SDK
- Uniswap V4 Hooks: Custom hook intercepts swap operations to check encrypted limit orders
- Keeper Network: Decentralized keepers monitor for triggered orders and execute swaps
- Privacy First: Order parameters remain encrypted using Inco Lightning's TEE technology
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β User Frontend β
β ββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββ β
β β Connect β β Create Order β β Inco Lightning β β
β β Wallet βββΆβ Interface βββΆβ SDK (Encryption) β β
β ββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β Encrypted Order Parameters
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Base Sepolia (Testnet) β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β ShadowOrdersHook.sol β β
β β β’ Stores TEE-encrypted limit orders β β
β β β’ Verifies encrypted conditions on swap β β
β β β’ Triggers order execution when limit reached β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Uniswap V4 PoolManager β β
β β β’ Executes swaps through hook callbacks β β
β β β’ Calls beforeSwap() and afterSwap() hooks β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β Order Triggered Event
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Keeper Network β
β β’ Monitors blockchain for triggered orders β
β β’ Executes swaps on behalf of users β
β β’ Transfers output tokens back to users β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Next.js 15.5.5 with Turbopack for fast development
- React 19 with server components
- TypeScript for type safety
- Tailwind CSS for styling
- Framer Motion for animations
- Base Sepolia Testnet (Chain ID: 84532)
- Uniswap V4 with hooks architecture
- Foundry for smart contract development
- Solidity 0.8.26 for contract language
- Inco Network Lightning SDK (
@inco/js) for client-side encryption - TEE (Trusted Execution Environment) for secure computations
- Base Sepolia integration for encrypted computations on Layer 2
- Wagmi v3.4.2 for React hooks
- Viem v2.45.1 for Ethereum interactions
- RainbowKit v2.2.10 for wallet connection
- MetaMask SDK for wallet support
- CoinGecko API for real-time price feeds
- React Query for data fetching and caching
// User specifies order parameters
const order = {
tokenIn: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238", // USDC
tokenOut: "0x4200000000000000000000000000000000000006", // WETH
limitPrice: 2900.0, // Target: $2,900/ETH
amount: 1000e6, // 1000 USDC (6 decimals)
isBuyOrder: true
};
// 1. Frontend encrypts using Inco Lightning SDK
const lightning = await Lightning.latest("testnet", 84532);
const encryptedLimitPrice = await lightning.encrypt64(limitPrice);
const encryptedAmount = await lightning.encrypt64(amount);
const encryptedDirection = await lightning.encryptBool(isBuyOrder);
// 2. Submit encrypted parameters to smart contract
const tx = await shadowOrdersHook.createOrder(
encryptedLimitPrice,
encryptedAmount,
encryptedDirection,
tokenIn,
tokenOut,
{ value: parseEther("0.0003") } // Small fee for TEE computation
);
// 3. Order is stored on-chain, fully encryptedAfter order creation, the frontend:
- Captures the current market price as the starting point
- Simulates realistic price movement (Β±12% per tick, 2-second intervals)
- Displays live progress toward the limit price
- Shows both the Order TX (FHE encryption) and eventual Swap TX
// In ShadowOrdersHook.sol - called on every swap
function beforeSwap(
address sender,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
bytes calldata hookData
) external override returns (bytes4, BeforeSwapDelta, uint24) {
// Check all active orders for this pool
for (uint256 i = 0; i < orderCount; i++) {
Order storage order = orders[i];
// Homomorphic comparison (happens on encrypted data!)
bool conditionMet = checkEncryptedCondition(
order.encryptedLimitPrice,
order.encryptedAmount,
currentPrice
);
if (conditionMet) {
// Emit event for keeper to execute
emit OrderTriggered(i, order.user, order.tokenIn, order.tokenOut);
}
}
return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
}// API Route: /api/execute-order/route.ts
export async function POST(req: Request) {
const { orderId } = await req.json();
// 1. Keeper pulls tokens from user
await tokenIn.transferFrom(userAddress, keeperAddress, amount);
// 2. Execute swap via Uniswap V4 PoolSwapTest
const swapParams = {
zeroForOne: order.isBuyOrder,
amountSpecified: -amount,
sqrtPriceLimitX96: getPriceLimitFromOrder(order)
};
const delta = await poolSwapTest.swap(poolKey, swapParams, testSettings);
// 3. Send output tokens back to user
await tokenOut.transfer(userAddress, outputAmount);
return { success: true, txHash: receipt.transactionHash };
}- Node.js 18+ and npm/yarn/pnpm
- MetaMask or compatible Web3 wallet
- Base Sepolia testnet ETH (get from faucet)
# Clone the repository
git clone https://github.com/yourusername/shadow-orders.git
cd shadow-orders
# Install frontend dependencies
cd frontend
npm install
# Set up environment variables
cp .env.example .env
# Edit .env with your keeper private key
# Start the development server
npm run devThe app will be available at http://localhost:3000
Only two environment variables are needed:
# Base Sepolia RPC endpoint
BASE_SEPOLIA_RPC_URL=https://sepolia.base.org
# Private key for keeper wallet (server-side only)
KEEPER_PRIVATE_KEY=your_private_key_herecd backend
# Install Foundry dependencies
forge install
# Deploy to Base Sepolia
forge script script/Deploy.s.sol --rpc-url $BASE_SEPOLIA_RPC_URL --broadcast// Core Protocol
export const SHADOW_ORDERS_HOOK = "0x18a398ec7893303Ee3fe2d64D98Edd806C6D80c4";
export const POOL_MANAGER = "0x7Da1D65F8B249183667cdE74C5CBD46dD38AA829";
export const POOL_SWAP_TEST = "0xe49d2815C231826caB58017e214Bed19fE1c2dD4";
// Mock Tokens for Testing
export const WETH = "0x4200000000000000000000000000000000000006";
export const USDC = "0x036CbD53842c5426634e7929541eC2318f3dCF7e";
export const DAI = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
export const WBTC = "0x9Fe9A663C2dA4F4F793F1c7a5b5BFCD0E4bA5D77";
// Keeper Wallet
export const KEEPER_ADDRESS = "0x5E48Fda9d06f646aa6Bc4714462Ecb21327bC30a";Visit the deployed app and use the "Get Test Tokens" button in the Trade page. This will send you:
- 1000 USDC
- 1000 DAI
- 0.1 WETH
- 0.01 WBTC
- Connect your wallet (MetaMask)
- Select token pair (e.g., USDC β WETH)
- Enter amount (e.g., 1000 USDC)
- Set limit price (e.g., $2,900 for ETH)
- Choose Buy or Sell
- Click "Create Shadow Order"
- Approve FHE encryption (pays ~0.0003 ETH for computation)
- Wait for transaction confirmation
- The app captures current market price
- Simulates price movement toward your limit
- Shows progress in real-time with a visual graph
- Status updates: "Pending" β "Active" β "Executing" β "Executed"
- When limit price is reached, order status changes to "Executing"
- Keeper automatically triggers the swap
- You'll see the Swap TX hash appear
- Output tokens arrive in your wallet
- Client-Side Encryption: All sensitive parameters encrypted in browser before transmission
- Trusted Execution Environment: Uses hardware-secured execution environment
- Privacy Preserving: Order parameters remain encrypted throughout execution
- Fast & Secure: Combines speed of TEE with strong privacy guarantees
- Reentrancy Guards: All external calls protected
- Access Control: Only authorized keepers can execute swaps
- Slippage Protection: Orders include slippage tolerance
- Order Expiration: Time-based expiration prevents stale orders
- Private Key Isolation: Keeper keys stored server-side only (never exposed to frontend)
- Gas Management: Automatic gas estimation and retry logic
- Error Handling: Comprehensive error handling for failed transactions
- Rate Limiting: Prevents spam and DoS attacks
- FHE integration with Inco Lightning SDK
- Uniswap V4 hook implementation
- Basic limit order functionality
- Keeper execution system
- Frontend UI with wallet connection
- Private mempool integration
- ZK proofs for order verification
- Multi-party computation for keeper network
- Encrypted order book (fully private)
- Stop-loss orders with TEE
- Take-profit orders
- Trailing stop orders
- Time-weighted average price (TWAP) orders
- Iceberg orders (hidden size)
- Audit by professional security firm
- Mainnet deployment on Base
- Cross-chain support (Arbitrum, Optimism)
- Decentralized keeper network with incentives
- Governance token for protocol parameters
- Create Order:
500k gas ($0.50 on Base Sepolia) - Order Execution: ~300k gas (paid by keeper)
- TEE Fee: 0.0003 ETH (~$0.90) for encryption computation
- Order Creation: 2-5 seconds (includes encryption + blockchain confirmation)
- Price Check: Real-time (simulated every 2 seconds)
- Order Execution: 10-20 seconds (keeper detection + swap execution)
- Order Parameters: 100% encrypted on-chain
- User Identity: Pseudonymous (wallet address only)
- Trading Strategy: Completely hidden from other users and MEV bots
We welcome contributions! Areas where you can help:
- Smart Contracts: Optimize gas usage, add new order types
- Frontend: Improve UI/UX, add mobile support
- Keeper Network: Implement decentralized keeper coordination
- Testing: Write comprehensive test suites
- Documentation: Improve guides and tutorials
# Create a feature branch
git checkout -b feature/your-feature-name
# Make your changes and test thoroughly
npm run test
# Submit a pull request
git push origin feature/your-feature-nameThis project is licensed under the MIT License - see the LICENSE file for details.
- Uniswap Labs for pioneering the hooks architecture in V4
- Inco Network for making TEE accessible on EVM chains
- Base Team for providing a fast and cheap Layer 2
- The Ethereum Community for endless inspiration
- Twitter: @shadow_orders
- Email: prazwalr07@gmail.com
Built with β€οΈ for the future of private DeFi