From 60cf57d56f617998e093e81b229e18c9a221e312 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 10 Feb 2023 23:36:34 -0800 Subject: prettier --- lib/payments.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/payments.js') diff --git a/lib/payments.js b/lib/payments.js index 2c0eb0c0..e946e41e 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -13,7 +13,7 @@ export const types = { bitcoin: "bitcoin", lightning: "lightning", pot: "pot", - classic: "classic", + classic: "classic" }; export let debit = async ( @@ -66,7 +66,7 @@ export let debit = async ( currency, type, ref, - created: Date.now(), + created: Date.now() }; await s(`payment:${id}`, p); @@ -98,7 +98,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { ref, type, confirmed: true, - created: Date.now(), + created: Date.now() }; let { tip } = invoice; -- cgit v1.2.3 From b0c778addeeeb72181c7f2f431004c8b9b42aa35 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 15 Feb 2023 22:52:01 -0800 Subject: buy endpoint --- lib/payments.js | 4 +-- routes/payments.js | 96 ++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 62 insertions(+), 38 deletions(-) (limited to 'lib/payments.js') diff --git a/lib/payments.js b/lib/payments.js index e946e41e..657590ee 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -4,7 +4,7 @@ import { emit } from "$lib/sockets"; import { v4 } from "uuid"; import { notify } from "$lib/notifications"; import { db, g, s, t } from "$lib/db"; -import { l, err } from "$lib/logging"; +import { l, err, warn } from "$lib/logging"; import { fail } from "$lib/utils"; import { callWebhook } from "$lib/webhooks"; @@ -81,7 +81,7 @@ export let debit = async ( export let credit = async (hash, amount, memo, ref, type = types.internal) => { amount = parseInt(amount); let invoice = await g(`invoice:${hash}`); - if (!invoice) return; + if (!invoice) return warn("failed to credit", ref, ", invoice not found"); if (!memo) ({ memo } = invoice); let user = await g(`user:${invoice.uid}`); diff --git a/routes/payments.js b/routes/payments.js index 6619c031..a120ec33 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -32,7 +32,7 @@ export default { await got .post(`${config.classic}/admin/credit`, { json: { username, amount, source }, - headers: { authorization: `Bearer ${config.admin}` }, + headers: { authorization: `Bearer ${config.admin}` } }) .json(); } else if (payreq) { @@ -88,7 +88,7 @@ export default { let payments = (await db.lRange(`${id}:payments`, 0, -1)) || []; payments = ( await Promise.all( - payments.map(async (id) => { + payments.map(async id => { let p = await g(`payment:${id}`); if (p.created < start || p.created > end) return; if (p.type === types.internal) p.with = await g(`user:${p.ref}`); @@ -96,7 +96,7 @@ export default { }) ) ) - .filter((p) => p) + .filter(p => p) .sort((a, b) => b.created - a.created); let total = payments.length; @@ -123,7 +123,7 @@ export default { let twoWeeksAgo = new Date(new Date().setDate(new Date().getDate() - 14)); let decoded = await ln.decodepay(payreq); let { msatoshi, payee } = decoded; - let node = nodes.find((n) => n.nodeid === payee); + let node = nodes.find(n => n.nodeid === payee); let alias = node ? node.alias : payee.substr(0, 12); res.send({ alias, amount: Math.round(msatoshi / 1000) }); @@ -133,13 +133,13 @@ export default { let amount = await g(`pot:${name}`); if (!amount) fail("pot not found"); let payments = (await db.lRange(`pot:${name}:payments`, 0, -1)) || []; - payments = await Promise.all(payments.map((hash) => g(`payment:${hash}`))); + payments = await Promise.all(payments.map(hash => g(`payment:${hash}`))); await Promise.all( - payments.map(async (p) => (p.user = await g(`user:${p.uid}`))) + payments.map(async p => (p.user = await g(`user:${p.uid}`))) ); - payments = payments.filter((p) => p); + payments = payments.filter(p => p); res.send({ amount, payments }); }, @@ -147,13 +147,16 @@ export default { amount = parseInt(amount); await t(`pot:${name}`, async (balance, db) => { if (balance < amount) fail("Insufficient funds"); - await db.multi().decrBy(`pot:${name}`, amount).exec(); + await db + .multi() + .decrBy(`pot:${name}`, amount) + .exec(); }); let hash = v4(); await s(`invoice:${hash}`, { uid: user.id, - received: 0, + received: 0 }); let payment = await credit(hash, amount, "", name, types.pot); @@ -194,7 +197,7 @@ export default { let tx = await bc.fundRawTransaction(raw, { feeRate, subtractFeeFromOutputs, - replaceable, + replaceable }); if (config.bitcoin.walletpass) @@ -233,7 +236,7 @@ export default { for (let { scriptPubKey: { address }, - value, + value } of tx.vout) { total += sats(value); if ( @@ -254,33 +257,54 @@ export default { res.send({ txid }); }, - async buy({ body: { amount, number, month, year, cvc } }, res) { + async buy({ body: { amount, number, year, month, cvc }, user }, res) { let stripe = "https://api.stripe.com/v1"; let { STRIPE: username } = process.env; - let { id: source } = await got.post(`${stripe}/tokens`, { - form: { - "card[number]": number, - "card[exp_month]": month, - "card[exp_year]": year, - "card[cvc]": cvc - }, - username - }); - - let { status } = await got.post(`${stripe}/charges`, { - form: { - amount, - currency: "cad", - source, - description: "starter coupon" - }, - username - }); - - if (status !== "succeeded") - throw error(500, { message: "Card payment failed" }); - - res.send(status); + let form = { + "card[number]": number, + "card[exp_month]": month, + "card[exp_year]": year, + "card[cvc]": cvc + }; + + let { id: source } = await got + .post(`${stripe}/tokens`, { + form, + username + }) + .json(); + + let currency = "CAD"; + form = { + amount, + currency, + source, + description: "starter coupon" + }; + + let r = await got + .post(`${stripe}/charges`, { + form, + username + }) + .json(); + + let { status } = r; + if (status === "succeeded") { + let hash = r.id; + let memo = "stripe"; + await s(`invoice:${hash}`, { + uid: user.id, + received: 0 + }); + amount = sats(amount / store.rates[currency]); + let uid = await g("user:coinos"); + let coinos = await g(`user:${uid}`); + let p = await debit(hash, amount, 0, memo, coinos); + await credit(hash, amount, memo, coinos.id); + } else fail("Card payment failed"); + + res.send({ status }); } }; -- cgit v1.2.3 From 38aa5fa9952bfce66e37fc0c92216bd477aba574 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 16 Feb 2023 19:40:47 -0800 Subject: messages fix --- index.js | 2 +- lib/payments.js | 2 +- routes/nostr.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/payments.js') diff --git a/index.js b/index.js index 8189144d..36cdb0b4 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,7 @@ app.get("/nostr.json", nostr.identities); app.get("/:pubkey/followers", nostr.followers); app.get("/:pubkey/follows", nostr.follows); app.get("/:pubkey/notes", nostr.notes); -app.get("/:a/:b/:since/messages", nostr.messages); +app.get("/:pubkey/:since/messages", nostr.messages); app.get("/event/:id", nostr.event); app.post("/event", nostr.broadcast); diff --git a/lib/payments.js b/lib/payments.js index 657590ee..89652ff4 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -81,7 +81,7 @@ export let debit = async ( export let credit = async (hash, amount, memo, ref, type = types.internal) => { amount = parseInt(amount); let invoice = await g(`invoice:${hash}`); - if (!invoice) return warn("failed to credit", ref, ", invoice not found"); + if (!invoice) return warn("failed to credit", hash, ", invoice not found"); if (!memo) ({ memo } = invoice); let user = await g(`user:${invoice.uid}`); diff --git a/routes/nostr.js b/routes/nostr.js index ad9f55aa..47ceaa25 100644 --- a/routes/nostr.js +++ b/routes/nostr.js @@ -70,7 +70,7 @@ export default { messages = await Promise.all( messages.map(async id => { let m = await g(`ev:${id}`); - let k = m.pubkey === pubkey ? m.tags[0].p : m.pubkey; + let k = m.pubkey === pubkey ? m.tags[0][1] : m.pubkey; let uid = await g(`user:${k}`); m.user = await g(`user:${uid}`); return m; -- cgit v1.2.3 From 797d10c9322b2de3ec14fa85689ae70435e98c8f Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 23 Feb 2023 15:47:16 -0800 Subject: fix fee subtraction --- lib/payments.js | 18 +++++++++++++----- routes/payments.js | 49 +++++++++++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 27 deletions(-) (limited to 'lib/payments.js') diff --git a/lib/payments.js b/lib/payments.js index 89652ff4..aaf39210 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -25,9 +25,11 @@ export let debit = async ( type = types.internal, id = v4() ) => { - let ref; + let ref, tip; let invoice = await g(`invoice:${hash}`); - if (invoice) ref = invoice.uid; + if (invoice) { + ref = invoice.uid; + } let { id: uid, currency, username } = user; @@ -44,16 +46,21 @@ export let debit = async ( let covered = Math.min(credit, ourfee) || 0; ourfee -= covered; - l("debiting", user.username, balance, amount, ourfee, amount + ourfee); - if (balance < amount + ourfee) fail("Insufficient funds"); + l("debiting", user.username, balance, amount, fee, ourfee, amount + fee + ourfee); + if (balance < amount + fee + ourfee) fail("Insufficient funds"); await db .multi() .decrBy(`credit:${type}:${uid}`, covered) - .decrBy(`balance:${uid}`, amount + ourfee) + .decrBy(`balance:${uid}`, amount + fee + ourfee) .exec(); }); }); + if (invoice?.tip) { + tip = invoice.tip; + amount -= tip; + } + let p = { id, amount: -amount, @@ -66,6 +73,7 @@ export let debit = async ( currency, type, ref, + tip, created: Date.now() }; diff --git a/routes/payments.js b/routes/payments.js index ea711cb1..38611d19 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -14,11 +14,10 @@ import ln from "$lib/ln"; export default { async create({ body, user }, res) { - let { amount, hash, maxfee, name, memo, payreq, tip, username } = body; + let { amount, hash, maxfee, name, memo, payreq, username } = body; amount = parseInt(amount); maxfee = maxfee ? parseInt(maxfee) : 0; - tip = parseInt(tip); await requirePin({ body, user }); @@ -181,20 +180,19 @@ export default { try { if (wallet === config.bitcoin.wallet) { let { confirmations, details } = await bc.getTransaction(txid); - if (details.category === "receive") { - for (let { address, amount, vout } of details) { - let p = await g(`payment:${txid}:${vout}`); - if (!p || confirmations < 1) { - await credit( - address, - sats(amount), - "", - `${txid}:${vout}`, - types.bitcoin - ); - } else { - await confirm(address, txid, vout); - } + for (let { address, amount, category, vout } of details) { + if (category !== "receive") continue; + let p = await g(`payment:${txid}:${vout}`); + if (!p || confirmations < 1) { + await credit( + address, + sats(amount), + "", + `${txid}:${vout}`, + types.bitcoin + ); + } else { + await confirm(address, txid, vout); } } } @@ -205,14 +203,17 @@ export default { } }, - async fee(req, res) { + async fee({ body: { amount, address, feeRate, subtract }, user }, res) { try { - let { amount, address, feeRate, subtract } = req.body; - let subtractFeeFromOutputs = subtract ? [0] : []; let replaceable = true; - let outs = [{ [address]: btc(amount) }]; - let count = await bc.getBlockCount(); + + let ourfee = Math.round(amount * config.fee); + let credit = await g(`credit:bitcoin:${user.id}`); + let covered = Math.min(credit, ourfee) || 0; + ourfee -= covered; + + if (subtract) amount -= ourfee; let { feerate: min } = await bc.estimateSmartFee(40); let { feerate: max } = await bc.estimateSmartFee(1); @@ -220,6 +221,7 @@ export default { if (feeRate) feeRate = btc(feeRate); else feeRate = max; + let outs = [{ [address]: btc(amount) }]; let raw = await bc.createRawTransaction([], outs, 0, replaceable); let tx = await bc.fundRawTransaction(raw, { @@ -236,6 +238,7 @@ export default { res.send({ feeRate, min, max, fee, tx }); } catch (e) { + console.log(e); bail(res, "problem estimating fee"); } }, @@ -245,7 +248,7 @@ export default { await requirePin(req); let { user } = req; - let { address, memo, tx } = req.body; + let { address, memo, tx, subtract } = req.body; let { hex, fee } = tx; if (config.bitcoin.walletpass) @@ -280,6 +283,8 @@ export default { total = total - change + fee; let amount = total - fee; + console.log("AMOUNT", amount); + if (change && !amount) fail("Cannot send to unregistered coinos address"); await debit(txid, amount, fee, null, user, types.bitcoin, txid); -- cgit v1.2.3