diff --git a/litewallet/ViewModels/Transaction.swift b/litewallet/ViewModels/Transaction.swift index 8dc0b840..e2276353 100644 --- a/litewallet/ViewModels/Transaction.swift +++ b/litewallet/ViewModels/Transaction.swift @@ -40,14 +40,23 @@ class Transaction { self.fee = fee + opsAmount let amountReceived = wallet.amountReceivedFromTx(tx) - let amountSent = wallet.amountSentByTx(tx) - opsAmount + + // This ensures that amountSent is always non-negative and within the valid range for UInt64. + let amountSent = wallet.amountSentByTx(tx) > opsAmount + ? wallet.amountSentByTx(tx) - opsAmount + : 0 if amountSent > 0, (amountReceived + fee) == amountSent { direction = .moved satoshis = amountSent } else if amountSent > 0 { direction = .sent - satoshis = amountSent - amountReceived - fee + // This ensures that satoshis is always non-negative and within the valid range for UInt64. + if amountSent >= (amountReceived + fee) { + satoshis = amountSent - amountReceived - fee + } else { + satoshis = 0 + } } else { direction = .received satoshis = amountReceived