-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
245 lines (227 loc) · 8.58 KB
/
server.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
try{
const express = require('express');
const app=express();
const cors = require('cors');
app.use(cors());
const Web3=require('web3');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const json = require('./build/contracts/TollCoin.json');
const abi = json['abi'];
if (typeof web3 !== 'undefined') {
var web3 = new Web3(web3.currentProvider)
} else {
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
}
const contractAdd="0xB9bF025560B429e1619e05a918805F267ab2fDD6";
var contract= new web3.eth.Contract(abi,contractAdd);
console.log('Server started in http://localhost:3000 in browser');
var deployerAddress
web3.eth.getAccounts().then(function(e){
deployerAddress=e[0];
console.log('Owner (deployer) Address: ' +deployerAddress)
}).catch(e=>console.log(e));
//New Contractor
app.post('/contReg',async function(req,res,next){
var contAdd=req.body.contractorAdd;
var contName=req.body.contractorName;
console.log(contAdd+" "+contName);
try{
await contract.methods.newContractor(contAdd,contName).send({ from: deployerAddress, gas: 150000 }).then(function(value){
console.log('Registered contractor: ' + value.transactionHash);
console.log(value);
res.status(201).json({success:true,address:contAdd,hash:value.transactionHash});
}).catch(e=>{
const data = e.data;
const txHash = Object.keys(data)[0]; // TODO improve
const reason = data[txHash].reason;
console.log(reason);
console.log(e); // prints "This is error message"
res.status(404).json({ success: false, error: reason });
});
}
catch(err){
res.status(404).json({ success: false, error: err.message });
console.log(err.message);
}
});
//New User
app.post('/userReg',async function(req,res,next){
var uAdd=req.body.userAdd;
var vNum=req.body.vehicleNum;
var rfid=req.body.rfid;
var name=req.body.uName;
try{
await contract.methods.newUser(uAdd,vNum,rfid,name).send({ from: deployerAddress, gas: 300000 ,value:0}).then(function(value){
console.log('Registered user: ' + value.transactionHash);
console.log(value);
res.status(201).json({success:true,address:uAdd,hash:value.transactionHash});
}).catch(e=>{
const data = e.data;
const txHash = Object.keys(data)[0]; // TODO improve
const reason = data[txHash].reason;
console.log(reason); // prints "This is error message"
res.status(404).json({ success: false, error: reason });
});
}
catch(err){
res.status(404).json({ success: false, error: err.message });
console.log(err.message);
}
});
/*app.post('/userReg', async function (req, res, next) {
var userAddress = req.body.add1
var vehNum = req.body.vnum
try {
await contract.methods.registerUser(userAddress, vehNum).send({ from: deployerAddress, gas: 150000 }).then(function (value) {
console.log('Registered user: ' + value.transactionHash);
console.log(value);
res.send("The user:" + userAddress + " has been registered! Transaction Hash: " + value.transactionHash);
});
} catch (err) {
await res.send(err.d);
console.log(err.message);
}
});*/
//User Profile
app.get('/userProfile',async function(req,res){
var userAd=req.body.userAdd;
try{
await contract.methods.getUserProfile(userAd).call().then(function(value){
console.log(value);
console.log("User Details:\n User Name:"+value[0]+"Vehicle Num:"+value[1]+"Balance:"+value[2]);
res.status(200).json({userName:value[0],vehicleNum:value[1],balance:value[2]});
}).catch(e=>{
const data = e.data;
const txHash = Object.keys(data)[0]; // TODO improve
const reason = data[txHash].reason;
console.log(reason); // prints "This is error message"
res.status(404).json({ success: false, error: reason });
});
}
catch(err){
res.status(404).json({success:false,message:err.message});
}
});
//Contractor Profile
app.get('/contProfile',async function(req,res){
var contAd=req.body.contAdd;
try{
await contract.methods.getContractorProfile(contAd).call().then(function(value){
console.log("Contractor details:\n Contractor Name:"+value[0]+"Balance:"+value[1]);
return res.status(200).json({contName:value[0],balance:value[1]});
}).catch(e=>{
const data = e.data;
const txHash = Object.keys(data)[0]; // TODO improve
const reason = data[txHash].reason;
console.log(reason);
console.log(e.message); // prints "This is error message"
res.status(404).json({ success: false, error: reason });
});
}
catch(err){
return res.status(404).json({success:false,message:err.message});
}
});
//Pay Toll
app.post('/rfid',async function(req,res){
var rfidCode=req.body.uid;
var contAdd=req.body.contAdd;
var amount=req.body.amount;
var etherValue=0;
console.log(req.body);
console.log("Someone accessed");
console.log("UID"+rfidCode);
try{
await contract.methods.rfidUserId(rfidCode).call().then(async function(value){
await contract.methods.payToll(contAdd,value,amount).send({ from: value, gas: 300000, value: etherValue }).then(function(value){
return res.status(200).json({success:true,message:"Access granted",transactionHash:value.transactionHash});
}).catch(e=>{
const data = e.data;
const txHash = Object.keys(data)[0]; // TODO improve
const reason = data[txHash].reason;
console.log(e); // prints "This is error message"
res.status(404).json({ success: false, error: reason });
});
})
}
catch(err){
res.status(404).json({success:false,message:err.message});
}
});
//Buy Tokens
app.post('/buy',async function(req,res){
var userAd=req.body.userAdd;
var reqTokens=req.body.token;
var etherValue = (reqTokens * 0.01);
await contract.methods.buyTokens(userAd,reqTokens).send({ from: userAd, gas: 150000, value: web3.utils.toWei(etherValue.toString(), 'ether') }).then(function(value){
console.log('Token Purchase: ' + value.transactionHash);
res.status(200).json({success:true,userAd:userAd,tokens:reqTokens,hash: value.transactionHash});
}).catch(e=>{
const data = e.data;
const txHash = Object.keys(data)[0]; // TODO improve
const reason = data[txHash].reason;
console.log(reason); // prints "This is error message"
res.status(404).json({ success: false, error: reason });
});
})
//Withdraw tokens
//Balance of user
app.get('/balance',async function(req,res){
var userAd=req.body.userAdd;
console.log(userAd);
try{
await contract.methods.checkBal(userAd).call().then(function(value){
res.status(200).json({balance:value});
}).catch(e=>{
const data = e.data;
const txHash = Object.keys(data)[0]; // TODO improve
const reason = data[txHash].reason;
console.log(e); // prints "This is error message"
res.status(404).json({ success: false, error: reason });
});
}
catch(err){
res.status(404).json({success:false,message:err.message});
}
})
app.get('/transactions',async function(req,res){
try{
await contract.methods.getTransactionList().call().then(function(value){
res.status(200).json({transactions:value});
}).catch(e=>{
const data = e.data;
const txHash = Object.keys(data)[0];
const reason = data[txHash].reason;
console.log(e);
res.status(404).json({ success: false, error: reason });
});
}
catch(err){
res.status(404).json({success:false,message:err.message});
}
});
app.get('/',function(req,res){
res.send("Success");
});
//Getting rfid num
/*
app.post('/rfid',function(req,res,next){
var rfidCode=req.body.uid;
console.log(req.body);
console.log("Someone accessed");
console.log("UID"+rfidCode);
if(rfidCode=="23024824484"){
res.status(200).json({success:true,message:"Access granted"});
}
else{
res.status(401).json({success:false,message:"Access denied"});
}
});*/
app.listen(3000,()=>{
console.log('Server is running on port 3000');
});
}
catch(e){
console.log(e);
}