Skip to content

Commit

Permalink
Fixed crash Swift runtime failure: arithmetic overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
azisramdhan committed Dec 22, 2024
1 parent c3c60fa commit c140d82
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions litewallet/ViewModels/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c140d82

Please sign in to comment.