-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use OTS namespace for get contract creation receipt in ape-geth…
… [APE-1453] (#1697)
- Loading branch information
Showing
5 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from functools import singledispatchmethod | ||
from typing import Iterator, Optional | ||
|
||
from ape.api import ReceiptAPI | ||
from ape.api.query import ContractCreationQuery, QueryAPI, QueryType | ||
from ape.exceptions import QueryEngineError | ||
from ape_geth.provider import BaseGethProvider | ||
|
||
|
||
class OTSQueryEngine(QueryAPI): | ||
@singledispatchmethod | ||
def estimate_query(self, query: QueryType) -> Optional[int]: # type: ignore[override] | ||
return None | ||
|
||
@singledispatchmethod | ||
def perform_query(self, query: QueryType) -> Iterator: # type: ignore[override] | ||
raise QueryEngineError( | ||
f"{self.__class__.__name__} cannot handle {query.__class__.__name__} queries." | ||
) | ||
|
||
@estimate_query.register | ||
def estimate_contract_creation_query(self, query: ContractCreationQuery) -> Optional[int]: | ||
if provider := self.network_manager.active_provider: | ||
if not isinstance(provider, BaseGethProvider): | ||
return None | ||
elif uri := provider.http_uri: | ||
return 225 if uri.startswith("http://") else 600 | ||
|
||
return None | ||
|
||
@perform_query.register | ||
def get_contract_creation_receipt(self, query: ContractCreationQuery) -> Iterator[ReceiptAPI]: | ||
if self.network_manager.active_provider and isinstance(self.provider, BaseGethProvider): | ||
if receipt := self.provider._get_contract_creation_receipt(query.contract): | ||
yield receipt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import List, Tuple | ||
|
||
from ape.exceptions import ChainError | ||
from tests.conftest import geth_process_test | ||
|
||
|
||
@geth_process_test | ||
def test_get_contract_creation_receipts(mock_geth, geth_contract, chain, networks, geth_provider): | ||
geth_provider.__dict__["explorer"] = None | ||
provider = networks.active_provider | ||
networks.active_provider = mock_geth | ||
mock_geth._web3.eth.get_block.side_effect = geth_provider.get_block | ||
|
||
try: | ||
mock_geth._web3.eth.get_code.return_value = b"123" | ||
|
||
# NOTE: Due to mocks, this next part may not actually find the contract. | ||
# but that is ok but we mostly want to make sure it tries OTS. There | ||
# are other tests for the brute-force logic. | ||
try: | ||
next(chain.contracts.get_creation_receipt(geth_contract.address), None) | ||
except ChainError: | ||
pass | ||
|
||
# Ensure we tried using OTS. | ||
actual = mock_geth._web3.provider.make_request.call_args | ||
expected: Tuple[str, List] = ("ots_getApiLevel", []) | ||
assert any(arguments == expected for arguments in actual) | ||
|
||
finally: | ||
networks.active_provider = provider |