Decentralized Raffle System is a blockchain-based smart contract built on Ethereum using Solidity. This contract allows users to participate in a decentralized raffle ticket-purchasing system, where users (buyers) can buy tickets, and an organizer can later draw a winner at random. The winner is granted the ability to withdraw the total collected Ether.
The contract demonstrates key features like:
- Role-based access control (organizers vs. buyers).
- Secure ticket purchasing mechanism.
- Pseudo-random winner selection.
- Secure fund withdrawal by the winner.
This project is a simple and secure solution for decentralized raffle systems, ensuring fairness and transparency through blockchain technology.
- Role-based Access: Users are assigned roles of either Organizer or Buyer.
- Ticket Purchase: Buyers can purchase tickets by sending Ether to the contract.
- Random Winner Selection: The Organizer can draw a winner when all tickets are sold.
- Secure Fund Withdrawal: Only the winner is allowed to withdraw the total collected Ether.
- Blockchain Transparency: Every transaction and role assignment is recorded on the Ethereum blockchain for transparency.
- AssignRole: Assigns a role (Organizer/Buyer) to a user.
- GetRole: Returns the assigned role of a specific user.
- getNoOfSoldTickets: Returns the total number of tickets sold.
- getNoOfAvailableTickets: Returns the number of tickets still available.
- purchaseTicket: Allows a buyer to purchase a ticket, provided certain conditions are met.
- DrawWinner: The Organizer can draw a random winner after all tickets are sold.
- getWinner: Allows the Organizer to view the selected winner.
- WithDrawEther: The winner can withdraw the total Ether collected from ticket sales.
- Solidity Version: 0.8.0
- Ethereum Network: Compatible with any Ethereum-based network.
- Remix IDE: This project is easily deployable and executable in the Remix Ethereum IDE.
- Remix IDE: The contract is designed to be deployed and tested using Remix.
- MetaMask: Install and configure MetaMask to connect to the Ethereum test network.
- Test Ether: Use a faucet to get test Ether on your chosen network (Ropsten, Rinkeby, etc.).
-
Clone the Repository:
Clone the GitHub repository where the smart contract is stored:git clone https://github.com/codeterrayt/Decentralized-Raffle-System.git
-
Open Remix IDE:
Go to Remix, an online Solidity IDE. -
Load the Contract:
- In Remix, click on the
File Explorer
panel on the left. - Create a new file and paste the smart contract code from this repository.
- In Remix, click on the
-
Compile the Contract:
- In Remix, click on the
Solidity Compiler
icon on the left. - Select the Solidity version
0.8.0
and compile the contract.
- In Remix, click on the
-
Deploy the Contract:
- Go to the
Deploy & Run Transactions
tab. - Click on
Deploy
.
- Go to the
-
Assign Roles:
- Use the
AssignRole
function to assignORGANIZER
to your address and assign other users asBUYER
.
- Use the
-
Purchase Tickets:
- Buyers can call the
purchaseTicket
function and send exactly1 Ether
to buy a ticket.
- Buyers can call the
-
Draw Winner:
- Once all tickets are sold, the Organizer can call the
DrawWinner
function to select a winner.
- Once all tickets are sold, the Organizer can call the
-
Withdraw Ether:
- The winner can then call the
WithDrawEther
function to withdraw the total Ether in the contract.
- The winner can then call the
- Role Verification: Only users with the
BUYER
role can purchase tickets, and only theORGANIZER
can draw a winner or check the winner's address. - Randomness: The winner selection uses pseudo-randomness from block attributes (timestamp and previous block random seed). While not perfect for high-value lotteries, it is sufficient for small-scale raffles.
- Ether Handling: The contract securely transfers Ether to the winner and ensures the contract has sufficient balance before processing the transfer.
This project demonstrates how smart contracts on the Ethereum blockchain can enable decentralized and trustless raffle systems. The transparency and immutability of the blockchain ensure fairness, while the role-based access control ensures that only authorized users can perform sensitive actions like drawing the winner and withdrawing the prize.