From 31a31b9dbb53163b0dc9237e29a8359d157be898 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Tue, 18 Jul 2023 14:48:42 -0500 Subject: [PATCH 1/2] feat: block id --- src/ape/api/providers.py | 41 ++++++++++++++++++++++--------- tests/functional/test_provider.py | 7 ++++++ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/ape/api/providers.py b/src/ape/api/providers.py index eb998c6cb8..f9be01b390 100644 --- a/src/ape/api/providers.py +++ b/src/ape/api/providers.py @@ -786,8 +786,9 @@ def estimate_gas_cost(self, txn: TransactionAPI, **kwargs) -> int: The transaction to estimate the gas for. kwargs: * ``block_identifier`` (:class:`~ape.types.BlockID`): The block ID - to use when estimating the transaction. Useful for - checking a past estimation cost of a transaction. + to use when estimating the transaction. Useful for checking a + past estimation cost of a transaction. Also, you can alias + ``block_id``. * ``state_overrides`` (Dict): Modify the state of the blockchain prior to estimation. @@ -813,7 +814,7 @@ def estimate_gas_cost(self, txn: TransactionAPI, **kwargs) -> int: txn_dict.pop("maxPriorityFeePerGas", None) try: - block_id = kwargs.pop("block_identifier", None) + block_id = kwargs.pop("block_identifier", kwargs.pop("block_id", None)) txn_params = cast(TxParams, txn_dict) return self.web3.eth.estimate_gas(txn_params, block_identifier=block_id) except (ValueError, Web3ContractLogicError) as err: @@ -892,20 +893,36 @@ def get_nonce(self, address: AddressType, **kwargs) -> int: address (AddressType): The address of the account. kwargs: * ``block_identifier`` (:class:`~ape.types.BlockID`): The block ID - for checking a previous account nonce. + for checking a previous account nonce. Also, you can use alias + ``block_id``. Returns: int """ - block_id = kwargs.pop("block_identifier", None) + block_id = kwargs.pop("block_identifier", kwargs.pop("block_id", None)) return self.web3.eth.get_transaction_count(address, block_identifier=block_id) def get_balance(self, address: AddressType) -> int: return self.web3.eth.get_balance(address) - def get_code(self, address: AddressType) -> ContractCode: - return self.web3.eth.get_code(address) + def get_code(self, address: AddressType, **kwargs) -> ContractCode: + """ + Get the bytes a contract. + + Args: + address (``AddressType``): The address of the contract. + kwargs: + * ``block_identifier`` (:class:`~ape.types.BlockID`): The block ID + for checking a previous account nonce. Also, you can use + alias ``block_id``. + + Returns: + :class:`~ape.types.ContractCode`: The contract bytecode. + """ + + block_id = kwargs.pop("block_identifier", kwargs.pop("block_id", None)) + return self.web3.eth.get_code(address, block_identifier=block_id) def get_storage_at(self, address: AddressType, slot: int, **kwargs) -> bytes: """ @@ -916,13 +933,14 @@ def get_storage_at(self, address: AddressType, slot: int, **kwargs) -> bytes: slot (int): Storage slot to read the value of. kwargs: * ``block_identifier`` (:class:`~ape.types.BlockID`): The block ID - for checking previous contract storage values. + for checking previous contract storage values. Also, you can use + alias ``block_id``. Returns: bytes: The value of the storage slot. """ - block_id = kwargs.pop("block_identifier", None) + block_id = kwargs.pop("block_identifier", kwargs.pop("block_id", None)) try: return self.web3.eth.get_storage_at( address, slot, block_identifier=block_id # type: ignore @@ -942,7 +960,8 @@ def send_call(self, txn: TransactionAPI, **kwargs) -> bytes: txn: :class:`~ape.api.transactions.TransactionAPI` kwargs: * ``block_identifier`` (:class:`~ape.types.BlockID`): The block ID - to use to send a call at a historical point of a contract. + to use to send a call at a historical point of a contract. Also, + you can us alias ``block_id``. checking a past estimation cost of a transaction. * ``state_overrides`` (Dict): Modify the state of the blockchain prior to sending the call, for testing purposes. @@ -1075,7 +1094,7 @@ def _prepare_call(self, txn: TransactionAPI, **kwargs) -> List: txn_dict.pop("maxFeePerGas", None) txn_dict.pop("maxPriorityFeePerGas", None) - block_identifier = kwargs.pop("block_identifier", "latest") + block_identifier = kwargs.pop("block_identifier", kwargs.pop("block_id", "latest")) if isinstance(block_identifier, int): block_identifier = to_hex(block_identifier) arguments = [txn_dict, block_identifier] diff --git a/tests/functional/test_provider.py b/tests/functional/test_provider.py index 1586630265..58d59df02c 100644 --- a/tests/functional/test_provider.py +++ b/tests/functional/test_provider.py @@ -237,3 +237,10 @@ def test_get_virtual_machine_error_panic(eth_tester_provider, mocker): def test_gas_price(eth_tester_provider): actual = eth_tester_provider.gas_price assert isinstance(actual, int) + + +def test_get_code(eth_tester_provider, vyper_contract_instance): + address = vyper_contract_instance.address + assert eth_tester_provider.get_code(address) == eth_tester_provider.get_code( + address, block_id=1 + ) From 875424e22a4f80c4ac110efaa7b3f3549d724c85 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Tue, 18 Jul 2023 15:00:22 -0500 Subject: [PATCH 2/2] feat: add to base --- src/ape/api/providers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ape/api/providers.py b/src/ape/api/providers.py index f9be01b390..1eb28ddb02 100644 --- a/src/ape/api/providers.py +++ b/src/ape/api/providers.py @@ -200,12 +200,13 @@ def get_balance(self, address: AddressType) -> int: """ @abstractmethod - def get_code(self, address: AddressType) -> ContractCode: + def get_code(self, address: AddressType, **kwargs) -> ContractCode: """ Get the bytes a contract. Args: address (``AddressType``): The address of the contract. + **kwargs: Additional, provider-specific kwargs. Returns: :class:`~ape.types.ContractCode`: The contract bytecode.