fix(corelightningrest): compute payment status fee as sent minus amount (#4300)
What changed, and why it matters
This commit fixes a bug in how LNbits calculates the fee for a completed Core Lightning REST payment. The old code incorrectly subtracted a negative 'amount sent' value, which produced the wrong fee. The fix changes the formula so the fee is simply the amount actually sent minus the original invoice amount. The test fixture was also updated to reflect that Core Lightning reports amount_sent_msat as a positive number, not negative.
Review whether any deployed instances using Core Lightning REST wallet reported incorrect fees, and verify that the corrected fee calculation matches Core Lightning's actual API output. No immediate emergency action is indicated, but the fix should be included in the next release.
Security signals we found
Incorrect arithmetic in financial/fee calculation
Test fixture updated to match corrected sign convention
Wallet backend payment status logic changed
Evidence from the diff
In lnbits/wallets/corelightningrest.py, get_payment_status() computes fee_msat for completed payments. The previous code did -int(pay["amount_sent_msat"][:-4]) - int(pay["amount_msat"][:-4]), which assumed amount_sent_msat was negative. The patch changes it to int(pay["amount_sent_msat"][:-4]) - int(pay["amount_msat"][:-4]). The accompanying test fixture changes amount_sent_msat from “-22000msat” to “22000msat”. This corrects the fee calculation when amount_sent_msat is positive.
Changed components
lnbits/wallets/corelightningrest.pytests/wallets/fixtures/json/fixtures_rest.jsonInspect captured patch +2 / −2
### lnbits/wallets/corelightningrest.py
@@ -272,7 +272,7 @@ async def get_payment_status(self, checking_id: str) -> PaymentStatus:
fee_msat, preimage = None, None
if self.statuses[pay["status"]]:
# cut off "msat" and convert to int
- fee_msat = -int(pay["amount_sent_msat"][:-4]) - int(
+ fee_msat = int(pay["amount_sent_msat"][:-4]) - int(
pay["amount_msat"][:-4]
)
preimage = pay["preimage"]
### tests/wallets/fixtures/json/fixtures_rest.json
@@ -2529,7 +2529,7 @@
{
"status": "complete",
"amount_msat": "21000msat",
- "amount_sent_msat": "-22000msat",
+ "amount_sent_msat": "22000msat",
"preimage": "0000000000000000000000000000000000000000000000000000000000000000"
}
]Why this scored 35/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.