-
-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1477 from ShivRaiGithub/main
Added Voting.sol and Raffle.sol to Blockchain/Intermediate
- Loading branch information
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters