-
Notifications
You must be signed in to change notification settings - Fork 702
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (36 loc) · 1.08 KB
/
index.js
File metadata and controls
44 lines (36 loc) · 1.08 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
const express = require("express");
const app = express();
const cors = require("cors");
const port = 3042;
app.use(cors());
app.use(express.json());
const balances = {
"030a516fbcb67b1fe1cbd06f89bf1972d46efe12583d6bb759d8ac62f017b37b8b": 100,
"034633f85e62385ae67846e10a2f676054e1708b600fc71f7207a38fff22be4fd0": 50,
"02e47ac1ff6eee8067bb631666a796a97a175db1beae3975f8929e5f4405fb5369": 75,
};
app.get("/balance/:address", (req, res) => {
const { address } = req.params;
const balance = balances[address] || 0;
res.send({ balance });
});
app.post("/send", (req, res) => {
const { sender, recipient, amount } = req.body;
setInitialBalance(sender);
setInitialBalance(recipient);
if (balances[sender] < amount) {
res.status(400).send({ message: "Not enough funds!" });
} else {
balances[sender] -= amount;
balances[recipient] += amount;
res.send({ balance: balances[sender] });
}
});
app.listen(port, () => {
console.log(`Listening on port ${port}!`);
});
function setInitialBalance(address) {
if (!balances[address]) {
balances[address] = 0;
}
}