{ "name": "coinos", "version": "0.0.1", "private": true, "scripts": { "start": "node app.js", "postinstall": "cp config.js.sample config.js" }, "dependencies": { "async": "0.9.0", "bcrypt": "0.7.8", "body-parser": "^1.5.0", "coffee-script": "1.7.1", "connect-assets": "4.0.1", "connect-redis": "2.0.0", "consolidate": "0.10.0", "cookie-parser": "^1.3.2", "express": "4.6.1", "express-session": "^1.6.5", "hogan-express": "0.5.2", "less": "1.7.3", "passport": "0.2.0", "passport-local": "1.0.0", "redis": "0.11.0", "request": "~2.40.0", "nodemailer": "~1.2.1", "sendgrid": "~1.2.0" }, "devDependencies": { "grunt": "^0.4.5", "grunt-contrib-coffee": "^0.10.1", "grunt-contrib-less": "^0.11.3", "grunt-contrib-watch": "^0.6.1" } } 0fb3f439'/> coinos server — legacy coinos.io backend (Lightning, on-chain, Liquid)coinos
aboutsummaryrefslogblamecommitdiffstats
path: root/lib/spend-budget.ts
blob: 3f2a70ac530bc6bf0c1067d3768f8356f284b990 (plain) (tree)






































                                                                                 
const number = (value: unknown) => {
  const parsed = Number(value);
  return Number.isFinite(parsed) ? parsed : 0;
};

export const debitTotal = ({
  amount,
  tip = 0,
  fee = 0,
  ourfee = 0,
}: {
  amount: unknown;
  tip?: unknown;
  fee?: unknown;
  ourfee?: unknown;
}) => number(amount) + number(tip) + number(fee) + number(ourfee);

// Enforce a caller-specific spending allowance at debit's final choke point,
// after mutable invoice metadata (notably an internal invoice tip) and fees have
// been captured. Checking earlier lets a recipient mutate the tip between the
// NWC budget check and the ledger debit.
export const assertWithinSpendLimit = ({
  amount,
  tip = 0,
  fee = 0,
  ourfee = 0,
  maxTotal,
}: {
  amount: unknown;
  tip?: unknown;
  fee?: unknown;
  ourfee?: unknown;
  maxTotal?: number;
}) => {
  const total = debitTotal({ amount, tip, fee, ourfee });
  if (maxTotal !== undefined && total > maxTotal)
    throw new Error(`Budget exceeded: ${total} of ${maxTotal} remaining`);
  return total;
};