port to js

This commit is contained in:
Bitcarrot
2021-10-05 23:35:25 -07:00
parent 11c0db4b1c
commit 74d469962a
7 changed files with 46 additions and 14 deletions

2
.gitignore vendored
View File

@@ -107,3 +107,5 @@ credentials.json
.vercel .vercel
env env
hkd_one
hkd_one~

View File

@@ -1,23 +1,60 @@
const axios = require('axios') const axios = require('axios')
const fs = require('fs').promises;
module.exports = { module.exports = {
bfx: async function() { bfx: async function() {
const btcDataURL = "https://api-pub.bitfinex.com/v2/ticker/tBTCUSD" const btcDataURL = "https://api-pub.bitfinex.com/v2/ticker/tBTCUSD"
const response = await axios.get(btcDataURL) const response = await axios.get(btcDataURL)
const data = response.data const data = response.data
console.log(data[6]) //console.log(data[6])
const satDenominator = 100000000 const satDenominator = 100000000
const hkdrate = 0.1287 const hkdrate = 0.1287
btcLastPrice = data[6] btcLastPrice = data[6]
const sathkd = Math.round((1 / btcLastPrice) * satDenominator * hkdrate) const sathkd = Math.round((1 / btcLastPrice) * satDenominator * hkdrate)
console.log("bitfinex last price: ", btcLastPrice, "current satHKD: ", sathkd) //console.log("bitfinex last price: ", btcLastPrice, "current satHKD: ", sathkd)
return data return sathkd
}, },
get10yr: function(lang) { get10yr: async function(lang) {
console.log("get10yr language: ", lang) console.log("get10yr language: ", lang)
return true let data = "testing"
try {
const content = await fs.readFile('./public/static/hkd_historical')
const historical = JSON.parse(content)
//console.log(historical[0])
hist_entries = []
// get all the years you need from 1 - 10
let datelist = []
for (let i = 1; i < 11; i++) {
const y = new Date(new Date().setFullYear(new Date().getFullYear() - i)).toISOString().slice(0, 10)
datelist.push(y)
}
for (let j = 0; j < historical.length; j++) {
const hdate = historical[j]['date']
if (datelist.includes(hdate)) {
//console.log('hit', hdate, historical[j])
hist_entries.push(historical[j])
}
}
// console.log(hist_entries)
let final_list = []
let today_sats = await this.bfx()
// console.log("today sats", today_sats)
for (var v = 0; v < hist_entries.length; v++) {
year = new Date(hist_entries[v]['date']).getFullYear();
rawsat = hist_entries[v]['sathkd_rate']
percentage = (-100 * ((rawsat - today_sats) / rawsat)).toFixed(3)
final_list.push({ "year": year, "sats": rawsat, "percentage": percentage });
}
// console.log(final_list.reverse())
return true
//return final_list.reverse()
} catch (error) {
console.error("Error trying to read file ", error)
}
} }
} }

View File

@@ -16,13 +16,8 @@ const zhcnjson = require('./locales/zh-cn.json');
const zhhkjson = require('./locales/zh-hk.json'); const zhhkjson = require('./locales/zh-hk.json');
const enjson = require('./locales/en.json'); const enjson = require('./locales/en.json');
const data = calculate.get10yr('en').then(value => { console.log(value) })
//calculate.bfx() //console.log(data)
//calculate.get10yr('en')
//const product = require("./api/product");
//app.use("/api/product", product);
const pydata = [{ 'year': '1 year ago', 'sats': '1,199 sats', 'percent': '-79.149%' }, const pydata = [{ 'year': '1 year ago', 'sats': '1,199 sats', 'percent': '-79.149%' },
{ 'year': '2 years ago', 'sats': '1,582 sats', 'percent': '-84.197%' }, { 'year': '2 years ago', 'sats': '1,582 sats', 'percent': '-84.197%' },
@@ -62,8 +57,6 @@ app.use(logger('dev'));
// will look for "./public/js/app.js". // will look for "./public/js/app.js".
app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public')));
//app.use(express.static(path.join(__dirname, 'views')));
//app.use(express.static(path.join(__dirname, 'views/layouts')));
// if you wanted to "prefix" you may use // if you wanted to "prefix" you may use