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.
pull/694/head
Andras Banki-Horvath 3 months ago
parent e1ddb50dfe
commit 5294b4ff07
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8

@ -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.

@ -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

Loading…
Cancel
Save