diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/binance.js | 21 | ||||
| -rw-r--r-- | lib/buildliquidtx.js | 4 | ||||
| -rw-r--r-- | lib/buildtx.js | 4 | ||||
| -rw-r--r-- | lib/logging.js | 6 | ||||
| -rw-r--r-- | lib/mail.js | 5 | ||||
| -rw-r--r-- | lib/notifications.js | 2 | ||||
| -rw-r--r-- | lib/passport.js | 2 | ||||
| -rw-r--r-- | lib/rates.js | 8 | ||||
| -rw-r--r-- | lib/register.js | 2 | ||||
| -rw-r--r-- | lib/rpc.js | 2 | ||||
| -rw-r--r-- | lib/send.js | 25 | ||||
| -rw-r--r-- | lib/sockets.js | 9 | ||||
| -rw-r--r-- | lib/store.js | 1 | ||||
| -rw-r--r-- | lib/sync.js | 21 | ||||
| -rw-r--r-- | lib/webhooks.js | 6 | ||||
| -rw-r--r-- | lib/wretch.js | 3 |
16 files changed, 68 insertions, 53 deletions
diff --git a/lib/binance.js b/lib/binance.js index 0f91ce8c..3e25e277 100644 --- a/lib/binance.js +++ b/lib/binance.js @@ -1,9 +1,14 @@ -import WebSocket from 'ws'; +import app from "../app.js"; +import WebSocket from "ws"; +import { err } from "./logging.js"; +import { rates } from "./store.js"; export default () => { - const binance = new WebSocket("wss://stream.binance.com:9443/ws/btcusdt@ticker"); + const binance = new WebSocket( + "wss://stream.binance.com:9443/ws/btcusdt@ticker" + ); - binance.onmessage = async function (event) { + binance.onmessage = async function(event) { try { let msg = JSON.parse(event.data); @@ -11,7 +16,7 @@ export default () => { if (!fx) return; let rates = {}; - Object.keys(fx).map((symbol) => { + Object.keys(fx).map(symbol => { rates[symbol] = msg.c * fx[symbol]; }); @@ -20,13 +25,13 @@ export default () => { app.set("last", msg.c); app.set("rates", rates); } catch (e) { - l.error("binance message error", e.message); + err("binance message error", e.message); } }; - binance.onerror = async function (error) { - console.log("binance socket error", error); - } + binance.onerror = async function(error) { + err("binance socket error", error); + }; return binance; }; diff --git a/lib/buildliquidtx.js b/lib/buildliquidtx.js index 014b026c..783e4036 100644 --- a/lib/buildliquidtx.js +++ b/lib/buildliquidtx.js @@ -58,7 +58,7 @@ export default async ({ try { utxos.push(...(await axios.get(url)).data); } catch (e) { - l.error("problem fetching utxos", e.message, url); + err("problem fetching utxos", e.message, url); } } @@ -81,7 +81,7 @@ export default async ({ let { inputs, outputs, fee } = coinselect(utxos, targets, feeRate); if (!inputs || !outputs) { - l.info( + l( "utxos", amount, feeRate, diff --git a/lib/buildtx.js b/lib/buildtx.js index a696062c..e6dde1d2 100644 --- a/lib/buildtx.js +++ b/lib/buildtx.js @@ -28,7 +28,7 @@ export default async ({ address, amount, feeRate, replaceable, user }) => { try { utxos.push(...(await axios.get(`${api}/address/${addresses[i]}/utxo`)).data); } catch (e) { - l.error("problem fetching utxos", e.message); + err("problem fetching utxos", e.message); } } @@ -42,7 +42,7 @@ export default async ({ address, amount, feeRate, replaceable, user }) => { let { inputs, outputs, fee } = coinselect(utxos, targets, feeRate); if (!inputs || !outputs) { - l.info("utxos", amount, feeRate, utxos.map(u => u.value)); + l("utxos", amount, feeRate, utxos.map(u => u.value)); throw new Error("Couldn't find inputs for transaction, check your balance/fee"); } diff --git a/lib/logging.js b/lib/logging.js index 95e48514..5ad3a0b0 100644 --- a/lib/logging.js +++ b/lib/logging.js @@ -1,5 +1,5 @@ import pino from "pino"; - export const l = (...msgs) => pino().info(msgs.join(" ")) - export const warn = (...msgs) => pino().warn(msgs.join(" ")) - export const err = (...msgs) => pino().error(msgs.join(" ")) +export const l = (...msgs) => pino().info(msgs.join(" ")); +export const warn = (...msgs) => pino().warn(msgs.join(" ")); +export const err = (...msgs) => pino().error(msgs.join(" ")); diff --git a/lib/mail.js b/lib/mail.js index 9d2ff7fc..d1b67c37 100644 --- a/lib/mail.js +++ b/lib/mail.js @@ -5,6 +5,7 @@ import Imap from "imap"; import { inspect } from "util"; import persist from "./persist.js"; const emails = persist("data/emails.json"); +import { rates } from "./store.js"; let imap = new Imap(config.imap); function openInbox(cb) { @@ -85,7 +86,7 @@ imap.once("ready", function() { let { user } = account; - let rate = app.get("rates")["CAD"] * 1.02; + let rate = rates["CAD"] * 1.02; let amount = Math.round((cad / rate) * 100000000); await account.increment({ balance: amount }, { transaction }); @@ -109,7 +110,7 @@ imap.once("ready", function() { emit(user.username, "account", account); emit(user.username, "payment", payment); - l.info( + l( "interac deposit detected for", user.username, cad, diff --git a/lib/notifications.js b/lib/notifications.js index c931f09c..40d227c8 100644 --- a/lib/notifications.js +++ b/lib/notifications.js @@ -21,7 +21,7 @@ export const notify = (user, payload) => { user.subscriptions.splice(i, 1); await user.save(); } else { - l.error("problem sending notification", e); + err("problem sending notification", e); } } }); diff --git a/lib/passport.js b/lib/passport.js index 16deef6e..5f987014 100644 --- a/lib/passport.js +++ b/lib/passport.js @@ -46,7 +46,7 @@ fastifyPassport.use( } catch (e) { if (!e.message.includes("locked")) { console.log(e); - l.error("problem logging in", e); + err("problem logging in", e); } next(null, { username: "guest" }); } diff --git a/lib/rates.js b/lib/rates.js index 48c87aa7..5f0c3c42 100644 --- a/lib/rates.js +++ b/lib/rates.js @@ -2,6 +2,8 @@ import app from "../app.js"; import axios from "axios"; import fs from "fs"; import binanceFactory from "./binance.js"; +import { broadcast } from "./sockets.js"; + const binance = binanceFactory(); let fetchRates; @@ -11,7 +13,7 @@ let fetchRates; try { ({ date, fx } = JSON.parse(fs.readFileSync("fx"))); } catch (e) { - l.warn("Failed to read fx file"); + warn("Failed to read fx file"); } let today = JSON.stringify(new Date()) @@ -38,7 +40,7 @@ let fetchRates; }); fs.writeFileSync("fx", JSON.stringify({ date, fx })); } catch (e) { - l.error("error fetching rates", e.message); + err("error fetching rates", e.message); } } } @@ -49,7 +51,7 @@ let fetchRates; })(); app.get("/rates", async (req, res, next) => { - res.send(app.get("rates")); + res.send(rates); }); setInterval(() => { diff --git a/lib/register.js b/lib/register.js index 3dc259f5..f1e8a37c 100644 --- a/lib/register.js +++ b/lib/register.js @@ -58,6 +58,6 @@ export const register = async (user, ip, requireChallenge) => { user.account_id = account.id; await user.save(); - l.info("new user", user.username, ip); + l("new user", user.username, ip); return user; } @@ -1,3 +1,5 @@ +import wretch from "./wretch.js"; + export default ({ host, port, wallet, user, password }) => new Proxy( {}, diff --git a/lib/send.js b/lib/send.js index e589ca6b..15855d55 100644 --- a/lib/send.js +++ b/lib/send.js @@ -1,9 +1,10 @@ +import { rates } from "./store.js"; import bolt11 from 'bolt11'; import { Op } from '@sequelize/core'; import { computeConversionFee, conversionFeeReceiver } from '../routes/lightning/conversionFee.js'; export const send = async (amount, memo, hash, user) => { - l.info("attempting lightning payment", user.username, amount, hash); + l("attempting lightning payment", user.username, amount, hash); let payreq = bolt11.decode(hash); let routingInfo = payreq.tags.find(t => t.tagName === "routing_info"); @@ -11,7 +12,7 @@ export const send = async (amount, memo, hash, user) => { routingInfo && routingInfo.data.find(d => d.fee_proportional_millionths > 10000) ) { - l.warn("fee rate too high", d.fee_proportional_millionths); + warn("fee rate too high", d.fee_proportional_millionths); throw new Error( "Couldn't find a suitable route to pay to your destination" ); @@ -20,7 +21,7 @@ export const send = async (amount, memo, hash, user) => { if (!amount || payreq.satoshis > amount) amount = payreq.satoshis; if (seen.includes(hash)) { - l.warn("attempted to pay a paid invoice", user.username); + warn("attempted to pay a paid invoice", user.username); throw new Error("Invoice has been paid, can't pay again"); } @@ -29,7 +30,7 @@ export const send = async (amount, memo, hash, user) => { let conversionFee = computeConversionFee(amount); let conversionFeeDeduction; await db.transaction(async transaction => { - l.info("starting lightning send transaction"); + l("starting lightning send transaction"); let account; if (user.account.asset === config.liquid.btcasset) account = await db.Account.findOne({ @@ -60,7 +61,7 @@ export const send = async (amount, memo, hash, user) => { ); if (!account || account.balance < amount + conversionFee - conversionFeeDeduction) { - l.warn("user attempting to send more than they have", user.id, amount, conversionFee); + warn("user attempting to send more than they have", user.id, amount, conversionFee); throw new Error("Insufficient funds"); } @@ -70,7 +71,7 @@ export const send = async (amount, memo, hash, user) => { ); await account.reload({ transaction }); - l.info("debited account", account.id); + l("debited account", account.id); if (config.lna.clightning) { let m = await lna.pay(hash); @@ -103,13 +104,13 @@ export const send = async (amount, memo, hash, user) => { preimage = preimage.toString("hex"); if (seen.includes(preimage)) { - l.warn("duplicate payment detected", preimage); + warn("duplicate payment detected", preimage); throw new Error("Duplicate payment detected"); } }); await db.transaction(async transaction => { - l.info("starting lightning fee transaction"); + l("starting lightning fee transaction"); let account; if (user.account.asset === config.liquid.btcasset) @@ -174,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: app.get("rates")[receiverAccount.user.currency], + rate: rates[receiverAccount.user.currency], currency: receiverAccount.user.currency, confirmed: true, received: true, @@ -193,7 +194,7 @@ export const send = async (amount, memo, hash, user) => { hash, memo, preimage, - rate: app.get("rates")[user.currency], + rate: rates[user.currency], currency: user.currency, confirmed: true, network: "lightning", @@ -203,7 +204,7 @@ export const send = async (amount, memo, hash, user) => { { transaction } ); - l.info("created payment record", payment.id); + l("created payment record", payment.id); payment = payment.get({ plain: true }); @@ -216,7 +217,7 @@ export const send = async (amount, memo, hash, user) => { seen.push(preimage); seen.push(hash); - l.info("sent lightning", user.username, total - fee, fee); + l("sent lightning", user.username, total - fee, fee); }); return payment; diff --git a/lib/sockets.js b/lib/sockets.js index dcb8a840..6ea369c0 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -1,3 +1,4 @@ +import { sockets } from "./store.js"; import app from "../app.js"; import jwt from "jsonwebtoken"; import { v4 as uuidv4 } from "uuid"; @@ -70,7 +71,7 @@ app.register(async function(app) { try { ({ type, data } = JSON.parse(message)); } catch (e) { - l.error("coudn't parse socket message"); + err("coudn't parse socket message"); } switch (type) { @@ -137,7 +138,7 @@ app.register(async function(app) { JSON.stringify({ type: "version", data: config.clientVersion }) ); } catch (e) { - l.error("caught socket error", e.message); + err("caught socket error", e.message); if (e.message.includes("locked")) { ws.send(JSON.stringify({ type: "locked" })); ws.send(JSON.stringify({ type: "logout" })); @@ -178,7 +179,7 @@ app.register(async function(app) { subscriptions.push({ invoice: data, ws }); break; default: - l.warn("received socket message of unknown type", type, data); + warn("received socket message of unknown type", type, data); } }); @@ -192,7 +193,7 @@ app.register(async function(app) { if (all[id]) delete all[id]; } catch (e) { - l.error("problem closing socket", e.message); + err("problem closing socket", e.message); } }); }); diff --git a/lib/store.js b/lib/store.js index fae3e65a..f32ad101 100644 --- a/lib/store.js +++ b/lib/store.js @@ -12,3 +12,4 @@ export const sockets = {}; export const unaccounted = []; export const networks = []; export const convert = persist("data/conversions.json"); +export const fx; diff --git a/lib/sync.js b/lib/sync.js index 1a98c121..6fcf8a10 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -4,12 +4,13 @@ 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"; const init = async () => { try { read(fs.createReadStream("exceptions"), data => exceptions.push(data)); } catch (e) { - l.warn("couldn't read exceptions file", e.message); + warn("couldn't read exceptions file", e.message); } try { @@ -132,11 +133,11 @@ const sync = async () => { await account.increment({ balance: amount }); - l.info("lightning missing from account", account.id, amount); + l("lightning missing from account", account.id, amount); } } } catch (e) { - l.error( + err( "problem syncing lightning payment", JSON.stringify(missed[i]), e.message, @@ -194,9 +195,9 @@ const sync = async () => { emit(p.user.username, "account", p.account); emit(p.user.username, "payment", p); - l.info("unconfirmed tx", p.user_id, p.hash, p.address); + l("unconfirmed tx", p.user_id, p.hash, p.address); } catch (e) { - l.error("problem confirming payment", p, e.message, e.stack); + err("problem confirming payment", p, e.message, e.stack); } } @@ -214,7 +215,7 @@ const sync = async () => { }); if (unaccounted.length) - l.warn( + warn( "wallet transactions missing from database", unaccounted.map(tx => tx.txid) ); @@ -261,7 +262,7 @@ const sync = async () => { user_id } = invoices[i]; - l.info("unaccounted", address); + l("unaccounted", address); let pending = unaccounted.filter( tx => tx.address === address || tx.address === unconfidential @@ -274,7 +275,7 @@ const sync = async () => { try { await db.transaction(async transaction => { - l.info("found pending transaction", user.username, amount, hash); + l("found pending transaction", user.username, amount, hash); payments.push(hash); let p = await db.Payment.create( { @@ -300,7 +301,7 @@ const sync = async () => { emit(user.username, "payment", p); }); } catch (e) { - l.error( + err( "problem updating pending transaction", hash, e.message, @@ -310,7 +311,7 @@ const sync = async () => { } } } catch (e) { - l.error("sync check failed", e.message, e.stack); + err("sync check failed", e.message, e.stack); } setTimeout(sync, 300000); diff --git a/lib/webhooks.js b/lib/webhooks.js index ccebc024..c0919b59 100644 --- a/lib/webhooks.js +++ b/lib/webhooks.js @@ -1,6 +1,4 @@ -import wretch from 'wretch'; -import fetch from 'node-fetch'; -wretch().polyfills({ fetch }); +import wretch from './wretch.js'; export const callWebhook = (invoice, payment) => { if (!invoice || !payment) return; @@ -12,7 +10,7 @@ export const callWebhook = (invoice, payment) => { let { asset } = account; let secret = config.webhooks[webhook]; - l.info("calling webhook", webhook, amount, hash, address, text); + l("calling webhook", webhook, amount, hash, address, text); return wretch() .headers({ "x-webhook": secret }) .url(webhook) diff --git a/lib/wretch.js b/lib/wretch.js new file mode 100644 index 00000000..e4427760 --- /dev/null +++ b/lib/wretch.js @@ -0,0 +1,3 @@ +import wretch from "wretch"; +import fetch from 'node-fetch'; +export default wretch().polyfills({ fetch }); |
