Skip to content

Instantly share code, notes, and snippets.

@encody
Created April 20, 2018 23:10
Show Gist options
  • Select an option

  • Save encody/d4750e04e421bf9eade55b6a575717a9 to your computer and use it in GitHub Desktop.

Select an option

Save encody/d4750e04e421bf9eade55b6a575717a9 to your computer and use it in GitHub Desktop.
Smart Contracts and Dapps Lecture Files
pragma solidity ^0.4.0;
contract Election_basic {
mapping (string => uint) votes;
function vote (string candidate) public {
votes[candidate]++;
}
function getVotes (string candidate) public view returns (uint) {
return votes[candidate];
}
}
pragma solidity ^0.4.0;
contract Election_expires {
mapping (string => uint) votes;
uint expires;
constructor () public {
expires = now + 1 minutes;
}
function vote (string candidate) public {
require(now < expires);
votes[candidate]++;
}
function getVotes (string candidate) public view returns (uint) {
require(now >= expires);
return votes[candidate];
}
}
pragma solidity ^0.4.0;
// This is the code that was written during the lecture
contract Election {
mapping (string => uint) votes;
mapping (address => bool) hasVoted;
function vote (string candidate) {
require(!hasVoted[msg.sender]);
hasVoted[msg.sender] = true;
votes[candidate] += 1;
}
function getVotes (string candidate) view returns (uint) {
return votes[candidate];
}
}
pragma solidity ^0.4.0;
contract Election_limit_voters {
struct Voter {
bool hasVoted;
bool allowedToVote;
}
mapping (address => Voter) voters;
mapping (string => uint) votes;
address admin;
constructor () public {
admin = msg.sender;
}
function vote (string candidate) public {
require(!voters[msg.sender].hasVoted && voters[msg.sender].allowedToVote);
voters[msg.sender].hasVoted = true;
votes[candidate] += 1;
}
function authorize (address voter) {
require(msg.sender == admin);
voters[voter].allowedToVote = true;
}
function getVotes (string candidate) public view returns (uint) {
return votes[candidate];
}
}
pragma solidity ^0.4.0;
contract Election_limit_votes {
mapping (address => bool) voters;
mapping (string => uint) votes;
function vote (string candidate) public {
require(!voters[msg.sender]);
voters[msg.sender] = true;
votes[candidate] += 1;
}
function getVotes (string candidate) public view returns (uint) {
return votes[candidate];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment