diff --git a/p2p_wallet/Common/Extensions/Double+Extensions.swift b/p2p_wallet/Common/Extensions/Double+Extensions.swift index 0d4058ac9f..26c12f10b6 100644 --- a/p2p_wallet/Common/Extensions/Double+Extensions.swift +++ b/p2p_wallet/Common/Extensions/Double+Extensions.swift @@ -145,3 +145,18 @@ extension Double { "\(toString(maximumFractionDigits: maximumFractionDigits, roundingMode: roundingMode)) \(currency.code)" } } + +extension Double { + // Check if lamport value is smaller than UInt64 max + func isLamportsBiggerThanUInt64(decimals: Int) -> Bool { + let value = (self * Double.pow(10, Double(decimals))).rounded() + let maxValue = Decimal(UInt64.max) + + if Decimal(value) > maxValue { + debugPrint("\(value) overflows Int64 max \(maxValue)") + return true + } + + return false + } +} diff --git a/p2p_wallet/Scenes/Main/Send/Input/Subviews/AmountView/SendInputAmountField.swift b/p2p_wallet/Scenes/Main/Send/Input/Subviews/AmountView/SendInputAmountField.swift index 6c1c687e8d..6b31f0eef4 100644 --- a/p2p_wallet/Scenes/Main/Send/Input/Subviews/AmountView/SendInputAmountField.swift +++ b/p2p_wallet/Scenes/Main/Send/Input/Subviews/AmountView/SendInputAmountField.swift @@ -103,6 +103,10 @@ struct SendInputAmountField: UIViewRepresentable { let number = Double(updatedText.replacingOccurrences(of: " ", with: "")) + if number?.isLamportsBiggerThanUInt64(decimals: countAfterDecimalPoint) == true { + return false + } + if (string.isEmpty || string.starts(with: "0")) && updatedText.starts(with: "0\(decimalSeparator)") && number == 0 diff --git a/p2p_wallet/UI/SwiftUI/TextField/AmountTextField.swift b/p2p_wallet/UI/SwiftUI/TextField/AmountTextField.swift index b486717c65..d192b298f9 100644 --- a/p2p_wallet/UI/SwiftUI/TextField/AmountTextField.swift +++ b/p2p_wallet/UI/SwiftUI/TextField/AmountTextField.swift @@ -173,12 +173,21 @@ final class AmountUITextField: UITextField, UITextFieldDelegate { let updatedText = text .replacingCharacters(in: textRange, with: string) .replacingOccurrences(of: decimalSeparator == "." ? "," : ".", with: decimalSeparator) + if (updatedText.components(separatedBy: decimalSeparator).count - 1) > 1 { return false } - if updatedText.components(separatedBy: decimalSeparator).last?.count ?? 0 > maxFractionDigits.wrappedValue { + + if updatedText.contains(decimalSeparator) && updatedText.components(separatedBy: decimalSeparator).last? + .count ?? 0 > maxFractionDigits.wrappedValue + { + return false + } + + if Double(updatedText)?.isLamportsBiggerThanUInt64(decimals: maxFractionDigits.wrappedValue) == true { return false } + return isNotMoreThanMax(text: updatedText.amountFormat( maxAfterComma: maxFractionDigits.wrappedValue, decimalSeparator: decimalSeparator