Skip to content

Instantly share code, notes, and snippets.

@shahzaintariq
Created March 13, 2020 17:52
Show Gist options
  • Save shahzaintariq/63a564f33e53952f4d4d62f5e466e720 to your computer and use it in GitHub Desktop.
Save shahzaintariq/63a564f33e53952f4d4d62f5e466e720 to your computer and use it in GitHub Desktop.
var web3 = new Web3('HTTP://127.0.0.1:8545')
var abi = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "",
"type": "uint256"
},
{
"indexed": false,
"internalType": "string",
"name": "",
"type": "string"
},
{
"indexed": false,
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "Candidate",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "",
"type": "uint256"
},
{
"indexed": false,
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "Vote",
"type": "event"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_id",
"type": "uint256"
},
{
"internalType": "string",
"name": "_name",
"type": "string"
}
],
"name": "addCandidate",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_id",
"type": "uint256"
}
],
"name": "getCandidateData",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
},
{
"internalType": "string",
"name": "",
"type": "string"
},
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function",
"constant": true
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_candidateId",
"type": "uint256"
}
],
"name": "vote",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "result",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function",
"constant": true
}
]
var contractAddress = '0xC72e33Bbe1b711d374d27D5DD9e2FE9F30B20478';
var contract = new web3.eth.Contract(abi,contractAddress);
//js func of adding candidates through browser console
function addSomeCandidate(_id,_name){
//contract's function of adding candidate in blockchain
contract.methods.addCandidate(_id,_name).send(
{
from: '0x4A6c7E8a3524FF7Edcd40452A8A86909335D3321',
gas: 4600000
}
).then((r) => {console.log(r)})
}
var cand = []
var Ids = []
function fetchData(_id) {
var table = document.getElementById('mytable');
var row = table.insertRow(1);
var id = row.insertCell(0);
var n = row.insertCell(1);
var vote = row.insertCell(2);
contract.methods.getCandidateData(_id).call().then( function(r){
id.innerHTML = r[0];
n.innerHTML = r[1];
vote.innerHTML = r[2];
cand.push(r[1]);
Ids.push(r[0]);
})
}
// fetchData(1);
// fetchData(3);
function loadDataForDropList() {
var data = document.querySelectorAll('a');
for(i=0; i<cand.length; i++){
data[i].innerHTML =` ${Ids[i]} : ${cand[i]}`;
}
}
account = '0x61bEb0a698DA264b535E9278F6b6f11D91c2b7E5'
function voteHere() {
contract.methods.vote(Ids[0]).send( {from: account} ).then( (r) => {console.log(r)} )
}
function voteHere1() {
contract.methods.vote(Ids[1]).send( {from: account} ).then( (r) => {console.log(r)} )
}
resultBtn = document.getElementById('result')
function resultF() {
console.log(contract.methods.result().send({from: "0x4A6c7E8a3524FF7Edcd40452A8A86909335D3321"}).then( (r) => {console.log(r)}))
contract.methods.result().call().then( (r) => {alert(`Winner : ${r}`)})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment