aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/account.js2
-rw-r--r--lib/binance.js17
-rw-r--r--lib/mail.js2
-rw-r--r--lib/passport.js2
-rw-r--r--lib/rates.js5
-rw-r--r--lib/send.js6
-rw-r--r--lib/sockets.js47
-rw-r--r--lib/store.js34
-rw-r--r--lib/sync.js15
-rw-r--r--lib/wretch.js3
10 files changed, 67 insertions, 66 deletions
diff --git a/lib/account.js b/lib/account.js
index c4cda1a4..a1f20dc7 100644
--- a/lib/account.js
+++ b/lib/account.js
@@ -22,7 +22,7 @@ export default async (asset, user, transaction) => {
let ticker = asset.substr(0, 3).toUpperCase();
let precision = 8;
- const assets = app.get("assets");
+ const assets = assets;
if (assets[asset]) {
({ domain, ticker, precision, name } = assets[asset]);
diff --git a/lib/binance.js b/lib/binance.js
index 3e25e277..1dc1facd 100644
--- a/lib/binance.js
+++ b/lib/binance.js
@@ -1,7 +1,7 @@
import app from "../app.js";
import WebSocket from "ws";
import { err } from "./logging.js";
-import { rates } from "./store.js";
+import store from "./store.js";
export default () => {
const binance = new WebSocket(
@@ -12,18 +12,15 @@ export default () => {
try {
let msg = JSON.parse(event.data);
- let fx = app.get("fx");
- if (!fx) return;
- let rates = {};
+ if (!store.fx) return;
- Object.keys(fx).map(symbol => {
- rates[symbol] = msg.c * fx[symbol];
+ Object.keys(store.fx).map(symbol => {
+ store.rates[symbol] = msg.c * store.fx[symbol];
});
- app.set("ask", msg.a);
- app.set("bid", msg.b);
- app.set("last", msg.c);
- app.set("rates", rates);
+ // app.set("ask", msg.a);
+ // app.set("bid", msg.b);
+ last = msg.c;
} catch (e) {
err("binance message error", e.message);
}
diff --git a/lib/mail.js b/lib/mail.js
index d1b67c37..d826b845 100644
--- a/lib/mail.js
+++ b/lib/mail.js
@@ -86,7 +86,7 @@ imap.once("ready", function() {
let { user } = account;
- let rate = rates["CAD"] * 1.02;
+ let rate = store.rates["CAD"] * 1.02;
let amount = Math.round((cad / rate) * 100000000);
await account.increment({ balance: amount }, { transaction });
diff --git a/lib/passport.js b/lib/passport.js
index 5f987014..36057ade 100644
--- a/lib/passport.js
+++ b/lib/passport.js
@@ -9,7 +9,7 @@ import fs from "fs";
import path from "path";
app.register(fastifySecureSession, {
- key: fs.readFileSync(config.jwt)
+ key: config.jwt
});
app.register(fastifyPassport.initialize());
diff --git a/lib/rates.js b/lib/rates.js
index 5f0c3c42..5e24cff0 100644
--- a/lib/rates.js
+++ b/lib/rates.js
@@ -3,6 +3,7 @@ import axios from "axios";
import fs from "fs";
import binanceFactory from "./binance.js";
import { broadcast } from "./sockets.js";
+import store from "./store.js";
const binance = binanceFactory();
@@ -45,7 +46,7 @@ let fetchRates;
}
}
- app.set("fx", fx);
+ store.fx = fx;
setTimeout(fetchRates, 7200000);
})();
@@ -55,7 +56,7 @@ app.get("/rates", async (req, res, next) => {
});
setInterval(() => {
- broadcast("rate", app.get("last"));
+ broadcast("rate", store.last);
}, 1000);
setInterval(() => {
diff --git a/lib/send.js b/lib/send.js
index 15855d55..dbc39325 100644
--- a/lib/send.js
+++ b/lib/send.js
@@ -1,4 +1,4 @@
-import { rates } from "./store.js";
+import store from "./store.js";
import bolt11 from 'bolt11';
import { Op } from '@sequelize/core';
import { computeConversionFee, conversionFeeReceiver } from '../routes/lightning/conversionFee.js';
@@ -175,7 +175,7 @@ export const send = async (amount, memo, hash, user) => {
memo: "Lightning conversion fee",
account_id: receiverAccount.id,
user_id: receiverAccount.user_id,
- rate: rates[receiverAccount.user.currency],
+ rate: store.rates[receiverAccount.user.currency],
currency: receiverAccount.user.currency,
confirmed: true,
received: true,
@@ -194,7 +194,7 @@ export const send = async (amount, memo, hash, user) => {
hash,
memo,
preimage,
- rate: rates[user.currency],
+ rate: store.rates[user.currency],
currency: user.currency,
confirmed: true,
network: "lightning",
diff --git a/lib/sockets.js b/lib/sockets.js
index 6ea369c0..de6986b1 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -1,7 +1,7 @@
-import { sockets } from "./store.js";
+import store from "./store.js";
import app from "../app.js";
import jwt from "jsonwebtoken";
-import { v4 as uuidv4 } from "uuid";
+import { v4 } from "uuid";
import WebSocket from "ws";
import { differenceInSeconds, subSeconds } from "date-fns";
import fastifyWs from "@fastify/websocket";
@@ -35,18 +35,18 @@ export const emit = (username, type, data) => {
}
}
- if (!sockets[username]) return;
- for (id in sockets[username]) {
- const ws = sockets[username][id];
+ if (!store.sockets[username]) return;
+ for (id in store.sockets[username]) {
+ const ws = store.sockets[username][id];
ws.send(JSON.stringify({ type, data }));
}
};
export const broadcast = (type, data) => {
const sent = [];
- for (const username in sockets) {
- for (const id in sockets[username]) {
- const ws = sockets[username][id];
+ for (const username in store.sockets) {
+ for (const id in store.sockets[username]) {
+ const ws = store.sockets[username][id];
ws.send(JSON.stringify({ type, data }));
sent.push(ws.id);
}
@@ -60,7 +60,7 @@ export const broadcast = (type, data) => {
app.register(fastifyWs);
app.register(async function(app) {
app.get("/ws", { websocket: true }, ({ socket: ws }, req) => {
- const id = uuidv4();
+ const id = v4();
ws.id = id;
ws.beats = 0;
ws.send(JSON.stringify({ type: "connected", data: id }));
@@ -92,17 +92,17 @@ app.register(async function(app) {
const username = jwt.decode(token).username;
if (!username) throw new Error("Invalid JWT token");
- if (!sockets[username]) sockets[username] = {};
- const existing = Object.keys(sockets[username]);
+ if (!store.sockets[username]) store.sockets[username] = {};
+ const existing = Object.keys(store.sockets[username]);
if (existing.length > 4) {
const p = existing.find(sid => sid !== id);
- sockets[username][p].send(JSON.stringify({ type: "logout" }));
+ store.sockets[username][p].send(JSON.stringify({ type: "logout" }));
setTimeout(() => {
- if (sockets[username][p])
- sockets[username][p].close(code, "too many sockets");
+ if (store.sockets[username][p])
+ store.sockets[username][p].close(code, "too many sockets");
}, 5000);
}
- sockets[username][id] = ws;
+ store.sockets[username][id] = ws;
names[id] = username;
let user = await getUser(username);
@@ -187,8 +187,8 @@ app.register(async function(app) {
// if (reason) console.log("closing socket", reason);
try {
const username = names[id];
- if (sockets[username] && sockets[username][id]) {
- delete sockets[username][id];
+ if (store.sockets[username] && store.sockets[username][id]) {
+ delete store.sockets[username][id];
}
if (all[id]) delete all[id];
@@ -199,19 +199,12 @@ app.register(async function(app) {
});
});
-app.listen({ port: 3000 }, err => {
- if (err) {
- fastify.log.error(err);
- process.exit(1);
- }
-});
-
const names = {};
setInterval(() => {
- for (const username in sockets) {
- for (const id in sockets[username]) {
- let ws = sockets[username][id];
+ for (const username in store.sockets) {
+ for (const id in store.sockets[username]) {
+ let ws = store.sockets[username][id];
ws.beats++;
if (ws.beats > 10) ws.close(code, `${username} ${id} lost heartbeat`);
}
diff --git a/lib/store.js b/lib/store.js
index f32ad101..5667474a 100644
--- a/lib/store.js
+++ b/lib/store.js
@@ -1,15 +1,23 @@
import persist from "./persist.js";
-export const addresses = {};
-export const challenge = {};
-export const change = [];
-export const exceptions = [];
-export const issuances = {};
-export const logins = {};
-export const seen = [];
-export const sessions = {};
-export const sockets = {};
-export const unaccounted = [];
-export const networks = [];
-export const convert = persist("data/conversions.json");
-export const fx;
+export default {
+ addresses: {},
+ challenge: {},
+ change: [],
+ exceptions: [],
+ issuances: {},
+ logins: {},
+ seen: [],
+ sessions: {},
+ sockets: {},
+ unaccounted: [],
+ networks: [],
+ convert: persist("data/conversions.json"),
+ rates: {},
+ fx: {},
+ assets: {},
+ bcAddressIndex: 0,
+ lqAddressIndex: 0,
+ last: {},
+ payments: []
+};
diff --git a/lib/sync.js b/lib/sync.js
index 6fcf8a10..f1d28421 100644
--- a/lib/sync.js
+++ b/lib/sync.js
@@ -4,11 +4,12 @@ import { differenceInDays } from "date-fns";
import fs from "fs";
import read from "../lib/read.js";
import { Op } from "@sequelize/core";
-import { addresses, exceptions } from "./store.js";
+import store from "./store.js";
+import { err } from "./logging.js";
const init = async () => {
try {
- read(fs.createReadStream("exceptions"), data => exceptions.push(data));
+ read(fs.createReadStream("exceptions"), data => store.exceptions.push(data));
} catch (e) {
warn("couldn't read exceptions file", e.message);
}
@@ -29,14 +30,14 @@ const init = async () => {
}
})
).map(({ address, user, unconfidential }) => {
- if (address && user) addresses[address] = user.username;
- if (unconfidential && user) addresses[unconfidential] = user.username;
+ if (address && user) store.addresses[address] = user.username;
+ if (unconfidential && user) store.addresses[unconfidential] = user.username;
});
} catch (e) {
console.log(e);
}
- payments = (
+ store.payments = (
await db.Payment.findAll({
attributes: ["hash"]
})
@@ -206,7 +207,7 @@ const sync = async () => {
transactions.map(tx => {
if (
!payments.find(p => p.hash === tx.txid) &&
- !exceptions.includes(tx.txid) &&
+ !store.exceptions.includes(tx.txid) &&
!unaccounted.find(a => a.txid === tx.txid) &&
new Date(tx.blocktime * 1000) >= twoWeeksAgo
) {
@@ -276,7 +277,7 @@ const sync = async () => {
try {
await db.transaction(async transaction => {
l("found pending transaction", user.username, amount, hash);
- payments.push(hash);
+ store.payments.push(hash);
let p = await db.Payment.create(
{
account_id,
diff --git a/lib/wretch.js b/lib/wretch.js
index e4427760..67e28b88 100644
--- a/lib/wretch.js
+++ b/lib/wretch.js
@@ -1,3 +1,4 @@
import wretch from "wretch";
import fetch from 'node-fetch';
-export default wretch().polyfills({ fetch });
+wretch().polyfills({ fetch });
+export default wretch;