Skip to content

Commit

Permalink
Merge pull request #1477 from ShivRaiGithub/main
Browse files Browse the repository at this point in the history
Added Voting.sol and Raffle.sol to Blockchain/Intermediate
  • Loading branch information
Kushal997-das authored Oct 23, 2024
2 parents 4c3ea0d + b059027 commit a9635f4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
66 changes: 66 additions & 0 deletions Blockchain Development/Intermediate/Raffle/Raffle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

contract Raffle {
address public owner;
address[] public participants;
address public winner;
uint public ticketPrice;
bool public isActive;

event EnterRaffle(address indexed participant);
event WinnerSelected(address winner);

modifier onlyOwner() {
require(msg.sender == owner, "Not the owner");
_;
}

constructor(uint _ticketPrice) {
owner = msg.sender;
ticketPrice = _ticketPrice;
isActive = true;
winner = address(0);
}

// Function for participants to enter the raffle
function enterRaffle() public payable {
require(isActive, "Raffle is not active");
require(msg.value == ticketPrice, "Incorrect ticket price");
participants.push(msg.sender);
emit EnterRaffle(msg.sender);
}

function endRaffle() public onlyOwner {
isActive=false;
}

// Function to randomly select a winner (only the owner can call this)
function selectWinner() public onlyOwner {
require(participants.length > 0, "No participants in the raffle");
require(!isActive, "Raffle is active");
uint randomIndex = random() % participants.length;
winner = participants[randomIndex];
emit WinnerSelected(winner);
}
// Winner withdraws the prize money
function getPrizeMoney() public {
require(msg.sender==winner,"Only winner can call");
payable(winner).transfer(address(this).balance);
winner = address(0);
}

// Pseudo-random number generator (for demonstration purposes)
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.prevrandao, block.timestamp, participants)));
}

// Get the total number of participants
function getParticipantsCount() public view returns (uint) {
return participants.length;
}

function getPrizePool() public view returns(uint){
return address(this).balance;
}
}
26 changes: 26 additions & 0 deletions Blockchain Development/Intermediate/Raffle/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Raffle Smart Contract

This is a simple **Raffle** smart contract where participants can join the raffle by paying a specified ticket price, and the contract owner can select a winner at the end of the raffle.

## Features

- **Enter the Raffle**: Participants can enter the raffle by paying the exact ticket price.
- **Select a Winner**: The contract owner can select a random winner from the participants.
- **Prize Money Withdrawal**: The winner can withdraw the prize pool.
- **View Participants Count**: Get the total number of participants.
- **Check Prize Pool**: Get the total amount of Ether collected in the raffle.

## Contract Details

- **Owner**: The person who deploys the contract will be the owner.
- **Ticket Price**: The price to enter the raffle, set at deployment.
- **Participants**: An array storing the addresses of all participants.
- **Winner**: The randomly selected winner of the raffle.

## How to Use

1. **Deploy the Contract**: Deploy the contract with the desired ticket price.
2. **Enter the Raffle**: Participants can enter the raffle by sending Ether equal to the ticket price.
3. **End the Raffle**: The owner can end the raffle to stop more entries.
4. **Select a Winner**: Once the raffle is ended, the owner can select a winner.
5. **Withdraw Prize**: The winner can withdraw the prize pool.
5 changes: 3 additions & 2 deletions Blockchain Development/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

## Level 2: Intermediate 🚀

<i>Nothing to showcase here!</i>

| S-No. | Projects | S-No. | Projects |
|:--:|:--:|:--:|:--:|
| 01. | [Raffle](https://github.com/Kushal997-das/Project-Guidance/tree/main/Blockchain%20Development/Intermediate/Raffle) |
## Level 3: Advanced 🚀

<i>Nothing to showcase here!</i>
Expand Down

0 comments on commit a9635f4

Please sign in to comment.