Commit 86df37d8 authored by Ganesh Katrapati's avatar Ganesh Katrapati
Browse files

Added Public folder

parents
Pipeline #3184 failed with stages
in 20 seconds
This is a simple truffle app to run an basic ethereum smart contract for voting.
What do you need : ethereum-testrpc (see npm install), truffle and solidity
<!DOCTYPE html>
<html>
<head>
<title>MetaCoin - Truffle Webpack Demo w/ Frontend</title>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<script src="./app.js"></script>
</head>
<body>
<h1>MetaCoin</h1>
<h2>Example Truffle Dapp</h2>
<h3>Candidate List <span class="black"><span id="candidates"></span> </span></h3>
<h3>You have <span class="black"><span id="balance"></span> Votes</span></h3>
<br><button id="send" onclick="App.upvote()">Upvote</button>
<br><br>
<span id="status"></span>
<br>
<span class="hint"><strong>Hint:</strong> open the browser developer console to view any errors and warnings.</span>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>MetaCoin - Truffle Webpack Demo w/ Frontend</title>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<script src="./app.js"></script>
</head>
<body>
<h1>MetaCoin</h1>
<h2>Example Truffle Dapp</h2>
<h3>You have <span class="black"><span id="balance"></span> Votes</span></h3>
<br><button id="send" onclick="App.upvote()">Upvote</button>
<br><br>
<span id="status"></span>
<br>
<span class="hint"><strong>Hint:</strong> open the browser developer console to view any errors and warnings.</span>
</body>
</html>
// Import the page's CSS. Webpack will know what to do with it.
import "../stylesheets/app.css";
// Import libraries we need.
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'
// Import our contract artifacts and turn them into usable abstractions.
import voting_artifacts from '../../build/contracts/Voting.json'
// MetaCoin is our usable abstraction, which we'll use through the code below.
var Voting = contract(voting_artifacts);
// The following code is simple to show off interacting with your contracts.
// As your needs grow you will likely need to change its form and structure.
// For application bootstrapping, check out window.addEventListener below.
var accounts;
var account;
window.App = {
start: function() {
var self = this;
// Bootstrap the MetaCoin abstraction for Use.
Voting.setProvider(web3.currentProvider);
// Get the initial account balance so it can be displayed.
web3.eth.getAccounts(function(err, accs) {
if (err != null) {
alert("There was an error fetching your accounts.");
return;
}
if (accs.length == 0) {
alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
return;
}
accounts = accs;
account = accounts[0];
console.log(accounts);
self.refreshBalance();
});
},
setStatus: function(message) {
var status = document.getElementById("status");
status.innerHTML = message;
},
fetchVotes : function(candidate_id, candidate) {
var self = this;
var meta;
Voting.deployed().then(function(instance) {
meta = instance;
// console.log(meta.candidates.length);
return meta.getvotes.call(candidate, {from: account});
}).then(function(value) {
console.log("Candidates", value.toString());
var balance_element = document.getElementById("candidate_"+candidate_id);
// for(var i=0; i < value.valueOf(); i++){
// }
balance_element.innerHTML = value.valueOf() + "&emsp;<button onclick=\"App.upvote('"+candidate+"')\">Upvote</button>";
//return value.valueOf();
}).catch(function(e) {
console.log(e);
self.setStatus("Error getting balance; see log.");
});
},
fetchCandidate : function(candidate_id) {
var self = this;
var meta;
Voting.deployed().then(function(instance) {
meta = instance;
// console.log(meta.candidates.length);
return meta.candidates.call(candidate_id, {from: account});
}).then(function(value) {
// console.log("Candidates", value.toString());
var balance_element = document.getElementById("candidates");
// for(var i=0; i < value.valueOf(); i++){
// }
balance_element.innerHTML += "<li>" + web3.toAscii(value.valueOf()) + "&emsp;<span id='candidate_"+candidate_id+"'></span></li>" ;
self.fetchVotes(candidate_id, value.valueOf());
}).catch(function(e) {
console.log(e);
self.setStatus("Error getting balance; see log.");
});
},
refreshBalance: function() {
var self = this;
var meta;
Voting.deployed().then(function(instance) {
meta = instance;
// console.log(meta.candidates.length);
return meta.getCandidateCount({from: account});
}).then(function(value) {
// console.log("Candidates", value.toString());
var balance_element = document.getElementById("candidates");
balance_element.innerHTML = "<ul>";
for(var i=0; i < value.valueOf(); i++){
self.fetchCandidate(i);
}
balance_element.innerHTML += "</ul>";
// balance_element.innerHTML = web3.toAscii(value.valueOf());
}).catch(function(e) {
console.log(e);
self.setStatus("Error getting balance; see log.");
});
},
upvote : function(candidate) {
var self = this;
this.setStatus("Initiating transaction... (please wait)");
var meta;
Voting.deployed().then(function(instance) {
meta = instance;
return meta.upvote(candidate, {from: account});
}).then(function() {
self.setStatus("Transaction complete!");
self.refreshBalance();
}).catch(function(e) {
console.log(e);
self.setStatus("Error sending coin; see log.");
});
}
};
window.addEventListener('load', function() {
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
}
App.start();
});
// Import the page's CSS. Webpack will know what to do with it.
import "../stylesheets/app.css";
// Import libraries we need.
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'
// Import our contract artifacts and turn them into usable abstractions.
import voting_artifacts from '../../build/contracts/Voting.json'
// MetaCoin is our usable abstraction, which we'll use through the code below.
var Voting = contract(voting_artifacts);
// The following code is simple to show off interacting with your contracts.
// As your needs grow you will likely need to change its form and structure.
// For application bootstrapping, check out window.addEventListener below.
var accounts;
var account;
window.App = {
start: function() {
var self = this;
// Bootstrap the MetaCoin abstraction for Use.
Voting.setProvider(web3.currentProvider);
// Get the initial account balance so it can be displayed.
web3.eth.getAccounts(function(err, accs) {
if (err != null) {
alert("There was an error fetching your accounts.");
return;
}
if (accs.length == 0) {
alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
return;
}
accounts = accs;
account = accounts[0];
console.log(accounts);
self.refreshBalance();
});
},
setStatus: function(message) {
var status = document.getElementById("status");
status.innerHTML = message;
},
refreshBalance: function() {
var self = this;
var meta;
Voting.deployed().then(function(instance) {
meta = instance;
// console.log(meta.candidates.length);
return meta.getCandidateCount({from: account});
}).then(function(value) {
console.log("Candidates", value.toString());
var balance_element = document.getElementById("candidates");
balance_element.innerHTML = web3.toAscii(value.valueOf());
}).catch(function(e) {
console.log(e);
self.setStatus("Error getting balance; see log.");
});
},
upvote : function() {
var self = this;
this.setStatus("Initiating transaction... (please wait)");
var meta;
Voting.deployed().then(function(instance) {
meta = instance;
return meta.upvote({from: account});
}).then(function() {
self.setStatus("Transaction complete!");
self.refreshBalance();
}).catch(function(e) {
console.log(e);
self.setStatus("Error sending coin; see log.");
});
}
};
window.addEventListener('load', function() {
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
}
App.start();
});
body {
margin-left: 25%;
margin-right: 25%;
margin-top: 10%;
font-family: "Open Sans", sans-serif;
}
label {
display: inline-block;
width: 100px;
}
input {
width: 500px;
padding: 5px;
font-size: 16px;
}
button {
font-size: 16px;
padding: 5px;
}
h1, h2 {
display: inline-block;
vertical-align: middle;
margin-top: 0px;
margin-bottom: 10px;
}
h2 {
color: #AAA;
font-size: 32px;
}
h3 {
font-weight: normal;
color: #AAA;
font-size: 24px;
}
.black {
color: black;
}
#balance {
color: black;
}
.hint {
color: #666;
}
{
"contractName": "ConvertLib",
"abi": [
{
"constant": false,
"inputs": [
{
"name": "amount",
"type": "uint256"
},
{
"name": "conversionRate",
"type": "uint256"
}
],
"name": "convert",
"outputs": [
{
"name": "convertedAmount",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x6060604052341561000f57600080fd5b60b08061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806396e4ee3d146044575b600080fd5b606160048080359060200190919080359060200190919050506077565b6040518082815260200191505060405180910390f35b60008183029050929150505600a165627a7a723058206985dc392177ebc441533bf0d56b14016afbae54d9df8594ac6306548d65735a0029",
"deployedBytecode": "0x606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806396e4ee3d146044575b600080fd5b606160048080359060200190919080359060200190919050506077565b6040518082815260200191505060405180910390f35b60008183029050929150505600a165627a7a723058206985dc392177ebc441533bf0d56b14016afbae54d9df8594ac6306548d65735a0029",
"sourceMap": "25:143:0:-;;;;;;;;;;;;;;;;;",
"deployedSourceMap": "25:143:0:-;;;;;;;;;;;;;;;;;;;;;;;;46:120;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;105:20;148:14;139:6;:23;132:30;;46:120;;;;:::o",
"source": "pragma solidity ^0.4.2;\n\nlibrary ConvertLib{\n\tfunction convert(uint amount,uint conversionRate) returns (uint convertedAmount)\n\t{\n\t\treturn amount * conversionRate;\n\t}\n}\n",
"sourcePath": "/storage/work/projects/voting/contracts/ConvertLib.sol",
"ast": {
"attributes": {
"absolutePath": "/storage/work/projects/voting/contracts/ConvertLib.sol",
"exportedSymbols": {
"ConvertLib": [
16
]
}
},
"children": [
{
"attributes": {
"literals": [
"solidity",
"^",
"0.4",
".2"
]
},
"id": 1,
"name": "PragmaDirective",
"src": "0:23:0"
},
{
"attributes": {
"baseContracts": [
null
],
"contractDependencies": [
null
],
"contractKind": "library",
"documentation": null,
"fullyImplemented": true,
"linearizedBaseContracts": [
16
],
"name": "ConvertLib",
"scope": 17
},
"children": [
{
"attributes": {
"constant": false,
"implemented": true,
"isConstructor": false,
"modifiers": [
null
],
"name": "convert",
"payable": false,
"scope": 16,
"stateMutability": "nonpayable",
"superFunction": null,
"visibility": "public"
},
"children": [
{
"children": [
{
"attributes": {
"constant": false,
"name": "amount",
"scope": 15,
"stateVariable": false,
"storageLocation": "default",
"type": "uint256",
"value": null,
"visibility": "internal"
},
"children": [
{
"attributes": {
"name": "uint",
"type": "uint256"
},
"id": 2,
"name": "ElementaryTypeName",
"src": "63:4:0"
}
],
"id": 3,
"name": "VariableDeclaration",
"src": "63:11:0"
},
{
"attributes": {
"constant": false,
"name": "conversionRate",
"scope": 15,
"stateVariable": false,
"storageLocation": "default",
"type": "uint256",
"value": null,
"visibility": "internal"
},
"children": [
{
"attributes": {
"name": "uint",
"type": "uint256"
},
"id": 4,
"name": "ElementaryTypeName",
"src": "75:4:0"
}
],
"id": 5,
"name": "VariableDeclaration",
"src": "75:19:0"
}
],
"id": 6,
"name": "ParameterList",
"src": "62:33:0"
},
{
"children": [
{
"attributes": {
"constant": false,
"name": "convertedAmount",
"scope": 15,
"stateVariable": false,
"storageLocation": "default",
"type": "uint256",
"value": null,
"visibility": "internal"
},
"children": [
{
"attributes": {
"name": "uint",
"type": "uint256"
},
"id": 7,
"name": "ElementaryTypeName",
"src": "105:4:0"
}
],
"id": 8,
"name": "VariableDeclaration",
"src": "105:20:0"
}
],
"id": 9,
"name": "ParameterList",
"src": "104:22:0"
},
{
"children": [
{
"attributes": {
"functionReturnParameters": 9
},
"children": [
{
"attributes": {
"argumentTypes": null,
"commonType": {
"typeIdentifier": "t_uint256",
"typeString": "uint256"
},
"isConstant": false,
"isLValue": false,
"isPure": false,
"lValueRequested": false,
"operator": "*",
"type": "uint256"
},
"children": [
{
"attributes": {
"argumentTypes": null,
"overloadedDeclarations": [
null
],
"referencedDeclaration": 3,
"type": "uint256",
"value": "amount"
},
"id": 10,
"name": "Identifier",
"src": "139:6:0"
},
{
"attributes": {
"argumentTypes": null,
"overloadedDeclarations": [
null
],
"referencedDeclaration": 5,
"type": "uint256",
"value": "conversionRate"
},
"id": 11,
"name": "Identifier",
"src": "148:14:0"
}
],
"id": 12,
"name": "BinaryOperation",
"src": "139:23:0"
}
],
"id": 13,
"name": "Return",
"src": "132:30:0"
}
],
"id": 14,
"name": "Block",
"src": "128:38:0"