Skip to content

Commit

Permalink
Check invoice status in finalizeAction
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed May 23, 2024
1 parent d90c119 commit fb43c48
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api/resolvers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ export const createItem = async (parent, { forward, options, ...item }, { me, mo
// since SubscribeToInvoices does not trigger if an invoice expired, we need a job that handles expired invoices
await tx.$queryRaw`
INSERT INTO pgboss.job (name, data, retrylimit, retrybackoff, startafter)
VALUES ('finalizeAction', jsonb_build_object('type', 'ITEM', 'id', ${item.id}::INTEGER), 21, true, ${expiresAt})`
VALUES ('finalizeAction', jsonb_build_object('hash', ${invoice.hash}::TEXT), 21, true, ${expiresAt})`
}, { isolationLevel: Prisma.TransactionIsolationLevel.Serializable })

// hmac is not required to submit action again but to allow user to cancel payment
Expand Down
4 changes: 2 additions & 2 deletions api/resolvers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import assertGofacYourself from './ofac'
import assertApiKeyNotPermitted from './apiKey'
import { createInvoice as createInvoiceCLN } from '@/lib/cln'
import { bolt11Tags } from '@/lib/bolt11'
import { handleActionError } from 'worker/wallet'
import { actionErrorQueries } from 'worker/action'

export async function getInvoice (parent, { id }, { me, models, lnd }) {
const inv = await models.invoice.findUnique({
Expand Down Expand Up @@ -391,7 +391,7 @@ export default {
cancelled: true
}
}),
...handleActionError({ data: inv, models })
...actionErrorQueries({ data: inv, models })
], { models }
)
return { ...inv, cancelled: true }
Expand Down
22 changes: 16 additions & 6 deletions worker/action.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { handleActionError } from './wallet'
import serialize from '@/api/resolvers/serial'

export async function finalizeAction ({ data: { type, id }, models }) {
const queries = handleActionError({ data: { actionType: type, actionId: id }, models })
if (queries.length === 0) return
export async function finalizeAction ({ data: { hash }, models }) {
const invoice = await models.invoice.findUnique({ where: { hash } })

if (invoice.confirmedAt) {
await handleAction({ data: invoice, models })
return
}

const queries = await actionErrorQueries({ data: invoice, models })
if (queries.length === 0) return
await models.$transaction(queries)
}

export async function handleAction ({ data: { msatsReceived, actionType, actionId, actionData }, models }) {
export async function handleAction ({ data: { actionType, actionId, actionData }, models }) {
if (!actionType || !actionId) return

if (actionType === 'ITEM') {
Expand All @@ -17,6 +23,10 @@ export async function handleAction ({ data: { msatsReceived, actionType, actionI
const { cost } = actionData
const item = await models.item.findUnique({ where: { id: actionId } })

if (item.status !== 'PENDING') {
return
}

await serialize([
models.item.update({
where: {
Expand Down Expand Up @@ -46,7 +56,7 @@ export async function handleAction ({ data: { msatsReceived, actionType, actionI
}
}

export function handleActionError ({ data: { actionType, actionId }, models }) {
export function actionErrorQueries ({ data: { actionType, actionId }, models }) {
if (!actionType || !actionId) return []

if (actionType === 'ITEM') {
Expand Down
4 changes: 2 additions & 2 deletions worker/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { datePivot, sleep } from '@/lib/time.js'
import retry from 'async-retry'
import { addWalletLog } from '@/api/resolvers/wallet'
import { msatsToSats, numWithUnits } from '@/lib/format'
import { handleAction, handleActionError } from './action'
import { handleAction, actionErrorQueries } from './action'

export async function subscribeToWallet (args) {
await subscribeToDeposits(args)
Expand Down Expand Up @@ -176,7 +176,7 @@ async function checkInvoice ({ data: { hash }, boss, models, lnd }) {
cancelled: true
}
}),
...handleActionError({ data: dbInv, models })
...actionErrorQueries({ data: dbInv, models })
], { models })
}
}
Expand Down

0 comments on commit fb43c48

Please sign in to comment.