Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ABW-2217 Value of some resources showing as 00 when contributing to Pool #736

Merged
merged 7 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Core/SharedModels/Assets/AccountPortfolio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ extension AccountPortfolio.PoolUnitResources {
self.poolResources = poolResources
}

public func redemptionValue(for resource: AccountPortfolio.FungibleResource) -> BigDecimal {
public func redemptionValue(for resource: AccountPortfolio.FungibleResource) -> String {
let poolUnitTotalSupply = poolUnitResource.totalSupply ?? .one
let unroundedRedemptionValue = poolUnitResource.amount * resource.amount / poolUnitTotalSupply
return resource.divisibility.map { unroundedRedemptionValue.withPrecision($0) } ?? unroundedRedemptionValue
return unroundedRedemptionValue.format(divisibility: resource.divisibility)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension AccountPortfolio.PoolUnitResources.PoolUnit {
PoolUnitResourceViewState(
thumbnail: .xrd,
symbol: Constants.xrdTokenName,
tokenAmount: redemptionValue(for: $0).format()
tokenAmount: redemptionValue(for: $0)
)
}

Expand All @@ -85,7 +85,7 @@ extension AccountPortfolio.PoolUnitResources.PoolUnit {
PoolUnitResourceViewState(
thumbnail: .known($0.iconURL),
symbol: $0.symbol ?? $0.name ?? L10n.Account.PoolUnits.unknownSymbolName,
tokenAmount: redemptionValue(for: $0).format()
tokenAmount: redemptionValue(for: $0)
)
}
)! // Safe to unwrap, guaranteed to not be empty
Expand Down
21 changes: 19 additions & 2 deletions Sources/Prelude/Extensions/BigDecimal+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Foundation
extension BigDecimal {
// Used for development purposes
public static let temporaryStandardFee: BigDecimal = 25
public static let defaultMaxPlacesFormattted: UInt = 8
}

extension BigDecimal {
Expand All @@ -16,7 +17,8 @@ extension BigDecimal {

/// Formats the number for human consumtion
public func format(
maxPlaces maxPlacesNonNegative: UInt = 8,
maxPlaces maxPlacesNonNegative: UInt = BigDecimal.defaultMaxPlacesFormattted,
divisibility: Int? = nil,
locale: Locale = .autoupdatingCurrent
) -> String {
// N.B. We cannot use `Local.current.decimalSeperator` here because
Expand All @@ -34,7 +36,22 @@ extension BigDecimal {
}

let integerPart = String(components[0])
let decimalPart = String(components[1])
let decimalComponents = components[1]
let decimalPart = {
guard let divisibility else {
return decimalComponents
}

guard divisibility > .zero else {
return ""
}

return decimalComponents.prefix(divisibility)
}()

guard !decimalPart.isEmpty else {
return integerPart
}

let numberOfDecimalDigits = max(1, Int(maxPlacesNonNegative) - integerPart.count)

Expand Down
41 changes: 41 additions & 0 deletions Tests/PreludeTests/BigDecimalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,45 @@ final class BigDecimalTests: TestCase {
try doTest("0.99999999999999999", expected: "$1")
try doTest("0.00000000000000001", expected: "$0")
}

func test_format_bigdecimal_with_divisibility() throws {
func doTest(_ bigDecimalString: String, divisibility: Int, expected: String, line: UInt = #line) throws {
let locale = Locale(identifier: "en_US_POSIX")
let bigDecimal = try BigDecimal(fromString: bigDecimalString)
XCTAssertEqual(bigDecimal.format(divisibility: divisibility, locale: locale), expected, line: line)
}

/// Big divisibility does not affect the basic result
try doTest("57896044618658097711785492504343953926634992332820282019728", divisibility: 18, expected: "57896044618658097711785492504343953926634992332820282019728")
try doTest("57896044618658097711785492504343953926634992332820282019728.0", divisibility: 18, expected: "57896044618658097711785492504343953926634992332820282019728")

try doTest(
"57896044618658097711785492504343953926634992332820282019728.792003956564819968",
divisibility: 18,
expected: "57896044618658097711785492504343953926634992332820282019728.8"
) // rounded `0.79` -> `0.80`
try doTest("1000000000.1", divisibility: 18, expected: "1000000000.1")
try doTest("1000000000", divisibility: 18, expected: "1000000000")
try doTest("1000.1234", divisibility: 18, expected: "1000.1234")
try doTest("1000.5", divisibility: 18, expected: "1000.5")
try doTest("0.1234567", divisibility: 18, expected: "0.1234567")
try doTest("0.4321", divisibility: 18, expected: "0.4321")
try doTest("0.99999999999999999", divisibility: 18, expected: "1")
try doTest("0.00000000000000001", divisibility: 18, expected: "0")
try doTest("0", divisibility: 18, expected: "0")
try doTest("1", divisibility: 18, expected: "1")
try doTest("0.0", divisibility: 18, expected: "0")
try doTest("1.0", divisibility: 18, expected: "1")

/// Zero divisibility.
/// Specifying decimal places for a resource with divisibility zero is actually invalid. No transaction will succeed with such value
try doTest("0.99999999999999999", divisibility: 0, expected: "0")
try doTest("1.99999999999999999", divisibility: 0, expected: "1")
/// Minimal divisibility
try doTest("1.99999999999999999", divisibility: 1, expected: "1.9")
/// Divisibility < Max formatted digits
try doTest("1.12345678", divisibility: 4, expected: "1.1234")
/// Max formatted digits
try doTest("1.12345678", divisibility: 8, expected: "1.1234568")
}
}
Loading