def to_4byte_hex(hex_or_str_or_bytes: Union[int, str, bytes]) -> str: size_of_4bytes = 4 * 8 byte_str = hexstr_if_str(to_bytes, hex_or_str_or_bytes) if len(byte_str) > 4: raise ValueError('expected value of size 4 bytes. Got: %d bytes' % len(byte_str)) hex_str = encode_hex(byte_str) return pad_hex(hex_str, size_of_4bytes)
async def _async_call(self, func, *args, **kwargs): tx = prepare_transaction(self.__contract.address, None, func.function_identifier, func.contract_abi, func.abi, {}, args, kwargs) if self.__block is None: return_data = await self.__contract.web3.eth.call(tx) else: return_data = await self.__contract.web3.eth.call(tx, self.__block) output_types = get_abi_output_types(func.abi) result = decode_abi(output_types, hexstr_if_str(to_bytes, return_data)) return result if len(func.abi['outputs']) > 1 else result[0]
def assemble(*codes): return b''.join(hexstr_if_str(to_bytes, element) for element in codes)
def __new__(cls: Type[bytes], val: Union[bytes, int, str]) -> "HexBytes": bytesval = hexstr_if_str(to_bytes, val) return cast( HexBytes, super().__new__(cls, bytesval) ) # type: ignore # https://github.com/python/typeshed/issues/2630 # noqa: E501
def __new__(cls, val): bytesval = hexstr_if_str(to_bytes, val) return super().__new__(cls, bytesval)
def bytecode(self): """ Contract bytecode """ return hexstr_if_str(to_bytes, self['bin'])
def abi_bytes_to_bytes(abi_type, data): base, sub, arrlist = process_type(abi_type) if base == 'bytes' and not arrlist: return abi_type, hexstr_if_str(to_bytes, data)
def __new__(cls, value): return super().__new__(cls, hexstr_if_str(to_bytes, value))
def __new__(cls, value): if value is None: value = 'latest' elif value not in ('latest', 'earliest', 'pending'): value = hexstr_if_str(to_hex, value) return super().__new__(cls, value)
def __new__(cls, value): return super().__new__( cls, to_checksum_address(hexstr_if_str(to_hex, value)))
def decode_logs(self, abi: EventABI, data: List[Dict]) -> Iterator["ContractLog"]: if not abi.anonymous: event_id_bytes = keccak(to_bytes(text=abi.selector)) matching_logs = [ log for log in data if log["topics"][0] == event_id_bytes ] else: matching_logs = data topics_list: List[EventABIType] = [] data_list: List[EventABIType] = [] for abi_input in abi.inputs: if abi_input.indexed: topics_list.append(abi_input) else: data_list.append(abi_input) abi_topics = LogInputABICollection(abi, topics_list) abi_data = LogInputABICollection(abi, data_list) duplicate_names = set(abi_topics.names).intersection(abi_data.names) if duplicate_names: duplicate_names_str = ", ".join([n for n in duplicate_names if n]) raise DecodingError( "The following argument names are duplicated " f"between event inputs: '{duplicate_names_str}'.") for log in matching_logs: indexed_data = log["topics"] if log.get( "anonymous", False) else log["topics"][1:] log_data = hexstr_if_str(to_bytes, log["data"]) # type: ignore if len(indexed_data) != len(abi_topics.types): raise DecodingError( f"Expected '{len(indexed_data)}' log topics. Got '{len(abi_topics.types)}'." ) def decode_items(abi_types, data): def decode_value(t, v) -> Any: if t == "address": return self.decode_address(v) elif t == "bytes32": return HexBytes(v) return v return [decode_value(t, v) for t, v in zip(abi_types, data)] decoded_topic_data = [ decode_single(topic_type, topic_data) # type: ignore for topic_type, topic_data in zip(abi_topics.types, indexed_data) ] decoded_log_data = decode_abi(abi_data.types, log_data) # type: ignore event_args = dict( itertools.chain( zip(abi_topics.names, decode_items(abi_topics.types, decoded_topic_data)), zip(abi_data.names, decode_items(abi_data.types, decoded_log_data)), )) yield ContractLog( # type: ignore name=abi.name, index=log["logIndex"], event_arguments=event_args, transaction_hash=log["transactionHash"], block_hash=log["blockHash"], block_number=log["blockNumber"], ) # type: ignore