Skip to content

Commit 5e841a0

Browse files
marioevzdanceratopz
andcommitted
Apply suggestions from code review
Co-authored-by: danceratopz <[email protected]>
1 parent ab6b7c9 commit 5e841a0

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

src/ethereum_test_base_types/base_types.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,8 @@ def __new__(cls, input: NumberConvertible | N):
9090
assert len(words) <= 2
9191
value_str = words[0]
9292
if len(words) > 1:
93-
unit = words[1]
94-
if unit == "wei":
95-
multiplier = 1
96-
elif unit == "kwei":
97-
multiplier = 10**3
98-
elif unit == "mwei":
99-
multiplier = 10**6
100-
elif unit == "gwei":
101-
multiplier = 10**9
102-
elif unit == "szabo":
103-
multiplier = 10**12
104-
elif unit == "finney":
105-
multiplier = 10**15
106-
elif unit == "ether":
107-
multiplier = 10**18
108-
else:
109-
raise ValueError(f"Invalid unit {unit}")
93+
unit = words[1].lower()
94+
multiplier = cls._get_multiplier(unit)
11095
value: int
11196
if "e" in value_str:
11297
value = int(float(value_str))
@@ -118,7 +103,28 @@ def __new__(cls, input: NumberConvertible | N):
118103
return super(Number, cls).__new__(cls, value * multiplier)
119104
return super(Number, cls).__new__(cls, to_number(input))
120105

121-
106+
@staticmethod
107+
def _get_multiplier(unit: str) -> int:
108+
"""
109+
Returns the multiplier for the given unit of wei, handling synonyms.
110+
"""
111+
match unit:
112+
case "wei":
113+
return 1
114+
case "kwei" | "babbage" | "femtoether":
115+
return 10**3
116+
case "mwei" | "lovelace" | "picoether":
117+
return 10**6
118+
case "gwei" | "shannon" | "nanoether" | "nano":
119+
return 10**9
120+
case "szabo" | "microether" | "micro":
121+
return 10**12
122+
case "finney" | "milliether" | "milli":
123+
return 10**15
124+
case "ether":
125+
return 10**18
126+
case _:
127+
raise ValueError(f"Invalid unit {unit}")
122128
class HexNumber(Number):
123129
"""
124130
Class that helps represent an hexadecimal numbers in tests.

src/ethereum_test_base_types/tests/test_base_types.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,29 @@ def test_comparisons(a: Any, b: Any, equal: bool):
6161
("10**18", 10**18),
6262
("1e18", 10**18),
6363
("1 ether", 10**18),
64+
("2 ether", 2*10**18),
65+
("2.1 ether", 2.1*10**18),
66+
("2.1 Ether", 2*10**18),
67+
("2.1 ETHER", 2*10**18),
6468
("1 wei", 1),
69+
("10**9 wei", 10**9),
6570
("1 gwei", 10**9),
6671
("1 szabo", 10**12),
6772
("1 finney", 10**15),
6873
("1 kwei", 10**3),
6974
("1 mwei", 10**6),
75+
("1 babbage", 10**3),
76+
("1 femtoether", 10**3),
77+
("1 Lovelace", 10**6),
78+
("1 Picoether", 10**6),
79+
("1 gwei", 10**9),
80+
("1 shannon", 10**9),
81+
("1 nanoether", 10**9),
82+
("1 nano", 10**9),
83+
("1 microether", 10**12),
84+
("1 micro", 10**12),
85+
("1 milliether", 10**15),
86+
("1 milli", 10**15),
7087
],
7188
)
7289
def test_wei_parsing(s: str, expected: int):

0 commit comments

Comments
 (0)