Browse Source

[+Camilla] Adds voting app example, refactors deployment script

Andrei Biasprozvanny 2 years ago
parent
commit
3e0ef9df9e

+ 1 - 1
ethereum-private/.gitignore

@@ -1,4 +1,4 @@
1 1
 geth/
2 2
 keystore/
3 3
 build/
4
-contracts/deployGreeter.js
4
+.idea/

+ 13 - 13
ethereum-private/README.md

@@ -99,39 +99,39 @@ If the two nodes are connected, and the syncing has ended, A and B should have t
99 99
 
100 100
 ## Deploy contract
101 101
 
102
-You can find a simple `greeter.sol` contract and its deployment script `deployGreeter.js` in the folder `contracts/`.
103
-The first step to deploy is to compile it. To do so install the `solcjs` dependency:
102
+You can find example contracts `greeter.sol` and `voting.sol` and a deployment script `deploy.js` in the folder `contracts/`.
103
+The first step to deploy is to compile the contract. To do so install the `solcjs` dependency:
104 104
 
105 105
 ```
106 106
 npm i -g solcjs
107 107
 ```
108 108
 
109
-Then use it to compile the contract:
109
+Then use it to compile the contract. In the following command, you can replace `contracts/greeter.sol` with the path to the contract of your choice:
110 110
 ```
111 111
 solcjs --bin --abi contracts/greeter.sol -o build
112 112
 ```
113 113
 
114 114
 This will create a folder `build` with the compiled contract in `.abi` and `.bin` formats. The .abi file specifies the methods available (interface) in your contract, the .bin is the bytecode that actually gets deployed onto the blockchain.
115 115
 
116
-Now we need to copy and edit the deployment script:
116
+In your Geth console load the script to generate function that will deploy the contract (you might need to unlock your account).
117 117
 
118 118
 ```
119
-cp contracts/deployGreeter_example.js contracts/deployGreeter.js
119
+> personal.unlockAccount(eth.accounts[0])
120
+> loadScript("contracts/deploy.js")
120 121
 ```
121 122
 
122
-Edit the script adding the content of the `.bin` and `.abi` file in the correct place.
123
+The generated function `deployContract` requires abi, bin and data to be passed as arguments. Assign values to these arguments.
123 124
 
124 125
 ```
125
-nano contracts/deployGreeter.js
126
-```
126
+> var bin = "<paste the contents of the .bin file in the build folder>"
127
+> var abi = <paste the contents of the .abi file in the build folder>
128
+> var data = <if your contract requires data, paste it here. for greeter example, that will be "hello world">
129
+``` 
127 130
 
128
-NOTE: make sure you add '0x' before the content of the `.bin` file.
129
-
130
-Now in your Geth console load the script to deploy the contract (you might need to unlock your account).
131
+Execute the deployment function.
131 132
 
132 133
 ```
133
-> personal.unlockAccount(eth.accounts[0])
134
-> loadScript("contracts/deployGreeter.js")
134
+> deployContract(abi, bin, data)
135 135
 ```
136 136
 
137 137
 You will need miners to accept the contract in the blockchain. Once the contract has been mined you should see in your console:

+ 1 - 1
ethereum-private/app/index.html

@@ -15,5 +15,5 @@
15 15
 </body>
16 16
 <script src="https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js"></script>
17 17
 <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
18
-<script src="./index.js"></script>
18
+<script src="index.js"></script>
19 19
 </html>

ethereum-private/app/index.js → ethereum-private/apps/greeter/index.js


+ 40 - 0
ethereum-private/apps/voting/index.html

@@ -0,0 +1,40 @@
1
+<!DOCTYPE html>
2
+<html>
3
+<head>
4
+  <title>Hello World DApp</title>
5
+  <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
6
+  <link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet' type='text/css'>
7
+</head>
8
+<body class="container">
9
+<h1>A Simple Hello World Voting Application</h1>
10
+<div class="table-responsive">
11
+  <table class="table table-bordered">
12
+    <thead>
13
+    <tr>
14
+      <th>Candidate</th>
15
+      <th>Votes</th>
16
+    </tr>
17
+    </thead>
18
+    <tbody>
19
+    <tr>
20
+      <td>Rylan</td>
21
+      <td id="candidate-1"></td>
22
+    </tr>
23
+    <tr>
24
+      <td>Camilla</td>
25
+      <td id="candidate-2"></td>
26
+    </tr>
27
+    <tr>
28
+      <td>Priya</td>
29
+      <td id="candidate-3"></td>
30
+    </tr>
31
+    </tbody>
32
+  </table>
33
+</div>
34
+<input type="text" id="candidate" />
35
+<a href="#" onclick="voteForCandidate()" class="btn btn-primary">Vote</a>
36
+</body>
37
+<script src="https://cdn.rawgit.com/ethereum/web3.js/develop/dist/web3.js"></script>
38
+<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
39
+<script src="./index.js"></script>
40
+</html>

File diff suppressed because it is too large
+ 24 - 0
ethereum-private/apps/voting/index.js


+ 27 - 0
ethereum-private/contracts/deploy.js

@@ -0,0 +1,27 @@
1
+function deployContract(abi, bin, data) {
2
+
3
+  var contract = web3.eth.contract(abi);
4
+
5
+  return contract.new(
6
+    data,
7
+
8
+    {
9
+
10
+      from: web3.eth.accounts[0],
11
+
12
+      data: '0x' + bin,
13
+
14
+      gas: '4700000',
15
+
16
+    }, function(e, contract) {
17
+
18
+      console.log(e, contract);
19
+
20
+      if (typeof contract.address !== 'undefined') {
21
+
22
+        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
23
+
24
+      }
25
+
26
+    });
27
+}

+ 0 - 27
ethereum-private/contracts/deployGreeter_example.js

@@ -1,27 +0,0 @@
1
-var _greeting = "Hello World";
2
-
3
-var greeterContract = web3.eth.contract(paste the compiled .abi contract as a single line);
4
-
5
-var greeter = greeterContract.new(
6
-
7
-   _greeting,
8
-
9
-   {
10
-
11
-     from: web3.eth.accounts[0],
12
-
13
-     data: 'paste the compiled .bin contract as a single line. NOTE: you need to add "0x" at the beginning',
14
-
15
-     gas: '4700000'
16
-
17
-   }, function (e, contract){
18
-
19
-    console.log(e, contract);
20
-
21
-    if (typeof contract.address !== 'undefined') {
22
-
23
-         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
24
-
25
-    }
26
-
27
- })

+ 47 - 0
ethereum-private/contracts/voting.sol

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

File diff suppressed because it is too large
+ 3447 - 0
ethereum-private/package-lock.json