Skip to content

About

FHEVM enabled Belief market

Resources

Stars

12 stars

Watchers

0 watching

Forks

Repository files navigation

Conviction Markets

A decentralized platform for opinion markets, hot takes, and belief-based betting using Fully Homomorphic Encryption (FHE) for privacy-preserving transactions.

🚀 Features

Privacy-First Betting

  • Encrypted Betting: Place confidential bets using ZAMA FHEVM with zero knowledge leakage
  • Homomorphic Encryption: Vote tallies computed without revealing individual choices
  • Temporal Privacy: Results hidden until official market resolution
  • Cryptographic Verification: Oracle-based decryption with signature validation

Prediction Markets

  • Binary Markets: Yes/No prediction markets on any topic
  • Fixed Stake Betting: Equal betting amounts set by market creators (min 0.005 ETH)
  • Flexible Duration: Markets from 5 minutes to 30 days
  • Proportional Payouts: Winners split prize pool based on participation

Social Features

  • Real-time Activity: Live market updates and community activity feeds
  • Comment System: Discussion threads for each market
  • Leaderboards: User rankings based on market participation
  • Community Engagement: Social betting with gamified elements

Web3 Integration

  • Multi-Wallet Support: MetaMask, Rainbow, and other Web3 wallets
  • Gas Optimization: Efficient smart contract interactions
  • Transaction Safety: Comprehensive error handling and validation
  • Cross-Platform: Responsive design for mobile and desktop

🛠️ Tech Stack

Core Technologies

  • Frontend: React 19.1.0 + TypeScript 5.8.3 + Vite 6.3.5
  • Styling: Tailwind CSS 4.1.8 with custom mint/orange design system
  • Smart Contracts: Solidity ^0.8.24 with ZAMA FHE integration
  • Blockchain: Ethereum Sepolia testnet + ZAMA FHEVM
  • Privacy Layer: ZAMA Fully Homomorphic Encryption (FHE)

Web3 Integration

  • Wallet Connection: RainbowKit 2.2.8 + Wagmi 2.15.6
  • Ethereum Interaction: Ethers.js 5.8.0
  • Contract Development: Hardhat 2.25.0
  • FHE Client: ZAMA fhevm-core libraries

🏃‍♂️ Quick Start

  1. Clone the repository

    git clone <repository-url>
    cd conviction-app
  2. Install dependencies

    npm install
  3. Set up environment variables

    cp .env.example .env
    # Add your WalletConnect Project ID
    VITE_WALLETCONNECT_PROJECT_ID=your_project_id
  4. Start development server

    npm run dev
  5. Open your browser Navigate to http://localhost:5173

🧪 Testing

Comprehensive FHE Test Suite

Our test suite provides complete coverage of all FHE operations used in the BeliefMarket contract:

Test Commands

# Run all tests
npm test

# Run only FHE tests
npx hardhat test --grep "FHE"

# Run specific test files
npx hardhat test test/BeliefMarketFHE.comprehensive.cjs
npx hardhat test test/BeliefMarketFHE.missing-operations.cjs

Test Results ✅

  BeliefMarketFHE - Comprehensive FHE Operations
Testing FHE operations with different weights...
Casting votes with different weights...
Testing FHE.toBytes32 and decryption...
✅ FHE.select() - Conditional weight addition works
✅ FHE.eq() - Vote type comparison works
✅ FHE.add() - Encrypted addition with different weights works
✅ FHE.allowThis() - External access permissions work
✅ FHE.toBytes32() - Conversion for decryption works
✅ FHE.requestDecryption() - Oracle request works
✅ FHE.checkSignatures() - Decryption verification works
    ✔ tests all FHE operations: select, eq, add, allowThis, toBytes32 (127ms)
Testing invalid input proof...
✅ FHE.fromExternal() correctly rejects invalid proofs
✅ FHE.fromExternal() accepts valid proofs
    ✔ tests FHE error handling: invalid proofs and malformed inputs
Testing zero weight...
Testing large weight...
✅ FHE operations handle zero weights correctly
✅ FHE operations handle large weights correctly
    ✔ tests FHE operations with edge cases: zero weights, maximum values (68ms)
