@@ -210,35 +210,39 @@ private class SwapMaker(remoteNodeId: PublicKey, shortChannelId: ShortChannelId,
210210 }
211211 }
212212
213- def createOpeningTx (request : SwapRequest , agreement : SwapAgreement , isInitiator : Boolean ): Behavior [SwapCommand ] = {
214- val receivePayment = ReceiveStandardPayment (Some (toMilliSatoshi(Satoshi (request.amount))), Left (" send-swap-in" ))
215- val createInvoice = context.spawnAnonymous(CreateInvoiceActor (nodeParams))
216- createInvoice ! CreateInvoiceActor .CreateInvoice (context.messageAdapter[Bolt11Invoice ](InvoiceResponse ).toClassic, receivePayment)
217-
218- receiveSwapMessage[CreateOpeningTxMessages ](context, " createOpeningTx" ) {
219- case InvoiceResponse (invoice : Bolt11Invoice ) => fundOpening(wallet, feeRatePerKw)((request.amount + agreement.premium).sat, makerPubkey(request.swapId), takerPubkey(request, agreement, isInitiator), invoice)
220- Behaviors .same
221- case OpeningTxFunded (invoice, fundingResponse) =>
222- commitOpening(wallet)(request.swapId, invoice, fundingResponse, " swap-in-sender-opening" )
223- Behaviors .same
224- case OpeningTxCommitted (invoice, openingTxBroadcasted) =>
225- db.add(SwapData (request, agreement, invoice, openingTxBroadcasted, Maker , isInitiator, remoteNodeId))
226- awaitClaimPayment(request, agreement, invoice, openingTxBroadcasted, isInitiator)
227- case OpeningTxFailed (error, None ) => swapCanceled(InternalError (request.swapId, s " failed to fund swap open tx, error: $error" ))
228- case OpeningTxFailed (error, Some (r)) => rollback(wallet)(error, r.fundingTx)
229- Behaviors .same
230- case RollbackSuccess (error, value) => swapCanceled(InternalError (request.swapId, s " rollback: Success( $value), error: $error" ))
231- case RollbackFailure (error, t) => swapCanceled(InternalError (request.swapId, s " rollback exception: $t, error: $error" ))
232- case SwapMessageReceived (_) => Behaviors .same // ignore
233- case StateTimeout =>
234- // TODO: are we sure the opening transaction has not yet been committed? should we rollback locked funding outputs?
235- swapCanceled(InternalError (request.swapId, " timeout during CreateOpeningTx" ))
236- case CancelRequested (replyTo) => replyTo ! SwapError (request.swapId, " Can not cancel swap after opening tx committed." )
237- Behaviors .same // ignore
238- case GetStatus (replyTo) => replyTo ! SwapStatus (request.swapId, context.self.toString, " createOpeningTx" , request, Some (agreement))
239- Behaviors .same
213+ def createOpeningTx (request : SwapRequest , agreement : SwapAgreement , isInitiator : Boolean ): Behavior [SwapCommand ] =
214+ db.find(request.swapId) match {
215+ case Some (s : SwapData ) =>
216+ awaitClaimPayment(request, agreement, s.invoice, s.openingTxBroadcasted, isInitiator)
217+ case None =>
218+ val receivePayment = ReceiveStandardPayment (Some (toMilliSatoshi(Satoshi (request.amount))), Left (" send-swap-in" ))
219+ val createInvoice = context.spawnAnonymous(CreateInvoiceActor (nodeParams))
220+ createInvoice ! CreateInvoiceActor .CreateInvoice (context.messageAdapter[Bolt11Invoice ](InvoiceResponse ).toClassic, receivePayment)
221+
222+ receiveSwapMessage[CreateOpeningTxMessages ](context, " createOpeningTx" ) {
223+ case InvoiceResponse (invoice : Bolt11Invoice ) => fundOpening(wallet, feeRatePerKw)((request.amount + agreement.premium).sat, makerPubkey(request.swapId), takerPubkey(request, agreement, isInitiator), invoice)
224+ Behaviors .same
225+ case OpeningTxFunded (invoice, fundingResponse) =>
226+ commitOpening(wallet)(request.swapId, invoice, fundingResponse, " swap-in-sender-opening" )
227+ Behaviors .same
228+ case OpeningTxCommitted (invoice, openingTxBroadcasted) =>
229+ db.add(SwapData (request, agreement, invoice, openingTxBroadcasted, Maker , isInitiator, remoteNodeId))
230+ awaitClaimPayment(request, agreement, invoice, openingTxBroadcasted, isInitiator)
231+ case OpeningTxFailed (error, None ) => swapCanceled(InternalError (request.swapId, s " failed to fund swap open tx, error: $error" ))
232+ case OpeningTxFailed (error, Some (r)) => rollback(wallet)(error, r.fundingTx)
233+ Behaviors .same
234+ case RollbackSuccess (error, value) => swapCanceled(InternalError (request.swapId, s " rollback: Success( $value), error: $error" ))
235+ case RollbackFailure (error, t) => swapCanceled(InternalError (request.swapId, s " rollback exception: $t, error: $error" ))
236+ case SwapMessageReceived (_) => Behaviors .same // ignore
237+ case StateTimeout =>
238+ // TODO: are we sure the opening transaction has not yet been committed? should we rollback locked funding outputs?
239+ swapCanceled(InternalError (request.swapId, " timeout during CreateOpeningTx" ))
240+ case CancelRequested (replyTo) => replyTo ! SwapError (request.swapId, " Can not cancel swap after opening tx committed." )
241+ Behaviors .same // ignore
242+ case GetStatus (replyTo) => replyTo ! SwapStatus (request.swapId, context.self.toString, " createOpeningTx" , request, Some (agreement))
243+ Behaviors .same
244+ }
240245 }
241- }
242246
243247 def awaitClaimPayment (request : SwapRequest , agreement : SwapAgreement , invoice : Bolt11Invoice , openingTxBroadcasted : OpeningTxBroadcasted , isInitiator : Boolean ): Behavior [SwapCommand ] =
244248 nodeParams.db.payments.getIncomingPayment(invoice.paymentHash) match {
0 commit comments