aboutsummaryrefslogtreecommitdiffstats
path: root/lib/payments.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-26 11:26:05 -0800
committerAdam Soltys <[email protected]>2023-01-26 11:26:09 -0800
commitf0543b6d94081f43a265bb9f754a654df9c4268a (patch)
treec2fee79d8e410a2b6914f2dd53d7abef19497e39 /lib/payments.js
parent143490db49b6d67e9ddb2c59ea92ae923d08911c (diff)
downloadcoinos-server-f0543b6d94081f43a265bb9f754a654df9c4268a.tar.gz
coinos-server-f0543b6d94081f43a265bb9f754a654df9c4268a.zip
sample notify script
Diffstat (limited to 'lib/payments.js')
-rw-r--r--lib/payments.js31
1 files changed, 24 insertions, 7 deletions
diff --git a/lib/payments.js b/lib/payments.js
index 386d84d8..d0593447 100644
--- a/lib/payments.js
+++ b/lib/payments.js
@@ -34,16 +34,30 @@ export let debit = async (
if (!amount || amount < 0) fail("Amount must be greater than zero");
- await t(`balance:${uid}`, async balance => {
- await new Promise(r => setTimeout(r, 100));
- if (balance < amount) fail("Insufficient funds");
- return balance - amount;
+ let ourfee = Math.round(amount * config.fee);
+ await t(`balance:${uid}`, async (balance, db) => {
+ await t(`credit:${type}:${uid}`, async (credit, db) => {
+ let covered = Math.min(credit, ourfee) || 0;
+ ourfee -= covered;
+
+ if (balance < amount + ourfee) fail("Insufficient funds");
+ await db
+ .multi()
+ .decrBy(`credit:${type}:${uid}`, covered)
+ .exec();
+ });
+
+ await db
+ .multi()
+ .decrBy(`balance:${uid}`, amount + (ourfee - credit))
+ .exec();
});
let p = {
id,
amount: -amount,
fee,
+ ourfee,
memo,
uid,
confirmed: true,
@@ -103,9 +117,12 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => {
p.id = hash;
}
- await db
- .multi()
- .set(`invoice:${hash}`, JSON.stringify(invoice))
+ let m = await db.multi();
+
+ if ([types.bitcoin, types.lightning].includes(type))
+ m.incrBy(`credit:${type}:${uid}`, Math.round(amount * config.fee));
+
+ m.set(`invoice:${hash}`, JSON.stringify(invoice))
.set(`payment:${p.id}`, JSON.stringify(p))
.lPush(`${uid}:payments`, p.id)
.incrBy(`${balance}:${uid}`, amount)