mirror of
https://github.com/aljazceru/satshkd-vercel.git
synced 2025-12-17 21:24:23 +01:00
58 lines
2.3 KiB
JavaScript
58 lines
2.3 KiB
JavaScript
const axios = require('axios')
|
|
const fs = require('fs');
|
|
|
|
module.exports = {
|
|
bfx: async function() {
|
|
const hkdrate = 0.1287 // approximate rate
|
|
const btcDataURL = "https://api-pub.bitfinex.com/v2/ticker/tBTCUSD"
|
|
const response = await axios.get(btcDataURL)
|
|
const data = response.data
|
|
//console.log(data[6])
|
|
|
|
const satDenominator = 100000000
|
|
// see docs : https://docs.bitfinex.com/reference#rest-public-ticker
|
|
btcLastPrice = data[6]
|
|
|
|
const sathkd = Math.round((1 / btcLastPrice) * satDenominator * hkdrate)
|
|
//console.log("bitfinex last price: ", btcLastPrice, "current satHKD: ", sathkd)
|
|
return sathkd
|
|
},
|
|
|
|
get10yr: async function() {
|
|
// console.log("get10yr")
|
|
try {
|
|
// const content = await fs.readFile('./public/hkd_historical')
|
|
const content = fs.readFileSync('./public/hkd_historical', { encoding: 'utf8' })
|
|
|
|
const historical = JSON.parse(content)
|
|
hist_entries = []
|
|
let datelist = []
|
|
|
|
// get all the years you need from 1 - 10
|
|
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)) {
|
|
hist_entries.push(historical[j])
|
|
}
|
|
}
|
|
// console.log(hist_entries)
|
|
let final_list = []
|
|
let today_sats = await this.bfx()
|
|
for (var v = 0; v < hist_entries.length; v++) {
|
|
const date = new Date(hist_entries[v]['date'])
|
|
year = date.getFullYear();
|
|
rawsat = hist_entries[v]['sathkd_rate']
|
|
percentage = (-100 * ((rawsat - today_sats) / rawsat)).toFixed(3)
|
|
final_list.push({ "year": date.toLocaleDateString(), "sats": rawsat.toLocaleString("en-US"), "percent": percentage });
|
|
}
|
|
return final_list.reverse()
|
|
} catch (error) {
|
|
console.error("Error trying to read file ", error)
|
|
}
|
|
}
|
|
|
|
} |