Created
September 17, 2021 18:21
-
-
Save ryestew/78962e72e072bed8076f6fc67b27796a to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
// naming the contract | |
contract BallotString { | |
// We start with the state variables: | |
// The Voter STRUCT is a template for what should be collected about a Voter - basically the Voter profile. | |
struct Voter { | |
// It has to be a STRUCT because the Voter has many "qualities" attached to it: | |
uint weight; | |
// weight is how much voting power the Voter has. | |
// UINT means "unsigned integer", a whole number (no decimals) & with no signs before it (no negative numbers, e.g.-6). | |
// This means only whole numbers from 0 onwards. | |
bool voted; | |
// if true, that person already voted. | |
//BOOL stands for boolean, something that is true/false | |
address delegate; | |
// If the Voter cannot vote, he/she will delegate to this other Voter's address | |
uint vote; | |
/** You vote for one of the members of the list (AKA array) of proposals. E.G. [Maria, Josh, Steph] | |
* Maria's position in the array above is 0 | |
* (its a "zero indexed" array - the 1st one is 0) | |
* Josh = 1, | |
* Steph = 2. | |
* If I vote for Josh, I will input his index number of his position in the list of proposals */ | |
} | |
// The Proposal STRUCT is a template for what should be collected about a Proposal - basically the Propoal profile. | |
struct Proposal { | |
string name; // name of the candidate | |
// typically smart contracts do NOT save strings (= text) but save byte1 or byte32 because it is cheaper in gas - cheaper for computations - see this string to bytes32 tool: https://what-if-i-invested.com/str-to-bytes32 | |
uint voteCount; // number of accumulated votes | |
} | |
address public chairperson; // this is the person / Ethereum address that is organizing this Ballot - so has special priviledges | |
mapping(address => Voter) public voters; // this mapping is a list of Voter profiles indexed by their address | |
Proposal[] public proposals; // a public list of proposals (Maria, Josh, Steph) called "proposals". In this case it is basic array - not a list of lists - like a mapping. | |
// The functions | |
constructor(string[] memory proposalNames) { | |
// the CONSTRUCTOR is a special function that deploys the contract. It only runs once. It sets the inital state of the contract | |
// To run this function needs an array (a list) that will be called proposalNamestext - this array should be composed of strings. | |
// Note the word "memory" - its saying this info is temporarily stored - read about the difference between MEMORY and STORAGE here: https://medium.com/cryptologic/memory-and-storage-in-solidity-4052c788ca86 | |
chairperson = msg.sender; // the CONSTRUCTOR initializes who is the chairperson - the msg.sender - which is the person that deploys this contract. | |
voters[chairperson].weight = 1; | |
// this line the chairperson variable is an address. The voters mapping is indexed by an address. | |
// This is selecting the chairperson's profile in the voters mapping and sets the weight "property" to 1. | |
// But the voters[chairperson] - has not been filled out yet... so this is actually inputting part of the chairperson's profile in the voters mapping - ""the key" - the chairperson's address and the weight property in the associated Voter struct. | |
// this is a "for loop" that does through the proposalNames array and does something to each element in the array | |
for (uint i = 0; i < proposalNames.length; i++) { | |
proposals.push(Proposal({ | |
name: proposalNames[i], | |
voteCount: 0 | |
})); | |
} | |
// this is the expression that 'populates' the proposals array (Proposal[] public proposals). | |
// The 'proposalNames' inserted in the form of an array when "CONSTRUCTING" (constructor(string[] memory proposalNames)) tell the contract to go and populate the 'proposals' array. We | |
// also want to make sure that each Proposal starts with a 0 in the voteCount. | |
// Here's an article to understand "for" loops in Solidity: https://medium.com/@blockchain101/looping-in-solidity-32c621e05c22 | |
} // closing bracket of the constructor | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment