generated from ApeWorX/project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize trace return and revert values (#81)
- Loading branch information
Showing
3 changed files
with
77 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from functools import cached_property | ||
from typing import Any, Optional | ||
|
||
from ape_ethereum.trace import TraceApproach, TransactionTrace | ||
from hexbytes import HexBytes | ||
|
||
|
||
class AlchemyTransactionTrace(TransactionTrace): | ||
call_trace_approach: TraceApproach = TraceApproach.PARITY | ||
|
||
@cached_property | ||
def return_value(self) -> Any: | ||
node = self._top_level_call | ||
if output := node.get("output"): | ||
output_bytes = HexBytes(output) | ||
if abi := self.root_method_abi: | ||
return self._ecosystem.decode_returndata(abi, output_bytes) | ||
|
||
# ABI is not known. | ||
return output_bytes | ||
|
||
return None | ||
|
||
@cached_property | ||
def revert_message(self) -> Optional[str]: | ||
node = self._top_level_call | ||
return node.get("revertReason") | ||
|
||
@cached_property | ||
def _top_level_call(self) -> dict: | ||
return self.provider.make_request( | ||
"debug_traceTransaction", | ||
[ | ||
self.transaction_hash, | ||
{"tracer": "callTracer", "tracerConfig": {"onlyTopLevelCall": True}}, | ||
], | ||
) |
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,38 @@ | ||
import pytest | ||
from ape import chain, networks | ||
from ethpm_types import ContractType | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def ethereum_mainnet_alchemy(): | ||
with networks.ethereum.mainnet.use_provider("alchemy"): | ||
yield | ||
|
||
|
||
def test_revert_message(): | ||
txn_hash = "0x36144f609e0fc7afd3cc570d6a54582091642a44c5223a5ad59aa20008dd9577" | ||
receipt = chain.history[txn_hash] | ||
actual = receipt.trace.revert_message | ||
expected = "UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT" | ||
assert actual == expected | ||
|
||
|
||
def test_return_value(): | ||
txn_hash = "0xe0897d735b67893648b20085ecef16232733425329df844292d5b2774cca436b" | ||
receipt = chain.history[txn_hash] | ||
|
||
# Ensure the ABI is cached so we can decode the return value. | ||
abi = [ | ||
{ | ||
"type": "function", | ||
"name": "submit", | ||
"stateMutability": "payable", | ||
"inputs": [{"name": "_referral", "type": "address"}], | ||
"outputs": [{"name": "", "type": "uint256"}], | ||
} | ||
] | ||
chain.contracts[receipt.receiver] = ContractType(abi=abi) | ||
|
||
actual = receipt.return_value | ||
expected = 1244617160572980465 | ||
assert actual == expected |