Skip to content

Commit 9332b54

Browse files
committed
Add invoiceId, withdrawalId to wallet logs
1 parent 4f17615 commit 9332b54

File tree

7 files changed

+84
-42
lines changed

7 files changed

+84
-42
lines changed

api/resolvers/wallet.js

+42-12
Original file line numberDiff line numberDiff line change
@@ -745,11 +745,45 @@ const resolvers = {
745745
return item
746746
},
747747
sats: fact => msatsToSatsDecimal(fact.msats)
748+
},
749+
750+
WalletLogEntry: {
751+
context: async ({ level, context, invoiceId, withdrawalId }, args, { models }) => {
752+
const isError = ['error', 'warn'].includes(level.toLowerCase())
753+
754+
if (withdrawalId) {
755+
const wdrwl = await models.withdrawl.findUnique({ where: { id: withdrawalId } })
756+
return {
757+
...await logContextFromBolt11(wdrwl.bolt11),
758+
...(wdrwl.preimage ? { preimage: wdrwl.preimage } : {}),
759+
...(isError ? { max_fee: formatMsats(wdrwl.msatsFeePaying) } : {})
760+
}
761+
}
762+
763+
if (invoiceId) {
764+
const inv = await models.invoice.findUnique({ where: { id: invoiceId } })
765+
return await logContextFromBolt11(inv.bolt11)
766+
}
767+
768+
return context
769+
}
748770
}
749771
}
750772

751773
export default injectResolvers(resolvers)
752774

