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
extension Double {
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 {
guard let currency = Double.currencyFormatter.string(from: NSNumber(value: self)) else {
return ""
......@@ -19,10 +27,6 @@ extension Double {
return currency
}
public func toXMR() -> UInt64 {
return UInt64(self * Double(Constants.atomicUnitsPerMonero))
}
private static let currencyFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.allowsFloats = true
......
......@@ -483,19 +483,18 @@ extension WalletCoordinator: AmountVCDelegate {
return
}
let requestedXmrInAtomicUnitsDouble = requestedXmrDouble * Double(Constants.atomicUnitsPerMonero)
let requestedXmrInAtomicUnits = UInt64(exactly: requestedXmrInAtomicUnitsDouble)
let requestedXmrInAtomicUnits = (requestedXmrDouble).toXMR()
let fiatValue = self.otherAmount(forXMRValue: requestedXmrInAtomicUnits)
viewController.showFiatValue(fiatValue, forCurrency: self.propertyStore.currency)
let available = self.moneroBag.wallet?.balance ?? 0
if (requestedXmrInAtomicUnits != nil && requestedXmrInAtomicUnits! > 0 && requestedXmrInAtomicUnits! <= available) {
if requestedXmrInAtomicUnits != nil && requestedXmrInAtomicUnits! > 0 && requestedXmrInAtomicUnits! <= available {
viewController.nextAllowed()
} else {
viewController.nextNotAllowed()
}
}
private func showPaymentIdViewController() {
let vc = self.storyboard.instantiateViewController(withIdentifier: PaymentIdSceneName) as! PaymentIdVC
......
......@@ -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.
super.tearDown()
}
public func test_toXMR() {
let expectedXmr: UInt64 = 22080000000000
public func test_toXMR_Success() {
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