Skip to content

Commit 05037d5

Browse files
committed
Store invoiceId with wallet logs
1 parent 5af61f4 commit 05037d5

File tree

7 files changed

+33
-57
lines changed

7 files changed

+33
-57
lines changed

api/resolvers/wallet.js

+2-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,
@@ -746,29 +746,13 @@ export const walletLogger = ({ wallet, models }) => {
746746
// server implementation of wallet logger interface on client
747747
const log = (level) => async (message, context = {}) => {
748748
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-
765749
await models.walletLog.create({
766750
data: {
767751
userId: wallet.userId,
768752
wallet: wallet.type,
769753
level,
770754
message,
771-
context
755+
...context
772756
}
773757
})
774758
} catch (err) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- AlterTable
2+
ALTER TABLE "WalletLog" ADD COLUMN "invoiceId" INTEGER;
3+
4+
-- AddForeignKey
5+
ALTER TABLE "WalletLog" ADD CONSTRAINT "WalletLog_invoiceId_fkey" FOREIGN KEY ("invoiceId") REFERENCES "Invoice"("id") ON DELETE SET NULL ON UPDATE CASCADE;

prisma/schema.prisma

+3
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ model WalletLog {
254254
wallet WalletType
255255
level LogLevel
256256
message String
257+
invoiceId Int?
258+
invoice Invoice? @relation(fields: [invoiceId], references: [id])
257259
context Json? @db.JsonB
258260
259261
@@index([userId, createdAt])
@@ -949,6 +951,7 @@ model Invoice {
949951
Upload Upload[]
950952
PollVote PollVote[]
951953
PollBlindVote PollBlindVote[]
954+
WalletLog WalletLog[]
952955
953956
@@index([createdAt], map: "Invoice.created_at_index")
954957
@@index([userId], map: "Invoice.userId_index")

wallets/server.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as webln from '@/wallets/webln'
1414
import { walletLogger } from '@/api/resolvers/wallet'
1515
import walletDefs from '@/wallets/server'
1616
import { parsePaymentRequest } from 'ln-service'
17-
import { toPositiveBigInt, toPositiveNumber, formatMsats, formatSats, msatsToSats } from '@/lib/format'
17+
import { toPositiveBigInt, toPositiveNumber, formatSats, msatsToSats } from '@/lib/format'
1818
import { PAID_ACTION_TERMINAL_STATES, WALLET_CREATE_INVOICE_TIMEOUT_MS } from '@/lib/constants'
1919
import { timeoutSignal, withTimeout } from '@/lib/time'
2020
import { canReceive } from './common'
@@ -34,11 +34,7 @@ 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))}`)
4238

4339
let invoice
4440
try {

worker/autowithdraw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function autoWithdraw ({ data: { id }, models, lnd }) {
4949
{ invoice, maxFee: msatsToSats(maxFeeMsats) },
5050
{ me: { id }, models, lnd, wallet, logger })
5151
} catch (err) {
52-
logger.error(`incoming payment failed: ${err}`, { bolt11: invoice })
52+
logger.error(`incoming payment failed: ${err}`, { invoiceId: invoice.id })
5353
throw err
5454
}
5555
}

worker/paidAction.js

+11-18
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,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+
invoiceId: transitionedInvoice.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+
invoiceId: transitionedInvoice.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+
invoiceId: transitionedInvoice.id
442+
})
450443
}
451444
}
452445

worker/payingAction.js

+9-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,10 @@ 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+
// TODO: test this
129+
invoiceId: transitionedWithdrawal.invoiceForward.invoice.id
130+
})
134131
}
135132
}
136133

@@ -166,11 +163,9 @@ export async function payingActionFailed ({ data: args, models, lnd, boss }) {
166163

167164
if (transitionedWithdrawal) {
168165
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-
})
166+
logger?.error(`incoming payment failed: ${message}`, {
167+
// TODO: test this
168+
invoiceId: transitionedWithdrawal.invoiceForward.invoice.id
169+
})
175170
}
176171
}

0 commit comments

Comments
 (0)