aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-10-02 00:50:07 -0400
committerAdam Soltys <[email protected]>2020-10-02 00:50:07 -0400
commite53fea3b779f2ea75ccf0470aad2c5d055146b12 (patch)
treefabdfe90068b6cbb6120752ace6aaf506a561b47 /lib
parent26b84321aab848733f70a51409495b3cdb3fccde (diff)
downloadcoinos-server-e53fea3b779f2ea75ccf0470aad2c5d055146b12.tar.gz
coinos-server-e53fea3b779f2ea75ccf0470aad2c5d055146b12.zip
fixer fallback
Diffstat (limited to 'lib')
-rw-r--r--lib/rates.js21
1 files changed, 18 insertions, 3 deletions
diff --git a/lib/rates.js b/lib/rates.js
index 952be031..fdb7f5ac 100644
--- a/lib/rates.js
+++ b/lib/rates.js
@@ -13,14 +13,29 @@ let fetchRates;
l.warn("Failed to read fx file");
}
- let today = JSON.stringify(new Date()).replace(/T.*/, "").replace(/"/, "");
+ let today = JSON.stringify(new Date())
+ .replace(/T.*/, "")
+ .replace(/"/, "");
if (date !== today) {
try {
- let { data: { rates: fx }} = await axios.get('https://api.exchangerate.host/latest?base=USD');
+ let {
+ data: { rates: fx }
+ } = await axios.get("https://api.exchangerate.host/latest?base=USD");
fs.writeFileSync("fx", JSON.stringify({ date, fx }));
} catch (e) {
- l.error("error fetching rates", e.message);
+ try {
+ let {
+ data: { rates: fx }
+ } = await axios.get(`http://data.fixer.io/api/latest?access_key=${config.fixer}`);
+ 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 }));
+ } catch (e) {
+ l.error("error fetching rates", e.message);
+ }
}
}