var tonicExpress = require("@runkit/runkit/express-endpoint/1.0.0")
var request = require("request")
var https = require('https');
// Just provide the exports object to the tonicExpress helper
var app = tonicExpress(module.exports)
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
function createjsonList(items) {
return [
{
"attachment":{
"type":"template",
"payload":{
"template_type":"list",
"top_element_style":"large",
"elements": items
}
}
}
];
}
function createJsonListInstrumentItem (id, name) {
return {
"title":"Coinbase/" + id + "",
"image_url":"http://cryptoicons.co/128/color/" + id.toLowerCase() + ".png",
"subtitle":"Predict " + name + " price on Coinbase",
"buttons":[
{
"set_attributes": {"current_product": id},
"block_names": ["en_choose_currency"],
"type": "show_block",
"title":"Predict " + id + "!"
}
]
}
}
function createJsonListCurrencyItem (source_id, id, name) {
return {
"title":"Coinbase/" + source_id + "/"+id,
"image_url":"http://cryptoicons.co/128/color/" + id.toLowerCase() + ".png",
"subtitle":"Predict price the to exchange '" + name + "' with '" + source_id +"' on Coinbase",
"buttons":[
{
"set_attributes": {"exchange_currency": id},
"block_names": ["en_predict_price"],
"type": "show_block",
"title":"Predict " + source_id + "/" + id + "!"
}
]
}
}
var collections;
app.get("/ping", (req, res) => res.send("pong!"))
app.get("/instruments", (req, res) => route_with_data(req, res, () => res.json(buildInstruments())))
app.get("/instrument/:id", (req, res) => route_with_data(req, res, () => res.json(buildInstrument(req.params.id))))
app.post("/predict/:to/:from", (req, res) => route_with_data(req, res, () => res.json(predict(req))))
//app.get("/", (req, res) => route_with_data(req, res, () => res.json(buildInstrument('BCH'))))
function predict(req) {
let to = req.params.to;
let from = req.params.from;
let price = req.body.prediction_price;
let days = req.body.prediction_days;
return {
"messages": [
{"text": "Well done!"},
{"text": "You predicted " + to + "/" + from " will raise the price " + price + " in " + days + " days"}
]
}
}
function buildInstrument(id) {
let products = collections['products']
let currencies = collections['currencies']
let ids = []
for (var i in products) {
if (products[i].from === id) {
ids.push (products[i].to)
}
}
let items = []
for (var i of ids) {
var e = createJsonListCurrencyItem (id, i, currencies[i])
items.push (e);
}
return createjsonList(items)
}
function buildInstruments() {
let ids = []
let products = collections['products']
let currencies = collections['currencies']
for (var i in products) {
ids.push (products[i].from)
}
let items = []
for (var i of uniq(ids)) {
var e = createJsonListInstrumentItem (i, currencies[i])
items.push (e);
}
return createjsonList(items)
}
function route_with_data (res, req, cb) {
if (collections) {
return cb()
}
return getFromGDAX (data => {
collections = data
return route_with_data (res, req, cb)
}, e => res.status(500).send('Sorry, cannot get data!'))
}
function getFromGDAX(cb, err) {
getJson (
'https://api-public.sandbox.gdax.com/products',
body => {
let products = extractProducts (body)
getJson (
'https://api-public.sandbox.gdax.com/currencies',
body2 => {
let currencies = extractCurrencies (body2)
cb ({products: products, currencies: currencies})
},
e2 => err(e2)
)
},
e => err(e)
)
}
function getJson(url, cb, err) {
request(url, { json: true, headers: { 'User-Agent': 'bullower'} }, (e, res, body) => {
if (e) { return cb(e) }
cb(body)
});
}
function extractCurrencies(data) {
xs = {}
for (var i in data) {
xs[data[i].id] = data[i].name
}
return xs
}
function extractProducts(data) {
xs = {}
for (var i in data) {
xs[data[i].display_name] = {
from: data[i].base_currency,
to: data[i].quote_currency,
}
}
return xs
}
function uniq(a) {
var seen = {};
return a.filter(function(item) {
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
}