Skip to content

Commit 363e65a

Browse files
authored
asyncify get_storage_at (#2350)
1 parent e36bffa commit 363e65a

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

docs/providers.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ Eth
429429
- :meth:`web3.eth.get_transaction() <web3.eth.Eth.get_transaction>`
430430
- :meth:`web3.eth.get_transaction_count() <web3.eth.Eth.get_transaction_count>`
431431
- :meth:`web3.eth.get_transaction_receipt() <web3.eth.Eth.get_transaction_receipt>`
432+
- :meth:`web3.eth.get_storage_at() <web3.eth.Eth.get_storage_at>`
432433
- :meth:`web3.eth.send_transaction() <web3.eth.Eth.send_transaction>`
433434
- :meth:`web3.eth.send_raw_transaction() <web3.eth.Eth.send_raw_transaction>`
434435
- :meth:`web3.eth.wait_for_transaction_receipt() <web3.eth.Eth.wait_for_transaction_receipt>`

newsfragments/2350.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+ Add async `eth.get_storage_at` method

web3/_utils/module_testing/eth_module.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,30 @@ async def test_async_eth_syncing(self, async_w3: "Web3") -> None:
10941094
assert is_integer(sync_dict['currentBlock'])
10951095
assert is_integer(sync_dict['highestBlock'])
10961096

1097+
@pytest.mark.asyncio
1098+
async def test_async_eth_get_storage_at(
1099+
self, async_w3: "Web3", emitter_contract_address: ChecksumAddress
1100+
) -> None:
1101+
storage = await async_w3.eth.get_storage_at(emitter_contract_address, 0) # type: ignore
1102+
assert isinstance(storage, HexBytes)
1103+
1104+
@pytest.mark.asyncio
1105+
@pytest.mark.xfail
1106+
async def test_async_eth_get_storage_at_ens_name(
1107+
self, async_w3: "Web3", emitter_contract_address: ChecksumAddress
1108+
) -> None:
1109+
with ens_addresses(async_w3, {'emitter.eth': emitter_contract_address}):
1110+
storage = await async_w3.eth.get_storage_at('emitter.eth', 0) # type: ignore
1111+
assert isinstance(storage, HexBytes)
1112+
1113+
@pytest.mark.asyncio
1114+
async def test_async_eth_get_storage_at_invalid_address(self, async_w3: "Web3") -> None:
1115+
coinbase = await async_w3.eth.coinbase # type: ignore
1116+
with pytest.raises(InvalidAddress):
1117+
await async_w3.eth.get_storage_at(
1118+
ChecksumAddress(HexAddress(HexStr(coinbase.lower()))),
1119+
0) # type: ignore
1120+
10971121
def test_async_provider_default_account(
10981122
self,
10991123
async_w3: "Web3",

web3/eth.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ def block_id_munger(
283283
block_identifier = self.default_block
284284
return (account, block_identifier)
285285

286+
def get_storage_at_munger(
287+
self,
288+
account: Union[Address, ChecksumAddress, ENS],
289+
position: int,
290+
block_identifier: Optional[BlockIdentifier] = None
291+
) -> Tuple[Union[Address, ChecksumAddress, ENS], int, BlockIdentifier]:
292+
if block_identifier is None:
293+
block_identifier = self.default_block
294+
return (account, position, block_identifier)
295+
286296
def call_munger(
287297
self,
288298
transaction: TxParams,
@@ -519,6 +529,19 @@ async def _wait_for_tx_receipt_with_timeout(
519529
f"after {timeout} seconds"
520530
)
521531

532+
_get_storage_at: Method[Callable[..., Awaitable[HexBytes]]] = Method(
533+
RPC.eth_getStorageAt,
534+
mungers=[BaseEth.get_storage_at_munger],
535+
)
536+
537+
async def get_storage_at(
538+
self,
539+
account: Union[Address, ChecksumAddress, ENS],
540+
position: int,
541+
block_identifier: Optional[BlockIdentifier] = None
542+
) -> HexBytes:
543+
return await self._get_storage_at(account, position, block_identifier)
544+
522545
async def call(
523546
self,
524547
transaction: TxParams,
@@ -636,19 +659,9 @@ def max_priority_fee(self) -> Wei:
636659
)
637660
return fee_history_priority_fee(self)
638661

639-
def get_storage_at_munger(
640-
self,
641-
account: Union[Address, ChecksumAddress, ENS],
642-
position: int,
643-
block_identifier: Optional[BlockIdentifier] = None
644-
) -> Tuple[Union[Address, ChecksumAddress, ENS], int, BlockIdentifier]:
645-
if block_identifier is None:
646-
block_identifier = self.default_block
647-
return (account, position, block_identifier)
648-
649662
get_storage_at: Method[Callable[..., HexBytes]] = Method(
650663
RPC.eth_getStorageAt,
651-
mungers=[get_storage_at_munger],
664+
mungers=[BaseEth.get_storage_at_munger],
652665
)
653666

654667
def get_proof_munger(

0 commit comments

Comments
 (0)