Testing complex FHE computation chains...
✅ Complex FHE computation chains work correctly
✅ Multiple FHE operations in sequence produce correct results
✅ FHE state management across multiple votes works
    ✔ tests complex FHE computation chains: multiple operations in sequence (120ms)

  BeliefMarketFHE - FHE E2E (mock)
    ✔ encrypts votes, casts, requests reveal via oracle mock, and resolves (51ms)
Creating encrypted votes for 8 users...
Casting votes...
Requesting tally reveal...
Waiting for decryption oracle...
Funding prize pool...
Checking reveal results...
FHE voting test completed successfully!
✅ 8 users voted with encrypted weights
✅ FHE decryption revealed correct totals (5 YES, 3 NO)
✅ YES won by majority
✅ Prize pool funded and ready for distribution
    ✔ comprehensive FHE test: 10 users with different weights, majority wins, prize distribution (150ms)

  BeliefMarketFHE - Missing FHE Operations Tests
✅ FHE.select() operation works correctly
✅ Conditional weight addition based on vote type works
    ✔ tests FHE.select() operation - conditional weight addition (58ms)
✅ FHE.eq() operation works correctly
✅ Encrypted equality comparison for vote types works
    ✔ tests FHE.eq() operation - encrypted equality comparison (72ms)
✅ FHE.add() operation works correctly with different weights
✅ Weighted voting through encrypted addition works
    ✔ tests FHE.add() with different weights - weighted voting (68ms)
✅ FHE.allowThis() operation works correctly
✅ External access permissions for encrypted values work
    ✔ tests FHE.allowThis() - external access permissions
✅ FHE.toBytes32() operation works correctly
✅ Conversion for decryption requests works
    ✔ tests FHE.toBytes32() - conversion for decryption requests (53ms)
✅ FHE.checkSignatures() operation works correctly
✅ Decryption proof verification works
    ✔ tests FHE.checkSignatures() - decryption proof verification (39ms)

  BeliefMarketFHE
    ✔ sets owner on deploy
    ✔ owner can update platformStake
    ✔ createBet succeeds with correct stake and params
    ✔ createBet reverts on incorrect stake, low voteStake, invalid duration, duplicate id
    ✔ requestTallyReveal guard checks: only creator and after expiry
    ✔ claimPrize/claimRefund reverts when not resolved
    ✔ full flow: simulate votes, resolve yes win, winner claims prize
    ✔ full flow: tie -> refund path

  FHECounter
    ✔ encrypted count should be uninitialized after deployment
    ✔ increment the counter by 1 (51ms)
    ✔ decrement the counter by 1 (41ms)

  FHECounterSepolia
This hardhat test suite can only run on Sepolia Testnet
    - increment the counter by 1


  23 passing (1s)
  1 pending

FHE Operations Tested 🔐

Operation Purpose Status
FHE.select() Conditional weight addition based on vote type ✅
FHE.eq() Encrypted equality comparison for vote types ✅
FHE.add() Encrypted addition with different weights ✅
FHE.allowThis() External access permissions for encrypted values ✅
FHE.toBytes32() Conversion for decryption requests ✅
FHE.requestDecryption() Oracle decryption requests ✅
FHE.checkSignatures() Decryption proof verification ✅
FHE.fromExternal() Input validation and error handling ✅

Test Coverage 📊

  • 23 passing tests with comprehensive FHE coverage
  • Edge cases: Zero weights, large weights, invalid inputs
  • Complex scenarios: Multiple operations in sequence, weighted voting
  • Error handling: Invalid proofs, malformed inputs
  • Integration testing: End-to-end FHE voting workflows

🔧 Development

  • Build: npm run build
  • Preview: npm run preview
  • Lint: npm run lint

📚 Documentation

Visit /docs in the app for comprehensive documentation including:

  • Architecture overview
  • Usage guide
  • FAQ
  • Technical references

🏗️ Technical Architecture

Smart Contract Architecture

The platform is built around the BeliefMarketFHE smart contract which leverages ZAMA's Fully Homomorphic Encryption (FHE) for privacy-preserving betting:

