Skip to content

Commit 5294b4f

Browse files
committed
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.
1 parent e1ddb50 commit 5294b4f

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

loopin.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -919,9 +919,7 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
919919
s.log.Infof("Htlc spend by tx: %v",
920920
spendDetails.SpenderTxHash)
921921

922-
err := s.processHtlcSpend(
923-
ctx, spendDetails, htlcValue, sweepFee,
924-
)
922+
err := s.processHtlcSpend(ctx, spendDetails, sweepFee)
925923
if err != nil {
926924
return err
927925
}
@@ -959,8 +957,6 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
959957
switch update.State {
960958
// Swap invoice was paid, so update server cost balance.
961959
case invpkg.ContractSettled:
962-
s.cost.Server -= update.AmtPaid
963-
964960
// If invoice settlement and htlc spend happen
965961
// in the expected order, move the swap to an
966962
// intermediate state that indicates that the
@@ -977,6 +973,8 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
977973

978974
invoiceFinalized = true
979975
htlcKeyRevealed = s.tryPushHtlcKey(ctx)
976+
s.cost.Server = s.AmountRequested -
977+
update.AmtPaid
980978

981979
// Canceled invoice has no effect on server cost
982980
// balance.
@@ -1023,19 +1021,14 @@ func (s *loopInSwap) tryPushHtlcKey(ctx context.Context) bool {
10231021
}
10241022

10251023
func (s *loopInSwap) processHtlcSpend(ctx context.Context,
1026-
spend *chainntnfs.SpendDetail, htlcValue,
1027-
sweepFee btcutil.Amount) error {
1024+
spend *chainntnfs.SpendDetail, sweepFee btcutil.Amount) error {
10281025

10291026
// Determine the htlc input of the spending tx and inspect the witness
10301027
// to find out whether a success or a timeout tx spent the htlc.
10311028
htlcInput := spend.SpendingTx.TxIn[spend.SpenderInputIndex]
10321029

10331030
if s.htlc.IsSuccessWitness(htlcInput.Witness) {
10341031
s.setState(loopdb.StateSuccess)
1035-
1036-
// Server swept the htlc. The htlc value can be added to the
1037-
// server cost balance.
1038-
s.cost.Server += htlcValue
10391032
} else {
10401033
// We needed another on chain tx to sweep the timeout clause,
10411034
// which we now include in our costs.

loopout.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ func (s *loopOutSwap) handlePaymentResult(result paymentResult) error {
452452
return nil
453453

454454
case result.status.State == lnrpc.Payment_SUCCEEDED:
455-
s.cost.Server += result.status.Value.ToSatoshis()
455+
s.cost.Server += result.status.Value.ToSatoshis() -
456+
s.AmountRequested
456457
s.cost.Offchain += result.status.Fee.ToSatoshis()
457458

458459
return nil
@@ -539,9 +540,7 @@ func (s *loopOutSwap) executeSwap(globalCtx context.Context) error {
539540

540541
sweepSuccessful := s.htlc.IsSuccessWitness(htlcInput.Witness)
541542
if sweepSuccessful {
542-
s.cost.Server -= htlcValue
543543
s.cost.Onchain = spend.OnChainFeePortion
544-
545544
s.state = loopdb.StateSuccess
546545
} else {
547546
s.state = loopdb.StateFailSweepTimeout

0 commit comments

Comments
 (0)