aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Lock.svelte
blob: fce994a26578a74396db2dcf53274911b2fb1191 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<svg
  xmlns="http://www.w3.org/2000/svg"
  class="absolute bottom-0 left-0 -mb-0.5 transform translate-x-1/2 -translate-y-1/2 text-black peer-placeholder-shown:text-gray-300 h-6 w-6"
  fill="none"
  viewBox="0 0 24 24"
  stroke="currentColor"
>
  <path
    stroke-linecap="round"
    stroke-linejoin="round"
    stroke-width="2"
    d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"
  />
</svg>
.highlight .mf { color: #00D; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #00D; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #00D; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #00D; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #D20; background-color: #FFF0F0 } /* Literal.String.Affix */ .highlight .sb { color: #D20; background-color: #FFF0F0 } /* Literal.String.Backtick */ .highlight .sc { color: #D20; background-color: #FFF0F0 } /* Literal.String.Char */ .highlight .dl { color: #D20; background-color: #FFF0F0 } /* Literal.String.Delimiter */ .highlight .sd { color: #D20; background-color: #FFF0F0 } /* Literal.String.Doc */ .highlight .s2 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Double */ .highlight .se { color: #04D; background-color: #FFF0F0 } /* Literal.String.Escape */ .highlight .sh { color: #D20; background-color: #FFF0F0 } /* Literal.String.Heredoc */ .highlight .si { color: #33B; background-color: #FFF0F0 } /* Literal.String.Interpol */ .highlight .sx { color: #2B2; background-color: #F0FFF0 } /* Literal.String.Other */ .highlight .sr { color: #080; background-color: #FFF0FF } /* Literal.String.Regex */ .highlight .s1 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Single */ .highlight .ss { color: #A60; background-color: #FFF0F0 } /* Literal.String.Symbol */ .highlight .bp { color: #038 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #06B; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #369 } /* Name.Variable.Class */ .highlight .vg { color: #D70 } /* Name.Variable.Global */ .highlight .vi { color: #33B } /* Name.Variable.Instance */ .highlight .vm { color: #369 } /* Name.Variable.Magic */ .highlight .il { color: #00D; font-weight: bold } /* Literal.Number.Integer.Long */
const bolt11 = require("bolt11");
const { Op } = require("sequelize");

send = async (amount, memo, hash, user) => {
  let payreq = bolt11.decode(hash);
  let routingInfo = payreq.tags.find(t => t.tagName === "routing_info");
  if (
    routingInfo &&
    routingInfo.data.find(d => d.fee_proportional_millionths > 100)
  )
    throw new Error("Fee rate too high");

  if (!amount || payreq.satoshis > amount) amount = payreq.satoshis;

  if (seen.includes(hash)) {
    l.warn("attempted to pay a paid invoice", user.username);
    throw new Error("Invoice has been paid, can't pay again");
  }

  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,
        balance: {
          [Op.gte]: amount
        }
      },
      lock: transaction.LOCK.UPDATE,
      transaction
    });

    if (!account || account.balance < amount) {
      throw new Error("Insufficient funds");
    }

    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);

      fee = parseInt((sent - total) / 1000);
      total = parseInt(total / 1000);
    } else {
      let m = await lna.sendPaymentSync({
        amt: amount,
        payment_request: hash,
        max_parts: 10,
        fee_limit: { fixed: Math.min(10000, account.balance) },
      });

      if (m.payment_error) throw new Error(m.payment_error);

      ({
        payment_route: { total_fees: fee, total_amt: total },
        payment_preimage: preimage
      } = m);
    }

    preimage = preimage.toString("hex");

    if (seen.includes(preimage)) {
      l.warn("duplicate payment detected", preimage);
      throw new Error("Duplicate payment detected");
    }
  });

  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
    });

    total = parseInt(total);
    fee = parseInt(fee);

    await account.decrement({ balance: fee }, { transaction });
    await account.reload({ transaction });

    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 }
    );

    payment = payment.get({ plain: true });
    payment.account = account.get({ plain: true });

    emit(user.username, "account", payment.account);
    emit(user.username, "payment", payment);

    seen.push(preimage);
    seen.push(hash);

    l.info("sent lightning", user.username, total - fee, fee);
  });

  return payment;
};