aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rates.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-11 22:09:48 -0800
committerAdam Soltys <[email protected]>2023-01-12 15:44:13 -0800
commitc3565838644123e77881ff4f8536792f074047b9 (patch)
tree96da43fd19940d2f0836e43a511f0a1fb365044d /lib/rates.js
parentded2c69ce89438326523a512b570be6eee4b23f0 (diff)
downloadcoinos-server-c3565838644123e77881ff4f8536792f074047b9.tar.gz
coinos-server-c3565838644123e77881ff4f8536792f074047b9.zip
migrating to redis
Diffstat (limited to 'lib/rates.js')
-rw-r--r--lib/rates.js34
1 files changed, 15 insertions, 19 deletions
diff --git a/lib/rates.js b/lib/rates.js
index 8970ca64..8064a117 100644
--- a/lib/rates.js
+++ b/lib/rates.js
@@ -1,23 +1,23 @@
import app from "$app";
-import axios from "axios";
+import got from "got";
import config from "$config";
import fs from "fs";
-// import binance from "./binance";
+import binance from "./binance";
import { broadcast } from "./sockets";
import store from "./store";
import { err, warn } from "$lib/logging";
-// let b = binance();
-// setInterval(() => {
-// b.terminate();
-// b = binance();
-// }, 360000);
+let b = binance();
+setInterval(() => {
+ b.terminate();
+ b = binance();
+}, 360000);
let fetchRates = async () => {
let date, fx;
try {
- ({ date, fx } = JSON.parse(fs.readFileSync("fx")));
+ ({ date, fx } = JSON.parse(fs.readFileSync("data/fx")));
} catch (e) {
warn("Failed to read fx file");
}
@@ -28,26 +28,22 @@ let fetchRates = async () => {
if (date !== today) {
try {
- let {
- data: { rates: fx }
- } = await axios.get(
+ let { rates: fx } = await got(
`https://api.exchangerate.host/latest?base=USD&v={(new Date()).toISOString().split('T')[0]}`
- );
- fs.writeFileSync("fx", JSON.stringify({ date, fx }));
+ ).json();
+ fs.writeFileSync("data/fx", JSON.stringify({ date, fx }));
} catch (e) {
try {
- let {
- data: { rates: fx }
- } = await axios.get(
+ let { rates: fx } = await got(
`http://data.fixer.io/api/latest?access_key=${config.fixer}`
- );
+ ).json();
let USD = fx["USD"];
Object.keys(fx).map(k => {
fx[k] = parseFloat((fx[k] / fx["USD"]).toFixed(6));
});
- fs.writeFileSync("fx", JSON.stringify({ date, fx }));
+ fs.writeFileSync("data/fx", JSON.stringify({ date, fx }));
} catch (e) {
err("error fetching rates", e.message);
}
@@ -58,7 +54,7 @@ let fetchRates = async () => {
setTimeout(fetchRates, 7200000);
};
-// fetchRates();
+fetchRates();
app.get("/rate", async (req, res, next) => {
res.send(store.last);