AEBet - Decentralized Betting Platform
Secure, scalable peer-to-peer betting powered by æternity state channels
Problem Statement
Traditional online betting platforms face several challenges:
- High transaction fees eating into user profits
- Slow settlement times
- Lack of transparency in odds and payouts
- Centralized control of user funds
- Limited scalability during peak betting periods
- Privacy concerns around betting activity
Solution
AEBet leverages æternity's state channels to enable:
- Off-chain betting with near-instant settlements
- Zero-fee transactions between players
- Fully transparent and verifiable betting logic
- Non-custodial betting (users control their funds)
- Private betting activity (only final settlements on-chain)
- Highly scalable infrastructure for concurrent betting
Key Features
1. State Channel Betting
- Direct peer-to-peer betting through state channels
- Instant bet placement and settlement
- Automatic payment distribution
- Privacy-preserving off-chain transactions
- Dispute resolution mechanism
2. Smart Contract Security
- Sophia smart contracts with formal verification
- Automated escrow of betting funds
- Fair and transparent betting logic
- Built-in timeout and dispute handling
- Emergency withdrawal mechanisms
3. Scalable Architecture
- Elixir/OTP middleware for high concurrency
- Connection pooling for channel management
- Automatic channel recovery and reconnection
- Load balancing of betting channels
- Efficient state synchronization
Architecture
Components
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐ │ Client │ │ Server │ │ Blockchain │ │ (TypeScript) │◄────┤ (Node.js/Elixir)│◄────┤ (Aeternity) │ └─────────────────┘ └──────────────────┘ └─────────────────┘ │ │ │ │ │ │ Channel Client Channel Service State Channels │ │ │ │ │ │ User Interface Channel Manager Smart Contracts
Layers
- Smart Contract Layer (
contracts/Betting.aes
)
- Manages betting state and logic
- Handles bet placement and resolution
- Controls fund distribution
- Implements dispute resolution
- Emits events for tracking
- Channel Service Layer (
server/src/channel-service.ts
)
- Manages state channel lifecycle
- Handles channel errors and recovery
- Synchronizes state between parties
- Processes betting events
- Manages channel connections
- Client Layer (
client/src/channel-client.ts
)
- Provides user interface
- Manages wallet connections
- Handles channel state
- Signs transactions
- Processes user actions
- Middleware Layer (
lib/channel_middleware.ex
)
- Manages concurrent channels
- Handles connection pooling
- Provides load balancing
- Ensures state persistence
- Processes channel events
Implementation Details
Smart Contract (Betting.aes
)
payable contract Betting =
record state = {
player1: address,
player2: address,
bet_amount: int,
game_state: int,
winner: option(address)
}
Channel Client (channel-client.ts
)
export class ChannelClient {
async placeBet(amount: number) {
return this.channel.callContract({
amount,
contract: this.config.contractAddress,
abiVersion: 1,
callData: 'cb_place_bet'
});
}
}
Channel Service (channel-service.ts
)
export class ChannelService {
private channels: Map<string, Channel> = new Map();
async createChannel(config: ChannelOptions): Promise<Channel> {
const channel = await Channel.initialize(config);
this.channels.set(channel.id(), channel);
return channel;
}
}
Setup & Installation
- Prerequisites
# Install Node.js dependencies
npm install
# Install Elixir dependencies
mix deps.get
# Start Aeternity node
docker-compose up -d
- Environment Configuration
# .env
AE_NODE_URL=http://localhost:3013
AE_COMPILER_URL=http://localhost:3080
AE_NETWORK_ID=ae_devnet
CHANNEL_RESERVE=10000
- Start Services
# Start middleware
mix run --no-halt
# Start backend
npm run start:server
# Start frontend
npm run start:client
Usage Guide
1. Initialize Channel
const client = new ChannelClient({
url: 'wss://testnet.aeternity.io/channel',
role: 'initiator',
initiatorId: 'ak_initiator...',
responderId: 'ak_responder...',
initiatorAmount: 10000,
responderAmount: 10000
});
await client.init();
2. Place Bet
// Place bet with amount
await client.placeBet(100);
3. Resolve Game
// Resolve game with winner
await client.resolveGame('ak_winner...');
4. Close Channel
// Close channel and settle
await client.closeChannel();
Error Handling
- Channel Errors
- Automatic reconnection on timeout
- Graceful shutdown on fatal errors
- State recovery after disconnection
- Transaction verification
- Contract Errors
- Invalid state transitions
- Insufficient funds
- Unauthorized actions
- Timeout handling
Testing
# Run contract tests
npm run test:contract
# Run channel service tests
npm run test:service
# Run integration tests
npm run test:integration
Deployment
- Local Development
npm run deploy:local
- Testnet Deployment
npm run deploy:testnet
- Mainnet Deployment
npm run deploy:mainnet
Security Considerations
1. Channel Security
- Regular state verification
- Timeout monitoring
- Dispute resolution
- Emergency withdrawals
2. Contract Security
- Access control
- State validation
- Fund protection
- Event monitoring
3. Infrastructure Security
- Encrypted communication
- Rate limiting
- Input validation
- Error logging