aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-02-04 14:05:09 -0800
committerAdam Soltys <[email protected]>2026-02-06 08:59:46 -0800
commitc0481268da25eed1f25281508b5c4777d26c2b03 (patch)
treef04cd4feacd920029dc5cfcb145a9debe329cad6 /lib
parent384c4778ca5421c091a7d282b90b00965f3596cd (diff)
downloadcoinos-server-c0481268da25eed1f25281508b5c4777d26c2b03.tar.gz
coinos-server-c0481268da25eed1f25281508b5c4777d26c2b03.zip
remove reconcile payments
Diffstat (limited to 'lib')
-rw-r--r--lib/locales/ar.json13
-rw-r--r--lib/locales/bn.json13
-rw-r--r--lib/locales/de.json13
-rw-r--r--lib/locales/el.json13
-rw-r--r--lib/locales/en.json11
-rw-r--r--lib/locales/es.json13
-rw-r--r--lib/locales/fa.json13
-rw-r--r--lib/locales/fr.json13
-rw-r--r--lib/locales/hi.json13
-rw-r--r--lib/locales/it.json13
-rw-r--r--lib/locales/ja.json13
-rw-r--r--lib/locales/ko.json13
-rw-r--r--lib/locales/nl.json13
-rw-r--r--lib/locales/pl.json13
-rw-r--r--lib/locales/pt.json13
-rw-r--r--lib/locales/ru.json13
-rw-r--r--lib/locales/th.json13
-rw-r--r--lib/locales/tr.json13
-rw-r--r--lib/locales/zh.json13
-rw-r--r--lib/payments.ts158
-rw-r--r--lib/types.ts1
21 files changed, 236 insertions, 168 deletions
diff --git a/lib/locales/ar.json b/lib/locales/ar.json
index 9b1d2e2b..8b52b007 100644
--- a/lib/locales/ar.json
+++ b/lib/locales/ar.json
@@ -1,9 +1,8 @@
{
- "balanceAdjustment": "تسوية الرصيد",
- "paymentReceived": "المدفوعات الواردة",
- "hello": "مرحبا",
- "youReceived": "لقد حصلت على دفعة",
- "withdrawalTriggered": "هذا أدى إلى سحب",
- "problemWithdrawing": "كان هناك مشكلة إرسال إلى عنوان سحب السيارة",
- "insufficientFunds": "عدم كفاية الأموال"
+ "paymentReceived": "المدفوعات الواردة",
+ "hello": "مرحبا",
+ "youReceived": "لقد حصلت على دفعة",
+ "withdrawalTriggered": "هذا أدى إلى سحب",
+ "problemWithdrawing": "كان هناك مشكلة إرسال إلى عنوان سحب السيارة",
+ "insufficientFunds": "عدم كفاية الأموال"
}
diff --git a/lib/locales/bn.json b/lib/locales/bn.json
index c0c1f568..0e1a80da 100644
--- a/lib/locales/bn.json
+++ b/lib/locales/bn.json
@@ -1,9 +1,8 @@
{
- "balanceAdjustment": "মাপে পরিবর্তন",
- "paymentReceived": "মূল্য প্রদান করা হয়েছে",
- "hello": "হ্যালো",
- "youReceived": "আপনি পেমেন্ট পেয়েছিলাম",
- "withdrawalTriggered": "এটা প্রত্যাহারের সূত্রপাত ঘটিয়েছে",
- "problemWithdrawing": "আপনার অটো-রান ঠিকানায় প্রেরিত হয়েছে",
- "insufficientFunds": "অপর্যাপ্ত অর্থ"
+ "paymentReceived": "মূল্য প্রদান করা হয়েছে",
+ "hello": "হ্যালো",
+ "youReceived": "আপনি পেমেন্ট পেয়েছিলাম",
+ "withdrawalTriggered": "এটা প্রত্যাহারের সূত্রপাত ঘটিয়েছে",
+ "problemWithdrawing": "আপনার অটো-রান ঠিকানায় প্রেরিত হয়েছে",
+ "insufficientFunds": "অপর্যাপ্ত অর্থ"
}
diff --git a/lib/locales/de.json b/lib/locales/de.json
index b6d101e2..c12119b4 100644
--- a/lib/locales/de.json
+++ b/lib/locales/de.json
@@ -1,9 +1,8 @@
{
- "hello": "Hallo",
- "youReceived": "Sie haben eine Zahlung von erhalten",
- "withdrawalTriggered": "Dies löste einen Rückzug von aus",
- "problemWithdrawing": "Beim Senden an Ihre automatische Auszahlungsadresse ist ein Problem aufgetreten",
- "paymentReceived": "Zahlung erhalten",
- "balanceAdjustment": "Bilanzanpassung",
- "insufficientFunds": "Unzureichende Mittel"
+ "hello": "Hallo",
+ "youReceived": "Sie haben eine Zahlung von erhalten",
+ "withdrawalTriggered": "Dies löste einen Rückzug von aus",
+ "problemWithdrawing": "Beim Senden an Ihre automatische Auszahlungsadresse ist ein Problem aufgetreten",
+ "paymentReceived": "Zahlung erhalten",
+ "insufficientFunds": "Unzureichende Mittel"
}
diff --git a/lib/locales/el.json b/lib/locales/el.json
index e3451349..9ababb1a 100644
--- a/lib/locales/el.json
+++ b/lib/locales/el.json
@@ -1,9 +1,8 @@
{
- "hello": "Γειά σου",
- "youReceived": "Λάβατε μια πληρωμή του",
- "withdrawalTriggered": "Αυτό προκάλεσε την απόσυρση του",
- "problemWithdrawing": "Παρουσιάστηκε πρόβλημα κατά την αποστολή στη διεύθυνση αυτόματης απόσυρσής σας",
- "paymentReceived": "Η πληρωμή ελήφθη",
- "balanceAdjustment": "Προσαρμογή υπολοίπου",
- "insufficientFunds": "Ανεπαρκή κεφάλαια"
+ "hello": "Γειά σου",
+ "youReceived": "Λάβατε μια πληρωμή του",
+ "withdrawalTriggered": "Αυτό προκάλεσε την απόσυρση του",
+ "problemWithdrawing": "Παρουσιάστηκε πρόβλημα κατά την αποστολή στη διεύθυνση αυτόματης απόσυρσής σας",
+ "paymentReceived": "Η πληρωμή ελήφθη",
+ "insufficientFunds": "Ανεπαρκή κεφάλαια"
}
diff --git a/lib/locales/en.json b/lib/locales/en.json
index fbe8e851..a1c38577 100644
--- a/lib/locales/en.json
+++ b/lib/locales/en.json
@@ -1,9 +1,8 @@
{
- "balanceAdjustment": "Balance adjustment",
- "paymentReceived": "Payment received",
- "hello": "Hello",
- "youReceived": "You received a payment of",
- "withdrawalTriggered": "This triggered a withdrawal of",
- "problemWithdrawing": "There was a problem sending to your autowithdrawal address",
+ "paymentReceived": "Payment received",
+ "hello": "Hello",
+ "youReceived": "You received a payment of",
+ "withdrawalTriggered": "This triggered a withdrawal of",
+ "problemWithdrawing": "There was a problem sending to your autowithdrawal address",
"insufficientFunds": "Insufficient funds"
}
diff --git a/lib/locales/es.json b/lib/locales/es.json
index 1bbb2b1b..312cf097 100644
--- a/lib/locales/es.json
+++ b/lib/locales/es.json
@@ -1,9 +1,8 @@
{
- "hello": "Hola",
- "youReceived": "Recibiste un pago de",
- "withdrawalTriggered": "Esto provocó la retirada de",
- "problemWithdrawing": "Hubo un problema al enviar a su dirección de retiro automático",
- "paymentReceived": "Pago recibido",
- "balanceAdjustment": "Ajuste del equilibrio",
- "insufficientFunds": "Fondos insuficientes"
+ "hello": "Hola",
+ "youReceived": "Recibiste un pago de",
+ "withdrawalTriggered": "Esto provocó la retirada de",
+ "problemWithdrawing": "Hubo un problema al enviar a su dirección de retiro automático",
+ "paymentReceived": "Pago recibido",
+ "insufficientFunds": "Fondos insuficientes"
}
diff --git a/lib/locales/fa.json b/lib/locales/fa.json
index da9f94d7..2dc618fc 100644
--- a/lib/locales/fa.json
+++ b/lib/locales/fa.json
@@ -1,9 +1,8 @@
{
- "hello": "سلام",
- "youReceived": "شما پرداختی دریافت کردید",
- "withdrawalTriggered": "این باعث خروج از",
- "problemWithdrawing": "مشکلی در ارسال به آدرس برداشت خودکار شما وجود داشت",
- "paymentReceived": "پرداخت دریافت شد",
- "balanceAdjustment": "تعدیل تعادل تعادل",
- "insufficientFunds": "صندوق های بی کفایت"
+ "hello": "سلام",
+ "youReceived": "شما پرداختی دریافت کردید",
+ "withdrawalTriggered": "این باعث خروج از",
+ "problemWithdrawing": "مشکلی در ارسال به آدرس برداشت خودکار شما وجود داشت",
+ "paymentReceived": "پرداخت دریافت شد",
+ "insufficientFunds": "صندوق های بی کفایت"
}
diff --git a/lib/locales/fr.json b/lib/locales/fr.json
index 63c69352..0721304c 100644
--- a/lib/locales/fr.json
+++ b/lib/locales/fr.json
@@ -1,9 +1,8 @@
{
- "hello": "Bonjour",
- "youReceived": "Vous avez reçu un paiement de",
- "withdrawalTriggered": "Cela a déclenché un retrait de",
- "problemWithdrawing": "Un problème est survenu lors de l'envoi à votre adresse de retrait automatique",
- "paymentReceived": "Paiement reçu",
- "balanceAdjustment": "Ajustement du solde",
- "insufficientFunds": "Insuffisance des fonds"
+ "hello": "Bonjour",
+ "youReceived": "Vous avez reçu un paiement de",
+ "withdrawalTriggered": "Cela a déclenché un retrait de",
+ "problemWithdrawing": "Un problème est survenu lors de l'envoi à votre adresse de retrait automatique",
+ "paymentReceived": "Paiement reçu",
+ "insufficientFunds": "Insuffisance des fonds"
}
diff --git a/lib/locales/hi.json b/lib/locales/hi.json
index 02b52e38..8cc272af 100644
--- a/lib/locales/hi.json
+++ b/lib/locales/hi.json
@@ -1,9 +1,8 @@
{
- "balanceAdjustment": "संतुलन समायोजन",
- "paymentReceived": "भुगतान प्राप्त",
- "hello": "नमस्ते",
- "youReceived": "आपको भुगतान प्राप्त हुआ",
- "withdrawalTriggered": "यह ट्रिगर की वापसी",
- "problemWithdrawing": "वहाँ एक समस्या अपने autowithdrawal पते पर भेज रहा था",
- "insufficientFunds": "अपर्याप्त निधि"
+ "paymentReceived": "भुगतान प्राप्त",
+ "hello": "नमस्ते",
+ "youReceived": "आपको भुगतान प्राप्त हुआ",
+ "withdrawalTriggered": "यह ट्रिगर की वापसी",
+ "problemWithdrawing": "वहाँ एक समस्या अपने autowithdrawal पते पर भेज रहा था",
+ "insufficientFunds": "अपर्याप्त निधि"
}
diff --git a/lib/locales/it.json b/lib/locales/it.json
index 1de81168..d49a7817 100644
--- a/lib/locales/it.json
+++ b/lib/locales/it.json
@@ -1,9 +1,8 @@
{
- "balanceAdjustment": "Regolazione del saldo",
- "paymentReceived": "Pagamento ricevuto",
- "hello": "Ciao",
- "youReceived": "Hai ricevuto un pagamento",
- "withdrawalTriggered": "Questo ha innescato un ritiro",
- "problemWithdrawing": "C'è stato un problema di inviare al tuo indirizzo di autoritiro",
- "insufficientFunds": "Fondi insufficienti"
+ "paymentReceived": "Pagamento ricevuto",
+ "hello": "Ciao",
+ "youReceived": "Hai ricevuto un pagamento",
+ "withdrawalTriggered": "Questo ha innescato un ritiro",
+ "problemWithdrawing": "C'è stato un problema di inviare al tuo indirizzo di autoritiro",
+ "insufficientFunds": "Fondi insufficienti"
}
diff --git a/lib/locales/ja.json b/lib/locales/ja.json
index 7d3f2ef3..fef3a2aa 100644
--- a/lib/locales/ja.json
+++ b/lib/locales/ja.json
@@ -1,9 +1,8 @@
{
- "hello": "こんにちは",
- "youReceived": "の支払いを受けました",
- "withdrawalTriggered": "これが撤退のきっかけとなった",
- "problemWithdrawing": "自動引き出しアドレスへの送信中に問題が発生しました",
- "paymentReceived": "お支払い頂いた",
- "balanceAdjustment": "バランス調整",
- "insufficientFunds": "十分な資金"
+ "hello": "こんにちは",
+ "youReceived": "の支払いを受けました",
+ "withdrawalTriggered": "これが撤退のきっかけとなった",
+ "problemWithdrawing": "自動引き出しアドレスへの送信中に問題が発生しました",
+ "paymentReceived": "お支払い頂いた",
+ "insufficientFunds": "十分な資金"
}
diff --git a/lib/locales/ko.json b/lib/locales/ko.json
index 93b052cb..7706989c 100644
--- a/lib/locales/ko.json
+++ b/lib/locales/ko.json
@@ -1,9 +1,8 @@
{
- "balanceAdjustment": "균형 조정",
- "paymentReceived": "지불받은",
- "hello": "이름 *",
- "youReceived": "당신은 지불을 받았습니다",
- "withdrawalTriggered": "이 방아쇠 인출",
- "problemWithdrawing": "Autowithdrawal 주소로 보내는 문제가 있었습니다",
- "insufficientFunds": "충분한 자금"
+ "paymentReceived": "지불받은",
+ "hello": "이름 *",
+ "youReceived": "당신은 지불을 받았습니다",
+ "withdrawalTriggered": "이 방아쇠 인출",
+ "problemWithdrawing": "Autowithdrawal 주소로 보내는 문제가 있었습니다",
+ "insufficientFunds": "충분한 자금"
}
diff --git a/lib/locales/nl.json b/lib/locales/nl.json
index 85fb65da..5043d327 100644
--- a/lib/locales/nl.json
+++ b/lib/locales/nl.json
@@ -1,9 +1,8 @@
{
- "balanceAdjustment": "Balansaanpassing",
- "paymentReceived": "Ontvangen betaling",
- "hello": "Hallo",
- "youReceived": "U ontving een betaling van",
- "withdrawalTriggered": "Dit leidde tot een intrekking van",
- "problemWithdrawing": "Er was een probleem met het verzenden naar uw automatisch intrekadres",
- "insufficientFunds": "Onvoldoende middelen"
+ "paymentReceived": "Ontvangen betaling",
+ "hello": "Hallo",
+ "youReceived": "U ontving een betaling van",
+ "withdrawalTriggered": "Dit leidde tot een intrekking van",
+ "problemWithdrawing": "Er was een probleem met het verzenden naar uw automatisch intrekadres",
+ "insufficientFunds": "Onvoldoende middelen"
}
diff --git a/lib/locales/pl.json b/lib/locales/pl.json
index 1baeadd2..69e0996a 100644
--- a/lib/locales/pl.json
+++ b/lib/locales/pl.json
@@ -1,9 +1,8 @@
{
- "balanceAdjustment": "Korekta salda",
- "paymentReceived": "Otrzymane płatności",
- "hello": "Halo",
- "youReceived": "Otrzymałeś zapłatę",
- "withdrawalTriggered": "Wywołało to wycofanie",
- "problemWithdrawing": "Był problem z wysłaniem na twój adres autowycofania",
- "insufficientFunds": "Niewystarczające fundusze"
+ "paymentReceived": "Otrzymane płatności",
+ "hello": "Halo",
+ "youReceived": "Otrzymałeś zapłatę",
+ "withdrawalTriggered": "Wywołało to wycofanie",
+ "problemWithdrawing": "Był problem z wysłaniem na twój adres autowycofania",
+ "insufficientFunds": "Niewystarczające fundusze"
}
diff --git a/lib/locales/pt.json b/lib/locales/pt.json
index 114e4f43..35413372 100644
--- a/lib/locales/pt.json
+++ b/lib/locales/pt.json
@@ -1,9 +1,8 @@
{
- "hello": "Olá",
- "youReceived": "Você recebeu um pagamento de",
- "withdrawalTriggered": "Isto desencadeou uma retirada de",
- "problemWithdrawing": "Ocorreu um problema ao enviar para seu endereço de retirada automática",
- "paymentReceived": "Pagamento recebido",
- "balanceAdjustment": "Ajustamento do saldo",
- "insufficientFunds": "Fundos insuficientes"
+ "hello": "Olá",
+ "youReceived": "Você recebeu um pagamento de",
+ "withdrawalTriggered": "Isto desencadeou uma retirada de",
+ "problemWithdrawing": "Ocorreu um problema ao enviar para seu endereço de retirada automática",
+ "paymentReceived": "Pagamento recebido",
+ "insufficientFunds": "Fundos insuficientes"
}
diff --git a/lib/locales/ru.json b/lib/locales/ru.json
index 78fd7701..9fb367ad 100644
--- a/lib/locales/ru.json
+++ b/lib/locales/ru.json
@@ -1,9 +1,8 @@
{
- "hello": "Привет",
- "youReceived": "Вы получили выплату в размере",
- "withdrawalTriggered": "Это послужило толчком к выводу",
- "problemWithdrawing": "Возникла проблема с отправкой на ваш адрес автоматического вывода средств.",
- "paymentReceived": "Платеж получен",
- "balanceAdjustment": "Корректировка баланса",
- "insufficientFunds": "Недостаточно средств"
+ "hello": "Привет",
+ "youReceived": "Вы получили выплату в размере",
+ "withdrawalTriggered": "Это послужило толчком к выводу",
+ "problemWithdrawing": "Возникла проблема с отправкой на ваш адрес автоматического вывода средств.",
+ "paymentReceived": "Платеж получен",
+ "insufficientFunds": "Недостаточно средств"
}
diff --git a/lib/locales/th.json b/lib/locales/th.json
index 7a3f4e1e..a6869a11 100644
--- a/lib/locales/th.json
+++ b/lib/locales/th.json
@@ -1,9 +1,8 @@
{
- "balanceAdjustment": "ปรับสมดุล",
- "paymentReceived": "ได้รับค่าจ้าง",
- "hello": "สวัสดี",
- "youReceived": "คุณได้รับเงิน",
- "withdrawalTriggered": "นี่กระตุ้นให้เกิดการถอน",
- "problemWithdrawing": "เกิดปัญหาในการส่งที่อยู่โดยอัตโนมัติของคุณ",
- "insufficientFunds": "เงินทุนไม่เพียงพอ"
+ "paymentReceived": "ได้รับค่าจ้าง",
+ "hello": "สวัสดี",
+ "youReceived": "คุณได้รับเงิน",
+ "withdrawalTriggered": "นี่กระตุ้นให้เกิดการถอน",
+ "problemWithdrawing": "เกิดปัญหาในการส่งที่อยู่โดยอัตโนมัติของคุณ",
+ "insufficientFunds": "เงินทุนไม่เพียงพอ"
}
diff --git a/lib/locales/tr.json b/lib/locales/tr.json
index 73d37b89..d856e591 100644
--- a/lib/locales/tr.json
+++ b/lib/locales/tr.json
@@ -1,9 +1,8 @@
{
- "balanceAdjustment": "Denge ayarı",
- "paymentReceived": "Ödeme alındı",
- "hello": "Merhaba",
- "youReceived": "Bir ödeme aldın",
- "withdrawalTriggered": "Bu, bir geri çekilmeyi tetikledi",
- "problemWithdrawing": "AutoWithdrawal adresinize gönderilen bir sorun vardı",
- "insufficientFunds": "Yetersiz fonlarda"
+ "paymentReceived": "Ödeme alındı",
+ "hello": "Merhaba",
+ "youReceived": "Bir ödeme aldın",
+ "withdrawalTriggered": "Bu, bir geri çekilmeyi tetikledi",
+ "problemWithdrawing": "AutoWithdrawal adresinize gönderilen bir sorun vardı",
+ "insufficientFunds": "Yetersiz fonlarda"
}
diff --git a/lib/locales/zh.json b/lib/locales/zh.json
index b1799118..729004b8 100644
--- a/lib/locales/zh.json
+++ b/lib/locales/zh.json
@@ -1,9 +1,8 @@
{
- "hello": "你好",
- "youReceived": "您收到了一笔付款",
- "withdrawalTriggered": "这引发了撤回",
- "problemWithdrawing": "发送至您的自动提款地址时出现问题",
- "paymentReceived": "已收到付款",
- "balanceAdjustment": "余额调整",
- "insufficientFunds": "资金不足"
+ "hello": "你好",
+ "youReceived": "您收到了一笔付款",
+ "withdrawalTriggered": "这引发了撤回",
+ "problemWithdrawing": "发送至您的自动提款地址时出现问题",
+ "paymentReceived": "已收到付款",
+ "insufficientFunds": "资金不足"
}
diff --git a/lib/payments.ts b/lib/payments.ts
index 7dae4a91..4fddcba8 100644
--- a/lib/payments.ts
+++ b/lib/payments.ts
@@ -11,6 +11,7 @@ import {
getTxHex,
getTxStatus,
hdVersions,
+ findLastUsedIndex,
} from "$lib/esplora";
import { HDKey } from "@scure/bip32";
import { generate } from "$lib/invoices";
@@ -1103,58 +1104,145 @@ export const catchUp = async () => {
}
};
-export const reconcile = async (account, initial = false) => {
+export const importAccountHistory = async (account) => {
try {
- const { id, uid, pubkey, fingerprint, nextIndex } = account;
+ const { id, uid, pubkey, fingerprint } = account;
const user = await getUser(uid);
- // Derive all addresses and sum confirmed UTXOs from esplora
- const count = (nextIndex || 0) + 1;
+ let nextIndex = account.nextIndex || 0;
+ if (!account.importedAt) {
+ const lastUsed = await findLastUsedIndex(pubkey, fingerprint);
+ if (lastUsed > nextIndex) nextIndex = lastUsed;
+ account.nextIndex = nextIndex;
+ }
+
+ const count = nextIndex + 1;
const externalAddrs = deriveAddresses(pubkey, fingerprint, count, false);
const internalAddrs = deriveAddresses(pubkey, fingerprint, count, true);
const allAddrs = [...externalAddrs, ...internalAddrs];
+ const addressSet = new Set(allAddrs);
const utxos = await getAddressUtxos(allAddrs);
- const total = utxos
- .filter((u) => u.status.confirmed)
- .reduce((sum, u) => sum + u.value, 0);
-
- const { balanceAdjustment: memo } = t(user);
+ const confirmedUtxos = utxos.filter(
+ (u) => u.status.confirmed && u.value >= 300,
+ );
+ const total = confirmedUtxos.reduce((sum, u) => sum + u.value, 0);
- const balance = await g(`balance:${id}`);
+ const rates = await g("rates");
+ let rate = rates[user.currency];
+ if (!rate) await sleep(1000);
+ rate = rate || rates[user.currency];
- const amount = Math.abs(total - balance);
- const hash = v4();
+ for (const u of confirmedUtxos) {
+ const existing = await getPayment(`${u.txid}:${u.vout}`);
+ if (existing) continue;
- if (total > balance) {
- const inv = {
- memo,
- type: PaymentType.reconcile,
- hash,
- amount,
- uid,
+ const created = u.status?.block_time
+ ? u.status.block_time * 1000
+ : Date.now();
+ const p = {
+ id: v4(),
aid: id,
+ amount: u.value,
+ fee: 0,
+ hash: u.address,
+ confirmed: true,
+ rate,
+ currency: user.currency,
+ type: PaymentType.bitcoin,
+ uid,
+ ref: `${u.txid}:${u.vout}`,
+ created,
};
- await s(`invoice:${hash}`, inv);
- await credit({
- hash,
- amount,
- type: PaymentType.reconcile,
- aid: id,
- });
- } else if (total < balance) {
- await debit({
+
+ await db
+ .multi()
+ .set(`payment:${u.txid}:${u.vout}`, p.id)
+ .set(`payment:${p.id}`, JSON.stringify(p))
+ .lPush(`${id}:payments`, p.id)
+ .set(`${id}:payments:last`, p.created)
+ .exec();
+ }
+
+ const txsById = new Map();
+ for (const address of allAddrs) {
+ const txs = await getAddressTxs(address);
+ for (const tx of txs) {
+ txsById.set(tx.txid, tx);
+ }
+ }
+
+ for (const tx of txsById.values()) {
+ if (!tx.status?.confirmed) continue;
+ const existing = await getPayment(tx.txid);
+ if (existing) continue;
+
+ let inputsFromUs = 0;
+ let outputsToUs = 0;
+ let totalIn = 0;
+ let totalOut = 0;
+
+ for (const vin of tx.vin || []) {
+ const prev = vin.prevout;
+ if (!prev) continue;
+ const value = Number.parseInt(prev.value) || 0;
+ totalIn += value;
+ if (addressSet.has(prev.scriptpubkey_address)) inputsFromUs += value;
+ }
+
+ for (const vout of tx.vout || []) {
+ const value = Number.parseInt(vout.value) || 0;
+ totalOut += value;
+ if (addressSet.has(vout.scriptpubkey_address)) outputsToUs += value;
+ }
+
+ if (!inputsFromUs) continue;
+
+ const net = outputsToUs - inputsFromUs;
+ if (net >= 0) continue;
+
+ const amount = totalOut - outputsToUs;
+ if (amount <= 0) continue;
+
+ const fee = Math.max(0, totalIn - totalOut);
+ const created = tx.status?.block_time
+ ? tx.status.block_time * 1000
+ : Date.now();
+
+ const p = {
+ id: v4(),
aid: id,
- amount,
- hash: v4(),
- memo,
- user,
- type: PaymentType.reconcile,
- });
+ amount: -amount,
+ fee,
+ hash: tx.txid,
+ confirmed: true,
+ rate,
+ currency: user.currency,
+ type: PaymentType.bitcoin,
+ uid,
+ created,
+ };
+
+ await db
+ .multi()
+ .set(`payment:${tx.txid}`, p.id)
+ .set(`payment:${p.id}`, JSON.stringify(p))
+ .lPush(`${id}:payments`, p.id)
+ .set(`${id}:payments:last`, p.created)
+ .exec();
}
+
+ await db
+ .multi()
+ .set(`balance:${id}`, total)
+ .set(`pending:${id}`, 0)
+ .exec();
+
+ account.importedAt = account.importedAt || Date.now();
+ await s(`account:${id}`, account);
} catch (e) {
console.log(e);
- warn("problem reconciling", e.message, account);
+ warn("problem importing account history", e.message, account);
}
};
diff --git a/lib/types.ts b/lib/types.ts
index 4e3b40c0..685c7f41 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -54,7 +54,6 @@ export enum PaymentType {
fund = "fund",
liquid = "liquid",
ecash = "ecash",
- reconcile = "reconcile",
bolt12 = "bolt12",
}