install_api_call_counter_middleware

Documentation for eth_defi.chain.install_api_call_counter_middleware function.

install_api_call_counter_middleware(web3)

Install API call counter middleware.

Measure total and per-API EVM call counts for your application.

  • Every time a Web3 API is called increase its count.

  • Attach web3.api_counter object to the connection

Compatible with both web3.py v6 and v7.

Example:

from eth_defi.chain import install_api_call_counter_middleware

web3 = Web3(tester)
counter = install_api_call_counter_middleware(web3)

# Make an API call
chain_id = web3.eth.chain_id
assert counter["total"] == 1
assert counter["eth_chainId"] == 1

# Make another API call
block_number = web3.eth.block_number
assert counter["total"] == 2
assert counter["eth_blockNumber"] == 1
Returns

Counter object with columns per RPC endpoint and “total”

Parameters

web3 (web3.main.Web3) –

Return type

collections.Counter