Skip to content

Commit 8c7e3c8

Browse files
committed
Store invoiceId with wallet logs
1 parent 0d93c92 commit 8c7e3c8

File tree

7 files changed

+34
-57
lines changed

7 files changed

+34
-57
lines changed

Diff for: 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,
@@ -764,29 +764,13 @@ export const walletLogger = ({ wallet, models }) => {
764764
// server implementation of wallet logger interface on client
765765
const log = (level) => async (message, context = {}) => {
766766
try {
767-
if (context?.bolt11) {
768-
// automatically populate context from bolt11 to avoid duplicating this code
769-
const decoded = await parsePaymentRequest({ request: context.bolt11 })
770-
context = {
771-
...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
779-
}
780-
}
781-
context.recv = true
782-
783767
await models.walletLog.create({
784768
data: {
785769
userId: wallet.userId,
786770
wallet: wallet.type,
787771
level,
788772
message,
789-
context
773+
...context
790774
}
791775
})
792776
} 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;

Diff for: prisma/schema.prisma

+3
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ model WalletLog {
255255
wallet WalletType
256256
level LogLevel
257257
message String
258+
invoiceId Int?
259+
invoice Invoice? @relation(fields: [invoiceId], references: [id])
258260
context Json? @db.JsonB
259261
260262
@@index([userId, createdAt])
@@ -952,6 +954,7 @@ model Invoice {
952954
Upload Upload[]
953955
PollVote PollVote[]
954956
PollBlindVote PollBlindVote[]
957+
WalletLog WalletLog[]
955958
956959
@@index([createdAt], map: "Invoice.created_at_index")
957960
@@index([userId], map: "Invoice.userId_index")

Diff for: 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'
@@ -38,11 +38,7 @@ export async function * createUserInvoice (userId, { msats, description, descrip
3838
const logger = walletLogger({ wallet, models })
3939

4040
try {
41-
logger.info(
42-
`↙ incoming payment: ${formatSats(msatsToSats(msats))}`,
43-
{
44-
amount: formatMsats(msats)
45-
})
41+
logger.info(`↙ incoming payment: ${formatSats(msatsToSats(msats))}`)
4642

4743
let invoice
4844
try {

Diff for: worker/autowithdraw.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export async function autoWithdraw ({ data: { id }, models, lnd }) {
5353
{ me: { id }, models, lnd, wallet, logger })
5454
} catch (err) {
5555
console.error('failed to create autowithdrawal:', err)
56-
logger?.error('incoming payment failed: ' + err.message, { bolt11: invoice })
56+
// TODO: this won't work since invoice is bolt11 string
57+
logger?.error('incoming payment failed: ' + err.message, { invoiceId: invoice.id })
5758
}
5859
}
5960

Diff for: 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

Diff for: 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)