@@ -7,15 +7,14 @@ import { msatsToSats } from '@/lib/format'
77import { payInBolt11Prospect , payInBolt11WrapProspect } from './lib/payInBolt11'
88import { isPessimistic , isProxyPayment , isWithdrawal } from './lib/is'
99import { PAY_IN_INCLUDE , payInCreate } from './lib/payInCreate'
10- import { NoReceiveWalletError , payOutBolt11Replacement } from './lib/payOutBolt11'
1110import { payInClone } from './lib/payInPrisma'
1211import { createHmac } from '../resolvers/wallet'
13- import { payOutCustodialTokenFromBolt11 } from './lib/payOutCustodialTokens'
1412
1513// grab a greedy connection for the payIn system on any server
1614// if we have lock contention of payIns, we don't want to block other queries
1715import createPrisma from '@/lib/create-prisma'
1816import { PayInFailureReasonError } from './errors'
17+ import { payInReplacePayOuts } from './lib/payInFailed'
1918const models = createPrisma ( { connectionParams : { connection_limit : 2 } } )
2019
2120export default async function pay ( payInType , payInArgs , { me, custodialOnly } ) {
@@ -371,43 +370,24 @@ export async function retry (payInId, { me }) {
371370 }
372371 const where = { id : payInId , userId : me . id , payInState : 'FAILED' , successorId : null , benefactorId : null }
373372
374- const payInFailed = await models . payIn . findFirst ( {
373+ const payInFailedInitial = await models . payIn . findFirst ( {
375374 where,
376375 include : { ...include , beneficiaries : { include } }
377376 } )
378- if ( ! payInFailed ) {
377+ if ( ! payInFailedInitial ) {
379378 throw new Error ( 'PayIn with id ' + payInId + ' not found' )
380379 }
381- if ( isWithdrawal ( payInFailed ) ) {
380+ if ( isWithdrawal ( payInFailedInitial ) ) {
382381 throw new Error ( 'Withdrawal payIns cannot be retried' )
383382 }
384- if ( isPessimistic ( payInFailed , { me } ) ) {
383+ if ( isPessimistic ( payInFailedInitial , { me } ) ) {
385384 throw new Error ( 'Pessimistic payIns cannot be retried' )
386385 }
387386
388- let payOutBolt11
389- if ( payInFailed . payOutBolt11 ) {
390- try {
391- payOutBolt11 = await payOutBolt11Replacement ( models , payInFailed . genesisId ?? payInFailed . id , payInFailed . payOutBolt11 )
392- } catch ( e ) {
393- console . error ( 'payOutBolt11Replacement failed' , e )
394- if ( ! ( e instanceof NoReceiveWalletError ) ) {
395- throw e
396- }
397- // if we can no longer produce a payOutBolt11, we fallback to custodial tokens
398- payInFailed . payOutCustodialTokens . push ( payOutCustodialTokenFromBolt11 ( payInFailed . payOutBolt11 ) )
399- // convert the routing fee to another rewards pool output
400- const routingFee = payInFailed . payOutCustodialTokens . find ( t => t . payOutType === 'ROUTING_FEE' )
401- if ( routingFee ) {
402- routingFee . payOutType = 'REWARDS_POOL'
403- routingFee . userId = USER_ID . rewards
404- }
405- payInFailed . payOutBolt11 = null
406- }
407- }
387+ const payInFailed = await payInReplacePayOuts ( models , payInFailedInitial )
408388
409389 const { payIn, result, mCostRemaining } = await models . $transaction ( async tx => {
410- const payInInitial = { ...payInClone ( { ... payInFailed , payOutBolt11 } ) , retryCount : payInFailed . retryCount + 1 }
390+ const payInInitial = { ...payInClone ( payInFailed ) , retryCount : payInFailed . retryCount + 1 }
411391 await obtainRowLevelLocks ( tx , payInInitial )
412392 const { payIn, mCostRemaining } = await payInCreate ( tx , payInInitial , undefined , { me } )
413393
0 commit comments