aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-09-23 16:23:19 -0700
committerAdam Soltys <[email protected]>2024-09-23 16:23:19 -0700
commit12ad5c9abb74cbe35c5d07233fdd9a7b7f5c2b70 (patch)
tree8e023771abd75a7b70e407c651aed3bcfe426ea3 /lib
parent01e1686265def675445c0f74a4b92ec4ffd71492 (diff)
downloadcoinos-server-12ad5c9abb74cbe35c5d07233fdd9a7b7f5c2b70.tar.gz
coinos-server-12ad5c9abb74cbe35c5d07233fdd9a7b7f5c2b70.zip
reconcile on-chain balance
Diffstat (limited to 'lib')
-rw-r--r--lib/invoices.ts5
-rw-r--r--lib/migrate.ts21
-rw-r--r--lib/payments.ts34
3 files changed, 23 insertions, 37 deletions
diff --git a/lib/invoices.ts b/lib/invoices.ts
index 4626e257..60bcba0f 100644
--- a/lib/invoices.ts
+++ b/lib/invoices.ts
@@ -30,13 +30,16 @@ export let generate = async ({ invoice, user }) => {
secret,
} = invoice;
- let account = await g(`account:${aid}`);
amount = parseInt(amount || 0);
tip = parseInt(tip) || null;
if (user) user = await getUser(user.username);
if (!user) fail("user not provided");
+ let account = await g(`account:${aid}`);
+ if (!account) account = await g(`account:${user.id}`);
+ if (!account) fail("account not found");
+
let rates = await g("rates");
if (!currency) currency = user.currency;
if (!rate) rate = rates[currency];
diff --git a/lib/migrate.ts b/lib/migrate.ts
index d26c8581..1ff280ea 100644
--- a/lib/migrate.ts
+++ b/lib/migrate.ts
@@ -12,26 +12,7 @@ async function migrate(id) {
let user = await g(`user:${id}`);
if (typeof user === "string") user = await g(`user:${user}`);
-
- if (user) {
- let k = `lock:${user.id}:accounts`;
- const lock = await db.set(k, "1", { NX: true, PX: 5000 });
- if (!lock) return user;
- try {
- let accounts = await db.lLen(`${user.id}:accounts`);
-
- if (!accounts) {
- await db.set(
- `account:${user.id}`,
- JSON.stringify({ id: user.id, name: "Cash", type: types.ecash }),
- );
- await db.lPush(`${user.id}:accounts`, user.id);
- }
- return user;
- } finally {
- await db.del(k);
- }
- }
+ if (user) return user;
user = await ga(`user:${id}`);
diff --git a/lib/payments.ts b/lib/payments.ts
index e2b44ea9..4bec9a93 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -40,6 +40,7 @@ export let types = {
fund: "fund",
liquid: "liquid",
ecash: "ecash",
+ reconcile: "reconcile",
};
export let debit = async ({
@@ -140,8 +141,8 @@ export let debit = async ({
export let credit = async ({
hash,
amount,
- memo = undefined,
- ref = undefined,
+ memo = "",
+ ref = "",
type = types.internal,
aid = undefined,
}) => {
@@ -369,9 +370,7 @@ export let sendOnchain = async (params) => {
let { tx, type } = await decode(hex);
let node =
- aid === user.id
- ? rpc(config[type])
- : rpc({ ...config[type], wallet: aid });
+ aid === user.id ? rpc(config[type]) : rpc({ ...config[type], wallet: aid });
let { txid } = tx;
try {
@@ -428,8 +427,7 @@ export let sendOnchain = async (params) => {
} of tx.vout) {
total += sats(value);
let invoice = await g(`invoice:${address}`);
- if (invoice?.aid === aid)
- fail("Cannot send to internal address");
+ if (invoice?.aid === aid) fail("Cannot send to internal address");
if ((await node.getAddressInfo(address)).ismine) {
change += sats(value);
@@ -604,12 +602,18 @@ export let sendLightning = async ({
}
};
-export let sendInternal = async ({ amount, invoice = undefined, recipient, sender }) => {
- if (!invoice) invoice = await generate({
- invoice: { amount, type: "lightning" },
- user: recipient,
- sender,
- });
+export let sendInternal = async ({
+ amount,
+ invoice = undefined,
+ recipient,
+ sender,
+}) => {
+ if (!invoice)
+ invoice = await generate({
+ invoice: { amount, type: "lightning" },
+ user: recipient,
+ sender,
+ });
let { hash } = invoice;
let memo;
@@ -644,9 +648,7 @@ export let build = async ({
let type = await getAddressType(address);
if (!aid) aid = user.id;
let node =
- aid === user.id
- ? rpc(config[type])
- : rpc({ ...config[type], wallet: aid });
+ aid === user.id ? rpc(config[type]) : rpc({ ...config[type], wallet: aid });
amount = parseInt(amount);
if (amount < 0) fail("invalid amount");