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
import Web3 from "web3"; | |
import votingArtifact from "../../build/contracts/Voting.json"; | |
let candidates = {"Rama": "candidate-1", "Nick": "candidate-2", "Jose": "candidate-3"} | |
const App = { | |
web3: null, | |
account: null, | |
voting: null, |
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
abi = JSON.parse('[{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"totalVotesFor","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"validCandidate","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"votesReceived","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"candidateList","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"candidate","type":"bytes32"}],"name":"voteForCandidate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"candidateNames","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpa |
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.18; | |
contract MyContract { | |
mapping(address => uint) totalTokens; | |
function buy() payable public { | |
totalTokens[msg.sender] = msg.value; | |
} |
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
window.voteForCandidate = function(candidate) { | |
let candidateName = $("#candidate").val(); | |
let msgParams = [ | |
{ | |
type: 'string', // Any valid solidity type | |
name: 'Message', // Any string label you want | |
value: 'Vote for ' + candidateName // The value to sign | |
} | |
] |
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 Voting = artifacts.require("./Voting.sol"); | |
var ECRecovery = artifacts.require("./ECRecovery.sol"); | |
const sigUtil = require("eth-sig-util") | |
var alice_vote_hash = sigUtil.typedSignatureHash([{ type: 'string', name: 'Message', value: "Vote for Alice"}]) | |
var bob_vote_hash = sigUtil.typedSignatureHash([{ type: 'string', name: 'Message', value: "Vote for Bob"}]) | |
var carol_vote_hash = sigUtil.typedSignatureHash([{ type: 'string', name: 'Message', value: "Vote for Carol"}]) | |
module.exports = function(deployer) { |
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.18; | |
import "./ECRecovery.sol"; | |
contract Voting { | |
using ECRecovery for bytes32; | |
mapping (bytes32 => uint8) public votesReceived; | |
mapping(bytes32 => bytes32) public candidateHash; |
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
window.submitVote = function(candidate) { | |
let candidateName = $("#candidate-name").val(); | |
let signature = $("#vote-signature").val(); | |
let voterAddress = $("#voter-address").val(); | |
$("#msg").html("Vote has been submitted. The vote count will increment as soon as the vote is recorded on the blockchain. Please wait.") | |
Voting.deployed().then(function(contractInstance) { | |
contractInstance.voteForCandidate(candidateName, voterAddress, signature, {gas: 140000, from: web3.eth.accounts[0]}).then(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
// We use the struct datatype to store the voter information. | |
struct voter { | |
address voterAddress; // The address of the voter | |
uint tokensBought; // The total no. of tokens this voter owns | |
uint[] tokensUsedPerCandidate; // Array to keep track of votes per candidate. | |
/* We have an array called candidateList initialized below. | |
Every time this voter votes with her tokens, the value at that | |
index is incremented. Example, if candidateList array declared | |
below has ["Rama", "Nick", "Jose"] and this | |
voter votes 10 tokens to Nick, the tokensUsedPerCandidate[1] |
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
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")) |
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
Verifying that "maheshmurthy.id" is my Blockstack ID. https://onename.com/maheshmurthy |