Skip to content

Commit 500741b

Browse files
committed
First commit
0 parents  commit 500741b

6 files changed

+311
-0
lines changed

Deploy.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Web3 = require("web3");
2+
solc = require('solc');
3+
fs = require("fs");
4+
5+
// Block chain apirpc port
6+
//web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8081"));
7+
8+
web3 = new Web3();
9+
var TestRPC = require("ethereumjs-testrpc");
10+
web3.setProvider(TestRPC.provider({unlocked_accounts:['0xfac3bc3201226bec30db2c9a68a8015e2ab8e267']}));
11+
12+
13+
// Read and Compile code
14+
//console.log('------------------ Code file read');
15+
code = fs.readFileSync('VotingContract.sol').toString();
16+
//console.log('------------------ Code compiling');
17+
compiledCode = solc.compile(code);
18+
// console.log(compiledCode);
19+
20+
console.log('------------------ Abi Definition');
21+
abiDefinition = JSON.parse(compiledCode.contracts[':VotingContract'].interface)
22+
//var str = JSON.stringify(abiDefinition);
23+
console.log(abiDefinition);
24+
25+
console.log('------------------ Reading Byte code ');
26+
byteCode = compiledCode.contracts[':VotingContract'].bytecode
27+
console.log('0x' + byteCode);
28+
29+
//console.log('------------------ Creating contract ')
30+
VotingContract = web3.eth.contract(abiDefinition)
31+
//console.log('------------------ Voting contract created ')
32+
//console.log(VotingContract.options);
33+
34+
web3.eth.getAccounts(function (error, result) {
35+
36+
var defaultAcc = result[0];
37+
//console.log("------------------ DefaultAcc Address => ", defaultAcc);
38+
39+
// console.log('------------------ Unlocking acc ')
40+
// web3.personal.unlockAccount(defaultAcc,'test12345'
41+
// , function(res){
42+
// console.log(res);
43+
// });
44+
45+
var deployedContract = null;
46+
//console.log('------------------ Deploying contract ');
47+
VotingContract.new(['Rama','Nick','Jose'],{data: '0x' + byteCode, from: defaultAcc, gas: 4700000}
48+
, function(err, contract){
49+
// console.log(err);
50+
// console.log(contract);
51+
deployedContract = contract; // 0x326ddddfd73948c83f6e1d86bc5cdffe0f4c5f06
52+
console.log('------------------ Deployed contract address : ' + deployedContract.address);
53+
})
54+
});

FinalContract.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Web3 = require("web3");
2+
solc = require('solc');
3+
fs = require("fs");
4+
5+
// Block chain apirpc port
6+
//web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8081"));
7+
8+
web3 = new Web3();
9+
var TestRPC = require("ethereumjs-testrpc");
10+
web3.setProvider(TestRPC.provider({unlocked_accounts:['0xfac3bc3201226bec30db2c9a68a8015e2ab8e267']}));
11+
12+
13+
// Read and Compile code
14+
console.log('------------------ Code file read');
15+
code = fs.readFileSync('VotingContract.sol').toString();
16+
console.log('------------------ Code compiling');
17+
compiledCode = solc.compile(code);
18+
// console.log(compiledCode);
19+
20+
console.log('------------------ Abi Definition');
21+
abiDefinition = JSON.parse(compiledCode.contracts[':VotingContract'].interface)
22+
// console.log('------------------ Abi : ');
23+
// var str = JSON.stringify(abiDefinition);
24+
// console.log(str);
25+
26+
console.log('------------------ Reading Byte code ');
27+
byteCode = compiledCode.contracts[':VotingContract'].bytecode
28+
// console.log('0x' + byteCode);
29+
30+
console.log('------------------ Creating contract ')
31+
VotingContract = web3.eth.contract(abiDefinition)
32+
console.log('------------------ Voting contract created ')
33+
//console.log(VotingContract.options);
34+
35+
web3.eth.getAccounts(function (error, result) {
36+
37+
var defaultAcc = result[0];
38+
console.log("------------------ DefaultAcc Address ")
39+
console.log(defaultAcc);
40+
41+
// console.log('------------------ Unlocking acc ')
42+
// web3.personal.unlockAccount(defaultAcc,'test12345'
43+
// , function(res){
44+
// console.log(res);
45+
// });
46+
47+
var deployedContract = null;
48+
console.log('------------------ Deploying contract ');
49+
VotingContract.new(['Rama','Nick','Jose'],{data: '0x' + byteCode, from: defaultAcc, gas: 4700000}
50+
, function(err, contract){
51+
// console.log(err);
52+
// console.log(contract);
53+
deployedContract = contract; // 0x326ddddfd73948c83f6e1d86bc5cdffe0f4c5f06
54+
console.log('------------------ Deployed contract address : ' + deployedContract.address);
55+
56+
console.log('------------------ ContractInstance --------------------')
57+
contractInstance = VotingContract.at(deployedContract.address);
58+
//console.log(contractInstance);
59+
60+
contractInstance.totalVotesFor('Rama', function(err,result){
61+
console.log('------------------ Total Votes for Rama --------------------')
62+
if(err){
63+
console.log('!!!...Error...!!!');
64+
console.log(err);
65+
}
66+
else{
67+
console.log("Total Votes => ", result.toString());
68+
}
69+
}) ;
70+
71+
contractInstance.voteForCandidate('Rama', {from: defaultAcc}, function(err,result){
72+
console.log('------------------ Voting for Rama 1 --------------------')
73+
if(err){
74+
console.log('!!!...Error...!!!');
75+
console.log(err);
76+
}
77+
else{
78+
console.log("Tx => ", result);
79+
}
80+
}) ;
81+
82+
contractInstance.voteForCandidate('Rama', {from: defaultAcc}, function(err,result){
83+
console.log('------------------ Voting for Rama 2 --------------------')
84+
if(err){
85+
console.log('!!!...Error...!!!');
86+
console.log(err);
87+
}
88+
else{
89+
console.log("Tx => ", result);
90+
}
91+
}) ;
92+
93+
contractInstance.totalVotesFor('Rama', function(err,result){
94+
console.log('------------------ After voting for Rama --------------------')
95+
if(err){
96+
console.log('!!!...Error...!!!');
97+
console.log(err);
98+
}
99+
else{
100+
console.log("Total Votes => ", result.toString());
101+
}
102+
}) ;
103+
})
104+
});

