The purpose of this page is to give you a sense of everything web3.py can do and to serve as a quick reference guide. You'll find a summary of each feature with links to learn more. You may also be interested in the :ref:`Examples <examples>` page, which demonstrates some of these features in greater detail.
After installing web3.py (via pip install web3
), you'll need to specify
async or sync web3, the provider, and any middleware you want to use
beyond the defaults.
Providers are how web3.py connects to the blockchain. The library comes with the following built-in providers:
Web3.IPCProvider
for connecting to ipc socket based JSON-RPC servers.Web3.HTTPProvider
for connecting to http and https based JSON-RPC servers.Web3.WebsocketProvider
for connecting to ws and wss websocket based JSON-RPC servers.AsyncWeb3.AsyncHTTPProvider
for connecting to http and https based JSON-RPC servers.
>>> from web3 import Web3
# IPCProvider:
>>> w3 = Web3(Web3.IPCProvider('./path/to/geth.ipc'))
# HTTPProvider:
>>> w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
# WebsocketProvider:
>>> w3 = Web3(Web3.WebsocketProvider('ws://127.0.0.1:8546'))
>>> w3.is_connected()
True
Note
The AsyncHTTPProvider is still under active development. Not all JSON-RPC methods and middleware are available yet. The list of available methods and middleware can be seen on the :class:`~web3.providers.async_rpc.AsyncHTTPProvider` docs
>>> from web3 import AsyncWeb3
>>> w3 = AsyncWeb3(AsyncWeb3.AsyncHTTPProvider('http://127.0.0.1:8545'))
>>> await w3.is_connected()
True
For more information, (e.g., connecting to remote nodes, provider auto-detection, using a test provider) see the :ref:`Providers <providers>` documentation.
Your web3.py instance may be further configured via middleware.
web3.py middleware is described using an onion metaphor, where each layer of middleware may affect both the incoming request and outgoing response from your provider. The documentation includes a :ref:`visualization <Modifying_Middleware>` of this idea.
Several middleware are :ref:`included by default <default_middleware>`. You may add to (:meth:`add <Web3.middleware_onion.add>`, :meth:`inject <Web3.middleware_onion.inject>`, :meth:`replace <Web3.middleware_onion.replace>`) or disable (:meth:`remove <Web3.middleware_onion.remove>`, :meth:`clear <Web3.middleware_onion.clear>`) any of these middleware.
Private keys are required to approve any transaction made on your behalf. The manner in which your key is secured will determine how you create and send transactions in web3.py.
A local node, like Geth, may manage your keys for you. You can reference those keys using the :attr:`web3.eth.accounts <web3.eth.Eth.accounts>` property.
A hosted node, like Infura, will have no knowledge of your keys. In this case, you'll need to have your private key available locally for signing transactions.
Full documentation on the distinction between keys can be found :ref:`here <eth-account>`.
The :ref:`Web3 <web3_base>` class includes a number of convenient utility functions:
- :meth:`Web3.is_encodable() <web3.w3.is_encodable>`
- :meth:`Web3.to_bytes() <web3.Web3.to_bytes>`
- :meth:`Web3.to_hex() <web3.Web3.to_hex>`
- :meth:`Web3.to_int() <web3.Web3.to_int>`
- :meth:`Web3.to_json() <web3.Web3.to_json>`
- :meth:`Web3.to_text() <web3.Web3.to_text>`
- :meth:`Web3.is_address() <web3.Web3.is_address>`
- :meth:`Web3.is_checksum_address() <web3.Web3.is_checksum_address>`
- :meth:`Web3.to_checksum_address() <web3.Web3.to_checksum_address>`
The most commonly used APIs for interacting with Ethereum can be found under the
web3.eth
namespace. As a reminder, the :ref:`Examples <examples>` page will
demonstrate how to use several of these methods.
Viewing account balances (:meth:`get_balance <web3.eth.Eth.get_balance>`), transactions (:meth:`get_transaction <web3.eth.Eth.get_transaction>`), and block data (:meth:`get_block <web3.eth.Eth.get_block>`) are some of the most common starting points in web3.py.
- :meth:`web3.eth.get_balance() <web3.eth.Eth.get_balance>`
- :meth:`web3.eth.get_block() <web3.eth.Eth.get_block>`
- :meth:`web3.eth.get_block_transaction_count() <web3.eth.Eth.get_block_transaction_count>`
- :meth:`web3.eth.get_code() <web3.eth.Eth.get_code>`
- :meth:`web3.eth.get_proof() <web3.eth.Eth.get_proof>`
- :meth:`web3.eth.get_storage_at() <web3.eth.Eth.get_storage_at>`
- :meth:`web3.eth.get_transaction() <web3.eth.Eth.get_transaction>`
- :meth:`web3.eth.get_transaction_by_block() <web3.eth.Eth.get_transaction_by_block>`
- :meth:`web3.eth.get_transaction_count() <web3.eth.Eth.get_transaction_count>`
- :meth:`web3.eth.get_uncle_by_block() <web3.eth.Eth.get_uncle_by_block>`
- :meth:`web3.eth.get_uncle_count() <web3.eth.Eth.get_uncle_count>`
The most common use cases will be satisfied with :meth:`send_transaction <web3.eth.Eth.send_transaction>` or the combination of :meth:`sign_transaction <web3.eth.Eth.sign_transaction>` and :meth:`send_raw_transaction <web3.eth.Eth.send_raw_transaction>`.
Note
If interacting with a smart contract, a dedicated API exists. See the next section, :ref:`Contracts <overview_contracts>`.
- :meth:`web3.eth.send_transaction() <web3.eth.Eth.send_transaction>`
- :meth:`web3.eth.sign_transaction() <web3.eth.Eth.sign_transaction>`
- :meth:`web3.eth.send_raw_transaction() <web3.eth.Eth.send_raw_transaction>`
- :meth:`web3.eth.replace_transaction() <web3.eth.Eth.replace_transaction>`
- :meth:`web3.eth.modify_transaction() <web3.eth.Eth.modify_transaction>`
- :meth:`web3.eth.wait_for_transaction_receipt() <web3.eth.Eth.wait_for_transaction_receipt>`
- :meth:`web3.eth.get_transaction_receipt() <web3.eth.Eth.get_transaction_receipt>`
- :meth:`web3.eth.sign() <web3.eth.Eth.sign>`
- :meth:`web3.eth.sign_typed_data() <web3.eth.Eth.sign_typed_data>`
- :meth:`web3.eth.estimate_gas() <web3.eth.Eth.estimate_gas>`
- :meth:`web3.eth.generate_gas_price() <web3.eth.Eth.generate_gas_price>`
- :meth:`web3.eth.set_gas_price_strategy() <web3.eth.Eth.set_gas_price_strategy>`
The two most common use cases involving smart contracts are deploying and executing functions on a deployed contract.
Deployment requires that the contract already be compiled, with its bytecode and ABI available. This compilation step can be done within Remix or one of the many contract development frameworks, such as Brownie.
Once the contract object is instantiated, calling transact
on the
:meth:`constructor <web3.contract.Contract.constructor>` method will deploy an
instance of the contract:
>>> ExampleContract = w3.eth.contract(abi=abi, bytecode=bytecode)
>>> tx_hash = ExampleContract.constructor().transact()
>>> tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
>>> tx_receipt.contractAddress
'0x8a22225eD7eD460D7ee3842bce2402B9deaD23D3'
Once loaded into a Contract object, the functions of a deployed contract are available
on the functions
namespace:
>>> deployed_contract = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)
>>> deployed_contract.functions.myFunction(42).transact()
If you want to read data from a contract (or see the result of transaction locally, without executing it on the network), you can use the :meth:`ContractFunction.call <web3.contract.ContractFunction.call>` method, or the more concise :attr:`ContractCaller <web3.contract.ContractCaller>` syntax:
# Using ContractFunction.call
>>> deployed_contract.functions.getMyValue().call()
42
# Using ContractCaller
>>> deployed_contract.caller().getMyValue()
42
For more, see the full :ref:`Contracts` documentation.
- :meth:`web3.eth.contract() <web3.eth.Eth.contract>`
- :attr:`Contract.address <web3.contract.Contract.address>`
- :attr:`Contract.abi <web3.contract.Contract.abi>`
- :attr:`Contract.bytecode <web3.contract.Contract.bytecode>`
- :attr:`Contract.bytecode_runtime <web3.contract.Contract.bytecode_runtime>`
- :attr:`Contract.functions <web3.contract.Contract.functions>`
- :attr:`Contract.events <web3.contract.Contract.events>`
- :attr:`Contract.fallback <web3.contract.Contract.fallback.call>`
- :meth:`Contract.constructor() <web3.contract.Contract.constructor>`
- :meth:`Contract.encodeABI() <web3.contract.Contract.encodeABI>`
- :attr:`web3.contract.ContractFunction <web3.contract.ContractFunction>`
- :attr:`web3.contract.ContractEvents <web3.contract.ContractEvents>`
If you want to react to new blocks being mined or specific events being emitted by a contract, you can leverage web3.py filters.
# Use case: filter for new blocks
>>> new_filter = web3.eth.filter('latest')
# Use case: filter for contract event "MyEvent"
>>> new_filter = deployed_contract.events.MyEvent.create_filter(fromBlock='latest')
# retrieve filter results:
>>> new_filter.get_all_entries()
>>> new_filter.get_new_entries()
More complex patterns for creating filters and polling for logs can be found in the :ref:`Filtering <filtering>` documentation.
- :meth:`web3.eth.filter() <web3.eth.Eth.filter>`
- :meth:`web3.eth.get_filter_changes() <web3.eth.Eth.get_filter_changes>`
- :meth:`web3.eth.get_filter_logs() <web3.eth.Eth.get_filter_logs>`
- :meth:`web3.eth.uninstall_filter() <web3.eth.Eth.uninstall_filter>`
- :meth:`web3.eth.get_logs() <web3.eth.Eth.get_logs>`
- :meth:`Contract.events.your_event_name.create_filter() <web3.contract.Contract.events.your_event_name.create_filter>`
- :meth:`Contract.events.your_event_name.build_filter() <web3.contract.Contract.events.your_event_name.build_filter>`
- :meth:`Filter.get_new_entries() <web3.utils.filters.Filter.get_new_entries>`
- :meth:`Filter.get_all_entries() <web3.utils.filters.Filter.get_all_entries>`
- :meth:`Filter.format_entry() <web3.utils.filters.Filter.format_entry>`
- :meth:`Filter.is_valid_entry() <web3.utils.filters.Filter.is_valid_entry>`
Some basic network properties are available on the web3.net
object:
ethPM allows you to package up your contracts for reuse or use contracts from another trusted registry. See the full details :ref:`here <ethpm>`.
Ethereum Name Service (ENS) provides the infrastructure
for human-readable addresses. As an example, instead of
0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359
, you can send funds to
ethereumfoundation.eth
. web3.py has support for ENS, documented
:ref:`here <ens_overview>`.