Here's a Solidity smart contract structure for your blockchain-based cinema booking system on Sepolia testnet, designed with role-based access control and decentralized ticketing logic.
✅ Key Features
- Users can buy tickets via Ether.
- Admins can view booking statistics.
- Owner has full control (add/remove admins, withdraw funds, manage movies).
- All transactions are recorded on-chain.
🧠 Data Structures
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract CinemaBooking is Ownable {
// --- Data Structures ---
struct Movie {
uint256 id;
string title;
string description;
uint256 duration; // in minutes
}
struct Showtime {
uint256 movieId;
uint256 timestamp;
uint256 availableSeats;
uint256 ticketPrice; // in wei
}
struct Ticket {
uint256 showtimeId;
address buyer;
bool isUsed;
}
// --- State Variables ---
uint256 public movieCount = 0;
uint256 public showtimeCount = 0;
uint256 public ticketCount = 0;
mapping(uint256 => Movie) public movies;
mapping(uint256 => Showtime) public showtimes;
mapping(uint256 => Ticket) public tickets;
mapping(address => bool) public isAdmin;
// --- Events ---
event MovieAdded(uint256 movieId, string title);
event ShowtimeAdded(uint256 showtimeId, uint256 movieId, uint256 timestamp);
event TicketPurchased(uint256 ticketId, uint256 showtimeId, address indexed buyer);
event TicketUsed(uint256 ticketId);
event FundsWithdrawn(address indexed owner, uint256 amount);
}
🔐 Role Management
// --- Modifiers ---
modifier onlyAdmin() {
require(isAdmin[msg.sender] || owner() == msg.sender, "Only admin or owner can call this");
_;
}
// --- Admin Management ---
function addAdmin(address newAdmin) public onlyOwner {
isAdmin[newAdmin] = true;
}
function removeAdmin(address adminToRemove) public onlyOwner {
isAdmin[adminToRemove] = false;
}
🎥 Movie Management (Owner/Admin)
// --- Movie Functions ---
function addMovie(string memory _title, string memory _description, uint256 _duration) public onlyOwner {
movieCount++;
movies[movieCount] = Movie(movieCount, _title, _description, _duration);
emit MovieAdded(movieCount, _title);
}
function addShowtime(uint256 _movieId, uint256 _timestamp, uint256 _seats, uint256 _ticketPrice) public onlyAdmin {
require(movies[_movieId].id != 0, "Movie does not exist");
showtimeCount++;
showtimes[showtimeCount] = Showtime(_movieId, _timestamp, _seats, _ticketPrice);
emit ShowtimeAdded(showtimeCount, _movieId, _timestamp);
}
🎟️ Ticket Booking (User)
// --- Ticket Functions ---
function buyTicket(uint256 _showtimeId) public payable {
Showtime storage show = showtimes[_showtimeId];
require(show.timestamp > block.timestamp, "Showtime has already passed");
require(show.availableSeats > 0, "No seats available");
require(msg.value >= show.ticketPrice, "Not enough Ether sent");
show.availableSeats--;
ticketCount++;
tickets[ticketCount] = Ticket(_showtimeId, msg.sender, false);
emit TicketPurchased(ticketCount, _showtimeId, msg.sender);
}
function useTicket(uint256 _ticketId) public {
Ticket storage ticket = tickets[_ticketId];
require(ticket.buyer == msg.sender, "Not ticket owner");
require(!ticket.isUsed, "Ticket already used");
ticket.isUsed = true;
emit TicketUsed(_ticketId);
}
📊 Admin Stats (Admin)
// --- View Functions for Admin ---
function getTotalTicketsSold() public view onlyAdmin returns (uint256) {
return ticketCount;
}
function getTotalRevenue() public view onlyAdmin returns (uint256) {
return address(this).balance;
}
function getShowtimeStats(uint256 _showtimeId) public view onlyAdmin returns (uint256 sold, uint256 remaining) {
uint256 initial = showtimes[_showtimeId].availableSeats + ticketsSoldCount(_showtimeId);
return (ticketsSoldCount(_showtimeId), initial - ticketsSoldCount(_showtimeId));
}
function ticketsSoldCount(uint256 _showtimeId) internal view returns (uint256 count) {
for (uint256 i = 1; i <= ticketCount; i++) {
if (tickets[i].showtimeId == _showtimeId) count++;
}
}
💸 Owner Controls
// --- Owner Withdrawal ---
function ownerWithdraw(uint256 _amount) public onlyOwner {
require(address(this).balance >= _amount, "Insufficient balance");
payable(owner()).transfer(_amount);
emit FundsWithdrawn(owner(), _amount);
}
📦 Helper Functions
// --- Utility Functions ---
function getShowtimesForMovie(uint256 _movieId) public view returns (uint256[] memory) {
uint256[] memory result = new uint256[](showtimeCount);
uint256 index = 0;
for (uint256 i = 1; i <= showtimeCount; i++) {
if (showtimes[i].movieId == _movieId) {
result[index] = i;
index++;
}
}
return result;
}
function getTicketDetails(uint256 _ticketId) public view returns (uint256, address, bool) {
Ticket memory t = tickets[_ticketId];
return (t.showtimeId, t.buyer, t.isUsed);
}
🛡️ Security Considerations
- Use OpenZeppelin’s Ownable for ownership control.
- Use modifiers to enforce access control.
- Prevent reentrancy by using
transfer() or call() with reentrancy guard.
- Validate input data in all functions.
- Add gas limits for large loops.
🧪 Testing Tips
- Test:
- Buying tickets with correct ETH.
- Overbooking prevention.
- Admin-only functions.
- Owner withdrawal.
- Use Hardhat or Foundry to simulate transactions and edge cases.
📁 Deployment
- Deploy to Sepolia testnet using:
- Hardhat + Alchemy/Infura.
- Truffle with HDWalletProvider.
- Remix IDE with Metamask.
- Fund contract with test ETH via faucet.
✅ Next Steps
Would you like:
- A frontend integration example (React + ethers.js)?
- A deployment script (Hardhat/Foundry)?
- Gas optimization tips?
- Security audit checklist?
Let me know and I’ll provide the next step!