From bcd229af935b8e7387e7ca23df6b791872634134 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 19 Nov 2024 16:31:26 +0100 Subject: [PATCH] Fix 'Cannot mix BigInt and other types' on zaps (#1609) * Fix 'Cannot mix BigInt and other types' on zaps * Also add sybilFeePercent to context in worker * add paymentMethod to context --------- Co-authored-by: k00b --- api/paidAction/index.js | 30 +++++++++++++----------------- api/paidAction/receive.js | 6 ++++-- worker/paidAction.js | 6 +++--- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/api/paidAction/index.js b/api/paidAction/index.js index 9397a50c..01fdb227 100644 --- a/api/paidAction/index.js +++ b/api/paidAction/index.js @@ -56,7 +56,8 @@ export default async function performPaidAction (actionType, args, incomingConte } const context = { ...contextWithMe, - cost: await paidAction.getCost(args, contextWithMe) + cost: await paidAction.getCost(args, contextWithMe), + sybilFeePercent: await paidAction.getSybilFeePercent?.(args, contextWithMe) } // special case for zero cost actions @@ -67,6 +68,7 @@ export default async function performPaidAction (actionType, args, incomingConte for (const paymentMethod of paidAction.paymentMethods) { console.log(`considering payment method ${paymentMethod}`) + const contextWithPaymentMethod = { ...context, paymentMethod } if (forcePaymentMethod && paymentMethod !== forcePaymentMethod) { @@ -77,7 +79,7 @@ export default async function performPaidAction (actionType, args, incomingConte // payment methods that anonymous users can use if (paymentMethod === PAID_ACTION_PAYMENT_METHODS.P2P) { try { - return await performP2PAction(actionType, args, context) + return await performP2PAction(actionType, args, contextWithPaymentMethod) } catch (e) { if (e instanceof NonInvoiceablePeerError) { console.log('peer cannot be invoiced, skipping') @@ -87,14 +89,14 @@ export default async function performPaidAction (actionType, args, incomingConte throw e } } else if (paymentMethod === PAID_ACTION_PAYMENT_METHODS.PESSIMISTIC) { - return await beginPessimisticAction(actionType, args, context) + return await beginPessimisticAction(actionType, args, contextWithPaymentMethod) } - // additionalpayment methods that logged in users can use + // additional payment methods that logged in users can use if (me) { if (paymentMethod === PAID_ACTION_PAYMENT_METHODS.FEE_CREDIT) { try { - return await performNoInvoiceAction(actionType, args, { ...context, paymentMethod }) + return await performNoInvoiceAction(actionType, args, contextWithPaymentMethod) } catch (e) { // if we fail with fee credits or reward sats, but not because of insufficient funds, bail console.error(`${paymentMethod} action failed`, e) @@ -103,7 +105,7 @@ export default async function performPaidAction (actionType, args, incomingConte } } } else if (paymentMethod === PAID_ACTION_PAYMENT_METHODS.OPTIMISTIC) { - return await performOptimisticAction(actionType, args, context) + return await performOptimisticAction(actionType, args, contextWithPaymentMethod) } } } @@ -186,25 +188,19 @@ async function beginPessimisticAction (actionType, args, context) { async function performP2PAction (actionType, args, incomingContext) { // if the action has an invoiceable peer, we'll create a peer invoice // wrap it, and return the wrapped invoice - const { cost, models, lnd, me } = incomingContext - const sybilFeePercent = await paidActions[actionType].getSybilFeePercent?.(args, incomingContext) + const { cost, sybilFeePercent, models, lnd, me } = incomingContext if (!sybilFeePercent) { throw new Error('sybil fee percent is not set for an invoiceable peer action') } - const contextWithSybilFeePercent = { - ...incomingContext, - sybilFeePercent - } - - const userId = await paidActions[actionType]?.getInvoiceablePeer?.(args, contextWithSybilFeePercent) + const userId = await paidActions[actionType]?.getInvoiceablePeer?.(args, incomingContext) if (!userId) { throw new NonInvoiceablePeerError() } - await assertBelowMaxPendingInvoices(contextWithSybilFeePercent) + await assertBelowMaxPendingInvoices(incomingContext) - const description = await paidActions[actionType].describe(args, contextWithSybilFeePercent) + const description = await paidActions[actionType].describe(args, incomingContext) const { invoice, wrappedInvoice, wallet, maxFee } = await createWrappedInvoice(userId, { msats: cost, feePercent: sybilFeePercent, @@ -213,7 +209,7 @@ async function performP2PAction (actionType, args, incomingContext) { }, { models, me, lnd }) const context = { - ...contextWithSybilFeePercent, + ...incomingContext, invoiceArgs: { bolt11: invoice, wrappedBolt11: wrappedInvoice, diff --git a/api/paidAction/receive.js b/api/paidAction/receive.js index f44e2bca..6f39c11f 100644 --- a/api/paidAction/receive.js +++ b/api/paidAction/receive.js @@ -53,8 +53,10 @@ export async function perform ({ } } -export async function describe ({ description }, { me, cost, sybilFeePercent }) { - const fee = sybilFeePercent ? cost * BigInt(sybilFeePercent) / 100n : 0n +export async function describe ({ description }, { me, cost, paymentMethod, sybilFeePercent }) { + const fee = paymentMethod === PAID_ACTION_PAYMENT_METHODS.P2P + ? cost * BigInt(sybilFeePercent) / 100n + : 0n return description ?? `SN: ${me?.name ?? ''} receives ${numWithUnits(msatsToSats(cost - fee))}` } diff --git a/worker/paidAction.js b/worker/paidAction.js index 8e9fdbd4..23adb024 100644 --- a/worker/paidAction.js +++ b/worker/paidAction.js @@ -119,11 +119,11 @@ async function performPessimisticAction ({ lndInvoice, dbInvoice, tx, models, ln const context = { tx, cost: BigInt(lndInvoice.received_mtokens), - me: dbInvoice.user + me: dbInvoice.user, + sybilFeePercent: await paidActions[dbInvoice.actionType].getSybilFeePercent?.() } - const sybilFeePercent = await paidActions[dbInvoice.actionType].getSybilFeePercent?.(args, context) - const result = await paidActions[dbInvoice.actionType].perform(args, { ...context, sybilFeePercent }) + const result = await paidActions[dbInvoice.actionType].perform(args, context) await tx.invoice.update({ where: { id: dbInvoice.id }, data: {