aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2025-02-09 23:33:32 -0800
committerAdam Soltys <[email protected]>2025-02-09 23:33:32 -0800
commit9312e1092d27714010b7942ee60b832d2a510321 (patch)
treedcc6ecfadb6371207ea938ffd058626a1460fb04 /lib
parente68c302e1082da29020d1efa932f986032ae5ce2 (diff)
downloadcoinos-server-9312e1092d27714010b7942ee60b832d2a510321.tar.gz
coinos-server-9312e1092d27714010b7942ee60b832d2a510321.zip
check nwc budget
Diffstat (limited to 'lib')
-rw-r--r--lib/nwc.ts21
1 files changed, 19 insertions, 2 deletions
diff --git a/lib/nwc.ts b/lib/nwc.ts
index 9e1ad597..1941c453 100644
--- a/lib/nwc.ts
+++ b/lib/nwc.ts
@@ -97,6 +97,23 @@ const handle = (method, params, ev, app, user) =>
const { id } = await ln.getinfo();
const amount = Math.round(amount_msat / 1000);
+ const periods = {
+ daily: 60 * 60 * 24,
+ weekly: 60 * 60 * 24 * 7,
+ monthly: 60 * 60 * 24 * 7 * 30,
+ yearly: 60 * 60 * 24 * 7 * 30 * 365,
+ never: 60 * 60 * 24 * 7 * 30 * 365 * 10,
+ };
+
+ let payments = await db.lRange(`${app.pubkey}:payments`, 0, -1);
+ payments = payments.filter(
+ (p) => p.created > Date.now() - periods[app.budget_renewal],
+ );
+
+ const spent = payments.reduce((a, b) => a + Math.abs(b.amount), 0);
+ if (spent + amount > app.max_amount)
+ fail(`Budget exceeded: ${spent + amount} of ${max_amount}`);
+
if (payee === id) {
const invoice = await getInvoice(pr);
const recipient = await g(`user:${invoice.uid}`);
@@ -111,7 +128,7 @@ const handle = (method, params, ev, app, user) =>
const preimage = pid;
if (app.pubkey !== user.pubkey)
- await db.lPush(`${pubkey}:payments`, pid);
+ await db.lPush(`${app.pubkey}:payments`, pid);
if (invoice.memo?.includes("9734")) {
const { invoices } = await ln.listinvoices({ invstring: pr });
@@ -135,7 +152,7 @@ const handle = (method, params, ev, app, user) =>
pr,
});
- if (app.pubkey !== user.pubkey) await db.lPush(`${pubkey}:payments`, pid);
+ await db.lPush(`${app.pubkey}:payments`, pid);
for (let i = 0; i < 10; i++) {
const { pays } = await ln.listpays(pr);