-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (73 loc) · 2.67 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
let express = require('express');
var cors = require('cors')
let PORT = process.env.PORT || '5000';
let app = express();
app.use(cors())
const ohm = {
price : process.env.OHM_STARTING_DOLLAR,
index : process.env.OHM_STARTING_INDEX,
balance: process.env.OHM_STARTING_TOKEN
}
const time = {
price : process.env.TIME_STARTING_DOLLAR,
index : process.env.TIME_STARTING_INDEX,
balance: process.env.TIME_STARTING_TOKEN
}
const klima = {
price : process.env.KLIMA_STARTING_DOLLAR,
index : process.env.KLIMA_STARTING_INDEX,
balance: process.env.KLIMA_STARTING_TOKEN
}
const { Client } = require('pg');
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
client.connect();
async function getLatestFromDB() {
return client.query('SELECT id, round(ohm_index::numeric,2) as ohm_index, ohm_price, round(time_index::numeric,2) as time_index, time_price, klima_price, round(klima_index::numeric,2) as klima_index, round(ohm_token::numeric,5) as ohm_token, round(time_token::numeric,5) as time_token, round(klima_token::numeric,5) as klima_token, "timestamp", round(ohm_value::numeric,2) as ohm_value, round(time_value::numeric,2) as time_value, round(klima_value::numeric,2) as klima_value, round(ohm_pnl::numeric,2) as ohm_pnl, round(time_pnl::numeric,2) as time_pnl, round(klima_pnl::numeric,2) as klima_pnl FROM public.indexes order by id desc limit 1;')
.then(res => {
return res.rows[0]
})
.catch(e => console.error(e.stack));
}
app.get('/', async (req, res) => {
let dbData = await getLatestFromDB()
const result = {
starting_data : {
investedAmount: 10000,
timestamp : process.env.TIMESTAMP,
ohm,
time,
klima
},
current_data : {
timestamp: dbData.timestamp,
ohm: {
price: dbData.ohm_price,
index: dbData.ohm_index,
balance: dbData.ohm_token,
currentNW: dbData.ohm_value,
pnl: dbData.ohm_pnl
},
time: {
price: dbData.time_price,
index: dbData.time_index,
balance: dbData.time_token,
currentNW: dbData.time_value,
pnl: dbData.time_pnl
},
klima: {
price: dbData.klima_price,
index: dbData.klima_index,
balance: dbData.klima_token,
currentNW: dbData.klima_value,
pnl: dbData.klima_pnl
}
}
}
res.json(result)
})
app.listen(PORT, () => console.log("Server started!"));