Skip to content

Commit

Permalink
Change subunitToUnit to BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
mederic committed Jun 18, 2018
1 parent 8c9ca93 commit 3cfe0be
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
4 changes: 4 additions & 0 deletions OmiseGO.xcodeproj/project.pbxproj
Expand Up @@ -99,6 +99,7 @@
039DA975203FA94100CCC56A /* PaginationParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 039DA974203FA94100CCC56A /* PaginationParams.swift */; };
039DA977203FAB0600CCC56A /* TransactionListParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 039DA976203FAB0600CCC56A /* TransactionListParams.swift */; };
039DA979203FB53F00CCC56A /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 039DA978203FB53F00CCC56A /* Pagination.swift */; };
03AA99AB20D7AFE100AFF1F8 /* OMGNumberFormatterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03AA99AA20D7AFE100AFF1F8 /* OMGNumberFormatterTests.swift */; };
03B1E7392040127600134CB4 /* DateDummy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03B1E7382040127600134CB4 /* DateDummy.swift */; };
03BCCCB31F8B311600F604DB /* OmiseGO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03BCCCA91F8B311600F604DB /* OmiseGO.framework */; };
03C2C07D1FB97F9C00B794AC /* RequestFixtureTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03C2C07C1FB97F9C00B794AC /* RequestFixtureTest.swift */; };
Expand Down Expand Up @@ -233,6 +234,7 @@
039DA974203FA94100CCC56A /* PaginationParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationParams.swift; sourceTree = "<group>"; };
039DA976203FAB0600CCC56A /* TransactionListParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionListParams.swift; sourceTree = "<group>"; };
039DA978203FB53F00CCC56A /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = "<group>"; };
03AA99AA20D7AFE100AFF1F8 /* OMGNumberFormatterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OMGNumberFormatterTests.swift; sourceTree = "<group>"; };
03B1E7382040127600134CB4 /* DateDummy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateDummy.swift; sourceTree = "<group>"; };
03BCCCA91F8B311600F604DB /* OmiseGO.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OmiseGO.framework; sourceTree = BUILT_PRODUCTS_DIR; };
03BCCCB21F8B311600F604DB /* OmiseGOTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OmiseGOTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -521,6 +523,7 @@
031EA6892071DE7F00A34D16 /* CodingTests */,
031EA6852071DE7F00A34D16 /* HTTPTests */,
03D793B3204FA2490055D7BC /* ToolsTests.swift */,
03AA99AA20D7AFE100AFF1F8 /* OMGNumberFormatterTests.swift */,
03BCCCB91F8B311600F604DB /* Info.plist */,
0370519D2040756F009FD899 /* secret.plist */,
);
Expand Down Expand Up @@ -843,6 +846,7 @@
0396627E203FFD6B004E5640 /* Extension.swift in Sources */,
0345BE291F96FE8C00B4CA16 /* ResponseLiveTest.swift in Sources */,
034FA0441FA31C930033B8D1 /* LogoutFixtureTests.swift in Sources */,
03AA99AB20D7AFE100AFF1F8 /* OMGNumberFormatterTests.swift in Sources */,
031EA6942071DE8000A34D16 /* DecodeTests.swift in Sources */,
217782191FB5579F002CBCA7 /* WalletFixtureTests.swift in Sources */,
03317D7C20D77D7400BA60DB /* BigUIntDummy.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion OmiseGOTests/Helpers/StubGenerator.swift
Expand Up @@ -77,7 +77,7 @@ class StubGenerator {
class func token(id: String? = nil,
symbol: String? = nil,
name: String? = nil,
subUnitToUnit: UInt64? = nil,
subUnitToUnit: BigInt? = nil,
metadata: [String: Any]? = nil,
encryptedMetadata: [String: Any]? = nil,
createdAt: Date? = nil,
Expand Down
53 changes: 53 additions & 0 deletions OmiseGOTests/OMGNumberFormatterTests.swift
@@ -0,0 +1,53 @@
//
// OMGNumberFormatterTests.swift
// OmiseGOTests
//
// Created by Mederic Petit on 18/6/18.
// Copyright © 2018 Omise Go Pte. Ltd. All rights reserved.
//

@testable import OmiseGO
import XCTest
import BigInt

class OMGNumberFormatterTests: XCTestCase {

let locale = NSLocale.current

func testStringToBigIntWithSubunitToUnit() {
let sut = OMGNumberFormatter(locale: self.locale, precision: 1)
let result = sut.number(from: "10", subunitToUnit: 1000000000000000000)
XCTAssertEqual(result, BigInt("10000000000000000000"))
}

func testStringToBigIntWithDecimal() {
let sut = OMGNumberFormatter(locale: self.locale, precision: 1)
let result = sut.number(from: "10", decimals: 18)
XCTAssertEqual(result, BigInt("10000000000000000000"))
}

func testStringToBigIntWithDecimalAndPrecision() {
let sut = OMGNumberFormatter(locale: self.locale, precision: 10000)
let result = sut.number(from: "10", decimals: 18)
XCTAssertEqual(result, BigInt("10000000000000000000"))
}

func testStringFromBigIntWithSubunitToUnit() {
let sut = OMGNumberFormatter(locale: self.locale, precision: 1)
let result = sut.string(from: BigInt("10000000000000000000"), subunitToUnit: 1000000000000000000)
XCTAssertEqual(result, "10")
}

func testStringFromBigIntWithDecimal() {
let sut = OMGNumberFormatter(locale: self.locale, precision: 1)
let result = sut.string(from: BigInt("10000000000000000000"), decimals: 18)
XCTAssertEqual(result, "10")
}

func testStringFromBigIntWithDecimalAndPrecision() {
let sut = OMGNumberFormatter(locale: self.locale, precision: 2)
let result = sut.string(from: BigInt("1"), decimals: 2)
XCTAssertEqual(result, "0\(self.locale.decimalSeparator ?? ".")01")
}

}
12 changes: 6 additions & 6 deletions Source/Helpers/OMGNumberFormatter.swift
Expand Up @@ -3,12 +3,12 @@
// OmiseGOTests
//
// Created by Mederic Petit on 15/6/18.
// Copyright © 2018 Omise Go Pte. Ltd. All rights reserved.
// Copyright © 2017-2018 Omise Go Pte. Ltd. All rights reserved.
//

import BigInt

final class OMGNumberFormatter {
public final class OMGNumberFormatter {

var minFractionDigits = 0
var maxFractionDigits = Int.max
Expand All @@ -21,12 +21,12 @@ final class OMGNumberFormatter {
self.maxFractionDigits = precision
}

func number(from string: String, subunitToUnit: UInt64) -> BigInt? {
public func number(from string: String, subunitToUnit: BigInt) -> BigInt? {
let decimals = Int(log10(Double(subunitToUnit)))
return number(from: string, decimals: decimals)
}

func number(from string: String, decimals: Int) -> BigInt? {
public func number(from string: String, decimals: Int) -> BigInt? {
guard let index = string.index(where: { String($0) == decimalSeparator }) else {
return BigInt(string).flatMap({ $0 * BigInt(10).power(decimals) })
}
Expand All @@ -45,12 +45,12 @@ final class OMGNumberFormatter {
}
}

func string(from number: BigInt, subunitToUnit: UInt64) -> String {
public func string(from number: BigInt, subunitToUnit: BigInt) -> String {
let decimals = Int(log10(Double(subunitToUnit)))
return string(from: number, decimals: decimals)
}

func string(from number: BigInt, decimals: Int) -> String {
public func string(from number: BigInt, decimals: Int) -> String {
precondition(minFractionDigits >= 0)
precondition(maxFractionDigits >= 0)

Expand Down
4 changes: 2 additions & 2 deletions Source/Models/Token.swift
Expand Up @@ -19,7 +19,7 @@ public struct Token {
public let name: String
/// The multiplier representing the value of 1 token. i.e: if I want to give or receive
/// 13 tokens and the subunitToUnit is 1000 then the amount will be 13*1000 = 13000
public let subUnitToUnit: UInt64
public let subUnitToUnit: BigInt
/// Any additional metadata that need to be stored as a dictionary
public let metadata: [String: Any]
/// Any additional encrypted metadata that need to be stored as a dictionary
Expand Down Expand Up @@ -49,7 +49,7 @@ extension Token: Decodable {
id = try container.decode(String.self, forKey: .id)
symbol = try container.decode(String.self, forKey: .symbol)
name = try container.decode(String.self, forKey: .name)
subUnitToUnit = try container.decode(UInt64.self, forKey: .subUnitToUnit)
subUnitToUnit = try container.decode(BigInt.self, forKey: .subUnitToUnit)
metadata = try container.decode([String: Any].self, forKey: .metadata)
encryptedMetadata = try container.decode([String: Any].self, forKey: .encryptedMetadata)
createdAt = try container.decode(Date.self, forKey: .createdAt)
Expand Down

0 comments on commit 3cfe0be

Please sign in to comment.