Commit 8bcf9694 authored by Tetsuro Takemoto's avatar Tetsuro Takemoto 🐝

fix: issue#21 the app crashes with big amount of XMR and test

parent eadc1f5f
...@@ -11,7 +11,15 @@ import Foundation ...@@ -11,7 +11,15 @@ import Foundation
extension Double { extension Double {
private static let exactNumberOfFractionDigits = 2 private static let exactNumberOfFractionDigits = 2
public func toXMR() -> UInt64? {
if self >= Double(UInt64.min) && self * Double(Constants.atomicUnitsPerMonero) < Double(UInt64.max) {
return UInt64(self * Double(Constants.atomicUnitsPerMonero))
} else {
return nil
}
}
public func toCurrency() -> String { public func toCurrency() -> String {
guard let currency = Double.currencyFormatter.string(from: NSNumber(value: self)) else { guard let currency = Double.currencyFormatter.string(from: NSNumber(value: self)) else {
return "" return ""
...@@ -19,10 +27,6 @@ extension Double { ...@@ -19,10 +27,6 @@ extension Double {
return currency return currency
} }
public func toXMR() -> UInt64 {
return UInt64(self * Double(Constants.atomicUnitsPerMonero))
}
private static let currencyFormatter: NumberFormatter = { private static let currencyFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter() let numberFormatter = NumberFormatter()
numberFormatter.allowsFloats = true numberFormatter.allowsFloats = true
......
...@@ -483,19 +483,18 @@ extension WalletCoordinator: AmountVCDelegate { ...@@ -483,19 +483,18 @@ extension WalletCoordinator: AmountVCDelegate {
return return
} }
let requestedXmrInAtomicUnitsDouble = requestedXmrDouble * Double(Constants.atomicUnitsPerMonero) let requestedXmrInAtomicUnits = (requestedXmrDouble).toXMR()
let requestedXmrInAtomicUnits = UInt64(exactly: requestedXmrInAtomicUnitsDouble)
let fiatValue = self.otherAmount(forXMRValue: requestedXmrInAtomicUnits) let fiatValue = self.otherAmount(forXMRValue: requestedXmrInAtomicUnits)
viewController.showFiatValue(fiatValue, forCurrency: self.propertyStore.currency) viewController.showFiatValue(fiatValue, forCurrency: self.propertyStore.currency)
let available = self.moneroBag.wallet?.balance ?? 0 let available = self.moneroBag.wallet?.balance ?? 0
if (requestedXmrInAtomicUnits != nil && requestedXmrInAtomicUnits! > 0 && requestedXmrInAtomicUnits! <= available) { if requestedXmrInAtomicUnits != nil && requestedXmrInAtomicUnits! > 0 && requestedXmrInAtomicUnits! <= available {
viewController.nextAllowed() viewController.nextAllowed()
} else { } else {
viewController.nextNotAllowed() viewController.nextNotAllowed()
} }
} }
private func showPaymentIdViewController() { private func showPaymentIdViewController() {
let vc = self.storyboard.instantiateViewController(withIdentifier: PaymentIdSceneName) as! PaymentIdVC let vc = self.storyboard.instantiateViewController(withIdentifier: PaymentIdSceneName) as! PaymentIdVC
......
...@@ -18,14 +18,22 @@ class Double_ExtensionTests: XCTestCase { ...@@ -18,14 +18,22 @@ class Double_ExtensionTests: XCTestCase {
// Put teardown code here. This method is called after the invocation of each test method in the class. // Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown() super.tearDown()
} }
public func test_toXMR() { public func test_toXMR_Success() {
let expectedXmr: UInt64 = 22080000000000 let givenDouble: Double = 11.01
let expectedUInt64: UInt64 = 11010000000000
let result = 22.08.toXMR() let result = givenDouble.toXMR()
XCTAssertEqual(expectedXmr, result) XCTAssertEqual(expectedUInt64, result)
} }
public func test_toXMR_Error() {
let givenDouble: Double = 99999999
let result = givenDouble.toXMR()
XCTAssertEqual(nil, result)
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment