A decentralized dice betting game built on the Hathor Network using Nano Contracts. This DApp allows users to place bets, provide liquidity, and interact with the HathorDice smart contract.
- Place Bets: Bet HTR tokens with customizable win chances and thresholds
- Liquidity Provision: Add or remove liquidity from the contract pool
- Wallet Integration: Connect via Reown or Metamask Snaps
- Network Support: India Testnet (Mainnet coming soon)
- Real-time Contract State: View current contract parameters and liquidity
- Mock Mode: Test the UI without connecting a real wallet
-
Hathor RPC Service (
lib/hathorRPC.ts)- Implements the Hathor wallet-to-DApp RPC API
- Supports mock mode for development
- Methods:
htr_getConnectedNetwork,htr_getBalance,htr_getAddress,htr_sendNanoContractTx
-
Hathor Core API (
lib/hathorCoreAPI.ts)- Fetches blockchain data from Hathor nodes
- Methods:
getBlueprintInfo,getContractState,getContractHistory,getTransaction
-
Hathor Context (
contexts/HathorContext.tsx)- Manages wallet connection state
- Provides contract state to components
- Handles bet placement and network switching
The DApp integrates with the HathorDice Nano Contract which includes:
- Token UID: The token used for betting (default: HTR)
- House Edge: Configurable house edge in basis points (e.g., 200 = 2%)
- Max Bet Amount: Maximum allowed bet per transaction
- Random Bit Length: Number of bits for random number generation (16-32)
- Liquidity Pool: Total liquidity provided by users
All bet calculations use contract parameters:
- Multiplier:
(2^randomBitLength / threshold) * (1 - houseEdge) - Payout:
(betAmount * 2^randomBitLength * (10000 - houseEdgeBasisPoints)) / (10000 * threshold) - Win Chance:
(threshold / 2^randomBitLength) * 100
Configure networks via environment variables:
NEXT_PUBLIC_USE_MOCK_WALLET=true
NEXT_PUBLIC_DEFAULT_NETWORK=india-testnet
NEXT_PUBLIC_HATHOR_NODE_URL_TESTNET=https://node1.india-testnet.hathor.network/v1a
NEXT_PUBLIC_HATHOR_NODE_URL_MAINNET=https://node1.mainnet.hathor.network/v1a
NEXT_PUBLIC_CONTRACT_IDS_TESTNET=["contract_id_1","contract_id_2"]
NEXT_PUBLIC_CONTRACT_IDS_MAINNET=[]- Node.js 18+ and npm
- A Hathor wallet (Hathor Wallet or Metamask with Hathor Snap)
- Clone the repository:
git clone <repository-url>
cd hathordice-dapp- Install dependencies:
npm install- Configure environment:
cp .env.local.example .env.localEdit .env.local with your configuration:
- Set
NEXT_PUBLIC_USE_MOCK_WALLET=falsefor production - Add your contract IDs to
NEXT_PUBLIC_CONTRACT_IDS_TESTNETand/orNEXT_PUBLIC_CONTRACT_IDS_MAINNET - Configure network URLs if using custom nodes
- Run development server:
npm run devFor testing without a wallet:
NEXT_PUBLIC_USE_MOCK_WALLET=trueMock mode simulates:
- Wallet connection
- Balance queries
- Transaction submissions
- Network information
- Click "Connect Wallet" in the header
- Choose connection method:
- Reown: For Hathor Wallet integration
- Metamask Snaps: For Metamask users
- Ensure wallet is connected
- Select bet amount (respects max bet limit)
- Choose win chance or threshold:
- Win Chance: Set percentage (1-99%)
- Threshold: Set raw threshold value
- Review multiplier and potential payout
- Click "Place Bet"
- Navigate to "Add Liquidity" card
- Enter amount to deposit
- Confirm transaction
- Receive liquidity provider tokens
- Navigate to "Remove Liquidity" card
- Enter amount to withdraw
- Confirm transaction
- Receive HTR tokens back
Place a bet with specified amount and threshold.
Parameters:
bet_amount: Amount to bet (in cents)threshold: Win threshold (1 to 2^randomBitLength - 1)
Actions:
- Deposit: Bet amount in contract token
Add liquidity to the contract pool.
Actions:
- Deposit: Amount to add as liquidity
Remove liquidity from the contract pool.
Actions:
- Withdrawal: Amount to remove
Claim winnings from contract balance.
Actions:
- Withdrawal: Amount to claim
Returns the network the wallet is connected to.
Get balance for specified tokens.
Get wallet address.
Send a nano contract transaction.
Fetch blueprint information.
Fetch current contract state.
Fetch contract transaction history.
hathordice-dapp/
├── app/
│ ├── layout.tsx # Root layout with providers
│ ├── page.tsx # Main page
│ └── globals.css # Global styles
├── components/
│ ├── Header.tsx # Header with wallet connection
│ ├── PlaceBetCard.tsx # Bet placement interface
│ ├── AddLiquidityCard.tsx
│ ├── RemoveLiquidityCard.tsx
│ ├── WithdrawCard.tsx
│ ├── ContractInfoPanel.tsx
│ ├── NetworkSelector.tsx
│ ├── WalletConnectionModal.tsx
│ └── ui/ # UI components
├── contexts/
│ ├── HathorContext.tsx # Hathor wallet & contract state
│ └── WalletContext.tsx # Legacy wallet context
├── lib/
│ ├── config.ts # Environment configuration
│ ├── hathorRPC.ts # RPC service
│ ├── hathorCoreAPI.ts # Core API service
│ ├── utils.ts # Utility functions
│ └── toast.tsx # Toast notifications
├── types/
│ └── hathor.ts # TypeScript types
└── .env.local # Environment variables
npm testnpm run build
npm startnpm run lint- Ensure you're on the correct network (India Testnet)
- Check that your wallet extension is installed and unlocked
- Try refreshing the page
- Check browser console for errors
- Verify sufficient balance
- Check bet amount doesn't exceed max bet
- Ensure contract has sufficient liquidity
- Verify network connection
- Check network configuration in
.env.local - Verify contract ID is correct
- Ensure node URL is accessible
- Check browser console for API errors
- Never commit
.env.localwith real contract IDs - Always verify transaction details before signing
- Use testnet for development and testing
- Audit smart contracts before mainnet deployment
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
MIT License - see LICENSE file for details
For issues and questions:
- GitHub Issues: [repository-url]/issues
- Hathor Documentation: https://docs.hathor.network
- Hathor Discord: https://discord.gg/hathor
- Mainnet support
- Multi-token support
- Advanced statistics dashboard
- Leaderboard
- Social features
- Mobile app
Built with ❤️ on Hathor Network