Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async get_block_tx_count #2687

Merged
merged 2 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/2687.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add async ``w3.eth.get_block_transaction_count``
44 changes: 44 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,50 @@ def test_async_provider_default_block(
# reset to default
async_w3.eth.default_block = "latest"

@pytest.mark.asyncio
async def test_eth_getBlockTransactionCountByHash_empty_block(
self, async_w3: "Web3", empty_block: BlockData
) -> None:
transaction_count = await async_w3.eth.get_block_transaction_count( # type: ignore # noqa: E501
empty_block["hash"]
)

assert is_integer(transaction_count)
assert transaction_count == 0

@pytest.mark.asyncio
async def test_eth_getBlockTransactionCountByNumber_empty_block(
self, async_w3: "Web3", empty_block: BlockData
) -> None:
transaction_count = await async_w3.eth.get_block_transaction_count( # type: ignore # noqa: E501
empty_block["number"]
)

assert is_integer(transaction_count)
assert transaction_count == 0

@pytest.mark.asyncio
async def test_eth_getBlockTransactionCountByHash_block_with_txn(
self, async_w3: "Web3", block_with_txn: BlockData
) -> None:
transaction_count = await async_w3.eth.get_block_transaction_count( # type: ignore # noqa: E501
block_with_txn["hash"]
)

assert is_integer(transaction_count)
assert transaction_count >= 1

@pytest.mark.asyncio
async def test_eth_getBlockTransactionCountByNumber_block_with_txn(
self, async_w3: "Web3", block_with_txn: BlockData
) -> None:
transaction_count = await async_w3.eth.get_block_transaction_count( # type: ignore # noqa: E501
block_with_txn["number"]
)

assert is_integer(transaction_count)
assert transaction_count >= 1


class EthModuleTest:
def test_eth_syncing(self, w3: "Web3") -> None:
Expand Down
26 changes: 13 additions & 13 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,19 @@ def call_munger(
RPC.eth_getTransactionReceipt, mungers=[default_root_munger]
)

"""
`eth_getBlockTransactionCountByHash`
`eth_getBlockTransactionCountByNumber`
"""
get_block_transaction_count: Method[Callable[[BlockIdentifier], int]] = Method(
method_choice_depends_on_args=select_method_for_block_identifier(
if_predefined=RPC.eth_getBlockTransactionCountByNumber,
if_hash=RPC.eth_getBlockTransactionCountByHash,
if_number=RPC.eth_getBlockTransactionCountByNumber,
),
mungers=[default_root_munger],
)

@overload
def contract(
self, address: None = None, **kwargs: Any
Expand Down Expand Up @@ -697,19 +710,6 @@ def get_block(
RPC.eth_getCode, mungers=[BaseEth.block_id_munger]
)

"""
`eth_getBlockTransactionCountByHash`
`eth_getBlockTransactionCountByNumber`
"""
get_block_transaction_count: Method[Callable[[BlockIdentifier], int]] = Method(
method_choice_depends_on_args=select_method_for_block_identifier(
if_predefined=RPC.eth_getBlockTransactionCountByNumber,
if_hash=RPC.eth_getBlockTransactionCountByHash,
if_number=RPC.eth_getBlockTransactionCountByNumber,
),
mungers=[default_root_munger],
)

"""
`eth_getUncleCountByBlockHash`
`eth_getUncleCountByBlockNumber`
Expand Down