-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (81 loc) · 2.42 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
'use strict';
require('console-stamp')(console, {
format: ':date(yyyy/mm/dd HH:MM:ss.l)'
} );
console.log("-- Starting server...");
const Order = require('./class_order.js')
const Config = require('./class_config.js')
const json = require('./config.json');
global.config = new Config(json)
const ccxt = require ('ccxt');
const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000
app.use(express.json())
/**************************************************
*
* Exchanges instances
*
***************************************************/
console.log("-- Initializing exchanges:")
config.exchanges.forEach(function(e){
console.log(" -", e.exchange, "("+e.account+")")
global[e.account] = new ccxt[e.exchange] ({
apiKey: e.key,
secret: e.secret
})
});
/**************************************************
*
* Routes
*
***************************************************/
app.get('/', function (req, res) {
console.debug("-> GET /")
res.send("Nothing to see here...")
})
app.get('/exchanges', function (req, res) {
(async function () {
console.debug("-> GET /exchanges")
res.send(ccxt.exchanges)
}) ();
})
app.get('/exchanges/:exchange?', function (req, res) {
let exchange = req.params.exchange
if (!exchange) {
res.send("Please provide an 'exchange' value.")
} else {
(async function () {
console.debug("-> GET /exchanges/"+exchange)
let name = config.getAccountByExchange(exchange)
res.send(await eval(name).loadMarkets())
}) ();
}
})
app.get('/exchanges/:exchange?/:currency?', function (req, res) {
let exchange = req.params.exchange
let currency = req.params.currency
if (!exchange || !currency) {
res.send("Please provide an 'exchange' and 'currency' value.")
} else {
(async function () {
console.debug("-> GET /exchanges/"+exchange+"/"+currency)
let name = config.getAccountByExchange(exchange)
try {
res.send(await eval(name).publicGetGetInstruments({currency: currency}))
} catch (e) {
res.send(e)
console.error(e)
}
}) ();
}
})
app.post('/trade', function (req, res) {
console.log("<- Received", req.body.orders.length, "order(s) for", req.body.account, ":", req.body.instrument)
req.body.orders.forEach(order => new Order(req.body.account, req.body.instrument, order).process());
res.end();
})
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("** Server listening on port", PORT);
});