Interact.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Web3 = require("web3");
2+
solc = require('solc');
3+
fs = require("fs");
4+
5+
// Block chain apirpc port
6+
//web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8081"));
7+
8+
web3 = new Web3();
9+
var TestRPC = require("ethereumjs-testrpc");
10+
web3.setProvider(TestRPC.provider());
11+
12+
13+
var abiDefinition = 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"},{"constant":true,"inputs":[],"name":"getCandidateList","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"candidateNames","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]');
14+
var address = "0x69522edac428aa6a369d60e42d374da5575cb911";
15+
16+
console.log('------------------ Creating Contract ');
17+
VotingContract = web3.eth.contract(abiDefinition);
18+
console.log('------------------ Contract Instance ');
19+
contractInstance = VotingContract.at(address);
20+
// console.log(contractInstance);
21+
22+
web3.eth.getAccounts(function (error, result) {
23+
24+
var defaultAcc = result[0];
25+
console.log("------------------ DefaultAcc Address => ", defaultAcc);
26+
27+
// console.log('------------------ Unlocking acc ')
28+
// web3.personal.unlockAccount(defaultAcc,'test12345'
29+
// , function(res){
30+
// console.log(res);
31+
// });
32+
33+
34+
35+
contractInstance.totalVotesFor('Rama', function(err,result){
36+
console.log('------------------ Total Votes for Rama --------------------')
37+
if(err){
38+
console.log('!!!...Error...!!!');
39+
console.log(err);
40+
}
41+
else{
42+
console.log("Total Votes => ", result.toString());
43+
}
44+
}) ;
45+
46+
contractInstance.voteForCandidate('Rama', {from: defaultAcc}, function(err,result){
47+
console.log('------------------ Voting for Rama 1 --------------------')
48+
if(err){
49+
console.log('!!!...Error...!!!');
50+
console.log(err);
51+
}
52+
else{
53+
console.log("Tx => ", result);
54+
}
55+
}) ;
56+
57+
contractInstance.voteForCandidate('Rama', {from: defaultAcc}, function(err,result){
58+
console.log('------------------ Voting for Rama 2 --------------------')
59+
if(err){
60+
console.log('!!!...Error...!!!');
61+
console.log(err);
62+
}
63+
else{
64+
console.log("Tx => ", result);
65+
}
66+
}) ;
67+
68+
contractInstance.totalVotesFor('Rama', function(err,result){
69+
console.log('------------------ After voting for Rama --------------------')
70+
if(err){
71+
console.log('!!!...Error...!!!');
72+
console.log(err);
73+
}
74+
else{
75+
console.log("Total Votes => ", result.toString());
76+
}
77+
}) ;
78+
79+
});

VotingContract.sol

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
pragma solidity ^0.4.18;
2+
3+
contract VotingContract {
4+
/* mapping field below is equivalent to an associative array or hash.
5+
The key of the mapping is candidate name stored as type bytes32 and value is
6+
an unsigned integer to store the vote count
7+
*/
8+
9+
mapping (bytes32 => uint8) public votesReceived;
10+
11+
/* Solidity doesn't let you pass in an array of strings in the constructor (yet).
12+
We will use an array of bytes32 instead to store the list of candidates
13+
*/
14+
15+
bytes32[] public candidateList;
16+
17+
/* This is the constructor which will be called once when you
18+
deploy the contract to the blockchain. When we deploy the contract,
19+
we will pass an array of candidates who will be contesting in the election
20+
*/
21+
function VotingContract(bytes32[] candidateNames) public {
22+
candidateList = candidateNames;
23+
}
24+
25+
// This function returns the total votes a candidate has received so far
26+
function totalVotesFor(bytes32 candidate) public view returns (uint8) {
27+
require(validCandidate(candidate));
28+
return votesReceived[candidate];
29+
}
30+
31+
// This function increments the vote count for the specified candidate. This
32+
// is equivalent to casting a vote
33+
function voteForCandidate(bytes32 candidate) public {
34+
require(validCandidate(candidate));
35+
votesReceived[candidate] += 1;
36+
}
37+
38+
function getCandidateList() public view returns(bytes32[]) {
39+
return candidateList;
40+
}
41+
42+
function validCandidate(bytes32 candidate) public view returns (bool) {
43+
for (uint i = 0; i < candidateList.length; i++) {
44+
if (candidateList[i] == candidate) {
45+
return true;
46+
}
47+
}
48+
return false;
49+
}
50+
}

contractInfo.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "nodeexpressapp1",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node server.js"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"body-parser": "^1.17.2",
14+
"ethereumjs-testrpc": "^6.0.3",
15+
"express": "^4.15.3",
16+
"solc": "^0.4.19",
17+
"web3": "^0.20.1"
18+
}
19+
}

0 commit comments

Comments
 (0)