diff --git a/pactus/types/amount.py b/pactus/types/amount.py index 23a36d2..7083f77 100644 --- a/pactus/types/amount.py +++ b/pactus/types/amount.py @@ -34,6 +34,14 @@ def __str__(self) -> str: pac_value = self.value / NANO_PAC_PER_PAC return f"{pac_value}" + def to_pac(self) -> float: + """Convert the amount from NanoPAC to PAC and return as float.""" + return self.value / NANO_PAC_PER_PAC + + def to_nano_pac(self) -> int: + """Return the amount in NanoPAC as an integer.""" + return self.value + @classmethod def from_nano_pac(cls, a: int) -> "Amount": """Store the value as NanoPAC in the Amount instance.""" diff --git a/tests/test_amount.py b/tests/test_amount.py index 8653892..e92e954 100644 --- a/tests/test_amount.py +++ b/tests/test_amount.py @@ -105,6 +105,120 @@ def test_str(self): result = str(case["input"]) self.assertEqual(result, case["expected"]) + def test_to_pac(self): + test_cases = [ + { + "input": Amount(0), + "expected": 0.0, + }, + { + "input": Amount.from_pac(42.5), + "expected": 42.5, + }, + { + "input": Amount.from_pac(1.0), + "expected": 1.0, + }, + { + "input": Amount.from_pac(0.5), + "expected": 0.5, + }, + { + "input": Amount.from_pac(1000000.0), + "expected": 1000000.0, + }, + { + "input": Amount.from_pac(0.000000001), + "expected": 0.000000001, + }, + { + "input": Amount.from_pac(-10.5), + "expected": -10.5, + }, + { + "input": Amount.from_nano_pac(1000000000), + "expected": 1.0, + }, + { + "input": Amount.from_nano_pac(500000000), + "expected": 0.5, + }, + { + "input": Amount.from_nano_pac(1), + "expected": 0.000000001, + }, + { + "input": Amount.from_nano_pac(123456789), + "expected": 0.123456789, + }, + ] + + for case in test_cases: + result = case["input"].to_pac() + self.assertAlmostEqual(result, case["expected"], places=9) + + def test_to_nano_pac(self): + test_cases = [ + { + "input": Amount(0), + "expected": 0, + }, + { + "input": Amount.from_pac(42.5), + "expected": int(42.5 * NANO_PAC_PER_PAC), + }, + { + "input": Amount.from_pac(1.0), + "expected": int(1.0 * NANO_PAC_PER_PAC), + }, + { + "input": Amount.from_pac(0.5), + "expected": int(0.5 * NANO_PAC_PER_PAC), + }, + { + "input": Amount.from_pac(1000000.0), + "expected": int(1000000.0 * NANO_PAC_PER_PAC), + }, + { + "input": Amount.from_pac(-10.5), + "expected": int(-10.5 * NANO_PAC_PER_PAC), + }, + { + "input": Amount.from_nano_pac(1000000000), + "expected": 1000000000, + }, + { + "input": Amount.from_nano_pac(500000000), + "expected": 500000000, + }, + { + "input": Amount.from_nano_pac(1), + "expected": 1, + }, + { + "input": Amount.from_nano_pac(123456789), + "expected": 123456789, + }, + ] + + for case in test_cases: + result = case["input"].to_nano_pac() + self.assertEqual(result, case["expected"]) + + def test_round_trip_conversion(self): + """Test that converting from PAC to NanoPAC and back preserves the value.""" + test_values = [0.0, 1.0, 42.5, 0.5, 1000000.0, 0.000000001, -10.5] + + for pac_value in test_values: + amount = Amount.from_pac(pac_value) + nano_pac = amount.to_nano_pac() + back_to_pac = amount.to_pac() + + # Create new amount from nano_pac and verify + amount_from_nano = Amount.from_nano_pac(nano_pac) + self.assertEqual(amount, amount_from_nano) + self.assertAlmostEqual(back_to_pac, pac_value, places=9) + if __name__ == "__main__": unittest.main()