Skip to content

Commit 891cdf0

Browse files
committed
wip: normalize wallet logs
1 parent 566a5f9 commit 891cdf0

File tree

6 files changed

+46
-65
lines changed

6 files changed

+46
-65
lines changed

api/resolvers/wallet.js

+5-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
import crypto, { timingSafeEqual } from 'crypto'
66
import { decodeCursor, LIMIT, nextCursorEncoded } from '@/lib/cursor'
77
import { SELECT, itemQueryWithMeta } from './item'
8-
import { formatMsats, msatsToSats, msatsToSatsDecimal, satsToMsats } from '@/lib/format'
8+
import { msatsToSats, msatsToSatsDecimal, satsToMsats } from '@/lib/format'
99
import {
1010
USER_ID, INVOICE_RETENTION_DAYS,
1111
PAID_ACTION_PAYMENT_METHODS,
@@ -745,30 +745,17 @@ export const walletLogger = ({ wallet, models }) => {
745745

746746
// server implementation of wallet logger interface on client
747747
const log = (level) => async (message, context = {}) => {
748+
const { invoiceForwardId, status, ...rest } = context
748749
try {
749-
if (context?.bolt11) {
750-
// automatically populate context from bolt11 to avoid duplicating this code
751-
const decoded = await parsePaymentRequest({ request: context.bolt11 })
752-
context = {
753-
...context,
754-
amount: formatMsats(decoded.mtokens),
755-
payment_hash: decoded.id,
756-
created_at: decoded.created_at,
757-
expires_at: decoded.expires_at,
758-
description: decoded.description,
759-
// payments should affect wallet status
760-
status: true
761-
}
762-
}
763-
context.recv = true
764-
765750
await models.walletLog.create({
766751
data: {
767752
userId: wallet.userId,
768753
wallet: wallet.type,
769754
level,
770755
message,
771-
context
756+
status: invoiceForwardId ? true : status,
757+
invoiceForwardId,
758+
context: rest
772759
}
773760
})
774761
} catch (err) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- AlterTable
2+
ALTER TABLE "WalletLog" ADD COLUMN "invoiceForwardId" INTEGER;
3+
ALTER TABLE "WalletLog" ADD COLUMN "status" BOOLEAN NOT NULL DEFAULT false;
4+
5+
-- AddForeignKey
6+
ALTER TABLE "WalletLog" ADD CONSTRAINT "WalletLog_invoiceForwardId_fkey" FOREIGN KEY ("invoiceForwardId") REFERENCES "InvoiceForward"("id") ON DELETE SET NULL ON UPDATE CASCADE;

prisma/schema.prisma

+15-11
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,17 @@ model VaultEntry {
247247
}
248248

249249
model WalletLog {
250-
id Int @id @default(autoincrement())
251-
createdAt DateTime @default(now()) @map("created_at")
252-
userId Int
253-
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
254-
wallet WalletType
255-
level LogLevel
256-
message String
257-
context Json? @db.JsonB
250+
id Int @id @default(autoincrement())
251+
createdAt DateTime @default(now()) @map("created_at")
252+
userId Int
253+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
254+
wallet WalletType
255+
level LogLevel
256+
message String
257+
invoiceForwardId Int?
258+
invoiceForward InvoiceForward? @relation(fields: [invoiceForwardId], references: [id])
259+
status Boolean @default(false)
260+
context Json? @db.JsonB
258261
259262
@@index([userId, createdAt])
260263
}
@@ -995,9 +998,10 @@ model InvoiceForward {
995998
invoiceId Int @unique
996999
withdrawlId Int? @unique
9971000
998-
invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
999-
wallet Wallet @relation(fields: [walletId], references: [id], onDelete: Cascade)
1000-
withdrawl Withdrawl? @relation(fields: [withdrawlId], references: [id], onDelete: SetNull)
1001+
invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
1002+
wallet Wallet @relation(fields: [walletId], references: [id], onDelete: Cascade)
1003+
withdrawl Withdrawl? @relation(fields: [withdrawlId], references: [id], onDelete: SetNull)
1004+
WalletLog WalletLog[]
10011005
10021006
@@index([invoiceId])
10031007
@@index([walletId])

wallets/server.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ export async function createInvoice (userId, { msats, description, descriptionHa
3434
const logger = walletLogger({ wallet, models })
3535

3636
try {
37-
logger.info(
38-
`↙ incoming payment: ${formatSats(msatsToSats(msats))}`,
39-
{
40-
amount: formatMsats(msats)
41-
})
37+
logger.info(`↙ incoming payment: ${formatSats(msatsToSats(msats))}`, {
38+
amount: formatMsats(msats)
39+
})
4240

4341
let invoice
4442
try {

worker/paidAction.js

+10-17
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,12 @@ export async function paidActionForwarded ({ data: { invoiceId, withdrawal, ...a
317317
}, { models, lnd, boss })
318318

319319
if (transitionedInvoice) {
320-
const { bolt11, msatsPaid } = transitionedInvoice.invoiceForward.withdrawl
320+
const { msatsPaid } = transitionedInvoice.invoiceForward.withdrawl
321321

322322
const logger = walletLogger({ wallet: transitionedInvoice.invoiceForward.wallet, models })
323-
logger.ok(
324-
`↙ payment received: ${formatSats(msatsToSats(Number(msatsPaid)))}`,
325-
{
326-
bolt11,
327-
preimage: transitionedInvoice.preimage
328-
// we could show the outgoing fee that we paid from the incoming amount to the receiver
329-
// but we don't since it might look like the receiver paid the fee but that's not the case.
330-
// fee: formatMsats(msatsFeePaid)
331-
})
323+
logger.ok(`↙ payment received: ${formatSats(msatsToSats(Number(msatsPaid)))}`, {
324+
invoiceForwardId: transitionedInvoice.invoiceForward.id
325+
})
332326
}
333327

334328
return transitionedInvoice
@@ -376,13 +370,10 @@ export async function paidActionFailedForward ({ data: { invoiceId, withdrawal:
376370
}, { models, lnd, boss })
377371

378372
if (transitionedInvoice) {
379-
const { bolt11, msatsFeePaying } = transitionedInvoice.invoiceForward.withdrawl
380373
const logger = walletLogger({ wallet: transitionedInvoice.invoiceForward.wallet, models })
381-
logger.warn(
382-
`incoming payment failed: ${message}`, {
383-
bolt11,
384-
max_fee: formatMsats(msatsFeePaying)
385-
})
374+
logger.warn(`incoming payment failed: ${message}`, {
375+
invoiceForwardId: transitionedInvoice.invoiceForward.id
376+
})
386377
}
387378

388379
return transitionedInvoice
@@ -446,7 +437,9 @@ export async function paidActionCanceling ({ data: { invoiceId, ...args }, model
446437
const { wallet, bolt11 } = transitionedInvoice.invoiceForward
447438
const logger = walletLogger({ wallet, models })
448439
const decoded = await parsePaymentRequest({ request: bolt11 })
449-
logger.info(`invoice for ${formatSats(msatsToSats(decoded.mtokens))} canceled by payer`, { bolt11 })
440+
logger.info(`invoice for ${formatSats(msatsToSats(decoded.mtokens))} canceled by payer`, {
441+
invoiceForwardId: transitionedInvoice.invoiceForward.id
442+
})
450443
}
451444
}
452445

worker/payingAction.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getPaymentFailureStatus, getPaymentOrNotSent } from '@/api/lnd'
22
import { walletLogger } from '@/api/resolvers/wallet'
3-
import { formatMsats, formatSats, msatsToSats, toPositiveBigInt } from '@/lib/format'
3+
import { formatSats, msatsToSats, toPositiveBigInt } from '@/lib/format'
44
import { datePivot } from '@/lib/time'
55
import { notifyWithdrawal } from '@/lib/webPush'
66
import { Prisma } from '@prisma/client'
@@ -124,13 +124,9 @@ export async function payingActionConfirmed ({ data: args, models, lnd, boss })
124124
await notifyWithdrawal(transitionedWithdrawal)
125125

126126
const logger = walletLogger({ models, wallet: transitionedWithdrawal.wallet })
127-
logger?.ok(
128-
`↙ payment received: ${formatSats(msatsToSats(transitionedWithdrawal.msatsPaid))}`,
129-
{
130-
bolt11: transitionedWithdrawal.bolt11,
131-
preimage: transitionedWithdrawal.preimage,
132-
fee: formatMsats(transitionedWithdrawal.msatsFeePaid)
133-
})
127+
logger?.ok(`↙ payment received: ${formatSats(msatsToSats(transitionedWithdrawal.msatsPaid))}`, {
128+
invoiceForwardId: transitionedWithdrawal.invoiceForward.id
129+
})
134130
}
135131
}
136132

@@ -166,11 +162,8 @@ export async function payingActionFailed ({ data: args, models, lnd, boss }) {
166162

167163
if (transitionedWithdrawal) {
168164
const logger = walletLogger({ models, wallet: transitionedWithdrawal.wallet })
169-
logger?.error(
170-
`incoming payment failed: ${message}`,
171-
{
172-
bolt11: transitionedWithdrawal.bolt11,
173-
max_fee: formatMsats(transitionedWithdrawal.msatsFeePaying)
174-
})
165+
logger?.error(`incoming payment failed: ${message}`, {
166+
invoiceForwardId: transitionedWithdrawal.invoiceForward.id
167+
})
175168
}
176169
}

0 commit comments

Comments
 (0)