diff options
| author | Adam Soltys <[email protected]> | 2023-12-28 22:50:56 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-12-28 22:51:02 -0800 |
| commit | cb38551cc4ec392911d18dda9b5a9fe97087ad61 (patch) | |
| tree | 939808cef6a568dbe86ed8b2ba6b95fcc2fe9178 | |
| parent | 73a36051979e7257d9198bed976128e8cea5235d (diff) | |
| download | coinos-server-cb38551cc4ec392911d18dda9b5a9fe97087ad61.tar.gz coinos-server-cb38551cc4ec392911d18dda9b5a9fe97087ad61.zip | |
liquid
| -rw-r--r-- | lib/invoices.js | 5 | ||||
| -rw-r--r-- | lib/migrate.js | 2 | ||||
| -rw-r--r-- | lib/payments.js | 13 | ||||
| -rw-r--r-- | routes/payments.js | 27 |
4 files changed, 36 insertions, 11 deletions
diff --git a/lib/invoices.js b/lib/invoices.js index b5718e23..8023bdbb 100644 --- a/lib/invoices.js +++ b/lib/invoices.js @@ -86,6 +86,11 @@ export let generate = async ({ invoice, user, sender, memo }) => { if (!user.username === 'lightningins') l(type, "invoice", user.username, amount); + if (type === "liquid") { + let { unconfidential } = await lq.getAddressInfo(hash); + await s(`invoice:${unconfidential}`, id); + } + await s(`invoice:${hash}`, id); await s(`invoice:${id}`, invoice); await db.lPush(`${user.id}:invoices`, id); diff --git a/lib/migrate.js b/lib/migrate.js index 81743934..b2c094e3 100644 --- a/lib/migrate.js +++ b/lib/migrate.js @@ -16,7 +16,7 @@ async function migrate(id) { .set(`user:${user.username}`, id) .set(`user:${user.pubkey}`, id) .set(`user:${id}`, JSON.stringify(user)) - .set(`${id}:contacts`, await archive.get(`${id}:contacts`) || '[]') + .set(`${id}:contacts`, (await archive.get(`${id}:contacts`)) || "[]") .set( `credit:bitcoin:${id}`, (await archive.get(`credit:bitcoin:${id}`)) || 0, diff --git a/lib/payments.js b/lib/payments.js index e0aff192..03daf93e 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -5,7 +5,7 @@ import { v4 } from "uuid"; import { notify } from "$lib/notifications"; import { db, g, s, t } from "$lib/db"; import { l, err, warn } from "$lib/logging"; -import { fail, wait, SATS } from "$lib/utils"; +import { fail, sleep, wait, SATS } from "$lib/utils"; import { callWebhook } from "$lib/webhooks"; import got from "got"; import { spawn } from "child_process"; @@ -61,7 +61,7 @@ export let debit = async ({ if (!amount || amount < 0) fail("Amount must be greater than zero"); - let ourfee = [types.bitcoin, types.lightning].includes(type) + let ourfee = [types.bitcoin, types.liquid, types.lightning].includes(type) ? Math.round(amount * config.fee) : 0; @@ -128,6 +128,9 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { let rate = store.rates[currency]; + if (!rate) await sleep(1000); + rate = store.rates[currency]; + let equivalentRate = invoice.rate * (store.rates[currency] / store.rates[invoice.currency]); @@ -153,11 +156,11 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { created: Date.now(), }; - if (type === types.bitcoin) invoice.pending += amount; + if ([types.bitcoin, types.liquid].includes(type)) invoice.pending += amount; else invoice.received += amount; let balance = "balance"; - if (type === types.bitcoin) { + if ([types.bitcoin, types.liquid].includes(type)) { let [txid, vout] = ref.split(":").slice(-2); p.confirmed = false; balance = "pending"; @@ -168,7 +171,7 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { let m = await db.multi(); - if ([types.bitcoin, types.lightning].includes(type)) + if ([types.bitcoin, types.liquid, types.lightning].includes(type)) m.incrBy(`credit:${type}:${uid}`, Math.round(amount * config.fee)); m.set(`invoice:${iid}`, JSON.stringify(invoice)) diff --git a/routes/payments.js b/routes/payments.js index f4846a64..a0bff656 100644 --- a/routes/payments.js +++ b/routes/payments.js @@ -16,13 +16,21 @@ import ln from "$lib/ln"; let seen = await db.sMembers("missing"); let catchUp = async () => { - let txns = await bc.listTransactions("*", 50); - txns = txns.filter((tx) => tx.category === "receive" && tx.confirmations > 0); - for (let { txid } of txns) { + let txns = []; + for (let [type, n] of Object.entries({ bitcoin: bc, liquid: lq })) { + txns.push( + ...(await n.listTransactions("*", 50)).filter((tx) => { + tx.type = type; + return tx.category === "receive" && tx.confirmations > 0; + }), + ); + } + + for (let { txid, type } of txns) { try { if (seen.includes(txid)) continue; await got.post(`http://localhost:${process.env.PORT || 3119}/confirm`, { - json: { txid, wallet: config.bitcoin.wallet, type: types.bitcoin }, + json: { txid, wallet: config.bitcoin.wallet, type }, }); seen.push(txid); @@ -240,6 +248,7 @@ export default { if (wallet === config.bitcoin.wallet) { let { confirmations, details } = await node.getTransaction(txid); for (let { address, amount, category, vout } of details) { + if (!address) continue; if (category !== "receive") continue; let p = await g(`payment:${txid}:${vout}`); if (typeof p === "string") p = await g(`payment:${p}`); @@ -253,17 +262,25 @@ export default { if (!p) return db.sAdd("missed", id); if (p.confirmed) return; + let iid = await g(`invoice:${address}`); + if (iid && iid.hash) iid = iid.hash; + let invoice = await g(`invoice:${iid}`); + p.confirmed = true; - emit(p.uid, "payment", p); + invoice.received += parseInt(invoice.pending); + invoice.pending = 0; l("confirming", txid); let r = await db .multi() + .set(`invoice:${iid}`, JSON.stringify(invoice)) .set(`payment:${p.id}`, JSON.stringify(p)) .decrBy(`pending:${p.uid}`, p.amount) .incrBy(`balance:${p.uid}`, p.amount) .exec(); + + emit(p.uid, "payment", p); } } } |
