Skip to content

Instantly share code, notes, and snippets.

pragma solidity ^0.4.18;
import "./ECRecovery.sol";
contract Voting {
using ECRecovery for bytes32;
mapping (bytes32 => uint8) public votesReceived;
mapping(bytes32 => bytes32) public candidateHash;
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() {
@maheshmurthy
maheshmurthy / sign_vote.js
Last active April 15, 2018 13:59
Ethereum Voting Dapp sign and verify
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
}
]
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
// 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]
<!DOCTYPE html>
<html>
<head>
<title>Hello World DApp</title>
<link href='https://fonts.googleapis.com/css?family=Open Sans:400,700' rel='stylesheet' type='text/css'>
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet' type='text/css'>
</head>
<body class="container">
<h1>A Simple Hello World Voting Application</h1>
<div class="table-responsive">
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,
@maheshmurthy
maheshmurthy / index.html
Last active December 7, 2021 21:07
Html file to cast and display votes
<!DOCTYPE html>
<html>
<head>
<title>Hello World DApp</title>
<link href='https://fonts.googleapis.com/css?family=Open Sans:400,700' rel='stylesheet' type='text/css'>
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet' type='text/css'>
</head>
<body class="container">
<h1>A Simple Hello World Voting Application</h1>
<div class="table-responsive">
@maheshmurthy
maheshmurthy / Voting.sol
Last active June 6, 2023 21:20
Simple solidity contract to vote for a candidate and see the results
pragma solidity ^0.6.4;
// We have to specify what version of compiler this code will compile with
contract Voting {
/* mapping field below is equivalent to an associative array or hash.
The key of the mapping is candidate name stored as type bytes32 and value is
an unsigned integer to store the vote count
*/
mapping (bytes32 => uint256) public votesReceived;
@maheshmurthy
maheshmurthy / gist:13896d903e6f26de39e2bc840f361231
Created January 9, 2017 05:04
onename verification maheshmurthy
Verifying that "maheshmurthy.id" is my Blockstack ID. https://onename.com/maheshmurthy