775+
const logContextFromBolt11 = async (bolt11) => {
776+
const decoded = await parsePaymentRequest({ request: bolt11 })
777+
return {
778+
bolt11,
779+
amount: formatMsats(decoded.mtokens),
780+
payment_hash: decoded.id,
781+
created_at: decoded.created_at,
782+
expires_at: decoded.expires_at,
783+
description: decoded.description
784+
}
785+
}
786+
753787
export const walletLogger = ({ wallet, models }) => {
754788
// no-op logger if wallet is not provided
755789
if (!wallet) {
@@ -762,31 +796,27 @@ export const walletLogger = ({ wallet, models }) => {
762796
}
763797

764798
// server implementation of wallet logger interface on client
765-
const log = (level) => async (message, context = {}) => {
799+
const log = (level) => async (message, ctx = {}) => {
766800
try {
767-
if (context?.bolt11) {
801+
let { invoiceId, withdrawalId, ...context } = ctx
802+
803+
if (context.bolt11) {
768804
// automatically populate context from bolt11 to avoid duplicating this code
769-
const decoded = await parsePaymentRequest({ request: context.bolt11 })
770805
context = {
771806
...context,
772-
amount: formatMsats(decoded.mtokens),
773-
payment_hash: decoded.id,
774-
created_at: decoded.created_at,
775-
expires_at: decoded.expires_at,
776-
description: decoded.description,
777-
// payments should affect wallet status
778-
status: true
807+
...await logContextFromBolt11(context.bolt11)
779808
}
780809
}
781-
context.recv = true
782810

783811
await models.walletLog.create({
784812
data: {
785813
userId: wallet.userId,
786814
wallet: wallet.type,
787815
level,
788816
message,
789-
context
817+
context,
818+
invoiceId,
819+
withdrawalId
790820
}
791821
})
792822
} catch (err) {

components/wallet-logger.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,13 @@ export function useWalletLogs (wallet, initialPage = 1, logsPerPage = 10) {
235235
const newLogs = data.walletLogs.entries.map(({ createdAt, wallet: walletType, ...log }) => ({
236236
ts: +new Date(createdAt),
237237
wallet: walletTag(getWalletByType(walletType)),
238-
...log
238+
...log,
239+
// required to resolve recv status
240+
context: {
241+
recv: true,
242+
status: !!log.context?.bolt11 && ['warn', 'error', 'success'].includes(log.level.toLowerCase()),
243+
...log.context
244+
}
239245
}))
240246
const combinedLogs = uniqueSort([...result.data, ...newLogs])
241247

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ALTER TABLE "WalletLog"
2+
ADD COLUMN "invoiceId" INTEGER,
3+
ADD COLUMN "withdrawalId" INTEGER;
4+
5+
ALTER TABLE "WalletLog" ADD CONSTRAINT "WalletLog_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "Invoice"("id") ON DELETE SET NULL ON UPDATE CASCADE;
6+
ALTER TABLE "WalletLog" ADD CONSTRAINT "WalletLog_withdrawalId_fkey" FOREIGN KEY ("withdrawalId") REFERENCES "Withdrawl"("id") ON DELETE SET NULL ON UPDATE CASCADE;

prisma/schema.prisma

+14-8
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,18 @@ model VaultEntry {
264264
}
265265

266266
model WalletLog {
267-
id Int @id @default(autoincrement())
268-
createdAt DateTime @default(now()) @map("created_at")
269-
userId Int
270-
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
271-
wallet WalletType
272-
level LogLevel
273-
message String
274-
context Json? @db.JsonB
267+
id Int @id @default(autoincrement())
268+
createdAt DateTime @default(now()) @map("created_at")
269+
userId Int
270+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
271+
wallet WalletType
272+
level LogLevel
273+
message String
274+
invoiceId Int?
275+
invoice Invoice? @relation(fields: [invoiceId], references: [id])
276+
withdrawalId Int?
277+
withdrawal Withdrawl? @relation(fields: [withdrawalId], references: [id])
278+
context Json? @db.JsonB
275279
276280
@@index([userId, createdAt])
277281
}
@@ -971,6 +975,7 @@ model Invoice {
971975
Upload Upload[]
972976
PollVote PollVote[]
973977
PollBlindVote PollBlindVote[]
978+
WalletLog WalletLog[]
974979
975980
@@index([createdAt], map: "Invoice.created_at_index")
976981
@@index([userId], map: "Invoice.userId_index")
@@ -1048,6 +1053,7 @@ model Withdrawl {
10481053
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
10491054
wallet Wallet? @relation(fields: [walletId], references: [id], onDelete: SetNull)
10501055
invoiceForward InvoiceForward?
1056+
WalletLog WalletLog[]
10511057
10521058
@@index([createdAt], map: "Withdrawl.created_at_index")
10531059
@@index([userId], map: "Withdrawl.userId_index")

wallets/server.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ export async function * createUserInvoice (userId, { msats, description, descrip
3939

4040
try {
4141
logger.info(
42-
`↙ incoming payment: ${formatSats(msatsToSats(msats))}`,
43-
{
42+
`↙ incoming payment: ${formatSats(msatsToSats(msats))}`, {
4443
amount: formatMsats(msats)
4544
})
4645

worker/paidAction.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getPaymentFailureStatus, hodlInvoiceCltvDetails, getPaymentOrNotSent }
22
import { paidActions } from '@/api/paidAction'
33
import { walletLogger } from '@/api/resolvers/wallet'
44
import { LND_PATHFINDING_TIME_PREF_PPM, LND_PATHFINDING_TIMEOUT_MS, PAID_ACTION_TERMINAL_STATES } from '@/lib/constants'
5-
import { formatMsats, formatSats, msatsToSats, toPositiveNumber } from '@/lib/format'
5+
import { formatSats, msatsToSats, toPositiveNumber } from '@/lib/format'
66
import { datePivot } from '@/lib/time'
77
import { Prisma } from '@prisma/client'
88
import {
@@ -317,17 +317,13 @@ 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 withdrawal = transitionedInvoice.invoiceForward.withdrawl
321321

322322
const logger = walletLogger({ wallet: transitionedInvoice.invoiceForward.wallet, models })
323323
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)
324+
`↙ payment received: ${formatSats(msatsToSats(Number(withdrawal.msatsPaid)))}`, {
325+
invoiceId: transitionedInvoice.id,
326+
withdrawalId: withdrawal.id
331327
})
332328
}
333329

@@ -376,12 +372,11 @@ export async function paidActionFailedForward ({ data: { invoiceId, withdrawal:
376372
}, { models, lnd, boss })
377373

378374
if (transitionedInvoice) {
379-
const { bolt11, msatsFeePaying } = transitionedInvoice.invoiceForward.withdrawl
380-
const logger = walletLogger({ wallet: transitionedInvoice.invoiceForward.wallet, models })
375+
const fwd = transitionedInvoice.invoiceForward
376+
const logger = walletLogger({ wallet: fwd.wallet, models })
381377
logger.warn(
382378
`incoming payment failed: ${message}`, {
383-
bolt11,
384-
max_fee: formatMsats(msatsFeePaying)
379+
withdrawalId: fwd.withdrawl.id
385380
})
386381
}
387382

@@ -446,7 +441,10 @@ export async function paidActionCanceling ({ data: { invoiceId, ...args }, model
446441
const { wallet, bolt11 } = transitionedInvoice.invoiceForward
447442
const logger = walletLogger({ wallet, models })
448443
const decoded = await parsePaymentRequest({ request: bolt11 })
449-
logger.info(`invoice for ${formatSats(msatsToSats(decoded.mtokens))} canceled by payer`, { bolt11 })
444+
logger.info(
445+
`invoice for ${formatSats(msatsToSats(decoded.mtokens))} canceled by payer`, {
446+
invoiceId: transitionedInvoice.id
447+
})
450448
}
451449
}
452450

worker/payingAction.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,8 @@ export async function payingActionConfirmed ({ data: args, models, lnd, boss })
125125

126126
const logger = walletLogger({ models, wallet: transitionedWithdrawal.wallet })
127127
logger?.ok(
128-
`↙ payment received: ${formatSats(msatsToSats(transitionedWithdrawal.msatsPaid))}`,
129-
{
130-
bolt11: transitionedWithdrawal.bolt11,
131-
preimage: transitionedWithdrawal.preimage,
132-
fee: formatMsats(transitionedWithdrawal.msatsFeePaid)
128+
`↙ payment received: ${formatSats(msatsToSats(transitionedWithdrawal.msatsPaid))}`, {
129+
withdrawalId: transitionedWithdrawal.id
133130
})
134131
}
135132
}

0 commit comments

Comments
 (0)