def match_fn(codec: ABICodec, match_values_and_abi: Collection[Tuple[str, Any]], data: Any) -> bool: """Match function used for filtering non-indexed event arguments. Values provided through the match_values_and_abi parameter are compared to the abi decoded log data. """ abi_types, all_match_values = zip(*match_values_and_abi) decoded_values = codec.decode_abi(abi_types, HexBytes(data)) for data_value, match_values, abi_type in zip(decoded_values, all_match_values, abi_types): if match_values is None: continue normalized_data = normalize_data_values(abi_type, data_value) for value in match_values: if not codec.is_encodable(abi_type, value): raise ValueError(f"Value {value} is of the wrong abi type. " f"Expected {abi_type} typed value.") if value == normalized_data: break else: return False return True
class Web3: # Providers HTTPProvider = HTTPProvider IPCProvider = IPCProvider EthereumTesterProvider = EthereumTesterProvider WebsocketProvider = WebsocketProvider # Managers RequestManager = DefaultRequestManager # Iban Iban = Iban # Encoding and Decoding toBytes = staticmethod(to_bytes) toInt = staticmethod(to_int) toHex = staticmethod(to_hex) toText = staticmethod(to_text) toJSON = staticmethod(to_json) # Currency Utility toWei = staticmethod(to_wei) fromWei = staticmethod(from_wei) # Address Utility isAddress = staticmethod(is_address) isChecksumAddress = staticmethod(is_checksum_address) toChecksumAddress = staticmethod(to_checksum_address) # mypy Types eth: Eth parity: Parity geth: Geth net: Net async_eth: AsyncEth def __init__( self, provider: Optional[BaseProvider] = None, middlewares: Optional[Sequence[Any]] = None, modules: Optional[Dict[str, Sequence[Any]]] = None, ens: ENS = cast(ENS, empty) ) -> None: self.manager = self.RequestManager(self, provider, middlewares) # this codec gets used in the module initialization, # so it needs to come before attach_modules self.codec = ABICodec(build_default_registry()) if modules is None: modules = get_default_modules() attach_modules(self, modules) self.ens = ens @property def middleware_onion(self) -> MiddlewareOnion: return self.manager.middleware_onion @property def provider(self) -> BaseProvider: return self.manager.provider @provider.setter def provider(self, provider: BaseProvider) -> None: self.manager.provider = provider @property def clientVersion(self) -> str: return self.manager.request_blocking(RPC.web3_clientVersion, []) @property def api(self) -> str: from web3 import __version__ return __version__ @staticmethod @deprecated_for("keccak") @apply_to_return_value(HexBytes) def sha3(primitive: Optional[Primitives] = None, text: Optional[str] = None, hexstr: Optional[HexStr] = None) -> bytes: return Web3.keccak(primitive, text, hexstr) @staticmethod @apply_to_return_value(HexBytes) def keccak(primitive: Optional[Primitives] = None, text: Optional[str] = None, hexstr: Optional[HexStr] = None) -> bytes: if isinstance(primitive, (bytes, int, type(None))): input_bytes = to_bytes(primitive, hexstr=hexstr, text=text) return eth_utils_keccak(input_bytes) raise TypeError( "You called keccak with first arg %r and keywords %r. You must call it with one of " "these approaches: keccak(text='txt'), keccak(hexstr='0x747874'), " "keccak(b'\\x74\\x78\\x74'), or keccak(0x747874)." % ( primitive, {'text': text, 'hexstr': hexstr} ) ) @combomethod @deprecated_for("solidityKeccak") def soliditySha3(cls, abi_types: List[TypeStr], values: List[Any]) -> bytes: return cls.solidityKeccak(abi_types, values) @combomethod def solidityKeccak(cls, abi_types: List[TypeStr], values: List[Any]) -> bytes: """ Executes keccak256 exactly as Solidity does. Takes list of abi_types as inputs -- `[uint24, int8[], bool]` and list of corresponding values -- `[20, [-1, 5, 0], True]` """ if len(abi_types) != len(values): raise ValueError( "Length mismatch between provided abi types and values. Got " "{0} types and {1} values.".format(len(abi_types), len(values)) ) if isinstance(cls, type): w3 = None else: w3 = cls normalized_values = map_abi_data([abi_ens_resolver(w3)], abi_types, values) hex_string = add_0x_prefix(HexStr(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values) ))) return cls.keccak(hexstr=hex_string) def isConnected(self) -> bool: return self.provider.isConnected() def is_encodable(self, _type: TypeStr, value: Any) -> bool: return self.codec.is_encodable(_type, value) @property def ens(self) -> ENS: if self._ens is cast(ENS, empty): return ENS.fromWeb3(self) else: return self._ens @ens.setter def ens(self, new_ens: ENS) -> None: self._ens = new_ens @property def pm(self) -> "PM": if hasattr(self, '_pm'): # ignored b/c property is dynamically set via enable_unstable_package_management_api return self._pm # type: ignore else: raise AttributeError( "The Package Management feature is disabled by default until " "its API stabilizes. To use these features, please enable them by running " "`w3.enable_unstable_package_management_api()` and try again." ) def enable_unstable_package_management_api(self) -> None: from web3.pm import PM # noqa: F811 if not hasattr(self, '_pm'): attach_modules(self, {'_pm': (PM,)}) def enable_strict_bytes_type_checking(self) -> None: self.codec = ABICodec(build_strict_registry())
class Web3: # Providers HTTPProvider = HTTPProvider IPCProvider = IPCProvider EthereumTesterProvider = EthereumTesterProvider WebsocketProvider = WebsocketProvider AsyncHTTPProvider = AsyncHTTPProvider # Managers RequestManager = DefaultRequestManager # Iban Iban = Iban # Encoding and Decoding @staticmethod @wraps(to_bytes) def toBytes(primitive: Primitives = None, hexstr: HexStr = None, text: str = None) -> bytes: return to_bytes(primitive, hexstr, text) @staticmethod @wraps(to_int) def toInt(primitive: Primitives = None, hexstr: HexStr = None, text: str = None) -> int: return to_int(primitive, hexstr, text) @staticmethod @wraps(to_hex) def toHex(primitive: Primitives = None, hexstr: HexStr = None, text: str = None) -> HexStr: return to_hex(primitive, hexstr, text) @staticmethod @wraps(to_text) def toText(primitive: Primitives = None, hexstr: HexStr = None, text: str = None) -> str: return to_text(primitive, hexstr, text) @staticmethod @wraps(to_json) def toJSON(obj: Dict[Any, Any]) -> str: return to_json(obj) # Currency Utility @staticmethod @wraps(to_wei) def toWei(number: Union[int, float, str, decimal.Decimal], unit: str) -> Wei: return cast(Wei, to_wei(number, unit)) @staticmethod @wraps(from_wei) def fromWei(number: int, unit: str) -> Union[int, decimal.Decimal]: return from_wei(number, unit) # Address Utility @staticmethod @wraps(is_address) def isAddress(value: Any) -> bool: return is_address(value) @staticmethod @wraps(is_checksum_address) def isChecksumAddress(value: Any) -> bool: return is_checksum_address(value) @staticmethod @wraps(to_checksum_address) def toChecksumAddress( value: Union[AnyAddress, str, bytes]) -> ChecksumAddress: return to_checksum_address(value) # mypy Types eth: Eth parity: Parity geth: Geth net: Net async_net: AsyncNet def __init__( self, provider: Optional[BaseProvider] = None, middlewares: Optional[Sequence[Any]] = None, modules: Optional[Dict[str, Union[Type[Module], Sequence[Any]]]] = None, external_modules: Optional[Dict[str, Union[Type[Module], Sequence[Any]]]] = None, ens: ENS = cast(ENS, empty) ) -> None: self.manager = self.RequestManager(self, provider, middlewares) # this codec gets used in the module initialization, # so it needs to come before attach_modules self.codec = ABICodec(build_default_registry()) if modules is None: modules = get_default_modules() self.attach_modules(modules) if external_modules is not None: self.attach_modules(external_modules) self.ens = ens @property def middleware_onion(self) -> MiddlewareOnion: return self.manager.middleware_onion @property def provider(self) -> BaseProvider: return self.manager.provider @provider.setter def provider(self, provider: BaseProvider) -> None: self.manager.provider = provider @property def clientVersion(self) -> str: return self.manager.request_blocking(RPC.web3_clientVersion, []) @property def api(self) -> str: from web3 import __version__ return __version__ @staticmethod @apply_to_return_value(HexBytes) def keccak(primitive: Optional[Primitives] = None, text: Optional[str] = None, hexstr: Optional[HexStr] = None) -> bytes: if isinstance(primitive, (bytes, int, type(None))): input_bytes = to_bytes(primitive, hexstr=hexstr, text=text) return eth_utils_keccak(input_bytes) raise TypeError( f"You called keccak with first arg {primitive!r} and keywords {{'text': {text!r}, " f"'hexstr': {hexstr!r}}}. You must call it with one of these approaches: " "keccak(text='txt'), keccak(hexstr='0x747874'), keccak(b'\\x74\\x78\\x74'), " "or keccak(0x747874).") @combomethod def solidityKeccak(cls, abi_types: List[TypeStr], values: List[Any]) -> bytes: """ Executes keccak256 exactly as Solidity does. Takes list of abi_types as inputs -- `[uint24, int8[], bool]` and list of corresponding values -- `[20, [-1, 5, 0], True]` """ if len(abi_types) != len(values): raise ValueError( "Length mismatch between provided abi types and values. Got " f"{len(abi_types)} types and {len(values)} values.") if isinstance(cls, type): w3 = None else: w3 = cls normalized_values = map_abi_data([abi_ens_resolver(w3)], abi_types, values) hex_string = add_0x_prefix( HexStr(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values)))) return cls.keccak(hexstr=hex_string) def attach_modules( self, modules: Optional[Dict[str, Union[Type[Module], Sequence[Any]]]]) -> None: """ Attach modules to the `Web3` instance. """ _attach_modules(self, modules) def isConnected(self) -> bool: return self.provider.isConnected() def is_encodable(self, _type: TypeStr, value: Any) -> bool: return self.codec.is_encodable(_type, value) @property def ens(self) -> ENS: if self._ens is cast(ENS, empty): return ENS.fromWeb3(self) else: return self._ens @ens.setter def ens(self, new_ens: ENS) -> None: self._ens = new_ens @property def pm(self) -> "PM": if hasattr(self, '_pm'): # ignored b/c property is dynamically set via enable_unstable_package_management_api return self._pm # type: ignore else: raise AttributeError( "The Package Management feature is disabled by default until " "its API stabilizes. To use these features, please enable them by running " "`w3.enable_unstable_package_management_api()` and try again.") def enable_unstable_package_management_api(self) -> None: from web3.pm import PM # noqa: F811 if not hasattr(self, '_pm'): self.attach_modules({'_pm': PM}) def enable_strict_bytes_type_checking(self) -> None: self.codec = ABICodec(build_strict_registry())