Skip to content

Commit a485654

Browse files
committed
fix: make decimal as string
1 parent 7537a60 commit a485654

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

Sources/Cadence/Value.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ extension Value {
380380
} else if let bigUInt = value as? BigUInt {
381381
result[$0.name] = bigUInt.description
382382
} else if let decimal = value as? Decimal {
383-
result[$0.name] = decimal
383+
result[$0.name] = decimal.description
384384
} else if let encodable = value as? Encodable {
385385
let value = try encodable.encoded(with: JSONEncoder())
386386
result[$0.name] = try JSONSerialization.jsonObject(

Tests/CadenceTests/ValueTests.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ final class ValueTests: XCTestCase {
6060
// Arrange
6161
struct TestStruct: Codable, Equatable {
6262
let name: String
63-
let salary: Decimal
63+
let salary: String
6464
}
6565
let value: Cadence.Value = .optional(.struct(
6666
id: "id",
@@ -74,7 +74,7 @@ final class ValueTests: XCTestCase {
7474
let result: TestStruct? = try value.toSwiftValue()
7575

7676
// Assert
77-
XCTAssertEqual(result, TestStruct(name: "Scott", salary: 0.01))
77+
XCTAssertEqual(result, TestStruct(name: "Scott", salary: "0.01"))
7878
XCTAssertThrowsError(try value.toSwiftValue(decodableType: Int.self))
7979
}
8080

@@ -445,7 +445,7 @@ final class ValueTests: XCTestCase {
445445
func testToSwiftValueEvent() throws {
446446
// Arrange
447447
struct TokensWithdrawn: Codable, Equatable {
448-
let amount: Decimal
448+
let amount: String
449449
let from: Address?
450450
}
451451
let amount = Decimal(string: "0.00000169")!
@@ -466,13 +466,13 @@ final class ValueTests: XCTestCase {
466466
let result: TokensWithdrawn = try value.toSwiftValue()
467467

468468
// Assert
469-
XCTAssertEqual(result, TokensWithdrawn(amount: amount, from: address))
469+
XCTAssertEqual(result, TokensWithdrawn(amount: amount.description, from: address))
470470
}
471471

472472
func testToSwiftValueEventWithNil() throws {
473473
// Arrange
474474
struct TokensWithdrawn: Codable, Equatable {
475-
let amount: Decimal
475+
let amount: String
476476
let from: Address?
477477
}
478478
let amount = Decimal(string: "0.00000169")!
@@ -492,7 +492,7 @@ final class ValueTests: XCTestCase {
492492
let result: TokensWithdrawn = try value.toSwiftValue()
493493

494494
// Assert
495-
XCTAssertEqual(result, TokensWithdrawn(amount: amount, from: nil))
495+
XCTAssertEqual(result, TokensWithdrawn(amount: amount.description, from: nil))
496496
}
497497

498498
func testToSwiftValueContract() throws {
@@ -661,6 +661,16 @@ final class ValueTests: XCTestCase {
661661
// Arrange
662662
struct TestStruct: Codable, Equatable {
663663
let value: Decimal
664+
665+
init(from decoder: Decoder) throws {
666+
let container = try decoder.container(keyedBy: CodingKeys.self)
667+
let value = try container.decode(String.self, forKey: .value)
668+
self.value = Decimal(string: value) ?? 0
669+
}
670+
671+
init(value: Decimal) {
672+
self.value = value
673+
}
664674
}
665675
let decimalValue = Decimal(string: "0.007601")!
666676
let value: Cadence.Value = .optional(.struct(

0 commit comments

Comments
 (0)