Skip to content

cryptoeights/ShadowSwap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ShadowSwap - Confidential Batch Auction DEX

ShadowSwap Logo

A privacy-preserving decentralized exchange with MEV protection powered by iExec DataProtector

License: MIT Solidity Next.js Deployed

Demo Video β€’ Features β€’ Quick Start β€’ Documentation


⭐ Hackathon Highlights

Built for iExec Confidential DeFi Hackathon

Criteria Implementation
βœ… Deployed on Arbitrum Sepolia View on Arbiscan
βœ… iExec DataProtector Integration Order encryption for MEV protection (details)
βœ… Real-World DeFi Problem Solves front-running & sandwich attacks
βœ… Code Quality TypeScript, Foundry, OpenZeppelin, comprehensive docs
βœ… User Experience Modern UI, real-time updates, intuitive flow
πŸ“ Feedback Document FEEDBACK.md

🎯 Overview

ShadowSwap is a next-generation decentralized exchange that combines batch auctions with confidential computing to provide:

  • MEV Protection: Orders are encrypted using iExec DataProtector, preventing front-running and sandwich attacks
  • Fair Price Discovery: Batch auction mechanism ensures uniform clearing price for all orders
  • Limit Orders: Set target prices and let the automated keeper bot execute when conditions are met
  • Instant Swaps: Real-time swaps with live price feeds for immediate execution

The Problem

Traditional DEXs suffer from:

  • Front-running: Validators/miners can see pending transactions and extract value
  • Sandwich attacks: Attackers profit by placing orders before and after user transactions
  • Poor price execution: Individual trades can significantly impact prices

Our Solution

ShadowSwap addresses these issues by:

  1. Encrypting orders with iExec DataProtector before on-chain submission
  2. Batching orders to execute at a single clearing price
  3. Automated execution via keeper bot when limit order conditions are met

✨ Features

Core Features

Feature Description
πŸ” Confidential Orders Orders encrypted with iExec DataProtector
πŸ“Š Batch Auctions Fair price discovery through order batching
⚑ Instant Swaps Real-time swaps with live CoinGecko prices
πŸ“ˆ Limit Orders Set target prices with automatic execution
πŸ€– Keeper Bot Automated price monitoring and order execution
πŸ“± Modern UI Beautiful, responsive interface with dark mode
πŸ“œ Transaction History Persistent history synced from blockchain
πŸ“Š Analytics Dashboard Real-time DEX statistics and metrics

iExec DataProtector Integration

ShadowSwap leverages iExec DataProtector for order confidentiality:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ User Order  │────▢│ iExec DataProt  │────▢│   ShadowPool    │────▢│   Batch     β”‚
β”‚  (Plain)    β”‚     β”‚   (Encrypt)     β”‚     β”‚   (On-chain)    β”‚     β”‚ Settlement  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚                     β”‚                       β”‚                     β”‚
      β–Ό                     β–Ό                       β–Ό                     β–Ό
  - tokenIn            Encrypted blob          Store encrypted      Decrypt & match
  - tokenOut           + dataset addr          Wait for batch       at uniform price
  - amountIn
  - limitPrice

Why iExec DataProtector?

Without Encryption With iExec DataProtector
❌ Order visible in mempool βœ… Order details hidden
❌ Validators can front-run βœ… No front-running possible
❌ Sandwich attacks possible βœ… Attack impossible without data
❌ Poor execution price βœ… Fair batch settlement

Technical Implementation:

// frontend/src/lib/encryption.ts
import { IExecDataProtector } from '@iexec/dataprotector';

const protectedData = await dataProtector.protectData({
    data: {
        tokenIn: order.tokenIn,
        tokenOut: order.tokenOut,
        amountIn: order.amountIn.toString(),
        limitPrice: order.limitPrice.toString(),
    },
    name: `ShadowSwap-Order-${Date.now()}`,
});

iExec iApp (TEE Application):

The order processor iApp runs inside Intel SGX enclaves to decrypt and process orders:

iexec-app/src/app.js  β†’  Deployed iApp: 0x834255dF01eE89d5096371a7eeFaF4332d4e2bfF
Docker Image: cryptoeights/shadowswap-order-processor:1.0.0-tee-scone-5.9.1-v16-prod-59e7aa07604b

