From 5294b4ff07a57e1c06cacd2dc00fe1eeba9d03bc Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Tue, 6 Feb 2024 17:04:41 +0100 Subject: [PATCH] loop: clean up server cost calculation for slightly better UX Previously we'd calculate the server costs (swap fees) by accounting for both the on-chain HTLC and the off-chain payment which was confusing as server cost fluctuated by the amount of the swap itself. Now we'll only add the actual cost when the swap happened, so the server cost will go form zero to the actual fee value paid. --- loopin.go | 15 ++++----------- loopout.go | 5 ++--- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/loopin.go b/loopin.go index 5aecb67..b92a3cf 100644 --- a/loopin.go +++ b/loopin.go @@ -919,9 +919,7 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context, s.log.Infof("Htlc spend by tx: %v", spendDetails.SpenderTxHash) - err := s.processHtlcSpend( - ctx, spendDetails, htlcValue, sweepFee, - ) + err := s.processHtlcSpend(ctx, spendDetails, sweepFee) if err != nil { return err } @@ -959,8 +957,6 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context, switch update.State { // Swap invoice was paid, so update server cost balance. case invpkg.ContractSettled: - s.cost.Server -= update.AmtPaid - // If invoice settlement and htlc spend happen // in the expected order, move the swap to an // intermediate state that indicates that the @@ -977,6 +973,8 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context, invoiceFinalized = true htlcKeyRevealed = s.tryPushHtlcKey(ctx) + s.cost.Server = s.AmountRequested - + update.AmtPaid // Canceled invoice has no effect on server cost // balance. @@ -1023,8 +1021,7 @@ func (s *loopInSwap) tryPushHtlcKey(ctx context.Context) bool { } func (s *loopInSwap) processHtlcSpend(ctx context.Context, - spend *chainntnfs.SpendDetail, htlcValue, - sweepFee btcutil.Amount) error { + spend *chainntnfs.SpendDetail, sweepFee btcutil.Amount) error { // Determine the htlc input of the spending tx and inspect the witness // to find out whether a success or a timeout tx spent the htlc. @@ -1032,10 +1029,6 @@ func (s *loopInSwap) processHtlcSpend(ctx context.Context, if s.htlc.IsSuccessWitness(htlcInput.Witness) { s.setState(loopdb.StateSuccess) - - // Server swept the htlc. The htlc value can be added to the - // server cost balance. - s.cost.Server += htlcValue } else { // We needed another on chain tx to sweep the timeout clause, // which we now include in our costs. diff --git a/loopout.go b/loopout.go index 9a0ab0f..1fc9cdd 100644 --- a/loopout.go +++ b/loopout.go @@ -452,7 +452,8 @@ func (s *loopOutSwap) handlePaymentResult(result paymentResult) error { return nil case result.status.State == lnrpc.Payment_SUCCEEDED: - s.cost.Server += result.status.Value.ToSatoshis() + s.cost.Server += result.status.Value.ToSatoshis() - + s.AmountRequested s.cost.Offchain += result.status.Fee.ToSatoshis() return nil @@ -539,9 +540,7 @@ func (s *loopOutSwap) executeSwap(globalCtx context.Context) error { sweepSuccessful := s.htlc.IsSuccessWitness(htlcInput.Witness) if sweepSuccessful { - s.cost.Server -= htlcValue s.cost.Onchain = spend.OnChainFeePortion - s.state = loopdb.StateSuccess } else { s.state = loopdb.StateFailSweepTimeout