Skip to content

Instantly share code, notes, and snippets.

@ulope
Created October 31, 2016 10:09
Show Gist options
  • Save ulope/a303b0ae31177eb6b172ad4ce78c8e85 to your computer and use it in GitHub Desktop.
Save ulope/a303b0ae31177eb6b172ad4ce78c8e85 to your computer and use it in GitHub Desktop.

Kurzzusammenfassung des "Poll" Workshops auf dem 2. Ethereum Mainz meetup

Verwendete Tools:

Offener Poll

  1. Wir haben zunächst poll_v1.sol implementiert, per Browser Solidity compiliert und mittels MetaMask auf der Blockchain erzeugt. Für den votelimit Parameter haben wir in unserem Test 10 gewählt.
  2. Um den Test mit mehreren Personen durchführen zu können haben wir den Source Code an alle Teilnehmer verteilt und in Browser Solidity mittels der At Address Funktion auf die Adresse des in Schritt 1 erzeugten contracts verwiesen.
  3. Nun haben die Teilnehmer mittels der vote Funktion Stimmen abgegeben.
  4. Wir haben mittels Etherscan und EtherCamp nachvollzogen, dass die NewVote Events wie erwartet erzeugt wurden und auch die Variablen der Poll Instanz aktualisiert wurden.

Teilnehmerbeschränkter Poll

  1. Im zweiten
pragma solidity ^0.4.2;
//Contract defining the Poll
contract NewPoll {
//defines the poll's properties
struct Poll {
address owner;
string title;
uint votelimit;
bool status;
uint numVotes;
}
// event tracking of all votes
event NewVote(string votechoice);
// declare a public poll called p
Poll public p;
//initiator function that stores the necessary poll information. Needs to have the same name as the contract!
function NewPoll(string _title, uint _votelimit) {
p.owner = msg.sender;
p.title = _title;
p.votelimit = _votelimit;
p.status = true;
p.numVotes = 0;
}
//function for user vote. input is a string choice
function vote(string choice) returns (bool) {
if (p.status != true) {
return false;
}
p.numVotes += 1;
NewVote(choice);
// if votelimit reached, end poll
if (p.votelimit > 0) {
endPoll();
}
return true;
}
//when time or vote limit is reached, set the poll status to false
function endPoll() returns (bool) {
if (p.numVotes < p.votelimit) {
return false;
}
p.status = false;
return true;
}
}
pragma solidity ^0.4.2;
//Contract defining the modifier onlyOwner
//Needed to whitelist Poll participants
contract owned {
address public owner;
function owned() {
owner = msg.sender;
}
modifier onlyOwner {
if (msg.sender != owner) throw;
_;
}
}
//Contract defining the Poll
contract NewPoll is owned {
//Maps an Address to a Boolean value to whitelist accounts
mapping (address => bool) public approvedAccount;
//defines the poll's properties
struct Poll {
address owner;
string title;
uint votelimit;
bool status;
uint numVotes;
}
// event tracking of all votes
event NewVote(string votechoice);
event ApprovedAddress(address target, bool approved);
// declare a public poll called p
Poll public p;
//initiator function that stores the necessary poll information. Needs to have the same name as the contract!
function NewPoll(string _title, uint _votelimit) {
p.owner = msg.sender;
p.title = _title;
p.votelimit = _votelimit;
p.status = true;
p.numVotes = 0;
}
//Function whitelisting poll paritcipants
function approveAddress(address target, bool approved) onlyOwner{
approvedAccount[target] = approved;
ApprovedAddress(target, approved);
}
//function for user vote. input is a string choice
function vote(string choice) returns (bool) {
if (p.status != true || approvedAccount[msg.sender] != true) {
return false;
}
p.numVotes += 1;
NewVote(choice);
// if votelimit reached, end poll
if (p.votelimit > 0) {
endPoll();
}
return true;
}
//when time or vote limit is reached, set the poll status to false
function endPoll() returns (bool) {
if (p.numVotes < p.votelimit) {
return false;
}
p.status = false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment