-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.js
396 lines (375 loc) · 13.4 KB
/
api.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
let config = require('./config');
const fetch = require('node-fetch');
var RAM = {
lastUpdate: 0,
lastQuote: 0,
lastUpdateMarket: 0,
lastDayBucket: 0,
lastBook: 0,
Book: '',
Hive: '',
Ticker: '',
Market: '',
DayBucket: ''
}
exports.pairs = (req, res, next) => {
res.setHeader('Content-Type', 'application/json');
const pairs = [
{
ticker_id: `HIVE_HBD`,
base: "HIVE",
target: "HBD",
}
]
res.send(JSON.stringify(pairs, null, 3))
}
exports.tickers = (req, res, next) => {
var dex = fetchHiveDexCurrentPrice()
res.setHeader('Content-Type', 'application/json');
Promise.all([dex])
.then(function(v) {
var HIVE_HBD = {
ticker_id: `HIVE_HBD`,
base_currency: "HIVE",
target_currency: "HBD",
last_price: RAM.Ticker.latest,
base_volume: parseFloat(parseInt(RAM.Ticker.hive_volume.amount)/1000).toFixed(3),
target_volume: parseFloat(parseInt(RAM.Ticker.hbd_volume.amount)/1000).toFixed(3),
bid: RAM.Ticker.highest_bid,
ask: RAM.Ticker.lowest_ask,
high: RAM.High,
low: RAM.Low
}
res.send(JSON.stringify(
[HIVE_HBD], null, 3))
})
.catch(function(err) {
console.log(err)
})
}
exports.orderbook = (req, res, next) => {
var depth = parseInt(req.query.depth) || 50
var dex = fetchHiveOrderBook(depth)
var orderbook = {
timestamp: Date.now(),
bids: [],
asks: []
}
var pair = req.params.ticker_id || req.query.ticker_id
res.setHeader('Content-Type', 'application/json');
switch (pair) {
case `HIVE_HBD`:
orderbook.ticker_id = `HIVE_HBD`
makeBook(orderbook, depth, [dex])
.then(r=>res.send(r))
.catch(e=>console.log(e))
break;
default:
res.send(JSON.stringify({
ERROR: `ticker_id must be HIVE_HBD`
}, null, 3))
break;
}
}
exports.historical_trades = (req, res, next) => {
var limit = parseInt(req.query.limit) || 500
if(limit>999)limit = 1000
var dex = fetchHiveTradeHistory(limit)
var orderbook = {
timestamp: Date.now(),
buys: [],
sells: []
}
var pair = req.params.ticker_id || req.query.ticker_id
var type = req.query.type
switch (type) {
case 'buy':
type = ['buy']
break;
case 'ask':
type = ['sell']
break;
default:
type = ['buy','sell']
break;
}
res.setHeader('Content-Type', 'application/json');
switch (pair) {
case `HIVE_HBD`:
getHistory([dex], 'HIVE_HBD', type, limit)
.then(r=>res.send(r))
.catch(e=>console.log(e))
break;
default:
res.send(JSON.stringify({
error: 'Ticker_ID is not supported'
}, null, 3))
break;
}
}
// fetch hive details
exports.detail = (req, res, next) => {
var hiveStats = fetchHiveDynamicProperties()
res.setHeader('Content-Type', 'application/json');
Promise.all([hiveStats])
.then(function(v) {
const HIVE ={
name: 'HIVE',
symbol: 'HIVE',
icon: 'https://www.dlux.io/img/hextacular.svg',
supply: RAM.hiveDyn.virtual_supply,
incirc: RAM.hiveDyn.current_supply,
wp:`https://hive.io/whitepaper.pdf`,
ws:`https://hive.io`,
be:`https://hiveblockexplorer.com/`,
text: `HIVE is a DPoS blockchain with free transactions and a method to post and rate content.`
},
HBD = {
name: 'Hive Backed Dollars',
symbol: 'HBD',
icon: 'https://www.dlux.io/img/hbd_green.svg',
supply: 'Dynamic, up to 10% of HIVE Cap',
incirc: RAM.hiveDyn.current_hbd_supply,
wp:`https://hive.io/whitepaper.pdf`,
ws:`https://hive.io`,
be:`https://hiveblockexplorer.com/`,
text: `Hive-backed dollars (HBD) are a unique type of trustless stablecoin that is backed by the underlying value of the Hive blockchain itself instead of external collateral or a centralized entity. HBD are pegged to value of USD. Staking HBD pays a variable APR, currently ${parseFloat(RAM.hiveDyn.hbd_interest_rate / 100).toFixed(2)}%.`
}
res.send(JSON.stringify({
coins: [HIVE,HBD]
}, null, 3))
})
.catch(function(err) {
console.log(err)
})
}
//heroku force https
exports.https_redirect = (req, res, next) => {
if (process.env.NODE_ENV === 'production') {
if (req.headers['x-forwarded-proto'] != 'https') {
return res.redirect('https://' + req.headers.host + req.url);
} else {
return next();
}
} else {
return next();
}
};
function fetchHiveDynamicProperties(){
return new Promise((resolve, reject)=>{
if (RAM.lastUpdate < Date.now() - config.CACHEMILSEC){
fetch(config.clientURL, {
body: `{"jsonrpc":"2.0", "method":"database_api.get_dynamic_global_properties", "id":1}`,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
})
.then(r => r.json())
.then(res => {
RAM.lastUpdate = Date.now()
RAM.hiveDyn = res.result
resolve('OK')
})
.catch(e=>reject(e))
} else {
resolve('OK')
}
})
}
function fetchHiveTradeHistory(lim){
return new Promise((resolve, reject)=>{
if (RAM.lastUpdate < Date.now() - config.CACHEMILSEC){
fetch(config.clientURL, {
body: `{"jsonrpc":"2.0", "method":"market_history_api.get_recent_trades", "params":{"limit":${parseInt(lim)}}, "id":1}`,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
})
.then(r => r.json())
.then(res => {
//add trades to RAM.History based on timestamps/amounts to minimize requests
RAM.lastHist = Date.now()
RAM.limHist = parseInt(lim)
RAM.History = res.result.trades
resolve('OK')
})
.catch(e=>reject(e))
} else {
resolve('OK')
}
})
}
function fetchHiveDexDayBucket(){
return new Promise((resolve, reject)=>{
const gun = Date.now()
const now = new Date(gun)
const then = new Date(now - 86400000)
if (RAM.lastUpdateMarket < now - config.CACHEMILSEC){
fetch(config.clientURL, {
body: `{"jsonrpc":"2.0", "method":"market_history_api.get_market_history", "params":{"bucket_seconds":3600,"start":"${then.toISOString().substr(0,19)}","end":"${now.toISOString().substr(0,19)}"}, "id":1}`,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
})
.then(r => r.json())
.then(res => {
var highest = 0,
lowest = 999999999999999
for(i=0; i < res.result.buckets.length; i++){
high = parseFloat(res.result.buckets[i].non_hive.high / res.result.buckets[i].hive.high)
low = parseFloat(res.result.buckets[i].non_hive.high / res.result.buckets[i].hive.high)
if(high > highest)highest = high
if(low < lowest)lowest = low
}
RAM.lastDayBucket = Date.now()
RAM.DayBucket = res.result.buckets
RAM.High = highest
RAM.Low = lowest
resolve('OK')
})
.catch(e=>reject(e))
} else {
resolve('OK')
}
})
}
function fetchHiveDexCurrentPrice(){
return new Promise((resolve, reject)=>{
if (RAM.lastUpdateMarket < Date.now() - config.CACHEMILSEC){
fetch(config.clientURL, {
body: `{"jsonrpc":"2.0", "method":"market_history_api.get_ticker", "id":1}`,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
})
.then(r => r.json())
.then(res => {
RAM.lastQuote = Date.now()
RAM.Ticker = res.result
resolve('OK')
})
.catch(e=>reject(e))
} else {
resolve('OK')
}
})
}
function fetchHiveOrderBook(lim){
var limit = parseInt(lim) || 100
return new Promise((resolve, reject)=>{
if (RAM.lastBook < Date.now() - config.CACHEMILSEC){
fetch(config.clientURL, {
body: `{"jsonrpc":"2.0", "method":"market_history_api.get_order_book", "params":{"limit":${limit}}, "id":1}`,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
})
.then(r => r.json())
.then(res => {
RAM.lastBook = Date.now()
RAM.Book = res.result
resolve('OK')
})
.catch(e=>reject(e))
} else {
resolve('OK')
}
})
}
function getHistory(promises, pair, typ, lim){
return new Promise((resolve, reject)=>{
Promise.all(promises)
.then(function(v) {
var buy = [],
sell = []
for(var i = 0; i < lim; i++){
var type = 'sell',
price = 0,
base_volume,
target_volume
if(RAM.History[i].current_pays.nai == '@@000000013'){type='buy'}
if(type == 'buy'){
price = parseFloat(RAM.History[i].current_pays.amount / RAM.History[i].open_pays.amount)
base_volume = parseFloat(RAM.History[i].open_pays.amount / 1000).toFixed(3)
target_volume = parseFloat(RAM.History[i].current_pays.amount / 1000).toFixed(3)
} else {
price = parseFloat(RAM.History[i].open_pays.amount / RAM.History[i].current_pays.amount)
base_volume = parseFloat(RAM.History[i].current_pays.amount / 1000).toFixed(3)
target_volume = parseFloat(RAM.History[i].open_pays.amount / 1000).toFixed(3)
}
const record = {
"trade_id":`${type}:${price}:${RAM.History[i].date}`,
price,
base_volume,
target_volume,
"trade_timestamp": RAM.History[i].date,
type
}
if(record.type == 'buy'){
buy.push(record)
} else {
sell.push(record)
}
}
if (typ.indexOf('buy') < 0){
buy = []
}
if (typ.indexOf('sell') < 0){
sell = []
}
resolve(JSON.stringify({
sell,
buy
}, null, 3))
})
.catch(function(err) {
reject(err)
})
})
}
function makeBook(orderbook, dep, promises){
return new Promise ((resolve, reject)=>{
var get = dep
if(!get)get = 50
Promise.all(promises)
.then(function(v) {
var count1 = 0, count2 = 0
console.log(RAM.Book)
for (i = 0; i < RAM.Book.asks.length; i++ ){
orderbook.asks.push([RAM.Book.asks[i].real_price,parseFloat(RAM.Book.asks[i].hbd / 1000).toFixed(3)])
count1++
if(count1 == get)break;
}
for (i = 0; i < RAM.Book.bids.length; i++ ){
orderbook.bids.push([RAM.Book.bids[i].real_price,parseFloat(RAM.Book.bids[i].hbd / 1000).toFixed(3)])
count2++
if(count2 == get)break;
}
resolve(JSON.stringify({
asks:orderbook.asks,
bids:orderbook.bids,
timestamp: orderbook.timestamp,
ticker_id: orderbook.ticker_id
}, null, 3))
})
.catch(function(err) {
reject(err)
})
})
}
function init(){
return new Promise((resolve, reject)=>{
var a = fetchHiveDynamicProperties(),
b = fetchHiveDexCurrentPrice(),
c = fetchHiveDexDayBucket(),
d = fetchHiveTradeHistory(1000),
e = fetchHiveOrderBook(200)
Promise.all([a, b, c, d, e]).then(r=>resolve('Ready')).catch(e=>console.log(e))
})
}
exports.init = init