Skip to content

Commit a48944a

Browse files
committed
loopout: fix negative reported fees
1 parent 2a3c70f commit a48944a

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

loopout.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,11 @@ func (s *loopOutSwap) handlePaymentResult(result paymentResult) error {
456456
return nil
457457

458458
case result.status.State == lnrpc.Payment_SUCCEEDED:
459-
s.cost.Server += result.status.Value.ToSatoshis() -
460-
s.AmountRequested
461-
s.cost.Offchain += result.status.Fee.ToSatoshis()
459+
if result.status.Preimage == s.SwapContract.Preimage {
460+
s.cost.Server += result.status.Value.ToSatoshis() -
461+
s.AmountRequested
462+
s.cost.Offchain += result.status.Fee.ToSatoshis()
463+
}
462464

463465
return nil
464466

sweepbatcher/sweep_batch.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,9 +1158,8 @@ func (b *batch) monitorConfirmations(ctx context.Context) error {
11581158
func getFeePortionForSweep(spendTx *wire.MsgTx, numSweeps int,
11591159
totalSweptAmt btcutil.Amount) (btcutil.Amount, btcutil.Amount) {
11601160

1161-
totalFee := spendTx.TxOut[0].Value - int64(totalSweptAmt)
1162-
feePortionPerSweep := (int64(totalSweptAmt) -
1163-
spendTx.TxOut[0].Value) / int64(numSweeps)
1161+
totalFee := int64(totalSweptAmt) - spendTx.TxOut[0].Value
1162+
feePortionPerSweep := totalFee / int64(numSweeps)
11641163
roundingDiff := totalFee - (int64(numSweeps) * feePortionPerSweep)
11651164

11661165
return btcutil.Amount(feePortionPerSweep), btcutil.Amount(roundingDiff)

sweepbatcher/sweep_batcher_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,3 +984,48 @@ func TestSweepBatcherComposite(t *testing.T) {
984984
require.True(t, batcherStore.AssertSweepStored(sweepReq5.SwapHash))
985985
require.True(t, batcherStore.AssertSweepStored(sweepReq6.SwapHash))
986986
}
987+
988+
// makeTestTx creates a test transaction with a single output of the given
989+
// value.
990+
func makeTestTx(value int64) *wire.MsgTx {
991+
tx := wire.NewMsgTx(wire.TxVersion)
992+
tx.AddTxOut(wire.NewTxOut(value, nil))
993+
return tx
994+
}
995+
996+
// TestGetFeePortionForSweep tests that the fee portion for a sweep is correctly
997+
// calculated.
998+
func TestGetFeePortionForSweep(t *testing.T) {
999+
tests := []struct {
1000+
name string
1001+
spendTxValue int64
1002+
numSweeps int
1003+
totalSweptAmt btcutil.Amount
1004+
expectedFeePortion btcutil.Amount
1005+
expectedRoundingDiff btcutil.Amount
1006+
}{
1007+
{
1008+
"Even Split",
1009+
100, 5, 200, 20, 0,
1010+
},
1011+
{
1012+
"Single Sweep",
1013+
100, 1, 200, 100, 0,
1014+
},
1015+
{
1016+
"With Rounding Diff",
1017+
200, 4, 350, 37, 2,
1018+
},
1019+
}
1020+
1021+
for _, tt := range tests {
1022+
t.Run(tt.name, func(t *testing.T) {
1023+
spendTx := makeTestTx(tt.spendTxValue)
1024+
feePortion, roundingDiff := getFeePortionForSweep(
1025+
spendTx, tt.numSweeps, tt.totalSweptAmt,
1026+
)
1027+
require.Equal(t, tt.expectedFeePortion, feePortion)
1028+
require.Equal(t, tt.expectedRoundingDiff, roundingDiff)
1029+
})
1030+
}
1031+
}

0 commit comments

Comments
 (0)