The iApp handles:

  • Decrypting order data inside TEE (invisible to outside)
  • Validating order parameters (tokens, amounts, prices)
  • Matching orders at uniform clearing price
  • Outputting settlement results

πŸ“– Full iExec Integration | πŸš€ iApp Deployment Guide | πŸ“ Feedback


πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         Frontend (Next.js)                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Swap    β”‚  β”‚ Limit    β”‚  β”‚Dashboard β”‚  β”‚    Analytics     β”‚ β”‚
β”‚  β”‚  Card    β”‚  β”‚ Orders   β”‚  β”‚          β”‚  β”‚                  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚       β”‚             β”‚             β”‚                  β”‚           β”‚
β”‚       β–Ό             β–Ό             β”‚                  β”‚           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚                  β”‚           β”‚
β”‚  β”‚  iExec DataProtector SDK β”‚    β”‚                  β”‚           β”‚
β”‚  β”‚  β€’ protectData()         β”‚    β”‚                  β”‚           β”‚
β”‚  β”‚  β€’ grantAccess()         β”‚    β”‚                  β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚                  β”‚           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚                  β”‚                  β”‚
                β–Ό                  β–Ό                  β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Smart Contracts (Arbitrum Sepolia)            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚                      ShadowPool                           β”‚   β”‚
β”‚  β”‚  β€’ submitOrder()      β€’ instantSwap()                     β”‚   β”‚
β”‚  β”‚  β€’ submitLimitOrder() β€’ executeLimitOrder()               β”‚   β”‚
β”‚  β”‚  β€’ triggerBatch()     β€’ cancelOrder()                     β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ MockPriceFeed β”‚  β”‚   Mock ERC20  β”‚  β”‚   MockWETH/mETH   β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β–²                                          β”‚
        β”‚                                          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    iExec iApp (TEE - Intel SGX)                  β”‚
β”‚  iexec-app/src/app.js                                           β”‚
β”‚  β€’ Decrypts order data inside TEE enclave                       β”‚
β”‚  β€’ Validates order parameters                                   β”‚
β”‚  β€’ Matches orders at uniform clearing price                     β”‚
β”‚  β€’ Settlement results output                                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β–²
        β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Keeper Bot (Node.js)                      β”‚
β”‚  β€’ Fetches live prices from CoinGecko                           β”‚
β”‚  β€’ Updates on-chain price feed                                  β”‚
β”‚  β€’ Monitors pending limit orders                                β”‚
β”‚  β€’ Auto-executes when conditions are met                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Node.js v18+ and npm
  • Foundry (for smart contracts)
  • MetaMask wallet with Arbitrum Sepolia testnet

1. Clone Repository

git clone https://github.com/pebfriantiya/shadowswap.git
cd shadowswap

2. Install Dependencies

# Frontend
cd frontend
npm install

# Keeper Bot
cd ../keeper-bot
npm install

# iExec iApp
cd ../iexec-app
npm install

# Smart Contracts (Foundry)
cd ../contracts_foundry
forge install

3. Environment Setup

# Frontend
cp frontend/.env.example frontend/.env.local
# Edit with your values

# Keeper Bot
cp keeper-bot/.env.example keeper-bot/.env
# Add your private key

4. Run Frontend

cd frontend
npm run dev

Open http://localhost:3000

5. Run Keeper Bot (Optional)

cd keeper-bot
npm start

πŸ“– Documentation

Document Description
Setup Guide Detailed installation instructions
Deployment Guide How to deploy smart contracts
Architecture System design and flow
iExec Integration DataProtector usage
iApp Deployment Build & deploy the iExec iApp
API Reference Smart contract functions

🎬 Demo Video

πŸ“Ί Watch Demo Video

The demo showcases:

  1. Connecting wallet to Arbitrum Sepolia
  2. Minting test tokens from faucet
  3. Performing instant swap with live prices
  4. Creating a limit order with target price
  5. Viewing order status and execution
  6. Transaction history and analytics

πŸ“ Smart Contracts

Deployed Contracts (Arbitrum Sepolia)

