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
require "test/unit/assertions" | |
include Test::Unit::Assertions | |
def recursive_flatten(array, results = []) | |
array.each do |element| | |
if element.class == Array | |
recursive_flatten(element, results) | |
else | |
results << element | |
end | |
end |
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
pragma solidity ^0.4.25; | |
contract Election { | |
// Model a Candidate | |
struct Candidate { | |
uint id; | |
string name; | |
uint voteCount; | |
} |
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
function vote (uint _candidateId) public { | |
// require that they haven't voted before | |
require(!voters[msg.sender]); | |
// require a valid candidate | |
require(_candidateId > 0 && _candidateId <= candidatesCount); | |
// record that voter has voted | |
voters[msg.sender] = true; |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Election Results</title> | |
<!-- Bootstrap --> | |
<link href="css/bootstrap.min.css" rel="stylesheet"> |
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
App = { | |
web3Provider: null, | |
contracts: {}, | |
account: '0x0', | |
init: function() { | |
return App.initWeb3(); | |
}, | |
initWeb3: function() { |
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
var Election = artifacts.require("./Election.sol"); | |
contract("Election", function(accounts) { | |
var electionInstance; | |
it("initializes with two candidates", function() { | |
return Election.deployed().then(function(instance) { | |
return instance.candidatesCount(); | |
}).then(function(count) { | |
assert.equal(count, 2); |
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
pragma solidity ^0.4.24; | |
contract Election { | |
// Model a Candidate | |
struct Candidate { | |
uint id; | |
string name; | |
uint voteCount; | |
} |
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
contract Election { | |
// ... | |
constructor () public { | |
addCandidate("Candidate 1"); | |
addCandidate("Candidate 2"); | |
} | |
// ... | |
} |
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
contract Election { | |
// ... | |
function addCandidate (string _name) private { | |
candidatesCount ++; | |
candidates[candidatesCount] = Candidate(candidatesCount, _name, 0); | |
} | |
} |
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
contract Election { | |
// Model a Candidate | |
struct Candidate { | |
uint id; | |
string name; | |
uint voteCount; | |
} | |
// Read/write Candidates | |
mapping(uint => Candidate) public candidates; |
NewerOlder