A provably fair Bitcoin block hash prediction game built on the Stacks blockchain. Users predict whether the last character of an upcoming Bitcoin block hash will be ODD or EVEN, with payouts distributed based on pool ratios.
This repository contains both the smart contracts (onchain) and the web application (offchain).
Before running the project, ensure you have the following installed:
- Docker: Required for running the local Stacks devnet.
- Clarinet: The Stacks smart contract development tool.
- Elixir: The programming language for the backend.
- Erlang: The runtime system for Elixir.
- Phoenix Framework: The web framework.
- PostgreSQL: The database.
- Node.js & npm: For managing frontend assets.
-
Clone the repository:
git clone <repository-url> cd hashbit
-
Setup Offchain Environment:
cd offchain # Copy environment file cp env_example .env.dev # Install dependencies and setup database mix setup cd ..
-
Setup Onchain Environment:
cd onchain npm install cd ..
You need to run the services in separate terminal windows:
Terminal 1 - Start Clarinet Devnet:
cd onchain
clarinet devnet startTerminal 2 - Start Phoenix Server:
cd offchain
source .env.dev
iex -S mix phx.serverAccess the application at http://localhost:4000.
Hashbit is a prediction game where users stake STX or sBTC tokens on whether the last character of a future Bitcoin block's hash will be ODD or EVEN.
The game is 100% provably fair because:
- Bitcoin block hashes are generated through cryptographic mining (proof-of-work)
- No one can predict or manipulate Bitcoin block hashes
- All results are publicly verifiable on the Bitcoin blockchain
- Smart contracts execute payouts automatically based on immutable blockchain data
- ODD characters:
1, 3, 5, 7, 9, B, D, F(hexadecimal odd values) - EVEN characters:
0, 2, 4, 6, 8, A, C, E(hexadecimal even values)
Payouts are determined by the pool ratio:
- If 60% predict EVEN and 40% predict ODD, and EVEN wins:
- Total pool = 100 (60 EVEN + 40 ODD)
- EVEN winners split: their original 60 + losing 40 = 100 total
- Each EVEN predictor gets back: ~1.67x their stake (100/60)
- If ODD wins instead:
- ODD winners split: their original 40 + losing 60 = 100 total
- Each ODD predictor gets back: ~2.5x their stake (100/40)
The less popular prediction yields higher payouts when correct, creating a dynamic, market-driven system.
- User connects Stacks wallet (Leather, Xverse, etc.)
- User views current pool data (ODD/EVEN ratios)
- User selects prediction type and enters stake amount in STX/sBTC
- Transaction sent to Stacks smart contract
- Backend monitors blockchain for new blocks
- When target block arrives, result is processed
- Smart contract calculates payouts
- User claims winnings via UI
- Phoenix PubSub broadcasts real-time updates to all connected clients
- Pool data updates instantly when new predictions are placed
- Block results broadcast immediately when blocks arrive
- No page refresh needed for any updates
- Elixir/Phoenix: Web framework and real-time communication
- PostgreSQL: Database for prediction tracking and block data
- Ecto: Database ORM and migrations
- GenServer Workers: Background processing for blockchain monitoring
- Svelte 5: Reactive UI components using runes
- LiveSvelte: Phoenix LiveView + Svelte integration
- Stacks.js: Blockchain interaction library
- TailwindCSS: Utility-first styling framework
- Stacks Blockchain: Smart contract platform
- Bitcoin: Source of unpredictable block hashes
- Clarity: Smart contract language (external contracts)
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is open source. See LICENSE file for details.