aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-01-16 23:25:02 -0800
committerAdam Soltys <[email protected]>2021-01-16 23:25:02 -0800
commitd9e10940710fbc631411dc66979410b95f084052 (patch)
tree16f44202d2c16bb162fcdd197054c1608766ee67 /lib
parent08ba62b278add9842307370f0e528c2a6fc75482 (diff)
downloadcoinos-server-d9e10940710fbc631411dc66979410b95f084052.tar.gz
coinos-server-d9e10940710fbc631411dc66979410b95f084052.zip
two transactions for ln payment
Diffstat (limited to 'lib')
-rw-r--r--lib/send.js156
1 files changed, 83 insertions, 73 deletions
diff --git a/lib/send.js b/lib/send.js
index a1052041..37cbdfdb 100644
--- a/lib/send.js
+++ b/lib/send.js
@@ -10,95 +10,105 @@ send = async (amount, memo, hash, user) => {
throw new Error("Invoice has been paid, can't pay again");
}
- let payment;
+ let error, fee, total, preimage, payment;
- await db.transaction(async (transaction) => {
- let account = await db.Account.findOne({
- where: {
- user_id: user.id,
- asset: config.liquid.btcasset,
- pubkey: null,
- },
- lock: transaction.LOCK.UPDATE,
- transaction,
- });
+ await db.transaction(async transaction => {
+ let account = await db.Account.findOne({
+ where: {
+ user_id: user.id,
+ asset: config.liquid.btcasset,
+ pubkey: null
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
- if (account.balance < amount) {
- throw new Error("Insufficient funds");
- }
+ if (account.balance < amount) {
+ throw new Error("Insufficient funds");
+ }
- let error, fee, total, preimage;
- await account.decrement({ balance: amount }, { transaction });
- await account.reload({ transaction });
+ await account.decrement({ balance: amount }, { transaction });
+ await account.reload({ transaction });
- if (config.lna.clightning) {
- let m = await lna.pay(hash);
- let sent;
- ({
- msatoshi: total,
- msatoshi_sent: sent,
- payment_preimage: preimage,
- } = m);
+ if (config.lna.clightning) {
+ let m = await lna.pay(hash);
+ let sent;
+ ({
+ msatoshi: total,
+ msatoshi_sent: sent,
+ payment_preimage: preimage
+ } = m);
- fee = parseInt((sent - total) / 1000);
- total = parseInt(total / 1000);
- } else {
- let m = await lna.sendPaymentSync({
- amt: amount,
- payment_request: hash,
- max_parts: 10,
- });
+ fee = parseInt((sent - total) / 1000);
+ total = parseInt(total / 1000);
+ } else {
+ let m = await lna.sendPaymentSync({
+ amt: amount,
+ payment_request: hash,
+ max_parts: 10
+ });
- if (m.payment_error) throw new Error(m.payment_error);
+ if (m.payment_error) throw new Error(m.payment_error);
- ({
- payment_route: { total_fees: fee, total_amt: total },
- payment_preimage: preimage,
- } = m);
- }
+ ({
+ payment_route: { total_fees: fee, total_amt: total },
+ payment_preimage: preimage
+ } = m);
+ }
- preimage = preimage.toString("hex");
+ preimage = preimage.toString("hex");
- if (seen.includes(preimage)) {
- l.warn("duplicate payment detected", preimage);
- throw new Error("Duplicate payment detected");
- }
+ if (seen.includes(preimage)) {
+ l.warn("duplicate payment detected", preimage);
+ throw new Error("Duplicate payment detected");
+ }
+ });
- total = parseInt(total);
- fee = parseInt(fee);
+ await db.transaction(async transaction => {
+ let account = await db.Account.findOne({
+ where: {
+ user_id: user.id,
+ asset: config.liquid.btcasset,
+ pubkey: null
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
- await account.decrement({ balance: fee }, { transaction });
- await account.reload({ transaction });
+ total = parseInt(total);
+ fee = parseInt(fee);
- payment = await db.Payment.create(
- {
- amount: -(total - fee),
- account_id: account.id,
- user_id: user.id,
- hash,
- memo,
- preimage,
- rate: app.get("rates")[user.currency],
- currency: user.currency,
- confirmed: true,
- network: "lightning",
- fee,
- },
- { transaction }
- );
+ await account.decrement({ balance: fee }, { transaction });
+ await account.reload({ transaction });
- payment = payment.get({ plain: true });
- payment.account = account.get({ plain: true });
+ payment = await db.Payment.create(
+ {
+ amount: -(total - fee),
+ account_id: account.id,
+ user_id: user.id,
+ hash,
+ memo,
+ preimage,
+ rate: app.get("rates")[user.currency],
+ currency: user.currency,
+ confirmed: true,
+ network: "lightning",
+ fee
+ },
+ { transaction }
+ );
- emit(user.username, "account", payment.account);
- emit(user.username, "payment", payment);
+ payment = payment.get({ plain: true });
+ payment.account = account.get({ plain: true });
- seen.push(preimage);
- seen.push(hash);
+ emit(user.username, "account", payment.account);
+ emit(user.username, "payment", payment);
- l.info("sent lightning", user.username, (total - fee), fee);
+ seen.push(preimage);
+ seen.push(hash);
- });
+ l.info("sent lightning", user.username, total - fee, fee);
+ });
- return payment;
+ return payment;
};