diff options
| author | Adam Soltys <[email protected]> | 2026-02-04 23:13:33 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-02-06 09:00:09 -0800 |
| commit | 1ebc40afc14a75d7fe157b2c6c20ffea9dedb5eb (patch) | |
| tree | 960318969072afa63536e9429c27fa40ca2919d3 /routes | |
| parent | c0481268da25eed1f25281508b5c4777d26c2b03 (diff) | |
| download | coinos-server-1ebc40afc14a75d7fe157b2c6c20ffea9dedb5eb.tar.gz coinos-server-1ebc40afc14a75d7fe157b2c6c20ffea9dedb5eb.zip | |
tigerbeetle
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/ecash.ts | 12 | ||||
| -rw-r--r-- | routes/payments.ts | 22 | ||||
| -rw-r--r-- | routes/users.ts | 39 |
3 files changed, 31 insertions, 42 deletions
diff --git a/routes/ecash.ts b/routes/ecash.ts index b7ce050e..57cc49c1 100644 --- a/routes/ecash.ts +++ b/routes/ecash.ts @@ -3,6 +3,7 @@ import { check, claim, get, mint } from "$lib/ecash"; import { err, l } from "$lib/logging"; import { credit, debit } from "$lib/payments"; import { emit } from "$lib/sockets"; +import { tbDebit } from "$lib/tb"; import { bail, fail, getInvoice } from "$lib/utils"; import { getEncodedToken } from "@cashu/cashu-ts"; import { v4 } from "uuid"; @@ -103,15 +104,16 @@ export default { const { lightning: type } = PaymentType; if (user.username !== "mint") fail("unauthorized"); const { id: uid, currency } = user; - const ourfee = await db.debit( - `balance:${uid}`, - `credit:${type}:${uid}`, - "Insufficient funds", + const ourfee = await tbDebit( + uid, + uid, + type, amount || 0, 0, 0, 0, - 0 + 0, + "Insufficient funds", ); const rates = await g("rates"); diff --git a/routes/payments.ts b/routes/payments.ts index f9225b87..5e31d8b8 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -21,6 +21,7 @@ import { sendOnchain, } from "$lib/payments"; import { emit } from "$lib/sockets"; +import { fundDebit, getBalance, getCredit, tbConfirm } from "$lib/tb"; import { PaymentType } from "$lib/types"; import { SATS, @@ -45,7 +46,7 @@ export default { const { body, user } = req; let { amount, hash, fee, fund, memo, payreq, aid } = body; - const balance = await g(`balance:${user.id}`); + const balance = await getBalance(user.id); try { if (typeof amount !== "undefined") { @@ -247,8 +248,8 @@ export default { const amount = Math.round(amount_msat / 1000); let ourfee = Math.round(amount * config.fee[PaymentType.lightning]); - const credit = await g(`credit:lightning:${user.id}`); - const covered = Math.min(credit, ourfee) || 0; + const creditBal = await getCredit(user.id, "lightning"); + const covered = Math.min(creditBal, ourfee) || 0; ourfee -= covered; res.send({ alias, amount, ourfee }); @@ -364,16 +365,7 @@ export default { const managers = await db.sMembers(`fund:${id}:managers`); if (managers.length && !managers.includes(user.id)) fail("Unauthorized"); - const result: any = await db.debit( - `fund:${id}`, - "", - "Insufficient funds", - amount, - 0, - 0, - 0, - 0, - ); + const result: any = await fundDebit(`fund:${id}`, amount, "Insufficient funds"); if (result.err) fail(result.err); const payment = await credit({ @@ -525,12 +517,12 @@ export default { l("confirming", id, p.id, p.amount); + await tbConfirm(p.aid || p.uid, p.amount); + await db .multi() .set(`invoice:${iid}`, JSON.stringify(invoice)) .set(`payment:${p.id}`, JSON.stringify(p)) - .decrBy(`pending:${p.aid || p.uid}`, p.amount) - .incrBy(`balance:${p.aid || p.uid}`, p.amount) .exec(); const user = await g(`user:${p.uid}`); diff --git a/routes/users.ts b/routes/users.ts index 3d9602cf..0a3cebeb 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -21,6 +21,7 @@ import { authenticator } from "otplib"; import { v4 } from "uuid"; import { PaymentType } from "$lib/types"; +import { createBalanceAccount, getBalance, getCredit } from "$lib/tb"; import { importAccountHistory } from "$lib/payments"; import type { ProfilePointer } from "nostr-tools/nip19"; @@ -83,7 +84,7 @@ export default { async me(req, res) { const { user } = req; try { - user.balance = await g(`balance:${user.id}`); + user.balance = await getBalance(user.id); user.locked = await ga(`balance:${user.id}`); if (user.locked) { @@ -116,16 +117,13 @@ export default { const users = []; - for await (const k of db.scanIterator({ MATCH: "balance:*" })) { - const uid = k.split(":")[1]; - const user = await getUser(uid); + for await (const k of db.scanIterator({ MATCH: "user:*" })) { + const val = await g(k); + if (!val || typeof val !== "object" || !val.id || !val.username) continue; + const uid = val.id; + const u = val; - if (!user) { - await db.del(`balance:${uid}`); - continue; - } - - user.balance = await g(k); + u.balance = await getBalance(uid); const payments = await db.lRange(`${uid}:payments`, 0, -1); @@ -139,8 +137,8 @@ export default { else total += p.tip || 0; } - user.expected = total; - users.push(user); + u.expected = total; + users.push(u); } res.send(users); @@ -822,7 +820,7 @@ export default { if (pos == null) fail("account not found"); const account = await g(`account:${id}`); - if (account) account.balance = await g(`balance:${id}`); + if (account) account.balance = await getBalance(id); res.send(account); }, @@ -837,7 +835,7 @@ export default { if (account.seed && account.pubkey && !account.importedAt) { await importAccountHistory(account); } - account.balance = await g(`balance:${id}`); + account.balance = await getBalance(id); accounts.push(account); } @@ -890,13 +888,13 @@ export default { importedAt: seed ? Date.now() : undefined, }; + await createBalanceAccount(id); + // ARK accounts don't need Bitcoin Core wallet setup if (type === "ark") { await db .multi() .set(`account:${id}`, JSON.stringify(account)) - .set(`balance:${id}`, 0) - .set(`pending:${id}`, 0) .lPush(`${user.id}:accounts`, id) .exec(); @@ -939,8 +937,6 @@ export default { await db .multi() .set(`account:${id}`, JSON.stringify(account)) - .set(`balance:${id}`, 0) - .set(`pending:${id}`, 0) .lPush(`${user.id}:accounts`, id) .exec(); @@ -988,7 +984,6 @@ export default { .multi() .lRem(`${uid}:accounts`, 1, id) .del(`account:${id}`) - .del(`balance:${id}`) .del(`${id}:payments`) .exec(); @@ -1161,9 +1156,9 @@ export default { async credits(req, res) { const { id } = req.user; - const bitcoin = await g(`credit:bitcoin:${id}`); - const lightning = await g(`credit:lightning:${id}`); - const liquid = await g(`credit:liquid:${id}`); + const bitcoin = await getCredit(id, "bitcoin"); + const lightning = await getCredit(id, "lightning"); + const liquid = await getCredit(id, "liquid"); res.send({ bitcoin, lightning, liquid }); }, |
