-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (99 loc) · 3.41 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
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
const express = require('express');
const cors = require('cors');
const etherscan = require('./src/etherscan.js');
const config = require('./common/config.json');
const Kyber = require('./src/kyber.js');
const Utils = require('./src/utils.js');
const DB = require('./src/db.js');
const db = new DB();
const app = express();
const port = process.env.PORT || 80;
app.use(cors());
app.use(express.json());
app.options('*', cors());
app.use(express.static(__dirname + '/public'));
// Return a list of currencies supported by Kyber.
app.get(config.app.path + '/currencies', async (req, res) => {
let currencies = [];
try {
currencies = await Kyber.getSupportedCurrencies();
} catch (err) {
}
res.status(200).send({status: "ok", results: currencies});
});
// Returns a lost of trades in the given token
app.get(config.app.path + '/currencies/:token/trades', async (req, res) => {
// Check if token is supported.
try {
Kyber.checkForTokenSupport(req.params.token);
} catch (err) {
res.status(400).send({status: "error", error: err.message});
return;
}
// Check for missing parameters.
if (typeof req.query.start === 'undefined') {
res.status(400).send({status: "error", error: "missing start timestamp"});
return;
}
if (typeof req.query.stop === 'undefined') {
res.status(400).send({status: "error", error: "missing stop timestamp"});
return;
}
// Convert the timestamp to block numbers.
try {
const ret = await db.getTokenTradeData(req.params.token, parseInt(req.query.start), parseInt(req.query.stop));
res.status(200).send({status: "ok", results: ret});
} catch (err) {
res.status(500).send({status: "error", error: err.message});
}
});
// Returns the most recent market stats of the token
app.get(config.app.path + '/currencies/:token/stats', async (req, res) => {
// Check if token is supported.
try {
Kyber.checkForTokenSupport(req.params.token);
} catch (err) {
res.status(400).send({status: "error", error: err.message});
return;
}
try {
const stats = await Kyber.getTokenStats(req.params.token);
res.status(200).send({status: "ok", results: stats});
} catch (err) {
res.status(500).send({status: "error", error: err.message});
}
});
app.get(config.app.path + '/test/:token', async (req, res) => {
// Check if token is supported.
try {
Kyber.checkForTokenSupport(req.params.token);
} catch (err) {
res.status(400).send({status: "error", error: err.message});
return;
}
// Check for missing parameters.
if (typeof req.query.startBlock === 'undefined') {
res.status(400).send({status: "error", error: "missing start block"});
return;
}
if (typeof req.query.stopBlock === 'undefined') {
res.status(400).send({status: "error", error: "missing stop block"});
return;
}
const stats = await etherscan.getTokenTxnsByAddressAndToken({
token: req.params.token,
startBlock: req.query.startBlock,
stopBlock: req.query.stopBlock
});
res.status(200).send({status: "ok", results: stats});
});
// Returns a list of the most recent orders in Kyber
app.get(config.app.path + '/currencies/orders', async (req, res) => {
try {
const orders = await Kyber.getLastOrders(parseInt(req.query.count));
res.status(200).send({status: "ok", results: orders});
} catch (err) {
res.status(500).send({status: "error", error: err.message});
}
});
app.listen(port, () => console.log(`app listening on ${port}`));