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

fix: correct conversion method for Amount class #28

Merged
merged 3 commits into from
Oct 6, 2024
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
27 changes: 16 additions & 11 deletions pactus/types/amount.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
NANO_PAC_PER_PAC = 1e9
MAX_NANO_PAC = 42e6 * NANO_PAC_PER_PAC


class Amount:
"""
The Amount class represents a quantity in NanoPAC.
Amount represents the atomic unit in the Pactus blockchain.
Each unit is equal to 1e-9 of a PAC.

The `from_NanoPAC` method creates an Amount from a floating-point value
The `from_pac` method creates an Amount from a floating-point value
representing an amount in PAC. It raises an error if the value is NaN or
+-Infinity, but it does not check whether the amount exceeds the total
±Infinity, but it does not check whether the amount exceeds the total
amount of PAC producible. This method is specifically for converting PAC
to NanoPAC. For creating a new Amount with an integer value representing
NanoPAC, you can initialize the Amount directly with an integer.
to NanoPAC. To create a new Amount with an integer value representing
NanoPAC, you can use the `from_nano_pac` method.
"""

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

@classmethod
def from_nano_pac(cls, f: float) -> "Amount":
def from_nano_pac(cls, a: int) -> "Amount":
"""Store the value as NanoPAC in the Amount instance."""
return cls(a)

@classmethod
def from_pac(cls, f: float) -> "Amount":
"""
Convert a floating-point value in PAC to NanoPAC and stores it in the Amount instance.
Convert a floating-point value in PAC to NanoPAC and store it in the Amount instance.

The conversion is invalid if the floating-point value is NaN or +-Infinity,
The conversion is invalid if the floating-point value is NaN or ±Infinity,
in which case a ValueError is raised.
"""
if math.isinf(f) or math.isnan(f):
msg = f"invalid PAC amount: {f}"
raise ValueError(msg)

return cls(int(cls.round(f * NANO_PAC_PER_PAC)))
return cls.from_nano_pac(int(cls.round(f * NANO_PAC_PER_PAC)))

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

return cls.from_nano_pac(f)
return cls.from_pac(f)

def round(self: float) -> float:
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
long_description = fh.read()

NAME = "pactus-sdk"
VERSION = "1.2.0"
VERSION = "1.1.1"
AUTHOR = "Pactus Development Team"
AUTHOR_EMAIL = "[email protected]"
DESCRIPTION = "Pactus Development Kit"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_amount.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class TestAmount(unittest.TestCase):
def test_from_nano_pac(self):
def test_from_pac(self):
test_cases = [
# Valid cases
{
Expand All @@ -27,9 +27,9 @@ def test_from_nano_pac(self):
for case in test_cases:
if case["raises"]:
with self.assertRaises(case["raises"]):
Amount.from_nano_pac(case["input"])
Amount.from_pac(case["input"])
else:
amt = Amount.from_nano_pac(case["input"])
amt = Amount.from_pac(case["input"])
self.assertEqual(amt, case["expected"])

def test_from_string(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_sign_transfer(self):
prv = PrivateKey.from_string(prv_str)
sender = Address.from_string("pc1z5x2a0lkt5nrrdqe0rkcv6r4pfkmdhrr3mawvua")
receiver = Address.from_string("pc1zt6qcdymkk48c5ds0fzfsaf6puwu8w8djn3ffpn")
amount = Amount.from_nano_pac(1.0) # 1 PAC
fee = Amount.from_nano_pac(0.001) # 0.001 PAC
amount = Amount.from_pac(1.0) # 1 PAC
fee = Amount.from_pac(0.001) # 0.001 PAC
lock_time = 0x123456
memo = "test"

Expand Down
Loading