Example #1
0
    def getTransactionError(self, txhash):
        if not txhash.startswith('0x'):
            txhash = '0x' + txhash

        trace = self.web3.manager.request_blocking('debug_traceTransaction', [
            txhash, {
                'disableStorage': True,
                'disableMemory': True,
                'disableStack': True,
            }
        ])

        if not trace.get('failed'):
            logger.error(
                'Transaction receipt indicates failure but trace succeeded')
            return 'Transaction receipt indicates failure but trace succeeded'

        # Parse out the revert error code if it exists
        # See https://solidity.readthedocs.io/en/v0.4.24/control-structures.html#error-handling-assert-require-revert-and-exceptions  # noqa: E501
        # Encode as if a function call to `Error(string)`
        rv = HexBytes(trace.get('returnValue'))

        # Trim off function selector for "Error"
        if not rv.startswith(HexBytes(Debug.ERROR_SELECTOR)):
            logger.error(
                'Expected revert encoding to begin with %s, actual is %s',
                Debug.ERROR_SELECTOR, rv[:4].hex())
            return 'Invalid revert encoding'
        rv = rv[4:]

        error = decode_abi(['string'], rv)[0]
        return error.decode('utf-8')
Example #2
0
def to_string(value):
    '''Convert a value to a string'''
    if type(value) in (bytes, HexBytes):
        value = HexBytes(value).hex()
    value = str(value)
    if value.startswith("0x") and eth_utils.is_hex(value):
        try:
            return eth_utils.to_text(hexstr=value)
        except UnicodeDecodeError as e:
            raise ValueError(e)
    return value
Example #3
0
def get_clean_address_or_throw(address):
    """Get a clean 42 character address with leading '0x' throw

    Keyword argument:
    address: hex-like address
    """
    if not isinstance(address, str):
        address = HexBytes(address).rjust(10, b"\0").hex()
    if not RE_ADDRESS.match(address.lower()):
        raise TypeError("address looks invalid: '{}'".format(address))
    if not address.startswith("0x"):
        address = "0x" + address
    return address