Repository: vyperlang/vyper

Issue:
Constant in as wei_value
### Version Information
b6 & master
### What's your issue about?
Using constant in as_wei_value breaks.
```
MIN_DEPOSIT: constant(uint256) = 1  # ETH
MAX_DEPOSIT: constant(uint256) = 32  # ETH

@public
def deposit(deposit_input: bytes[2048]):
    assert msg.value >= as_wei_value(MIN_DEPOSIT, "ether")
    assert msg.value <= as_wei_value(MAX_DEPOSIT, "ether")
```
### How can it be fixed?

Fill this in if you know how to fix it.

#### Cute Animal Picture

> put a cute animal picture here.



Discussion:

Resolving patch:
diff --git a/vyper/functions/functions.py b/vyper/functions/functions.py
--- a/vyper/functions/functions.py
+++ b/vyper/functions/functions.py
@@ -372,7 +372,11 @@ def as_wei_value(expr, args, kwargs, context):
         )
     # Compute the amount of wei and return that value
     if isinstance(args[0], (int, float)):
-        numstring, num, den = get_number_as_fraction(expr.args[0], context)
+        expr_args_0 = expr.args[0]
+        # On constant reference fetch value node of constant assignment.
+        if context.constants.ast_is_constant(expr.args[0]):
+            expr_args_0 = context.constants._constants_ast[expr.args[0].id]
+        numstring, num, den = get_number_as_fraction(expr_args_0, context)
         if denomination % den:
             raise InvalidLiteralException("Too many decimal places: %s" % numstring, expr.args[0])
         sub = num * denomination // den
diff --git a/vyper/parser/constants.py b/vyper/parser/constants.py
--- a/vyper/parser/constants.py
+++ b/vyper/parser/constants.py
@@ -22,6 +22,7 @@ class Constants(object):
 
     def __init__(self):
         self._constants = dict()
+        self._constants_ast = dict()
 
     def __contains__(self, key):
         return key in self._constants
@@ -53,7 +54,7 @@ def unroll_constant(self, const, global_ctx):
                 fail = False
 
         if fail:
-            raise TypeMismatchException('Invalid value for constant type, expected %r' % annotation_type, const.value)
+            raise TypeMismatchException('Invalid value for constant type, expected %r got %r instead' % (annotation_type, expr.typ), const.value)
 
         ann_expr = copy.deepcopy(expr)
         ann_expr.typ = annotation_type
@@ -69,6 +70,7 @@ def add_constant(self, item, global_ctx):
             c_name = item.target.id
             if global_ctx.is_valid_varname(c_name, item):
                 self._constants[c_name] = self.unroll_constant(item, global_ctx)
+                self._constants_ast[c_name] = item.value
         else:
             raise StructureException('Incorrectly formatted struct', item)
 


Test patch:
diff --git a/tests/parser/syntax/test_constants.py b/tests/parser/syntax/test_constants.py
--- a/tests/parser/syntax/test_constants.py
+++ b/tests/parser/syntax/test_constants.py
@@ -116,6 +116,16 @@ def test1():
 def test():
     for i in range(CONST / 4):
         pass
+    """,
+    """
+MIN_DEPOSIT: constant(uint256) = 1  # ETH
+MAX_DEPOSIT: constant(decimal) = 32.0  # ETH
+
+@payable
+@public
+def deposit(deposit_input: bytes[2048]):
+    assert msg.value >= as_wei_value(MIN_DEPOSIT, "ether")
+    assert msg.value <= as_wei_value(MAX_DEPOSIT, "ether")
     """
 ]