Skip to content

Instantly share code, notes, and snippets.

@raghavgarg1257
Forked from codeanyway11/Contest.sol
Last active December 16, 2017 07:49
Show Gist options
  • Save raghavgarg1257/65679c9e80c778b7d21f1922a7e5c303 to your computer and use it in GitHub Desktop.
Save raghavgarg1257/65679c9e80c778b7d21f1922a7e5c303 to your computer and use it in GitHub Desktop.
Solidity Contract for Hackathon.
pragma solidity ^0.4.0;
contract Contest{
struct Participant {
string email;
address account;
bool participated;
}
Participant[] participants;
address[] public participantsAccts;
string public company_name;
uint public prize_amount = 0; // need to change it to accept the float
uint public expiry_time;
address public creator;
address public beneficiary;
bool public ended = false;
function Contest(string _company_name, uint _expiry_time) public {
require(now <= _expiry_time);
creator = msg.sender;
beneficiary = msg.sender;
company_name = _company_name;
expiry_time = _expiry_time;
}
// get money from creator and add it to the global wallet.
function load_wallet() public payable {
// only owner of contract can load the wallet
require(msg.sender == creator);
require(msg.value > 0);
prize_amount = msg.value;
}
// adding the particant to the array
function participate(string _email) public {
// require(!ended);
require(msg.sender != creator);
// TODO: every participant can participate only once
participants.push(Participant({
email: _email,
account: msg.sender,
participated: true
}));
participantsAccts.push(msg.sender) -1;
}
function end() public {
// require(!ended);
beneficiary = choose_winner();
transfer_money(beneficiary);
}
// randomly picking a winner and calling the function `transfer_money`
function choose_winner() public view returns (address) {
require(!ended);
// TODO: if expiry is greater than now
// require(expiry_time < now);
require(participants.length != 0);
uint total_participant = participants.length;
uint random_winner_index = uint(block.blockhash(block.number-1))%total_participant;
address winner = participants[random_winner_index].account;
ended = true;
return winner;
}
// transfer the money from the wallet to either the winner(participant) or the creeator conditionally
function transfer_money(address winner_account) public {
require(!ended);
require(msg.sender == creator);
winner_account.transfer(prize_amount-1);
}
// to get all the participants
function get_participants() view public returns (address[]) {
return participantsAccts;
}
function kill() public {
require(msg.sender == creator);
selfdestruct(creator);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment