aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Spinner.svelte
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/Spinner.svelte')
0 files changed, 0 insertions, 0 deletions
'>85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
import config from "$config";
import { generate } from "$lib/invoices";
import { emit } from "$lib/sockets";
import { v4 } from "uuid";
import { notify } from "$lib/notifications";
import { db, g, s } from "$lib/db";
import { l, err, warn } from "$lib/logging";
import {
  f,
  btc,
  fail,
  fiat,
  getInvoice,
  getPayment,
  getUser,
  sleep,
  SATS,
  sats,
  formatReceipt,
  t,
} from "$lib/utils";
import { callWebhook } from "$lib/webhooks";
import got from "got";
import { mqtt1, mqtt2 } from "$lib/mqtt";
import { mail, templates } from "$lib/mail";
import api from "$lib/api";
import { bech32 } from "bech32";
import rpc from "@coinos/rpc";

import ln from "$lib/ln";

let bc = rpc(config.bitcoin);
let lq = rpc(config.liquid);

let { URL } = process.env;
let dust = 547;

export let types = {
  internal: "internal",
  bitcoin: "bitcoin",
  lightning: "lightning",
  fund: "fund",
  liquid: "liquid",
  ecash: "ecash",
  reconcile: "reconcile",
};

export let debit = async ({
  aid = undefined,
  hash,
  amount,
  fee = 0,
  memo = undefined,
  user,
  type = types.internal,
  rate = undefined,
}) => {
  amount = parseInt(amount);
  if (type !== types.internal && (await g("freeze")))
    fail("Problem sending payment");
  // if (!user.unlimited && amount > 1000000)
  //   fail(`⚡️${amount} exceeds max withdrawal of ⚡️1,000,000`);
  let ref;
  let { id: uid, currency } = user;

  let rates = await g("rates");
  if (!rate) rate = rates[currency];

  let invoice = await getInvoice(hash);
  let iid;

  if (invoice) {
    if (invoice.received >= amount) fail("Invoice already paid");
    ({ id: iid } = invoice);

    ref = invoice.uid;

    let equivalentRate =
      invoice.rate * (rates[currency] / rates[invoice.currency]);

    if (Math.abs(invoice.rate / rates[invoice.currency] - 1) < 0.01) {
      rate = equivalentRate;
    } else {
      warn("rate slipped", hash, invoice.rate, equivalentRate);
    }
  }

  let tip = parseInt(invoice?.tip) || null;
  if (tip < 0) fail("Invalid tip");

  if (!amount || amount < 0) fail("Amount must be greater than zero");

  let ourfee: any = [types.bitcoin, types.liquid, types.lightning].includes(
    type,
  )
    ? Math.round((amount + fee + tip) * config.fee)
    : 0;

  if (aid) ourfee = 0;

  ourfee = await db.debit(
    `balance:${aid || uid}`,
    `credit:${type}:${aid ? 0 : uid}`,
    amount || 0,
    tip || 0,
    fee || 0,
    ourfee || 0,
  );

  if (ourfee.err) fail(ourfee.err);

  let id = v4();
  let p = {
    id,
    aid,
    amount: -amount,
    fee,
    hash,
    hex: undefined,
    ourfee,
    memo,
    iid,
    uid,
    confirmed: ![types.bitcoin, types.liquid].includes(type),
    rate,
    currency,
    type,
    ref,
    tip,
    created: Date.now(),
  };

  await s(`payment:${hash}`, id);
  await s(`payment:${id}`, p);
  await db.lPush(`${aid || uid}:payments`, id);

  l(user.username, "sent", type, amount);
  //emit(user.id, "payment", p);

  return p;
};

export let credit = async ({
  hash,
  amount,
  memo = "",
  ref = "",
  type = types.internal,
  aid = undefined,
}) => {
  amount = parseInt(amount) || 0;

  let inv = await getInvoice(hash);
  if (!inv) {
    await db.sAdd("missing", ref.split(":")[0]);
    return;
  }

  let { path, tip } = inv;
  tip = parseInt(tip) || 0;

  if (!memo) ({ memo } = inv);
  if (memo && memo.length > 5000) fail("memo too long");
  if (amount < 0 || tip < 0) fail("Invalid amount");
  if (type === types.internal) amount += tip;

  let user = await getUser(inv.uid);
  let { id: uid, currency, username } = user;

  let rates = await g("rates");
  let rate = rates[currency];

  if (!rate) await sleep(1000);
  rate = rates[currency];

  let equivalentRate = inv.rate * (rates[currency] / rates[inv.currency]);

  if (Math.abs(inv.rate / rates[inv.currency] - 1) < 0.01) {
    rate = equivalentRate;
  } else {
    // warn("rate slipped", hash, invoice.rate, equivalentRate);
  }

  let id = v4();
  let p = {
    aid,
    id,
    iid: inv.id,
    hash,
    amount: amount - tip,
    path,
    uid,
    rate,
    currency,
    memo,
    ref,
    tip,
    type,
    confirmed: true,
    created: Date.now(),
    items: undefined,
  };

  if ([types.bitcoin, types.liquid].includes(type)) inv.pending += amount;
  else {
    inv.received += amount;
    inv.preimage = ref;
  }

  let balanceKey = "balance";
  if ([types.bitcoin, types.liquid].includes(type)) {
    let [txid, vout] = ref.split(":").slice(-2);
    p.confirmed = false;
    balanceKey = "pending";
    await s(`payment:${txid}:${vout}`, id);
  } else {
    await s(`payment:${hash}`, id);
  }

  let m = await db.multi();

  if ([types.bitcoin, types.liquid, types.lightning].includes(type))
    m.incrBy(`credit:${type}:${uid}`, Math.round(amount * config.fee));

  m.set(`invoice:${inv.id}`, JSON.stringify(inv))
    .set(`payment:${p.id}`, JSON.stringify(p))
    .lPush(`${aid || uid}:payments`, p.id)
    .incrBy(`${balanceKey}:${aid || uid}`, amount)
    .exec();

  emit(uid, "payment", p);
  notify(user, `Received ${amount} sats`);
  callWebhook(inv, p);
  l(username, "received", type, amount);

  let items;
  if (inv.items && inv.items.length) {
    formatReceipt(inv.items, inv.currency);
    p.items = inv.items;
  }

  if (config.mqtt1) {
    if (!mqtt1.connected) await mqtt1.reconnect();
    mqtt1.publish(
      username,
      `pay:${p.amount}:${p.tip}:${p.rate}:${p.created}:${p.id}:${p.memo}:${items}`,
    );
  }

  if (config.mqtt2) {
    if (!mqtt2.connected) await mqtt2.reconnect();
    mqtt2.publish(
      username,
      `pay:${p.amount}:${p.tip}:${p.rate}:${p.created}:${p.id}:${p.memo}:${items}`,
    );
  }

  try {
    await completePayment(p, user);
  } catch (e) {
    console.log(e);
  }

  return p;
};

let fmt = (sats) =>
  "⚡️" +
  new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 }).format(sats);

export let completePayment = async (p, user) => {
  let { id, autowithdraw, threshold, reserve, destination, username } = user;
  let link = (id) => `${URL}/payment/${id}`;
  if (p.confirmed) {
    let withdrawal;
    if (autowithdraw) {
      try {
        let to = destination.trim();
        let balance = await g(`balance:${id}`);
        let amount = balance - reserve;
        if (balance > threshold) {
          l("initiating autowithdrawal", amount, to, balance, threshold);
          let w = await pay({ amount, to, user });
          withdrawal = {
            amount: fmt(-w.amount),
            link: link(w.id),
          };
        }
      } catch (e) {
        withdrawal = { failed: true };
        warn(username, "autowithdraw failed", e.message);
      }
    }

    let { paymentReceived } = t(user);
    if (user.verified && user.notify) {
      mail(user, paymentReceived, templates.paymentReceived, {
        ...t(user),
        username,
        payment: {
          amount: fmt(p.amount),
          link: link(p.id),
          tip: p.tip ? fmt(p.tip) : undefined,
          fiat: f(fiat(p.amount, p.rate), p.currency),
          fiatTip: p.tip ? f(fiat(p.tip, p.rate), p.currency) : undefined,
          memo: p.memo,
          items:
            p.items &&
            p.items.map((i) => {
              return {
                quantity: i.quantity,
                name: i.name,
                total: i.quantity * i.price,
                totalFiat: f(i.quantity * i.price, p.currency),
              };
            }),
        },
        withdrawal,
      });
    }
  }
};

let pay = async ({ aid = undefined, amount, to, user }) => {
  if (!aid) aid = user.id;
  amount = parseInt(amount) || 0;
  let lnurl, pr;
  if (to.includes("@") && to.includes(".")) {
    let [name, domain] = to.split("@");
    lnurl = `https://${domain}/.well-known/lnurlp/${name}`;
  } else if (to.startsWith("lnurl")) {
    lnurl = Buffer.from(
      bech32.fromWords(bech32.decode(to, 20000).words),
    ).toString();
  }

  let maxfee = getMaxFee(amount);
  if (lnurl) {
    amount -= maxfee;
    let { callback } = (await got(lnurl).json()) as any;
    ({ pr } = (await got(`${callback}?amount=${amount * 1000}`).json()) as any);
  } else if (to.startsWith("ln")) {
    amount -= maxfee;
    pr = to;
  }

  return pr
    ? await sendLightning({ user, pr, amount, maxfee })
    : await sendOnchain({ aid, amount, address: to, user, subtract: true });
};

export let decode = async (hex) => {
  let type, tx;
  try {
    tx = await bc.decodeRawTransaction(hex);
    type = types.bitcoin;
  } catch (e) {
    try {
      tx = await lq.decodeRawTransaction(hex);
      type = types.liquid;
    } catch (e) {
      err("invalid hex", hex);
      fail("unrecognized tx");
    }
  }

  return { tx, type };
};

let inflight = {};
export let sendOnchain = async (params) => {
  let { aid, hex, rate, user, signed } = params;
  if (!aid) aid = user.id;
  if (!hex) ({ hex } = await build(params));

  let { tx, type } = await decode(hex);
  let node =
    aid === user.id ? rpc(config[type]) : rpc({ ...config[type], wallet: aid });
  let { txid } = tx;

  try {
    if (inflight[txid]) fail("payment in flight");
    inflight[txid] = true;

    if (!signed) {
      if (config[type].walletpass)
        await node.walletPassphrase(config[type].walletpass, 300);

      ({ hex } = await node.signRawTransactionWithWallet(
        type === types.liquid ? await node.blindRawTransaction(hex) : hex,
      ));
    }

    ({ txid } = await node.decodeRawTransaction(hex));

    let r = await node.testMempoolAccept([hex]);
    if (!r[0].allowed) fail("transaction rejected");

    let total = 0;
    let fee = 0;
    let change = 0;

    if (type === types.liquid) {
      for (let {
        asset,
        scriptPubKey: { address, type },
        value,
      } of tx.vout) {
        if (asset !== config.liquid.btc) fail("only L-BTC supported");
        if (type === "fee") fee = sats(value);
        else {
          total += sats(value);

          if (address) {
            if ((await node.getAddressInfo(address)).ismine) {
              change += sats(value);
            }
          }
        }
      }
    } else {
      let totalIn = 0;
      for await (let { txid, vout } of tx.vin) {
        let hex = await node.getRawTransaction(txid);
        let tx = await node.decodeRawTransaction(hex);
        totalIn += sats(tx.vout[vout].value);
      }

      for (let {
        scriptPubKey: { address },
        value,
      } of tx.vout) {
        total += sats(value);
        let invoice = await g(`invoice:${address}`);
        if (invoice?.aid === aid) fail("Cannot send to internal address");

        if ((await node.getAddressInfo(address)).ismine) {
          change += sats(value);
        }
      }

      fee = totalIn - total;
    }

    let amount = total - change;

    let p = await debit({
      aid,
      hash: txid,
      amount,
      fee,
      rate,
      user,
      type,
    });

    p.hex = hex;
    await s(`payment:${p.id}`, p);

    await node.sendRawTransaction(hex);

    delete inflight[txid];
    return p;
  } catch (e) {
    delete inflight[txid];
    throw e;
  }
};

let getMaxFee = (n) =>
  Math.round(
    n < 100
      ? n * 5
      : n < 1000
        ? n
        : n < 10000
          ? n * 0.5
          : n < 100000
            ? n * 0.1
            : n < 1000000
              ? n * 0.05
              : n * 0.01,
  );

export let sendLightning = async ({
  user,
  pr,
  amount,
  maxfee,
  memo = undefined,
}) => {
  let p;

  if (typeof amount !== "undefined") {
    amount = parseInt(amount);
    if (amount < 0 || amount > SATS || isNaN(amount)) fail("Invalid amount");
  }

  let total = amount;
  let decoded = await ln.decode(pr);
  let { amount_msat } = decoded;
  if (amount_msat) total = Math.round(amount_msat / 1000);

  maxfee = parseInt(maxfee) || getMaxFee(total);

  if (maxfee < 0) fail("Max fee cannot be negative");

  let { pays } = await ln.listpays(pr);
  if (pays.find((p) => p.status === "complete"))
    fail("Invoice has already been paid");

  if (pays.find((p) => p.status === "pending"))
    fail("Payment is already underway");

  await db.sAdd("pending", pr);
  p = await debit({
    hash: pr,
    amount: total,
    fee: maxfee,
    memo,
    user,
    type: types.lightning,
  });

  l("paying lightning invoice", pr.substr(-8), total, amount, maxfee);

  let r;
  try {
    r = await ln.pay({
      bolt11: pr.replace(/\s/g, "").toLowerCase(),
      amount_msat: amount_msat ? undefined : amount * 1000,
      maxfee: maxfee * 1000,
      retry_for: 5,
    });

    try {
      if (r.status === "complete") {
        p = await finalize(r, p);
        await db.sRem("pending", pr);
      }
    } catch (e) {
      console.log("failed to process payment", e, p);
    }
  } catch (e) {
    await reverse(p);
    throw e;
  }

  return p;
};

export let sendInternal = async ({
  amount,
  invoice = undefined,
  recipient,
  sender,
}) => {
  if (!invoice)
    invoice = await generate({
      invoice: { amount, type: "lightning" },
      user: recipient,
    });

  let { hash } = invoice;
  let memo;

  let p = await debit({ hash, amount, memo, user: sender });
  await credit({ hash, amount, memo, ref: sender.id });
  return p;
};

let getAddressType = async (a) => {
  try {
    await bc.getAddressInfo(a);
    return types.bitcoin;
  } catch (e) {
    try {
      await lq.getAddressInfo(a);
      return types.liquid;
    } catch (e) {
      fail("unrecognized address");
    }
  }
};

export let build = async ({
  aid,
  amount,
  address,
  feeRate,
  subtract,
  user,
}) => {
  let type = await getAddressType(address);
  if (!aid) aid = user.id;
  let node =
    aid === user.id ? rpc(config[type]) : rpc({ ...config[type], wallet: aid });

  amount = parseInt(amount);
  if (amount < 0) fail("invalid amount");

  let fees: any = await fetch(`${api[type]}/fees/recommended`).then((r) =>
    r.json(),
  );

  if (!feeRate) {
    feeRate = fees.halfHourFee;
  }

  if (feeRate < fees.economyFee) fail("fee rate too low");

  let replaceable = false;

  let outs = [{ [address]: btc(amount) }];

  if (type === types.liquid)
    outs = outs.map((o) => ({ ...o, asset: config.liquid.btc }));

  let raw = await node.createRawTransaction([], outs, 0, replaceable);

  let fee = 0;
  let tx;

  try {
    tx = await node.fundRawTransaction(raw, {
      fee_rate: feeRate,
      replaceable,
      subtractFeeFromOutputs: [],
    });

    fee = sats(tx.fee);
  } catch (e) {
    if (e.message.startsWith("Insufficient")) subtract = true;
    else throw e;
  }

  let balance = await g(`balance:${aid}`);
  let ourfee = Math.round(amount * config.fee);
  let credit = await g(`credit:${type}:${aid}`);
  let covered = Math.min(credit, ourfee);
  ourfee -= covered;

  if (aid) ourfee = 0;
  if (subtract || amount + fee + ourfee > balance) {
    if (amount <= fee + ourfee + dust)
      fail(
        `insufficient funds ⚡️${balance} of ⚡️${amount + fee + ourfee + dust}`,
      );

    outs = [{ [address]: btc(amount - ourfee) }];
    raw = await node.createRawTransaction([], outs, 0, replaceable);

    tx = await node.fundRawTransaction(raw, {
      fee_rate: feeRate,
      replaceable,
      subtractFeeFromOutputs: [0],
    });

    fee = sats(tx.fee);
  }

  let inputs = [];
  let { vin } = await node.decodeRawTransaction(tx.hex);

  for (let { txid, vout } of vin) {
    let rawTx = await node.getRawTransaction(txid);
    let tx = await node.decodeRawTransaction(rawTx);
    let prevOutput = tx.vout[vout];
    let { address } = prevOutput.scriptPubKey;
    let { hdkeypath: path } = await node.getAddressInfo(address);
    let witnessUtxo = {
      amount: Math.round(prevOutput.value * SATS),
      script: prevOutput.scriptPubKey.hex,
    };
    inputs.push({ witnessUtxo, path });
  }

  return { feeRate, ourfee, fee, fees, hex: tx.hex, inputs };
};

export let catchUp = async () => {
  try {
    let txns = [];
    for (let [type, n] of Object.entries({ bitcoin: bc, liquid: lq })) {
      txns.push(
        ...(await n.listTransactions("*", 10)).filter((tx) => {
          tx.type = type;
          return tx.category === "receive" && tx.confirmations > 0;
        }),
      );
    }

    for (let { txid, type } of txns) {
      try {
        if (await db.zScore("seen", txid)) continue;
        await got.post(`http://localhost:${process.env.PORT || 3119}/confirm`, {
          json: { txid, wallet: config[type].wallet, type },
        });

        await db.zAdd("seen", { score: Date.now(), value: txid });
        if ((await db.zCard("seen")) > 10000)
          await db.zRemRangeByRank("seen", 0, 0);
      } catch (e) {
        err("problem confirming", e.message);
      }
    }
  } catch (e) {
    err("problem syncing", e.message);
  }

  setTimeout(catchUp, 10000);
};

export let reconcile = async (account, initial = false) => {
  try {
    let { descriptors, id, uid, type } = account;
    let user = await getUser(uid);
    let node = rpc({ ...config[type], wallet: id });

    let total;

    if (initial) {
      let progress = await node.scanTxOutSet("status");
      if (progress) return setTimeout(() => reconcile(account, initial), 1000);

      let { total_amount } = await node.scanTxOutSet("start", descriptors);
      total = Math.round(total_amount * SATS);
    } else {
      total = Math.round((await node.getBalance({ minconf: 1 })) * SATS);
    }

    let { balanceAdjustment: memo } = t(user);

    let balance = await g(`balance:${id}`);

    let amount = Math.abs(total - balance);
    let hash = v4();

    if (total > balance) {
      let inv = {
        memo,
        type: types.reconcile,
        hash,
        amount,
        uid,
        aid: id,
      };
      await s(`invoice:${hash}`, inv);
      await credit({
        hash,
        amount,
        type: types.reconcile,
        aid: id,
      });
    } else if (total < balance) {
      await debit({
        aid: id,
        amount,
        hash: v4(),
        memo,
        user,
        type: types.reconcile,
      });
    }
  } catch (e) {
    console.log(e);
    warn("problem reconciling", e.message, account);
    if (e.message.includes("progress"))
      return setTimeout(() => reconcile(account, initial), 1000);
  }
};

export let check = async () => {
  try {
    let payments = await db.sMembers("pending");

    for (let pr of payments) {
      let p = await getPayment(pr);
      if (!p) continue;
      let { pays } = await ln.listpays(pr);

      let failed = !pays.length || pays.every((p) => p.status === "failed");
      let completed = pays.find((p) => p.status === "complete");

      if (completed) await finalize(completed, p);
      else if (failed) await reverse(p);
    }
  } catch (e) {
    console.log("payment check failed", e);
  }

  setTimeout(check, 2000);
};

let finalize = async (r, p) => {
  await db.sRem("pending", p.hash);
  l("payment completed", p.id, r.payment_preimage);

  let maxfee = p.fee;
  p.fee = Math.round((r.amount_sent_msat - r.amount_msat) / 1000);
  p.ref = r.payment_preimage;

  await s(`payment:${p.id}`, p);

  l("refunding fee", maxfee, p.fee, maxfee - p.fee, p.ref);
  await db.incrBy(`balance:${p.uid}`, maxfee - p.fee);

  return p;
};

let reverse = async (p) => {
  await db.del(`payment:${p.hash}`);
  await db.del(`payment:${p.id}`);

  let total = Math.abs(p.amount);
  let ourfee = p.ourfee || 0;
  let credit = Math.round(total * config.fee) - ourfee;
  await db.incrBy(`balance:${p.uid}`, total + p.fee + ourfee);

  await db.incrBy(`credit:${types.lightning}:${p.uid}`, credit);
  await db.lRem(`${p.uid}:payments`, 1, p.id);
};