diff --git a/docs/providers.rst b/docs/providers.rst index 736e748f0d..e966d83984 100644 --- a/docs/providers.rst +++ b/docs/providers.rst @@ -415,6 +415,7 @@ Eth - :meth:`web3.eth.hashrate ` - :meth:`web3.eth.max_priority_fee ` - :meth:`web3.eth.mining ` +- :meth:`web3.eth.syncing ` - :meth:`web3.eth.call() ` - :meth:`web3.eth.estimate_gas() ` - :meth:`web3.eth.generate_gas_price() ` diff --git a/newsfragments/2331.feature.rst b/newsfragments/2331.feature.rst new file mode 100644 index 0000000000..8e69fd1ee0 --- /dev/null +++ b/newsfragments/2331.feature.rst @@ -0,0 +1 @@ +Add async `eth.syncing` method diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index 7097d71f03..887201fa39 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -1025,6 +1025,24 @@ async def test_async_eth_get_logs_with_logs_none_topic_args(self, async_w3: "Web result = await async_w3.eth.get_logs(filter_params) # type: ignore assert len(result) == 0 + @pytest.mark.asyncio + async def test_async_eth_syncing(self, async_w3: "Web3") -> None: + syncing = await async_w3.eth.syncing # type: ignore + + assert is_boolean(syncing) or is_dict(syncing) + + if is_boolean(syncing): + assert syncing is False + elif is_dict(syncing): + sync_dict = cast(SyncStatus, syncing) + assert 'startingBlock' in sync_dict + assert 'currentBlock' in sync_dict + assert 'highestBlock' in sync_dict + + assert is_integer(sync_dict['startingBlock']) + assert is_integer(sync_dict['currentBlock']) + assert is_integer(sync_dict['highestBlock']) + def test_async_provider_default_account( self, async_w3: "Web3", diff --git a/web3/eth.py b/web3/eth.py index 8edd74f9da..1e644609e6 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -322,6 +322,11 @@ def call_munger( mungers=None, ) + _is_syncing: Method[Callable[[], Union[SyncStatus, bool]]] = Method( + RPC.eth_syncing, + mungers=None, + ) + _get_transaction_receipt: Method[Callable[[_Hash32], TxReceipt]] = Method( RPC.eth_getTransactionReceipt, mungers=[default_root_munger] @@ -377,6 +382,10 @@ async def max_priority_fee(self) -> Wei: async def mining(self) -> bool: return await self._is_mining() # type: ignore + @property + async def syncing(self) -> Union[SyncStatus, bool]: + return await self._is_syncing() # type: ignore + async def fee_history( self, block_count: int, @@ -551,14 +560,9 @@ def protocolVersion(self) -> str: ) return self.protocol_version - is_syncing: Method[Callable[[], Union[SyncStatus, bool]]] = Method( - RPC.eth_syncing, - mungers=None, - ) - @property def syncing(self) -> Union[SyncStatus, bool]: - return self.is_syncing() + return self._is_syncing() @property def coinbase(self) -> ChecksumAddress: