def test_attach_modules_with_wrong_module_format(): mods = {"eth": (MockEth, MockGeth, MockGethPersonal)} w3 = Web3(EthereumTesterProvider, modules={}) with pytest.raises( ValidationError, match="Module definitions can only have 1 or 2 elements"): attach_modules(w3, mods)
def test_attach_modules_with_existing_modules(): mods = { "eth": (MockEth,), } w3 = Web3(EthereumTesterProvider, modules=mods) with pytest.raises(AttributeError, match="The web3 object already has an attribute with that name"): attach_modules(w3, mods)
def __init__(self, provider=None, middlewares=None, modules=None, ens=empty): self.manager = self.RequestManager(self, provider, middlewares) if modules is None: modules = get_default_modules() attach_modules(self, modules) self.ens = ens
def test_attach_modules(): mods = { "geth": (MockGeth, { "personal": (MockGethPersonal,), "admin": (MockGethAdmin,), }), "eth": (MockEth,), } w3 = Web3(EthereumTesterProvider(), modules={}) attach_modules(w3, mods) assert w3.eth.block_number() == 42 assert w3.geth.personal.unlock_account() is True assert w3.geth.admin.start_ws() is True
def flashbot(w3: Web3, flashbots_key_id: str, flashbots_secret, flashbots_url = DEFAULT_FLASHBOTS_RELAY): """ Injects the flashbots module and middleware to w3. """ headers = { "Authorization": flashbots_key_id + ":" + flashbots_secret , # auth "Content-type": "application/json", # need to set the header again } flashbots_provider = HTTPProvider(flashbots_url, request_kwargs = { 'headers': headers }) middleware = construct_flashbots_middleware(flashbots_provider) w3.middleware_onion.add(middleware) # attach modules to add the new namespace commands attach_modules(w3, { "flashbots" : (Flashbots,) })
def flashbot( w3: Web3, signature_account: LocalAccount, ): """ Injects the flashbots module and middleware to w3. """ flashbots_provider = FlashbotProvider(signature_account) flash_middleware = construct_flashbots_middleware(flashbots_provider) w3.middleware_onion.add(flash_middleware) # attach modules to add the new namespace commands attach_modules(w3, {"flashbots": (Flashbots, )})
def __init__(self, provider=None, middlewares=None, modules=None, ens=empty): self.manager = self.RequestManager(self, provider, middlewares) if modules is None: modules = get_default_modules() attach_modules(self, modules) self.codec = ABICodec(build_default_registry()) self.ens = ens
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) if modules is None: modules = get_default_modules() attach_modules(self, modules) self.codec = ABICodec(build_default_registry()) self.ens = ens
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
def flashbot( w3: Web3, signature_account: LocalAccount, endpoint_uri: Optional[Union[URI, str]] = None, ): """ Injects the flashbots module and middleware to w3. """ flashbots_provider = FlashbotProvider(signature_account, endpoint_uri) # goerli connection requires extra PoA middleware if endpoint_uri is not None and "goerli" in endpoint_uri: w3.middleware_onion.inject(geth_poa_middleware, layer=0) flash_middleware = construct_flashbots_middleware(flashbots_provider) w3.middleware_onion.add(flash_middleware) # attach modules to add the new namespace commands attach_modules(w3, {"flashbots": (Flashbots, )})
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,)})