Web3.py Patterns - Customization
Web3.py Patterns - Customization
py Patterns: Customization
Web3.py
Web3.py Patterns:
Customization
Marc Garreau
May 25, 2022 • 3 min read
1. Middleware
What
Middleware allows you to add some behavior to existing methods prior to making
a request or after a result is received.
When
Reach for middleware when you want something to happen every time a certain
RPC call or set of calls are performed, e.g., logging, data visualization, data
conversion, etc.
How
# do post-processing here
Middleware is executed in a particular order, so the API allows you to add your
new middleware to the end of the list, inject , replace or remove a layer, or
clear the whole middleware stack.
2. Custom Methods
What
When
Registering custom methods can be handy if you're working with a client with
nonstandard RPC commands or are testing some custom functionality within a
forked client.
Custom methods can also be used to overwrite existing methods, if you want to
apply your own request or result formatter.
How
https://snakecharmers.ethereum.org/web3-py-patterns-customizations/ 1/3
5/30/24, 8:05 PM Web3.py Patterns: Customization
w3.eth.attach_methods({"create_access_list": Method("eth_createAccessList")})
w3.eth.create_access_list(...)
You may optionally include custom handlers for input munging, request, and
result formatters.
w3.eth.attach_methods({
"example": Method(
"eth_example",
mungers=[...],
request_formatters=[...],
result_formatters=[...],
is_property=False,
),
})
w3.eth.example()
3. External Modules
What
External modules offer still more flexibility by allowing you to import groups of
APIs under one banner. Think: plugins.
When
How
Modules need only be classes and can reference the parent Web3 instance.
Configure your external modules at the time of Web3 instantiation using the
external_modules keyword argument, or at any point via the attach_modules
method:
4. Custom Providers
What
When
Building a custom provider is only relevant for the rare occasions that you're
plugging into a custom testing framework, or something in that vein. If you're
just looking to connect to another EVM blockchain, sidechain, or rollup, typically
you can just configure one of the existing options: HTTPProvider, IPCProvider,
or WebsocketProvider.
How
class CustomProvider(BaseProvider):
middlewares = ()
https://snakecharmers.ethereum.org/web3-py-patterns-customizations/ 2/3
5/30/24, 8:05 PM Web3.py Patterns: Customization
def isConnected(self):
print(True)
w3 = Web3(CustomProvider())
w3.eth.get_block("latest")
# AttributeDict({"welp": "lol"})
Wrapping up
The options above are ordered roughly from least to most flexibility they offer. In
practice, I expect middleware and external modules to get the most mileage,
particularly once trusted external modules become commonplace.
Deliberately not included in this post is monkey patching. If you've gone down
that path... is everything okay? Seriously though, open an issue if you have
another vector you'd like to customize that requires monkey patching today.
Powered by Ghost
https://snakecharmers.ethereum.org/web3-py-patterns-customizations/ 3/3