Contract Address Arbiscan
ShadowPool 0xfFCdCE40dfD214F2e13F67d9337B0E0e22024F09 View
MockPriceFeed 0xb87889a99AcCF70a2aeA7F63Fdcde302fCd2e006 View
mUSDC 0xcC5f8FC3CcAB02157F82afb7E19Fc65f4808849e View
mDAI 0xda222533d71C37A9370C6b5a26BcB4C07EcB0454 View
mWETH 0xe160dc7BD1E9d63A47a1d4CD082c332DD19D870c View
mETH 0x62b64cC9B1Aa2F2c9d612f0b4a58Cfba0eEc9bE2 View
iExec iApp (TEE) 0x834255dF01eE89d5096371a7eeFaF4332d4e2bfF View

Key Functions

// Submit encrypted order for batch auction
function submitOrder(
    bytes calldata encryptedData,
    bytes32 datasetAddress,
    address tokenIn,
    address tokenOut,
    uint256 amountIn
) external returns (bytes32 orderId)

// Create limit order
function submitLimitOrder(
    bytes calldata encryptedData,
    bytes32 datasetAddress,
    address tokenIn,
    address tokenOut,
    uint256 amountIn,
    uint256 limitPrice,
    uint256 expiry
) external returns (bytes32 orderId)

// Instant swap with price feed
function instantSwap(
    address tokenIn,
    address tokenOut,
    uint256 amountIn,
    uint256 minAmountOut
) external nonReentrant

// Execute limit order when conditions met
function executeLimitOrder(bytes32 orderId) external nonReentrant

πŸ”§ Tech Stack

Frontend

  • Next.js 16 - React framework
  • TypeScript - Type safety
  • Tailwind CSS - Styling
  • wagmi/viem - Ethereum interactions
  • RainbowKit - Wallet connection

Smart Contracts

  • Solidity 0.8.20 - Contract language
  • Foundry - Development framework
  • OpenZeppelin - Security libraries

Backend

  • Node.js - Keeper bot runtime
  • viem - Blockchain interactions
  • CoinGecko API - Price feeds

πŸ” iExec DataProtector Usage

Order Encryption Flow

// 1. Create order data
const orderData = {
    tokenIn: "0x...",
    tokenOut: "0x...",
    amountIn: "1000000000000000000",
    minAmountOut: "990000000000000000"
};

// 2. Encrypt with DataProtector
const encryptedData = await dataProtector.encrypt(orderData);

// 3. Submit encrypted order
await shadowPool.submitOrder(
    encryptedData,
    datasetAddress,
    tokenIn,
    tokenOut,
    amountIn
);

Privacy Guarantees

  • Order details (amounts, prices) are encrypted
  • Only the batch settlement can decrypt
  • Prevents front-running and MEV extraction

πŸ§ͺ Testing

# Run contract tests
cd contracts_foundry
forge test -vvv

# Run frontend in development
cd frontend
npm run dev

πŸ›£οΈ Roadmap

  • Confidential batch auctions with iExec DataProtector
  • Instant swaps with price feeds
  • Limit orders with auto-execution
  • Keeper bot for price monitoring
  • Transaction history from blockchain
  • Analytics dashboard
  • Multi-chain deployment (Mainnet, Base, Optimism)
  • Advanced order types (stop-loss, trailing stop)
  • Liquidity provider incentives
  • Governance token

πŸ† Hackathon Submission

What Was Built During the Hackathon

This project was built from scratch during the hackathon:

  1. Smart Contracts: ShadowPool with batch auctions, limit orders, instant swaps
  2. iExec iApp: TEE application for confidential order processing (iexec-app/)
  3. iExec DataProtector: Order encryption in frontend (frontend/src/lib/encryption.ts)
  4. Frontend: Complete dApp with swap interface, limit orders, dashboard
  5. Keeper Bot: Automated price monitoring and order execution
  6. Documentation: Comprehensive setup, deployment, and usage guides

Technologies Used

  • iExec DataProtector: Order encryption and confidential computing
  • Arbitrum Sepolia: L2 deployment for low gas costs
  • CoinGecko API: Real-time price feeds

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


🀝 Contributing

Contributions are welcome! Please read our Contributing Guide for details.


πŸ‘₯ Team

Built with ❀️ by the ShadowSwap Team


About

Confidential Batch Auction DEX with MEV protection using iExec DataProtector

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors