diff options
Diffstat (limited to 'lib/sendInternal.js')
| -rw-r--r-- | lib/sendInternal.js | 134 |
1 files changed, 57 insertions, 77 deletions
diff --git a/lib/sendInternal.js b/lib/sendInternal.js index f801c9cd..0ba0f0b4 100644 --- a/lib/sendInternal.js +++ b/lib/sendInternal.js @@ -7,12 +7,12 @@ import { notify } from "$lib/notifications"; import { l, err } from "$lib/logging"; import { fail } from "$lib/utils"; import ln from "$lib/ln"; -import { g, s, rd } from "$lib/redis"; +import { g, s, db } from "$lib/db"; const { HOSTNAME: hostname } = process.env; export default async ( - { amount, address, payreq, unconfidential, memo, username, tip }, + { amount, hash, memo, tip }, user ) => { amount = parseInt(amount); @@ -20,97 +20,77 @@ export default async ( if (!amount || amount < 0) fail("Amount must be greater than zero"); let fee = 0; - let recipient; + let invoice = await g(`invoice:${hash}`); let t = async () => { - await rd.watch(`user:${user_id}`); - user = await g(`user:${user_id}`); - if (user.balance < amount) throw new Error("Insufficient funds"); - + await db.watch(`user:${user.id}`); + user = await g(`user:${user.id}`); + if (user.balance < amount) fail("Insufficient funds"); user.balance -= amount; - let m = await rd.multi(); + let m = await db.multi(); await s(`user:${user.id}`, user); - if (username && (recipient = await g(`user:${username}`))) { - let { payment_hash: h } = await ln.decode(payreq); - let invoice = payreq && (await g(`invoice:${h}`)); + let p1 = { + hash, + amount: -amount, + memo, + uid: user.id, + rate: store.rates[user.currency], + currency: user.currency, + type: "internal", + created: Date.now() + }; + + await s(`payment:${hash}`, p1); + await db.lPush(`${user.id}:payments`, hash); + + l("sent internal", user.username, -p1.amount); + emit(user.id, "payment", p1); - let t = async () => { - await rd.watch(`user:${recipient.id}`); - recipient = await g(`user:${recipient.id}`); + let { uid } = invoice; + if (uid !== user.id) { + let recipient = await g(`user:${uid}`); + + let u = async () => { + await db.watch(`user:${uid}`); + recipient = await g(`user:${uid}`); recipient.balance += amount; - let m = await rd.multi(); - await s(`user:${recipient.id}`, recipient); - if (!(await m.exec())) t(); + let m = await db.multi(); + await s(`user:${uid}`, recipient); + if (!(await m.exec())) u(); }; - await t(); - - if (!(await m.exec())) t(); - } - await t(); + await u(); - let p2 = { - id: v4(), - amount, - user_id: recipient.id, - rate: store.rates[recipient.currency], - currency: recipient.currency, - memo, - type: "internal", - with_id: user.id, - created_at: Date.now() - }; + let p2 = { + hash, + amount, + uid: recipient.id, + rate: store.rates[recipient.currency], + currency: recipient.currency, + memo, + type: "internal", + with_id: user.id, + created: Date.now() + }; - if (invoice) { let { tip } = invoice; - params.invoice_id = invoice.id; if (tip) { - params.tip = tip; - params.amount -= tip; + p2.tip = tip; + p2.amount -= tip; } invoice.received += amount; - await s(`invoice:${h}`, invoice); - } - - if (invoice) p2.invoice = invoice; - await s(`payment:${p2.id}`, p2); - await rd.lPush(`${recipient.id}:payments`, p2.id); + await s(`invoice:${hash}`, invoice); + await s(`payment:${id}`, p2); + await db.lPush(`${recipient.id}:payments`, id); - emit(recipient.username, "payment", p2); - - l("received internal", recipient.username, amount); - notify(recipient, `Received ${amount} sats`); - callWebhook(invoice, p2); - }; + l("received internal", recipient.username, amount); + emit(recipient.username, "payment", p2); + notify(recipient, `Received ${amount} sats`); + callWebhook(invoice, p2); + } - let p1 = { - id: v4(), - amount: -amount, - memo, - user_id: user.id, - rate: store.rates[user.currency], - currency: user.currency, - with_id: recipient && recipient.id, - hash: `#${v4().substr(0, 6)} ${ - username ? `Payment to ${username}` : "Internal Transfer" - }`, - type: "internal", - created_at: Date.now() + if (!(await m.exec())) t(); }; - - if (!username) { - l("creating redeemable payment"); - params.redeemcode = v4(); - params.hash = `${hostname}/redeem/${params.redeemcode}`; - } - - await s(`payment:${p1.id}`, p1); - await rd.lPush(`${user.id}:payments`, p1.id); - - l("sent internal", user.username, -p1.amount); - - emit(user.username, "payment", p1); - - return p1; + await t(); }; |
