Example #1
0
    def __init__(self, val, constant_type=None, subdenomination=None):  # pylint: disable=too-many-branches
        super().__init__()
        assert isinstance(val, str)

        self._original_value = val
        self._subdenomination = subdenomination

        if subdenomination:
            val = str(convert_subdenomination(val, subdenomination))

        if constant_type:  # pylint: disable=too-many-nested-blocks
            assert isinstance(constant_type, ElementaryType)
            self._type = constant_type
            if constant_type.type in Int + Uint + ["address"]:
                self._val = convert_string_to_int(val)
            elif constant_type.type == "bool":
                self._val = (val == "true") | (val == "True")
            else:
                self._val = val
        else:
            if val.isdigit():
                self._type = ElementaryType("uint256")
                self._val = int(Decimal(val))
            else:
                self._type = ElementaryType("string")
                self._val = val
Example #2
0
    def __init__(self, val, type=None, subdenomination=None):
        super(Constant, self).__init__()
        assert isinstance(val, str)

        self._original_value = val
        self._subdenomination = subdenomination

        if subdenomination:
            val = str(convert_subdenomination(val, subdenomination))

        if type:
            assert isinstance(type, ElementaryType)
            self._type = type
            if type.type in Int + Uint + ['address']:
                if val.startswith('0x') or val.startswith('0X'):
                    self._val = int(val, 16)
                else:
                    if 'e' in val:
                        base, expo = val.split('e')
                        self._val = int(float(base) * (10**int(expo)))
                    elif 'E' in val:
                        base, expo = val.split('E')
                        self._val = int(float(base) * (10**int(expo)))
                    else:
                        self._val = int(float(val))
            elif type.type == 'bool':
                self._val = val == 'true'
            else:
                self._val = val
        else:
            if val.isdigit():
                self._type = ElementaryType('uint256')
                self._val = int(val)
            else:
                self._type = ElementaryType('string')
                self._val = val
Example #3
0
 def __str__(self):
     if self.subdenomination:
         return str(convert_subdenomination(self._value, self.subdenomination))
     # be sure to handle any character
     return str(self._value)