Skip to content

Commit

Permalink
make invoices retriable two times more with feecredits and qr
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Nov 6, 2024
1 parent 7a3a6d6 commit 860fab0
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 23 deletions.
12 changes: 9 additions & 3 deletions api/paidAction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ async function performFeeCreditAction (invoiceData, actionType, paidAction, args
return {
result,
paymentMethod: 'FEE_CREDIT',
retriable: invoiceData.retriable
retriable: invoiceEntry.retriable,
retriableWithFeeCredits: invoiceEntry.retriableWithFeeCredits,
retriableManually: invoiceEntry.retriableManually
}
} catch (e) {
console.error('fee credit action failed', e)
Expand Down Expand Up @@ -220,7 +222,9 @@ async function performOptimisticAction (invoiceData, actionType, paidAction, arg
invoice: invoiceEntry,
result: await performAction(invoiceEntry, paidAction, args, context),
paymentMethod: 'OPTIMISTIC',
retriable: invoiceData.retriable
retriable: invoiceEntry.retriable,
retriableWithFeeCredits: invoiceEntry.retriableWithFeeCredits,
retriableManually: invoiceEntry.retriableManually
}
}
if (context.tx) return await performInvoicedAction(context.tx)
Expand All @@ -243,7 +247,9 @@ async function performPessimisticAction (invoiceData, actionType, paidAction, ar
return {
invoice: invoiceEntry,
paymentMethod: 'PESSIMISTIC',
retriable: invoiceData.retriable
retriable: invoiceEntry.retriable,
retriableWithFeeCredits: invoiceEntry.retriableWithFeeCredits,
retriableManually: invoiceEntry.retriableManually
}
}
if (context.tx) return await performInvoicedAction(context.tx)
Expand Down
12 changes: 12 additions & 0 deletions api/typeDefs/paidAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,53 @@ interface PaidAction {
invoice: Invoice
paymentMethod: PaymentMethod!
retriable: Boolean
retriableWithFeeCredits: Boolean
retriableManually: Boolean
}
type ItemPaidAction implements PaidAction {
result: Item
invoice: Invoice
paymentMethod: PaymentMethod!
retriable: Boolean
retriableWithFeeCredits: Boolean
retriableManually: Boolean
}
type ItemActPaidAction implements PaidAction {
result: ItemActResult
invoice: Invoice
paymentMethod: PaymentMethod!
retriable: Boolean
retriableWithFeeCredits: Boolean
retriableManually: Boolean
}
type PollVotePaidAction implements PaidAction {
result: PollVoteResult
invoice: Invoice
paymentMethod: PaymentMethod!
retriable: Boolean
retriableWithFeeCredits: Boolean
retriableManually: Boolean
}
type SubPaidAction implements PaidAction {
result: Sub
invoice: Invoice
paymentMethod: PaymentMethod!
retriable: Boolean
retriableWithFeeCredits: Boolean
retriableManually: Boolean
}
type DonatePaidAction implements PaidAction {
result: DonateResult
invoice: Invoice
paymentMethod: PaymentMethod!
retriable: Boolean
retriableWithFeeCredits: Boolean
retriableManually: Boolean
}
`
2 changes: 1 addition & 1 deletion components/item-act.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export function useZap () {
const sats = nextTip(meSats, { ...me?.privates })

const variables = { id: item.id, sats, act: 'TIP' }
const optimisticResponse = { act: { __typename: 'ItemActPaidAction', result: { path: item.path, ...variables } } }
const optimisticResponse = { act: { __typename: 'ItemActPaidAction', result: { path: item.path, ...variables }, retriable: true, retriableManually: true, retriableWithFeeCredits: true } }

try {
await abortSignal.pause({ me, amount: sats })
Expand Down
2 changes: 1 addition & 1 deletion components/pay-bounty.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function PayBounty ({ children, item }) {
const variables = { id: item.id, sats: root.bounty, act: 'TIP' }
const act = useAct({
variables,
optimisticResponse: { act: { __typename: 'ItemActPaidAction', result: { ...variables, path: item.path } } },
optimisticResponse: { act: { __typename: 'ItemActPaidAction', result: { ...variables, path: item.path }, retriable: true, retriableManually: true, retriableWithFeeCredits: true } },
...payBountyCacheMods
})

Expand Down
2 changes: 1 addition & 1 deletion components/poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function Poll ({ item }) {
onClick={me
? async () => {
const variables = { id: v.id }
const optimisticResponse = { pollVote: { __typename: 'PollVotePaidAction', result: { id: v.id } } }
const optimisticResponse = { pollVote: { __typename: 'PollVotePaidAction', result: { id: v.id }, retriable: true, retriableManually: true, retriableWithFeeCredits: true } }
try {
const { error } = await pollVote({
variables,
Expand Down
31 changes: 19 additions & 12 deletions components/use-paid-mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function usePaidMutation (mutation,
console.error('could not cancel old invoice', err)
}

console.log(response)
if (response.retriable) {
// every wallet failed, but we can still retry, so let's ask for a better invoice
console.log('could not pay with any wallet, will retry with a new invoice...')
Expand All @@ -77,19 +78,25 @@ export function usePaidMutation (mutation,
}
}

// we try one last time using fee credits
try {
console.log('could not pay with any wallet, will try with fee credits...')
const retry = await retryPaidAction({ variables: { invoiceId: parseInt(invoice.id), forceFeeCredits: true } })
response = retry.data?.retryPaidAction
invoice = undefined
return { invoice, response }
} catch (err) {
console.log('could not pay with fee credits, will fallback to another method')
walletErrors.push(err)
console.log(response)
if (response.retriableWithFeeCredits) {
// we try one last time using fee credits
try {
console.log('could not pay with any wallet, will try with fee credits...')
const retry = await retryPaidAction({ variables: { invoiceId: parseInt(invoice.id), forceFeeCredits: true } })
response = retry.data?.retryPaidAction
invoice = undefined
return { invoice, response }
} catch (err) {
console.log('could not pay with fee credits, will fallback to another method')
walletErrors.push(err)
}
} else {
console.log('actions is not retriable with fee credits, will fallback to another method')
}

if (alwaysShowQROnFailure) {
console.log(response)
if (alwaysShowQROnFailure && response.retriableManually) {
console.log('show qr code for manual payment')
const retry = await retryPaidAction({ variables: { invoiceId: parseInt(invoice.id) } })
response = retry.data?.retryPaidAction
Expand Down Expand Up @@ -187,7 +194,7 @@ export function usePaidMutation (mutation,
function addOptimisticResponseExtras (optimisticResponse) {
if (!optimisticResponse) return optimisticResponse
const key = Object.keys(optimisticResponse)[0]
optimisticResponse[key] = { invoice: null, paymentMethod: 'OPTIMISTIC', ...optimisticResponse[key] }
optimisticResponse[key] = { invoice: null, paymentMethod: 'OPTIMISTIC', retriable: true, retriableManually: true, retriableWithFeeCredits: true, ...optimisticResponse[key] }
return optimisticResponse
}

Expand Down
2 changes: 2 additions & 0 deletions fragments/paidAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const PAID_ACTION = gql`
}
paymentMethod
retriable
retriableWithFeeCredits
retriableManually
}`

const ITEM_PAID_ACTION_FIELDS = gql`
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "Invoice" ADD COLUMN "actionAttempt" INTEGER NOT NULL DEFAULT 0,
ADD COLUMN "retriable" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN "retriableManually" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN "retriableWithFeeCredits" BOOLEAN NOT NULL DEFAULT false;
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -913,8 +913,8 @@ model Invoice {
comment String?
lud18Data Json?
retriable Boolean @default(false)
retriableWithFeeCredits Boolean @default(true)
retriableManually Boolean @default(true)
retriableWithFeeCredits Boolean @default(false)
retriableManually Boolean @default(false)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
invoiceForward InvoiceForward?
Expand Down

0 comments on commit 860fab0

Please sign in to comment.