3
3
NANO_PAC_PER_PAC = 1e9
4
4
MAX_NANO_PAC = 42e6 * NANO_PAC_PER_PAC
5
5
6
-
7
6
class Amount :
8
7
"""
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.
10
10
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
12
12
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
14
14
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 .
17
17
"""
18
18
19
19
def __init__ (self , amt : int = 0 ) -> None :
@@ -26,18 +26,23 @@ def __eq__(self, other: "Amount") -> bool:
26
26
return False
27
27
28
28
@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" :
30
35
"""
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.
32
37
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,
34
39
in which case a ValueError is raised.
35
40
"""
36
41
if math .isinf (f ) or math .isnan (f ):
37
42
msg = f"invalid PAC amount: { f } "
38
43
raise ValueError (msg )
39
44
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 )))
41
46
42
47
@classmethod
43
48
def from_string (cls , s : str ) -> "Amount" :
@@ -53,7 +58,7 @@ def from_string(cls, s: str) -> "Amount":
53
58
msg = "invalid PAC amount"
54
59
raise ValueError (msg ) from e
55
60
56
- return cls .from_nano_pac (f )
61
+ return cls .from_pac (f )
57
62
58
63
def round (self : float ) -> float :
59
64
"""
0 commit comments