Skip to content

akatsuk10/chronix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

31 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Chronix - Decentralized Betting Platform

A comprehensive decentralized betting platform built with Next.js, Node.js, and Solidity smart contracts. Users can place bets on cryptocurrency price movements using AVAX tokens. Chainlink is used for secure price feeds, randomness, and data streams throughout the stack.

πŸ—οΈ Architecture

Frontend (Next.js)

  • Framework: Next.js 14 with TypeScript
  • Styling: Tailwind CSS with custom components
  • State Management: Redux Toolkit
  • Wallet Integration: Reown AppKit
  • Charts: TradingView Widget
  • UI Components: Custom neumorphic design system
  • Chainlink: Data Streams, price feeds, and lottery randomness

Backend (Node.js)

  • Framework: Express.js with TypeScript
  • Database: PostgreSQL with Prisma ORM
  • Authentication: JWT tokens
  • Blockchain Integration: Ethers.js
  • Event Listening: Real-time blockchain event processing
  • Chainlink: Listens to contract events powered by Chainlink

Smart Contracts (Solidity)

  • Framework: Hardhat
  • Network: Avalanche Fuji Testnet
  • Contracts: Betting, Vault, Carbon Credits, Lottery
  • Chainlink: VRF, Price Feeds, Automation, Functions

πŸ”— Chainlink Integration

Chronix leverages Chainlink for:

  • Price Feeds (secure, decentralized market data)
  • VRF (verifiable randomness for lotteries)
  • Automation (automated contract execution)
  • Functions/Data Streams (off-chain computation and data)

Files using or referencing Chainlink:

Frontend

Smart Contracts


πŸ“ Project Structure

chronix/
β”œβ”€β”€ frontend/                 # Next.js frontend application
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ app/             # App router pages
β”‚   β”‚   β”œβ”€β”€ components/      # React components
β”‚   β”‚   β”œβ”€β”€ hooks/          # Custom React hooks
β”‚   β”‚   β”œβ”€β”€ lib/            # Utilities and services
β”‚   β”‚   β”œβ”€β”€ store/          # Redux store
β”‚   β”‚   └── abis/           # Contract ABIs
β”‚   └── public/             # Static assets
β”œβ”€β”€ backend/                 # Express.js backend server
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ controllers/    # API controllers
β”‚   β”‚   β”œβ”€β”€ routes/         # API routes
β”‚   β”‚   β”œβ”€β”€ services/       # Business logic
β”‚   β”‚   β”œβ”€β”€ lib/           # Database and utilities
β”‚   β”‚   └── abi/           # Contract ABIs
β”‚   β”œβ”€β”€ prisma/            # Database schema and migrations
β”‚   └── scripts/           # Utility scripts
└── smartcontract/          # Solidity smart contracts
    β”œβ”€β”€ contracts/         # Smart contract source code
    β”œβ”€β”€ scripts/           # Deployment scripts
    └── test/              # Contract tests

πŸš€ Quick Start

Prerequisites

  • Node.js (v18 or higher)
  • npm or yarn
  • PostgreSQL database
  • MetaMask or compatible wallet
  • AVAX tokens for testing (Fuji testnet)

1. Clone the Repository

git clone <repository-url>
cd chronix

2. Environment Setup

Create .env files in each directory:

Frontend (.env.local)

NEXT_PUBLIC_BACKEND_URL=http://localhost:5000
NEXT_PUBLIC_CONTRACT_ADDRESS=0x4d2Fb695465c8fbbCFb2b9E424093BBdFC4E612B
NEXT_PUBLIC_VAULT_ADDRESS=0x...
NEXT_PUBLIC_NETWORK_ID=43113

Backend (.env)

DATABASE_URL="postgresql://username:password@localhost:5432/chronix_db"
JWT_SECRET=your_jwt_secret_here
BTC_BETTING_CONTRACT_ADDRESS=0x4d2Fb695465c8fbbCFb2b9E424093BBdFC4E612B
AVALANCHE_FUJI_RPC_URL=https://api.avax-test.network/ext/bc/C/rpc
PORT=5000

Smart Contract (.env)

PRIVATE_KEY=your_private_key_here
AVALANCHE_FUJI_RPC_URL=https://api.avax-test.network/ext/bc/C/rpc

3. Database Setup

cd backend
npm install
npx prisma generate
npx prisma db push

4. Install Dependencies

# Frontend
cd frontend
npm install

# Backend
cd ../backend
npm install

# Smart Contracts
cd ../smartcontract
npm install

5. Deploy Smart Contracts

cd smartcontract
npx hardhat compile
npx hardhat run scripts/deploy.js --network fuji

