From e2fa6d09ffba05c47e5ed6241eb30f4914d19af0 Mon Sep 17 00:00:00 2001 From: kclowes Date: Thu, 20 Oct 2022 10:13:52 -0600 Subject: [PATCH 1/2] Add async get_block_tx_count --- web3/_utils/module_testing/eth_module.py | 44 ++++++++++++++++++++++++ web3/eth.py | 26 +++++++------- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index aea6409740..3db2b5decc 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -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: diff --git a/web3/eth.py b/web3/eth.py index c58220abea..b95f712a8c 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -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 @@ -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` From 2bb2ce0a0c30c9df397209f43e9cc5b07e88c2aa Mon Sep 17 00:00:00 2001 From: kclowes Date: Thu, 27 Oct 2022 12:14:38 -0600 Subject: [PATCH 2/2] Add newsfragment --- newsfragments/2687.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/2687.feature.rst diff --git a/newsfragments/2687.feature.rst b/newsfragments/2687.feature.rst new file mode 100644 index 0000000000..0c2f1e0ade --- /dev/null +++ b/newsfragments/2687.feature.rst @@ -0,0 +1 @@ +Add async ``w3.eth.get_block_transaction_count``