aboutsummaryrefslogtreecommitdiffstats
path: root/sendPayment.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2020-03-11 15:10:40 -0700
committerAdam Soltys <[email protected]>2020-03-11 15:10:40 -0700
commitc0888fab024cc9f7c99812bfabf9d7774312dbf7 (patch)
treecad95683af97432f6092a2c2bae10fbb7a588db5 /sendPayment.js
parentbf2b51f0c2ae9b8f0f330dde6411e45fc0da8a25 (diff)
downloadcoinos-server-c0888fab024cc9f7c99812bfabf9d7774312dbf7.tar.gz
coinos-server-c0888fab024cc9f7c99812bfabf9d7774312dbf7.zip
refactor
Diffstat (limited to 'sendPayment.js')
-rw-r--r--sendPayment.js95
1 files changed, 0 insertions, 95 deletions
diff --git a/sendPayment.js b/sendPayment.js
deleted file mode 100644
index 31e3f602..00000000
--- a/sendPayment.js
+++ /dev/null
@@ -1,95 +0,0 @@
-const bolt11 = require("bolt11");
-const config = require("./config");
-
-const l = require("pino")();
-
-module.exports = (app, db, emit, seen, lna, lnb) => async (req, res) => {
- let hash = req.body.payreq;
- let payreq = bolt11.decode(hash);
- let { route } = req.body;
- let { user } = req;
-
- 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");
- }
-
- try {
- await db.transaction(async transaction => {
- let { balance } = await db.User.findOne(
- {
- where: {
- username: user.username
- }
- },
- { transaction }
- );
-
- if (balance < payreq.satoshis) {
- throw new Error();
- }
-
- user.balance -= payreq.satoshis;
- await user.save({ transaction });
- });
- } catch (e) {
- return res.status(500).send("Not enough satoshis");
- }
-
- let m;
- try {
- let paymentHash = payreq.tags.find(t => t.tagName === 'payment_hash').data;
- m = lna.sendToRouteSync({
- "payment_hash": Buffer.from(paymentHash, 'hex'),
- route
- }, async function(err, m) {
- if (m.payment_error) return res.status(500).send(m.payment_error);
- if (seen.includes(m.payment_preimage)) return;
- seen.push(m.payment_preimage);
-
- let total = parseInt(m.payment_route.total_amt);
- let fee = m.payment_route.total_fees;
-
- user.balance -= total - payreq.satoshis;
-
- await db.transaction(async transaction => {
- await user.save({ transaction });
-
- const payment = await db.Payment.create(
- {
- amount: -total,
- fee,
- user_id: user.id,
- hash,
- rate: app.get("rates")[user.currency],
- currency: user.currency,
- confirmed: true,
- asset: 'LNBTC',
- },
- { transaction }
- );
-
- l.info("sent lightning", user.username, -payment.amount);
- emit(user.username, "payment", payment);
- emit(user.username, "user", user);
-
- if (payreq.payeeNodeKey === config.lnb.id) {
- lna.addInvoice({ value: payreq.satoshis }, (err, invoice) => {
- let payback = lnb.sendPayment(lnb.meta, {});
-
- /* eslint-disable-next-line */
- let { payment_request } = invoice;
- /* eslint-disable-next-line */
- payback.write({ payment_request });
- });
- }
-
- seen.push(hash);
- res.send(payment);
- });
- });
- } catch (e) {
- l.info(e);
- return res.status(500).send(e);
- }
-};