Note: Update the contract addresses in your environment files after deployment.

6. Start the Backend

cd backend
npm run dev

The backend will start on http://localhost:5000

7. Start the Frontend

cd frontend
npm run dev

The frontend will start on http://localhost:3000

🎯 Features

Frontend Features

  • Dashboard: Real-time trading charts and betting interface
  • Vault Management: Deposit/withdraw AVAX tokens
  • Betting Interface: Place long/short bets on BTC price movements
  • Bet History: View all past bets with statistics
  • Wallet Integration: Connect MetaMask or other wallets
  • Responsive Design: Works on desktop and mobile

Backend Features

  • RESTful API: Complete betting and user management
  • Real-time Event Listening: Captures blockchain events
  • User Authentication: JWT-based authentication
  • Database Management: PostgreSQL with Prisma ORM
  • Bet Statistics: Win/loss tracking and analytics

Smart Contract Features

  • Betting Contract: Handles bet placement and settlement
  • Vault Contract: Manages user deposits and withdrawals
  • Price Oracle Integration: Chainlink price feeds
  • Carbon Credits: Environmental impact tracking
  • Lottery System: Prize distribution mechanism

πŸ”§ Development

Frontend Development

cd frontend
npm run dev          # Start development server
npm run build        # Build for production
npm run lint         # Run ESLint

Backend Development

cd backend
npm run dev          # Start development server
npm run build        # Build TypeScript
npm run start        # Start production server

Smart Contract Development

cd smartcontract
npx hardhat compile  # Compile contracts
npx hardhat test     # Run tests
npx hardhat node     # Start local node

πŸ“Š API Endpoints

Authentication

  • POST /api/auth/login - User login
  • POST /api/auth/register - User registration
  • POST /api/auth/verify - Verify JWT token

Betting

  • GET /api/bets - Get all bets with pagination
  • GET /api/bets/user/:address - Get user's bets
  • GET /api/bets/stats - Get betting statistics
  • GET /api/bets/recent - Get recent bets

Event Management

  • POST /api/events/restart - Restart event listener
  • POST /api/events/process-historical - Process historical events
  • GET /api/events/status - Get event listener status

🎲 How to Use

1. Connect Wallet

  • Click "Connect Wallet" in the header
  • Approve the connection in MetaMask
  • Ensure you're on Avalanche Fuji testnet

2. Deposit to Vault

  • Click the "+" button next to your vault balance
  • Enter the amount of AVAX to deposit
  • Confirm the transaction

3. Place a Bet

  • Navigate to the Dashboard
  • Select your bet amount (0.1, 0.5, 1.0 AVAX or custom)
  • Choose Long (price up) or Short (price down)
  • Confirm the bet transaction

4. Wait for Settlement

  • Bets settle after 5 minutes
  • The system compares start and end prices
  • Winners receive their payout automatically

5. View History

  • Check the Bet History section
  • View your win/loss statistics
  • Track all your betting activity

πŸ› οΈ Troubleshooting

Common Issues

Event Listener Not Working

cd backend
node scripts/restart-events.js all

Database Connection Issues

cd backend
npx prisma db push
npx prisma generate

Contract Deployment Issues

cd smartcontract
npx hardhat clean
npx hardhat compile
npx hardhat run scripts/deploy.js --network fuji

Frontend Build Issues

cd frontend
rm -rf .next
npm run build

Debug Commands

# Check backend status
curl http://localhost:5000/api/health

# Check bet statistics
curl http://localhost:5000/api/bets/stats

# Restart event listener
curl -X POST http://localhost:5000/api/events/restart

# Process recent events
curl -X POST http://localhost:5000/api/events/process-historical \
  -H "Content-Type: application/json" \
  -d '{"recent": true}'

πŸ”’ Security

  • Private Keys: Never commit private keys to version control
  • Environment Variables: Use .env files for sensitive data
  • Database: Use strong passwords and secure connections
  • Smart Contracts: All contracts are audited and tested
  • API Security: JWT tokens with proper expiration

πŸ“ˆ Performance

  • Frontend: Optimized with Next.js 14 and React 18
  • Backend: Efficient event processing with chunked queries
  • Database: Indexed queries for fast bet retrieval
  • Blockchain: Optimized for Avalanche network

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

πŸ“„ License

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

πŸ†˜ Support

For support and questions:

  • Create an issue in the repository
  • Check the troubleshooting section
  • Review the API documentation

πŸ”„ Updates

Stay updated with the latest changes:

  • Monitor the repository for updates
  • Check the changelog for new features
  • Follow the deployment instructions

Note: This is a testnet deployment. For mainnet deployment, ensure all security measures are in place and contracts are thoroughly audited.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors