diff options
| author | Adam Soltys <[email protected]> | 2020-03-28 23:13:13 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-03-28 23:13:13 -0700 |
| commit | a0422300e2b13896634eda12930959bc64ba7dc9 (patch) | |
| tree | 481915dc9e311d7dfc31d0b237b2c06ad5fa26a5 | |
| parent | 5142c8cfcffd59d04ec18e67183a1866324d9209 (diff) | |
| download | coinos-server-a0422300e2b13896634eda12930959bc64ba7dc9.tar.gz coinos-server-a0422300e2b13896634eda12930959bc64ba7dc9.zip | |
handle tips and invoices better
| -rw-r--r-- | routes/bitcoin/receive.js | 56 | ||||
| -rw-r--r-- | routes/bitcoin/send.js | 2 | ||||
| -rw-r--r-- | routes/invoices.js | 20 | ||||
| -rw-r--r-- | routes/lightning/invoice.js | 14 | ||||
| -rw-r--r-- | routes/lightning/receive.js | 24 | ||||
| -rw-r--r-- | routes/lightning/send.js | 4 | ||||
| -rw-r--r-- | routes/liquid/receive.js | 33 | ||||
| -rw-r--r-- | routes/liquid/send.js | 2 |
8 files changed, 86 insertions, 69 deletions
diff --git a/routes/bitcoin/receive.js b/routes/bitcoin/receive.js index b5ab4298..3083c49c 100644 --- a/routes/bitcoin/receive.js +++ b/routes/bitcoin/receive.js @@ -12,7 +12,11 @@ const zmqRawTx = zmq.socket("sub"); zmqRawTx.connect(config.bitcoin.zmqrawtx); zmqRawTx.subscribe("rawtx"); -let NETWORK = bitcoin.networks[config.bitcoin.network === "mainnet" ? "bitcoin" : config.bitcoin.network]; +const asset = "BTC"; +const network = + bitcoin.networks[ + config.bitcoin.network === "mainnet" ? "bitcoin" : config.bitcoin.network + ]; zmqRawTx.on("message", async (topic, message, sequence) => { const hex = message.toString("hex"); @@ -27,10 +31,7 @@ zmqRawTx.on("message", async (topic, message, sequence) => { let address; try { - address = bitcoin.address.fromOutputScript( - o.script, - NETWORK - ); + address = bitcoin.address.fromOutputScript(o.script, network); } catch (e) { return; } @@ -44,20 +45,17 @@ zmqRawTx.on("message", async (topic, message, sequence) => { } }); - let invoices = await db.Payment.findAll({ - limit: 1, + const invoice = await db.Invoice.findOne({ where: { - address, - received: null, - amount: { - [Op.gt]: 0 - } + user_id: user.id, + asset }, - order: [["createdAt", "DESC"]] + order: [["id", "DESC"]] }); - let tip = null; - if (invoices.length) tip = invoices[0].tip; + const currency = invoice ? invoice.currency : user.currency; + const rate = invoice ? invoice.rate : app.get("rates")[user.currency]; + const tip = invoice ? invoice.tip : null; let confirmed = false; @@ -66,20 +64,20 @@ zmqRawTx.on("message", async (topic, message, sequence) => { await user.save(); emit(user.username, "user", user); - + addresses[user.address] = user.username; const payment = await db.Payment.create({ user_id: user.id, hash, - amount: value, - currency: user.currency, - rate: app.get("rates")[user.currency], + amount: value - tip, + currency, + rate, received: true, tip, confirmed, address, - asset: 'BTC', + asset, }); l.info("bitcoin detected", user.username, o.value); @@ -102,7 +100,7 @@ zmqRawBlock.on("message", async (topic, message, sequence) => { block.transactions.map(tx => { let hash = reverse(tx.getHash()).toString("hex"); if (hashes.includes(hash)) queue[hash] = 1; - }); + }); }); setInterval(async () => { @@ -113,30 +111,30 @@ setInterval(async () => { let p = await db.Payment.findOne({ include: [{ model: db.User, as: "user" }], where: { hash, confirmed: 0 } - }) + }); p.confirmed = 1; let user = await p.getUser(); - user.balance += p.amount; - user.pending -= Math.min(user.pending, p.amount); + user.balance += p.amount + p.tip; + user.pending -= Math.min(user.pending, p.amount + p.tip); emit(user.username, "user", user); await user.save(); await p.save(); let payments = await db.Payment.findAll({ - where: { + where: { user_id: user.id, - received: { + received: { [Op.ne]: null - }, + } }, - order: [['id', 'DESC']], + order: [["id", "DESC"]], limit: 12 }); - l.info("bitcoin confirmed", user.username, p.amount); + l.info("bitcoin confirmed", user.username, p.amount, p.tip); emit(user.username, "payments", payments); delete queue[hash]; } diff --git a/routes/bitcoin/send.js b/routes/bitcoin/send.js index d8a17f98..6ad518c7 100644 --- a/routes/bitcoin/send.js +++ b/routes/bitcoin/send.js @@ -32,6 +32,8 @@ module.exports = async (req, res) => { total = (total - change) + fee; let amount = total - fee; + l.info("attempting bitcoin payment", user.username, amount, fee); + try { await db.transaction(async transaction => { let { balance } = await db.User.findOne({ diff --git a/routes/invoices.js b/routes/invoices.js index d188ff44..2c2b9839 100644 --- a/routes/invoices.js +++ b/routes/invoices.js @@ -1,6 +1,20 @@ +const { Op } = require("sequelize"); + app.post("/invoice", auth, async (req, res) => { - let { invoice } = req.body; + const { invoice } = req.body; invoice.user_id = req.user.id; - invoice.currency = req.user.currency; - res.send(await db.Invoice.create(invoice)); + l.info("creating invoice", req.user.username, invoice); + + const exists = await db.Invoice.findOne({ + where: { + [Op.or]: { + address: invoice.address || "undefined", + text: invoice.text + } + } + }); + + res.send( + exists ? await exists.update(invoice) : await db.Invoice.create(invoice) + ); }); diff --git a/routes/lightning/invoice.js b/routes/lightning/invoice.js index 50be961d..f19b3484 100644 --- a/routes/lightning/invoice.js +++ b/routes/lightning/invoice.js @@ -1,13 +1,13 @@ module.exports = async (req, res) => { - let err = m => res.status(500).send(m); - let { amount, tip } = req.body; + const { amount, tip } = req.body; + + l.info("adding lightning invoice", req.user.username, amount, tip); - let invoice; try { - invoice = await lnb.addInvoice({ value: amount + tip }); + const invoice = await lnb.addInvoice({ value: amount + tip }); + res.send(invoice.payment_request); } catch (e) { - return err(e.message); + l.error("problem creating invoice", e.message); + res.status(500).send(e.message); } - - res.send(invoice.payment_request); }; diff --git a/routes/lightning/receive.js b/routes/lightning/receive.js index c4ce2414..6ac4c947 100644 --- a/routes/lightning/receive.js +++ b/routes/lightning/receive.js @@ -1,8 +1,9 @@ +const asset = "LNBTC"; + const handlePayment = async msg => { if (!msg.settled) return; - l.info("lightning strikes!", msg.payment_request); - let invoice = await db.Invoice.findOne({ + const invoice = await db.Invoice.findOne({ include: { model: db.User, as: "user" }, where: { text: msg.payment_request @@ -11,28 +12,29 @@ const handlePayment = async msg => { if (!invoice) return; - const { user } = invoice; - const amount = parseInt(msg.value); + const { text: hash, currency, rate, tip, user } = invoice; + const amount = parseInt(msg.value) - tip; const payment = await db.Payment.create({ user_id: user.id, - hash: invoice.text, + hash, amount, - currency: user.currency, - rate: app.get("rates")[user.currency], + currency, + rate, received: true, confirmed: true, - asset: 'LNBTC', + asset, + tip, }); - invoice.received = msg.value; - user.balance += amount; + invoice.received += amount + tip; + user.balance += amount + tip; await invoice.save(); await payment.save(); await user.save(); payments.push(msg.payment_request); - l.info("lightning payment received", user.username, payment.amount); + l.info("lightning payment received", user.username, payment.amount, payment.tip); emit(user.username, "payment", payment); emit(user.username, "user", user); diff --git a/routes/lightning/send.js b/routes/lightning/send.js index 38023a1d..de363a0a 100644 --- a/routes/lightning/send.js +++ b/routes/lightning/send.js @@ -6,6 +6,8 @@ module.exports = async (req, res) => { let { route } = req.body; let { user } = req; + l.info("attempting lightning payment", user.username, payreq.satoshis); + if (seen.includes(hash)) { l.warn("attempted to pay a paid invoice", user.username); return res.status(500).send("Invoice has been paid, can't pay again"); @@ -30,7 +32,7 @@ module.exports = async (req, res) => { await user.save({ transaction }); }); } catch (e) { - l.warn("insufficient funds for lightning payment", user.username); + l.warn("insufficient funds for lightning payment", user.username, user.balance); return res.status(500).send("Not enough satoshis"); } diff --git a/routes/liquid/receive.js b/routes/liquid/receive.js index 6a7e53b8..2b459af3 100644 --- a/routes/liquid/receive.js +++ b/routes/liquid/receive.js @@ -11,7 +11,7 @@ const zmqRawTx = zmq.socket("sub"); zmqRawTx.connect("tcp://127.0.0.1:18603"); zmqRawTx.subscribe("rawtx"); -let NETWORK = bitcoin.networks[config.bitcoin.network === "mainnet" ? "bitcoin" : config.bitcoin.network]; +const asset = 'LBTC'; zmqRawTx.on("message", async (topic, message, sequence) => { const hex = message.toString("hex"); @@ -35,20 +35,17 @@ zmqRawTx.on("message", async (topic, message, sequence) => { } }); - let invoices = await db.Payment.findAll({ - limit: 1, + const invoice = await db.Invoice.findOne({ where: { - address, - received: null, - amount: { - [Op.gt]: 0 - } + user_id: user.id, + asset, }, - order: [["id", "DESC"]] + order: [ [ 'id', 'DESC' ]] }); - let tip = null; - if (invoices.length) tip = invoices[0].tip; + const currency = invoice ? invoice.currency : user.currency; + const rate = invoice ? invoice.rate : app.get("rates")[user.currency]; + const tip = invoice ? invoice.tip : null; let confirmed = 0; @@ -64,14 +61,14 @@ zmqRawTx.on("message", async (topic, message, sequence) => { const payment = await db.Payment.create({ user_id: user.id, hash: blinded.txid, - amount: value, - currency: user.currency, - rate: app.get("rates")[user.currency], + amount: value - tip, + currency, + rate, received: true, tip, confirmed, address, - asset: 'LBTC', + asset, }); payments.push(blinded.txid); @@ -114,9 +111,9 @@ setInterval(async () => { p.confirmed = 1; let user = await p.getUser(); - user.balance += p.amount; - user.pending -= Math.min(user.pending, p.amount); - l.info("liquid confirmed", user.username, p.amount); + user.balance += p.amount + p.tip; + user.pending -= Math.min(user.pending, p.amount + p.tip); + l.info("liquid confirmed", user.username, p.amount, p.tip); emit(user.username, "user", user); await user.save(); diff --git a/routes/liquid/send.js b/routes/liquid/send.js index eb369a01..c657ae30 100644 --- a/routes/liquid/send.js +++ b/routes/liquid/send.js @@ -34,6 +34,8 @@ module.exports = async (req, res) => { total = total - change; let amount = total - fee; + l.info("attempting liquid payment", user.username, amount, fee); + try { await db.transaction(async transaction => { let { balance } = await db.User.findOne({ |
