-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprototype_server.js
More file actions
73 lines (64 loc) · 2.52 KB
/
Copy pathprototype_server.js
File metadata and controls
73 lines (64 loc) · 2.52 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import express from 'express';
import morgan from 'morgan';
import {Web3} from 'web3';
import dotenv from 'dotenv';
import {voting_abi} from "./abi.js";
dotenv.config();
const app = express();
const web3 = new Web3(`${process.env.PROVIDER_URL}`);
const voting_contract= new web3.eth.Contract(voting_abi,process.env.CONTRACT_ADDRESS)
const owner_account = web3.eth.accounts.wallet.add(`${process.env.OWNER_PRIVATE_KEY}`);
const ownerAddress = web3.eth.accounts.wallet[0].address
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(morgan('dev'));
const block_number=await web3.eth.getBlockNumber();
app.get('/', (req, res) => {
res.send(`hello world!`);
});
app.get('/candidates',async (req,res)=>{
try {
const raw = await voting_contract.methods.getCandidates().call();
console.log("OWNER:", process.env.OWNER_PRIVATE_KEY);
console.log("ADDED ADDRESS:", ownerAddress);
const candidates = raw.map((c) => ({
name: c.name,
voteCount: Number(c.votes), // ensure number
}));
return res.json({ candidates });
} catch (err) {
console.error(err);
return res.status(500).json({ error: 'Failed to fetch candidates' });
}
});
app.post('/candidates',async (req,res)=>{
try {const {candidate_name,privatekey} = req.body;
if(privatekey!==process.env.OWNER_PRIVATE_KEY){
return res.status(403).json({error:'Only owner can add candidates'});
}
const txReceipt = await voting_contract.methods.addCandidate(candidate_name).send({ from: ownerAddress });
return res.json({message:'Candidate added successfully',transactionHash:txReceipt.transactionHash});
} catch (err) {
console.error(err);
return res.status(500).json({ error: 'Failed to add candidate' });
}
});
app.post('/vote',async (req,res)=>{
try {const {candidate_index,address} = req.body;
const txReceipt = await voting_contract.methods.vote(candidate_index).send({ from: address });
return res.json({message:'Vote cast successfully',transactionHash:txReceipt.transactionHash});
} catch (err) {
console.error(err);
return res.status(500).json({ error: 'Failed to cast vote' });
}
});
app.get('/winner',async(req,res)=>{
try {
const winnerIndex = await voting_contract.methods.getWinner().call();
return res.json({winner:winnerIndex});
} catch (err) {
console.error(err);
return res.status(500).json({ error: 'Failed to fetch winner' });
}
});
app.listen(process.env.PORT || 4000,console.log('Express started on port 4000'));