Skip to content

Commit

Permalink
improve error msg for as_wei_value
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed Nov 17, 2023
1 parent 2520b90 commit 3d7d317
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
24 changes: 23 additions & 1 deletion tests/functional/syntax/test_as_wei_value.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import pytest

from vyper.exceptions import ArgumentException, InvalidType, OverflowException, StructureException
from vyper.exceptions import (
ArgumentException,
InvalidLiteral,
InvalidType,
OverflowException,
StructureException,
)

fail_list = [
(
"""
@external
def foo():
x: uint256 = as_wei_value(5, szabo)
""",
ArgumentException,
),
(
"""
@external
def foo():
x: uint256 = as_wei_value(5, "szaboo")
""",
Expand Down Expand Up @@ -39,6 +53,14 @@ def foo() -> uint256:
""",
OverflowException,
),
(
"""
@external
def foo():
x: uint256 = as_wei_value(-1, "szabo")
""",
InvalidLiteral,
),
]


Expand Down
9 changes: 8 additions & 1 deletion vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,12 +977,16 @@ class AsWeiValue(BuiltinFunction):
("kether", "grand"): 10**21,
}

def get_denomination(self, node):
def _get_denomination_node(self, node):
value = node.args[1]._metadata.get("folded_value")
if not isinstance(value, vy_ast.Str):
raise ArgumentException(
"Wei denomination must be given as a literal string", node.args[1]
)
return value

def get_denomination(self, node):
value = self._get_denomination_node(node)
try:
denom = next(v for k, v in self.wei_denoms.items() if value.value in k)
except StopIteration:
Expand All @@ -1009,6 +1013,9 @@ def fetch_call_return(self, node):
return self._return_type

def infer_arg_types(self, node, expected_return_typ=None):
# raise a better error message by first calling this function
# for its side effects of checking the denom
self._get_denomination_node(node)
self._validate_arg_types(node)
# return a concrete type instead of abstract type
value_type = get_possible_types_from_node(node.args[0]).pop()
Expand Down

0 comments on commit 3d7d317

Please sign in to comment.