diff options
| author | Adam Soltys <[email protected]> | 2023-02-17 02:35:57 +0000 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-02-17 02:36:21 +0000 |
| commit | 66d005cfd01b0e27f9c954da0a4d8e326c288e04 (patch) | |
| tree | 8d6f8e6002c19d7c12e778e2c919a544592bdf38 | |
| parent | 3ad6f0557b8cbf3abdf542f582e99135e3ac8362 (diff) | |
| download | coinos-server-66d005cfd01b0e27f9c954da0a4d8e326c288e04.tar.gz coinos-server-66d005cfd01b0e27f9c954da0a4d8e326c288e04.zip | |
check transaction type
| -rw-r--r-- | lib/db.js | 9 | ||||
| -rw-r--r-- | routes/payments.js | 63 | ||||
| -rw-r--r-- | routes/users.js | 8 |
3 files changed, 43 insertions, 37 deletions
@@ -1,6 +1,7 @@ import config from "$config"; import { sleep } from "$lib/utils"; import { createClient } from "redis"; +import { warn } from "$lib/logging"; export const db = createClient({ url: config.db }); await db.connect(); @@ -14,7 +15,13 @@ export let g = async k => { return v; } }; -export let s = (k, v) => db.set(k, JSON.stringify(v)); +export let s = (k, v) => { + if (k === "user:null") { + warn("###### NULL USER #######") + console.trace(); + } + db.set(k, JSON.stringify(v)); +} let retries = {}; export let t = async (k, f) => { diff --git a/routes/payments.js b/routes/payments.js index 04ebf00f..633b94ee 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,16 +147,13 @@ 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); @@ -168,18 +165,20 @@ export default { async bitcoin({ body: { txid, wallet } }, res) { if (wallet === config.bitcoin.wallet) { let { confirmations, details } = await bc.getTransaction(txid); - 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); + 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); + } } } } @@ -197,7 +196,7 @@ export default { let tx = await bc.fundRawTransaction(raw, { feeRate, subtractFeeFromOutputs, - replaceable + replaceable, }); if (config.bitcoin.walletpass) @@ -236,7 +235,7 @@ export default { for (let { scriptPubKey: { address }, - value + value, } of tx.vout) { total += sats(value); if ( @@ -265,13 +264,13 @@ export default { "card[number]": number, "card[exp_month]": month, "card[exp_year]": year, - "card[cvc]": cvc + "card[cvc]": cvc, }; let { id: source } = await got .post(`${stripe}/tokens`, { form, - username + username, }) .json(); @@ -280,13 +279,13 @@ export default { amount, currency, source, - description: "starter coupon" + description: "starter coupon", }; let r = await got .post(`${stripe}/charges`, { form, - username + username, }) .json(); @@ -296,7 +295,7 @@ export default { let memo = "stripe"; await s(`invoice:${hash}`, { uid: user.id, - received: 0 + received: 0, }); amount = sats(amount / store.rates[currency]); let uid = await g("user:coinos"); @@ -306,5 +305,5 @@ export default { } else fail("Card payment failed"); res.send({ status }); - } + }, }; diff --git a/routes/users.js b/routes/users.js index 1c177fef..91564aaa 100644 --- a/routes/users.js +++ b/routes/users.js @@ -388,11 +388,11 @@ export default { }, async lower(req, res) { - for await (let k of db.scanIterator({ MATCH: "balance:*" })) { - let b = await g(k); - if (b < 0) console.log(k, b); + for await (let k of db.scanIterator({ MATCH: "payment:*" })) { + let p = await g(k); + if (p.ref && p.ref.startsWith('2dad97')) console.log(k, p); } res.send("ok"); - } + }, }; |
