Skip to content

Commit

Permalink
wip: normalize wallet logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Jan 18, 2025
1 parent 566a5f9 commit fa46bd3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 66 deletions.
23 changes: 5 additions & 18 deletions api/resolvers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import crypto, { timingSafeEqual } from 'crypto'
import { decodeCursor, LIMIT, nextCursorEncoded } from '@/lib/cursor'
import { SELECT, itemQueryWithMeta } from './item'
import { formatMsats, msatsToSats, msatsToSatsDecimal, satsToMsats } from '@/lib/format'
import { msatsToSats, msatsToSatsDecimal, satsToMsats } from '@/lib/format'
import {
USER_ID, INVOICE_RETENTION_DAYS,
PAID_ACTION_PAYMENT_METHODS,
Expand Down Expand Up @@ -745,30 +745,17 @@ export const walletLogger = ({ wallet, models }) => {

// server implementation of wallet logger interface on client
const log = (level) => async (message, context = {}) => {
const { invoiceForwardId, status, ...rest } = context
try {
if (context?.bolt11) {
// automatically populate context from bolt11 to avoid duplicating this code
const decoded = await parsePaymentRequest({ request: context.bolt11 })
context = {
...context,
amount: formatMsats(decoded.mtokens),
payment_hash: decoded.id,
created_at: decoded.created_at,
expires_at: decoded.expires_at,
description: decoded.description,
// payments should affect wallet status
status: true
}
}
context.recv = true

await models.walletLog.create({
data: {
userId: wallet.userId,
wallet: wallet.type,
level,
message,
context
status: invoiceForwardId ? true : status,
invoiceForwardId,
context: rest
}
})
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- AlterTable
ALTER TABLE "WalletLog" ADD COLUMN "invoiceForwardId" INTEGER;
ALTER TABLE "WalletLog" ADD COLUMN "status" BOOLEAN NOT NULL DEFAULT false;

-- AddForeignKey
ALTER TABLE "WalletLog" ADD CONSTRAINT "WalletLog_invoiceForwardId_fkey" FOREIGN KEY ("invoiceForwardId") REFERENCES "InvoiceForward"("id") ON DELETE SET NULL ON UPDATE CASCADE;
26 changes: 15 additions & 11 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,17 @@ model VaultEntry {
}

model WalletLog {
id Int @id @default(autoincrement())
createdAt DateTime @default(now()) @map("created_at")
userId Int
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
wallet WalletType
level LogLevel
message String
context Json? @db.JsonB
id Int @id @default(autoincrement())
createdAt DateTime @default(now()) @map("created_at")
userId Int
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
wallet WalletType
level LogLevel
message String
invoiceForwardId Int?
invoiceForward InvoiceForward? @relation(fields: [invoiceForwardId], references: [id])
status Boolean @default(false)
context Json? @db.JsonB
@@index([userId, createdAt])
}
Expand Down Expand Up @@ -995,9 +998,10 @@ model InvoiceForward {
invoiceId Int @unique
withdrawlId Int? @unique
invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
wallet Wallet @relation(fields: [walletId], references: [id], onDelete: Cascade)
withdrawl Withdrawl? @relation(fields: [withdrawlId], references: [id], onDelete: SetNull)
invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
wallet Wallet @relation(fields: [walletId], references: [id], onDelete: Cascade)
withdrawl Withdrawl? @relation(fields: [withdrawlId], references: [id], onDelete: SetNull)
WalletLog WalletLog[]
@@index([invoiceId])
@@index([walletId])
Expand Down
8 changes: 3 additions & 5 deletions wallets/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ export async function createInvoice (userId, { msats, description, descriptionHa
const logger = walletLogger({ wallet, models })

try {
logger.info(
`↙ incoming payment: ${formatSats(msatsToSats(msats))}`,
{
amount: formatMsats(msats)
})
logger.info(`↙ incoming payment: ${formatSats(msatsToSats(msats))}`, {
amount: formatMsats(msats)
})

let invoice
try {
Expand Down
29 changes: 11 additions & 18 deletions worker/paidAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getPaymentFailureStatus, hodlInvoiceCltvDetails, getPaymentOrNotSent }
import { paidActions } from '@/api/paidAction'
import { walletLogger } from '@/api/resolvers/wallet'
import { LND_PATHFINDING_TIME_PREF_PPM, LND_PATHFINDING_TIMEOUT_MS, PAID_ACTION_TERMINAL_STATES } from '@/lib/constants'
import { formatMsats, formatSats, msatsToSats, toPositiveNumber } from '@/lib/format'
import { formatSats, msatsToSats, toPositiveNumber } from '@/lib/format'
import { datePivot } from '@/lib/time'
import { Prisma } from '@prisma/client'
import {
Expand Down Expand Up @@ -317,18 +317,12 @@ export async function paidActionForwarded ({ data: { invoiceId, withdrawal, ...a
}, { models, lnd, boss })

if (transitionedInvoice) {
const { bolt11, msatsPaid } = transitionedInvoice.invoiceForward.withdrawl
const { msatsPaid } = transitionedInvoice.invoiceForward.withdrawl

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

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

if (transitionedInvoice) {
const { bolt11, msatsFeePaying } = transitionedInvoice.invoiceForward.withdrawl
const logger = walletLogger({ wallet: transitionedInvoice.invoiceForward.wallet, models })
logger.warn(
`incoming payment failed: ${message}`, {
bolt11,
max_fee: formatMsats(msatsFeePaying)
})
logger.warn(`incoming payment failed: ${message}`, {
invoiceForwardId: transitionedInvoice.invoiceForward.id
})
}

return transitionedInvoice
Expand Down Expand Up @@ -446,7 +437,9 @@ export async function paidActionCanceling ({ data: { invoiceId, ...args }, model
const { wallet, bolt11 } = transitionedInvoice.invoiceForward
const logger = walletLogger({ wallet, models })
const decoded = await parsePaymentRequest({ request: bolt11 })
logger.info(`invoice for ${formatSats(msatsToSats(decoded.mtokens))} canceled by payer`, { bolt11 })
logger.info(`invoice for ${formatSats(msatsToSats(decoded.mtokens))} canceled by payer`, {
invoiceForwardId: transitionedInvoice.invoiceForward.id
})
}
}

Expand Down
21 changes: 7 additions & 14 deletions worker/payingAction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getPaymentFailureStatus, getPaymentOrNotSent } from '@/api/lnd'
import { walletLogger } from '@/api/resolvers/wallet'
import { formatMsats, formatSats, msatsToSats, toPositiveBigInt } from '@/lib/format'
import { formatSats, msatsToSats, toPositiveBigInt } from '@/lib/format'
import { datePivot } from '@/lib/time'
import { notifyWithdrawal } from '@/lib/webPush'
import { Prisma } from '@prisma/client'
Expand Down Expand Up @@ -124,13 +124,9 @@ export async function payingActionConfirmed ({ data: args, models, lnd, boss })
await notifyWithdrawal(transitionedWithdrawal)

const logger = walletLogger({ models, wallet: transitionedWithdrawal.wallet })
logger?.ok(
`↙ payment received: ${formatSats(msatsToSats(transitionedWithdrawal.msatsPaid))}`,
{
bolt11: transitionedWithdrawal.bolt11,
preimage: transitionedWithdrawal.preimage,
fee: formatMsats(transitionedWithdrawal.msatsFeePaid)
})
logger?.ok(`↙ payment received: ${formatSats(msatsToSats(transitionedWithdrawal.msatsPaid))}`, {
invoiceForwardId: transitionedWithdrawal.invoiceForward.id
})
}
}

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

if (transitionedWithdrawal) {
const logger = walletLogger({ models, wallet: transitionedWithdrawal.wallet })
logger?.error(
`incoming payment failed: ${message}`,
{
bolt11: transitionedWithdrawal.bolt11,
max_fee: formatMsats(transitionedWithdrawal.msatsFeePaying)
})
logger?.error(`incoming payment failed: ${message}`, {
invoiceForwardId: transitionedWithdrawal.invoiceForward.id
})
}
}

0 comments on commit fa46bd3

Please sign in to comment.