-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (47 loc) · 1.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require('dotenv').config();
const express = require('express');
const app = express();
const fileUpload = require('express-fileupload');
app.use(
fileUpload({
extended:true
})
)
app.use(express.static(__dirname));
app.use(express.json());
const path = require("path");
const ethers = require('ethers');
var port = 3000;
const API_URL = process.env.API_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS;
const {abi} = require('./artifacts/contracts/Voting.sol/Voting.json');
const provider = new ethers.providers.JsonRpcProvider(API_URL);
const signer = new ethers.Wallet(PRIVATE_KEY, provider);
const contractInstance = new ethers.Contract(CONTRACT_ADDRESS, abi, signer);
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
})
app.get("/index.html", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
})
app.post("/vote", async (req, res) => {
var vote = req.body.vote;
console.log(vote)
async function storeDataInBlockchain(vote) {
console.log("Adding the candidate in voting contract...");
const tx = await contractInstance.addCandidate(vote);
await tx.wait();
}
const bool = await contractInstance.getVotingStatus();
if (bool == true) {
await storeDataInBlockchain(vote);
res.send("The candidate has been registered in the smart contract");
}
else {
res.send("Voting is finished");
}
});
app.listen(port, function () {
console.log("App is listening on port 3000")
});