-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
194 lines (150 loc) · 4.58 KB
/
routes.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
const shortid = require('short-id');
const ObjectId = require('mongodb').ObjectID;
function routes(app, db, lms, accounts) {
let dbUser = db.collection('povio-users');
let dbTransaction = db.collection('povio-transaction');
app.post('/register', async (req, res) => {
let email = req.body.email;
let address = req.body.address;
if (!(email && address)) {
res.status(400).json({ status: 'Failed', reason: 'wrong input' });
}
const user = await dbUser.findOne({ email });
if (user) {
res.status(400).json({
status: 'Failed',
reason: 'Already registered',
});
}
const dbRes = await dbUser.insertOne({ email, address });
res.json({ status: 'success', id: dbRes.insertedId });
});
app.post('/login', async (req, res) => {
let email = req.body.email;
if (!email) {
res.status(400).json({ status: 'Failed', reason: 'wrong input' });
}
const user = await dbUser.findOne({ email });
if(!user){
res.status(404).json({
status: 'Failed',
reason: 'Not found!',
});
}
res.json({ status: 'success', user });
});
app.get('/:id/getBalance', async (req, res) => {
let id = req.params.id;
if (!id) {
res.status(404).json({ status: 'Not Found', reason: 'Not Found' });
}
const user = await dbUser.findOne({ _id: ObjectId(id) });
const balanceOfUser = await lms.balanceOf(user.address);
res.json({ status: 'success', balance: balanceOfUser.toString() });
});
app.post('/sendCoinsFromMainAcc', async (req, res) => {
let address = req.body.to;
let amount = req.body.amount || 0;
if (!address) {
res.status(400).json({ status: 'Failed', reason: 'wrong input' });
}
try {
const transfer = await lms.transfer(address, amount, { from: accounts[0]});
if (transfer) {
const date = new Date();
const today = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
const dbRes = await dbTransaction.insertOne({
sender: accounts[0],
receiver: address,
amount,
transcationHash: transfer.receipt.transactionHash,
created_at: today,
});
res.json({ status: 'success', transcationId: dbRes.insertedId });
}
res.status(500).json({
status: 'Failed',
reason: 'Something went wrong!',
});
} catch (error) {
res.status(500).json({
status: 'Failed',
reason: error,
});
}
});
app.get('/getTransactions', async (req, res) => {
return await dbTransaction
.find({})
.toArray(function (err, result) {
if (err) {
res.send(err);
} else {
res.send(result);
}
});
});
app.get('/:id/allowance', async (req, res) => {
let id = req.params.id;
if (!id) {
res.status(404).json({ status: 'Not Found', reason: 'Not Found' });
}
const user = await dbUser.findOne({ _id: ObjectId(id) });
if (!user) {
res.status(404).json({ status: 'Failed', reason: 'Not Found!' });
}
const allowanceCoins = await lms.allowance(accounts[0], user.address, {from: accounts[0]});
return res.send({ allowed: allowanceCoins.toString()});
});
app.post('/approve', async (req, res) => {
let email = req.body.from;
let amount = req.body.amount || 0;
if (!email) {
res.status(404).json({ status: 'Not Found', reason: 'Not Found' });
}
const user = await dbUser.findOne({ email });
if (!user) {
res.status(404).json({ status: 'Failed', reason: 'Not Found!' });
}
const allowanceCoins = await lms.approve(user.address, amount, {from: accounts[0]});
return res.send( allowanceCoins);
});
app.post('/sendCoins', async (req, res) => {
let email = req.body.from;
let address = req.body.to;
let amount = req.body.amount || 0;
if (!(email && address)) {
res.status(400).json({ status: 'Failed', reason: 'wrong input' });
}
const user = await dbUser.findOne({ email });
if (!user) {
res.status(404).json({ status: 'Failed', reason: 'Not Found!' });
}
try {
const transfer = await lms.transferFrom(user.address, address, amount,{from: accounts[0]});
console.log(transfer)
if (transfer) {
const date = new Date();
const today = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
const dbRes = await dbTransaction.insertOne({
sender: user.address,
receiver: address,
amount,
transcationHash: transfer.receipt.transactionHash,
created_at: today,
});
return res.json({ status: 'success', transcationId: dbRes.insertedId });
}
res.status(500).json({
status: 'Failed',
reason: 'Couldn\'t approve transaction',
});
} catch (error) {
res.status(500).json({
status: 'Failed',
reason: error,
});
}
});
}
module.exports = routes;