contract BeliefMarketFHE {
    struct BetInfo {
        address creator;
        uint256 platformStake;    // 0.02 ETH platform fee
        uint256 voteStake;        // Min 0.005 ETH per bet
        uint256 expiryTime;       // 5 minutes to 30 days
        bool isResolved;
        euint64 yesVotes;         // Encrypted vote tally
        euint64 noVotes;          // Encrypted vote tally
        uint64 revealedYes;       // Decrypted results
        uint64 revealedNo;        // Decrypted results
        uint256 prizePool;        // Total ETH staked
        bool yesWon;              // Winner determination
    }
}

Privacy-Preserving Betting Mechanism

1. Encrypted Vote Submission

  • All votes are encrypted client-side using ZAMA FHE before blockchain submission
  • Vote choices (Yes/No) and amounts remain hidden until market resolution
  • Uses euint64 encrypted integers for vote tallies

2. Homomorphic Vote Aggregation

// Votes are aggregated without revealing individual choices
bet.yesVotes = FHE.add(bet.yesVotes, FHE.select(isYes, weight, zero));
bet.noVotes = FHE.add(bet.noVotes, FHE.select(isNo, weight, zero));

3. Decryption & Resolution Process

  • After market expiry, only the creator can request vote decryption
  • ZAMA oracle service decrypts the tallies via requestDecryption()
  • Results are revealed through a cryptographic callback mechanism

Market Lifecycle

Phase 1: Market Creation

  1. Creator pays platform fee (0.02 ETH)
  2. Sets minimum bet amount (≥0.005 ETH)
  3. Defines market duration (5 minutes - 30 days)
  4. Market becomes active for betting

Phase 2: Betting Period

  1. Users place encrypted bets with equal stakes
  2. Vote choices encrypted using FHE before submission
  3. Real-time bet counting without revealing individual votes
  4. Prize pool accumulates with each bet

Phase 3: Market Resolution

  1. Market expires, betting closes
  2. Creator requests tally decryption from ZAMA oracle
  3. Oracle decrypts vote totals and triggers callback
  4. Winner determined by majority vote (Yes > No or vice versa)

Phase 4: Prize Distribution

  1. Winners claim proportional share of prize pool
  2. Ties result in full refunds to all participants
  3. One-time claiming with replay protection

Prize Distribution Formula

// Winner's prize = (Total Prize Pool × User Stake) ÷ Total Winning Stakes
prize = (prizePool * userStake) / totalWinningWeight

Security Features

Smart Contract Security

  • Reentrancy protection on all state-changing functions
  • Input validation and access controls
  • Emergency pause functionality
  • Overflow protection with SafeMath operations

Privacy Guarantees

  • Zero Knowledge: Individual vote choices never revealed on-chain
  • Encrypted Aggregation: Vote tallies computed homomorphically
  • Temporal Privacy: Results hidden until official resolution
  • Cryptographic Proofs: ZAMA oracle provides verifiable decryption

Data Architecture

On-Chain Data (Encrypted)

  • Market metadata and parameters
  • Encrypted vote tallies (euint64)
  • Prize pool and resolution status
  • User participation tracking

Off-Chain Data (Firebase)

  • Market descriptions and metadata
  • User comments and social features
  • Activity feeds and leaderboards
  • Real-time UI updates

Technical Stack Integration

graph TB
    A["React Frontend"] --> B["RainbowKit Wallet"]
    A --> C["Firebase Firestore"]
    A --> D["ZAMA FHE Client"]
    
    D --> E["BeliefMarketFHE Contract"]
    E --> F["ZAMA Oracle Network"]
    
    B --> G["Ethereum/Sepolia"]
    E --> G
    F --> E
    
    C --> H["Real-time Updates"]
    H --> A
Loading

🔐 Privacy & Security

  • Fully Homomorphic Encryption: All betting data encrypted using ZAMA FHEVM
  • Zero Information Leakage: Individual votes never revealed until resolution
  • Cryptographic Verification: Oracle-based decryption with signature validation
  • Secure Wallet Integration: RainbowKit handles wallet connections securely

🌐 Live Demo

[beliefprotocol.netlify.app]


Built with ❤️ using ZAMA FHE

About

FHEVM enabled Belief market

Resources

Stars

12 stars

Watchers

0 watching

Forks

Contributors

Languages