Skip to content

Commit 357a8b9

Browse files
authored
fix: correct conversion method for Amount class (#28)
1 parent ca9bd18 commit 357a8b9

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

pactus/types/amount.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
NANO_PAC_PER_PAC = 1e9
44
MAX_NANO_PAC = 42e6 * NANO_PAC_PER_PAC
55

6-
76
class Amount:
87
"""
9-
The Amount class represents a quantity in NanoPAC.
8+
Amount represents the atomic unit in the Pactus blockchain.
9+
Each unit is equal to 1e-9 of a PAC.
1010
11-
The `from_NanoPAC` method creates an Amount from a floating-point value
11+
The `from_pac` method creates an Amount from a floating-point value
1212
representing an amount in PAC. It raises an error if the value is NaN or
13-
+-Infinity, but it does not check whether the amount exceeds the total
13+
±Infinity, but it does not check whether the amount exceeds the total
1414
amount of PAC producible. This method is specifically for converting PAC
15-
to NanoPAC. For creating a new Amount with an integer value representing
16-
NanoPAC, you can initialize the Amount directly with an integer.
15+
to NanoPAC. To create a new Amount with an integer value representing
16+
NanoPAC, you can use the `from_nano_pac` method.
1717
"""
1818

1919
def __init__(self, amt: int = 0) -> None:
@@ -26,18 +26,23 @@ def __eq__(self, other: "Amount") -> bool:
2626
return False
2727

2828
@classmethod
29-
def from_nano_pac(cls, f: float) -> "Amount":
29+
def from_nano_pac(cls, a: int) -> "Amount":
30+
"""Store the value as NanoPAC in the Amount instance."""
31+
return cls(a)
32+
33+
@classmethod
34+
def from_pac(cls, f: float) -> "Amount":
3035
"""
31-
Convert a floating-point value in PAC to NanoPAC and stores it in the Amount instance.
36+
Convert a floating-point value in PAC to NanoPAC and store it in the Amount instance.
3237
33-
The conversion is invalid if the floating-point value is NaN or +-Infinity,
38+
The conversion is invalid if the floating-point value is NaN or ±Infinity,
3439
in which case a ValueError is raised.
3540
"""
3641
if math.isinf(f) or math.isnan(f):
3742
msg = f"invalid PAC amount: {f}"
3843
raise ValueError(msg)
3944

40-
return cls(int(cls.round(f * NANO_PAC_PER_PAC)))
45+
return cls.from_nano_pac(int(cls.round(f * NANO_PAC_PER_PAC)))
4146

4247
@classmethod
4348
def from_string(cls, s: str) -> "Amount":
@@ -53,7 +58,7 @@ def from_string(cls, s: str) -> "Amount":
5358
msg = "invalid PAC amount"
5459
raise ValueError(msg) from e
5560

56-
return cls.from_nano_pac(f)
61+
return cls.from_pac(f)
5762

5863
def round(self: float) -> float:
5964
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
long_description = fh.read()
1111

1212
NAME = "pactus-sdk"
13-
VERSION = "1.2.0"
13+
VERSION = "1.1.1"
1414
AUTHOR = "Pactus Development Team"
1515
AUTHOR_EMAIL = "info@pactus.org"
1616
DESCRIPTION = "Pactus Development Kit"

tests/test_amount.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
class TestAmount(unittest.TestCase):
7-
def test_from_nano_pac(self):
7+
def test_from_pac(self):
88
test_cases = [
99
# Valid cases
1010
{
@@ -27,9 +27,9 @@ def test_from_nano_pac(self):
2727
for case in test_cases:
2828
if case["raises"]:
2929
with self.assertRaises(case["raises"]):
30-
Amount.from_nano_pac(case["input"])
30+
Amount.from_pac(case["input"])
3131
else:
32-
amt = Amount.from_nano_pac(case["input"])
32+
amt = Amount.from_pac(case["input"])
3333
self.assertEqual(amt, case["expected"])
3434

3535
def test_from_string(self):

tests/test_transaction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def test_sign_transfer(self):
1212
prv = PrivateKey.from_string(prv_str)
1313
sender = Address.from_string("pc1z5x2a0lkt5nrrdqe0rkcv6r4pfkmdhrr3mawvua")
1414
receiver = Address.from_string("pc1zt6qcdymkk48c5ds0fzfsaf6puwu8w8djn3ffpn")
15-
amount = Amount.from_nano_pac(1.0) # 1 PAC
16-
fee = Amount.from_nano_pac(0.001) # 0.001 PAC
15+
amount = Amount.from_pac(1.0) # 1 PAC
16+
fee = Amount.from_pac(0.001) # 0.001 PAC
1717
lock_time = 0x123456
1818
memo = "test"
1919

0 commit comments

Comments
 (0)