Skip to content

Instantly share code, notes, and snippets.

@codeanyway11
Last active December 15, 2017 19:24
Show Gist options
  • Save codeanyway11/f7e81891414588c00a3abdd5a3c7c8b0 to your computer and use it in GitHub Desktop.
Save codeanyway11/f7e81891414588c00a3abdd5a3c7c8b0 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;
string public company_name;
uint public prize_amount = 0;
uint public expiry_time;
address public creator;
address public beneficiary;
bool ended;
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 {
// TODO: 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() public {
require(!ended);
require(msg.sender != creator);
// require(participants[]);
participants.push(Participant({
email: 'charu@gmail.com',
account: msg.sender,
participated: true
}));
}
function end() public {
require(!ended);
beneficiary = choose_winner();
transfer_money();
}
// randomly picking a winner and calling the function `transfer_money`
function choose_winner() public view returns (address winner) {
require(!ended);
// 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;
winner = participants[random_winner_index].account;
ended = true;
}
// transfer the money from the wallet to either the winner(participant) or the creeator conditionally
function transfer_money() public {
require(!ended);
beneficiary.transfer(prize_amount);
}
// Not working
// function get_participants() public view returns (address[] _participants) {
// for(uint p = 0; p < participants.length; p++) {
// _participants.push( participants[p].account );
// }
// }
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