Skip to content

Commit d09ac5d

Browse files
authored
fix: issues with web3 v7 / hex strings missing 0x prefix (#120)
1 parent c819138 commit d09ac5d

File tree

6 files changed

+52
-46
lines changed

6 files changed

+52
-46
lines changed

ape-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ foundry:
2424
ethereum:
2525
mainnet:
2626
upstream_provider: alchemy
27-
block_number: 15776634
27+
block_number: 21418244
2828
sepolia:
2929
upstream_provider: alchemy
3030
block_number: 3091950

ape_foundry/provider.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def _extract_custom_error(self, **kwargs) -> str:
639639
elif "txn" in kwargs:
640640
txn = kwargs["txn"]
641641
try:
642-
txn_hash = txn.txn_hash if isinstance(txn.txn_hash, str) else txn.txn_hash.hex()
642+
txn_hash = txn.txn_hash if isinstance(txn.txn_hash, str) else to_hex(txn.txn_hash)
643643
trace = self.get_transaction_trace(txn_hash)
644644
except Exception:
645645
pass
@@ -654,7 +654,7 @@ def set_block_gas_limit(self, gas_limit: int) -> bool:
654654

655655
def set_code(self, address: "AddressType", code: "ContractCode") -> bool:
656656
if isinstance(code, bytes):
657-
code = code.hex()
657+
code = to_hex(code)
658658

659659
elif isinstance(code, str) and not is_0x_prefixed(code):
660660
code = add_0x_prefix(HexStr(code))
@@ -670,8 +670,8 @@ def set_storage(self, address: "AddressType", slot: int, value: HexBytes):
670670
"anvil_setStorageAt",
671671
[
672672
address,
673-
HashBytes32.__eth_pydantic_validate__(slot).hex(),
674-
HashBytes32.__eth_pydantic_validate__(value).hex(),
673+
to_hex(HashBytes32.__eth_pydantic_validate__(slot)),
674+
to_hex(HashBytes32.__eth_pydantic_validate__(value)),
675675
],
676676
)
677677

tests/expected_traces.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
LOCAL_TRACE = r"""
22
Call trace for '0x([A-Fa-f0-9]{64})'
33
tx\.origin=0x[a-fA-F0-9]{40}
4-
ContractA\.methodWithoutArguments\(\) -> 0x00\.\.93bc \[\d+ gas\]
4+
ContractA\.methodWithoutArguments\(\) -> 0x0000\.\.93bc \[\d+ gas\]
55
├── SYMBOL\.supercluster\(x=234444\) -> \[
66
│ \[23523523235235, 11111111111, 234444\],
77
│ \[
@@ -55,50 +55,55 @@
5555
└── ContractC\.methodC1\(windows95="simpler", jamaica=111, cardinal=Contract[A|C]\) \[\d+ gas\]
5656
"""
5757
MAINNET_FAIL_TRACE_FIRST_10_LINES = r"""
58-
Call trace for '0x053cba5c12172654d894f66d5670bab6215517a94189a9ffc09bc40a589ec04d'
59-
reverted with message: "UNIV3R: min return"
60-
tx\.origin=0xd2f91C13e2D7ABbA4408Cd3D86285b7835524ad7
61-
AggregationRouterV4\.uniswapV3Swap\(
62-
amount=12851675475480000000000,
63-
minReturn=4205588148,
64-
pools=\[
65-
682631518358379038160760928734868612545194078373,
66-
5789604461865809771178549250512551984713807685540901737341300416798777562476
58+
Call trace for '0x605ebd5a54b7d99d9bb61a228a57bfdf8614148c063a5f44e5d52b5a81c2679c'
59+
reverted with message: "BAL#508"
60+
tx\.origin=0xF36BCB79C3AD71Bd4E9343f78c402b0f6C99bF34
61+
AggregationRouterV5\.swap\(
62+
executor=AggregationRouterV5,
63+
desc=\[
64+
AggregationRouterV5,
65+
AggregationRouterV5,
66+
AggregationRouterV5,
67+
tx\.origin,
68+
6769
"""
6870
MAINNET_FAIL_TRACE_LAST_10_LINES = r"""
69-
├── AggregationRouterV4\.uniswapV3SwapCallback\(
70-
│ amount0Delta=-4192051335,
71-
│ amount1Delta=2098831888913057968,
72-
│ 0x00\.\.097d
73-
│ \) \[9861 gas\]
74-
│ ├── UniswapV3Pool.token0\(\) -> FiatTokenProxy \[266 gas\]
75-
│ ├── UniswapV3Pool\.token1\(\) -> WETH \[308 gas\]
76-
│ ├── UniswapV3Pool\.fee\(\) -> 500 \[251 gas\]
77-
│ └── WETH\.transfer\(dst=UniswapV3Pool, wad=2098831888913057968\) -> True \[6062 gas\]
78-
└── WETH\.balanceOf\(UniswapV3Pool\) -> 68359883632315875514968 \[534 gas\]
71+
48534500000000000000,
72+
0x0000\.\.0000
73+
],
74+
funds=\['Vault', 0, 'Vault', 0\],
75+
limit=0,
76+
deadline=1734390153
77+
\) ->
78+
0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000
79+
000000000000000000000000000000000000000000000742414c2335303800000000000000000000000000000000
80+
000000000000000000 48534500000000000000 \[7542 gas\]
81+
7982
"""
8083
MAINNET_TRACE_FIRST_10_LINES = r"""
8184
Call trace for '0xb7d7f1d5ce7743e821d3026647df486f517946ef1342a1ae93c96e4a8016eab7'
8285
tx\.origin=0x5668EAd1eDB8E2a4d724C8fb9cB5fFEabEB422dc
83-
DSProxy\.execute\(_target=LoanShifterTaker, _data=0x35\.\.0000\) -> "" \[\d+ gas\]
84-
└── \(delegate\) LoanShifterTaker\.moveLoan\(
85-
_exchangeData=\[
86-
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE,
87-
ZERO_ADDRESS,
88-
0,
89-
0,
86+
87+
Events emitted:
88+
log LogPermit\(src=0x0000\.\.09ad, dst=0x0000..52dd, sig=0x1cff\.\.0000\)
89+
log Transfer\(
90+
from=CErc20Delegator,
91+
to=CErc20Delegator,
92+
amount=48354786024690521017562
93+
\)\[x2\]
9094
"""
9195
MAINNET_TRACE_LAST_10_LINES = r"""
92-
│ └── LendingRateOracle\.getMarketBorrowRate\(_asset=DAI\) ->
9396
│ 35000000000000000000000000 \[1164 gas\]
94-
├── DSProxy\.authority\(\) -> DSGuard \[1291 gas\]
95-
├── DSGuard\.forbid\(src=LoanShifterReceiver, dst=DSProxy, sig=0x1c\.\.0000\) \[5253 gas\]
97+
├── CErc20Delegator\.0xbf7e214f\(\) ->
98+
│ 0x000000000000000000000000b67f15159e1c60d7e5f5b60316c4588b014c61fa \[1291 gas\]
99+
├── DSGuard\.forbid\(src=DSGuard, dst=DSGuard, sig=0x1cff\.\.0000\) \[5253 gas\]
96100
└── DefisaverLogger\.Log\(
97-
_contract=DSProxy,
101+
_contract=DefisaverLogger,
98102
_caller=tx\.origin,
99103
_logName="LoanShifter",
100-
_data=0x00\.\.0000
104+
_data=0x0000\.\.0000
101105
\) \[6057 gas\]
106+
102107
"""
103108
LOCAL_GAS_REPORT = r"""
104109
+ContractA Gas

tests/test_fork_provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def test_reset_fork_specify_block_number_via_config(mainnet_fork_provider):
113113
mainnet_fork_provider.reset_fork()
114114
block_num_after_reset = mainnet_fork_provider.get_block("latest").number
115115
block_base_fee_after_reset = mainnet_fork_provider.get_block("latest").base_fee
116-
assert block_num_after_reset == 15776634 # Specified in ape-config.yaml
117-
assert block_base_fee_after_reset == 28831496753
116+
assert block_num_after_reset == 21418244 # Specified in ape-config.yaml
117+
assert block_base_fee_after_reset == 12273900931
118118

119119

120120
@pytest.mark.fork

tests/test_provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ape_ethereum.trace import Trace
1010
from ape_ethereum.transactions import TransactionStatusEnum, TransactionType
1111
from eth_pydantic_types import HashBytes32
12-
from eth_utils import to_int
12+
from eth_utils import to_hex, to_int
1313
from evm_trace import CallType
1414
from hexbytes import HexBytes
1515

@@ -425,7 +425,7 @@ def test_send_transaction_when_no_error_and_receipt_fails(
425425
receipt_data = {
426426
"failed": True,
427427
"blockNumber": 0,
428-
"txnHash": tx_hash.hex(),
428+
"txnHash": to_hex(tx_hash),
429429
"status": TransactionStatusEnum.FAILING.value,
430430
"sender": owner.address,
431431
"receiver": vyper_contract_instance.address,

tests/test_trace.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import re
22
import shutil
33
from pathlib import Path
4-
from typing import List
54

65
import pytest
76
from ape.exceptions import ContractLogicError
87
from ape.utils import create_tempdir
8+
from eth_utils import to_hex
99
from hexbytes import HexBytes
1010

1111
from .expected_traces import (
@@ -17,7 +17,7 @@
1717
MAINNET_TRACE_LAST_10_LINES,
1818
)
1919

20-
MAINNET_FAIL_TXN_HASH = "0x053cba5c12172654d894f66d5670bab6215517a94189a9ffc09bc40a589ec04d"
20+
MAINNET_FAIL_TXN_HASH = "0x605ebd5a54b7d99d9bb61a228a57bfdf8614148c063a5f44e5d52b5a81c2679c"
2121
MAINNET_TXN_HASH = "0xb7d7f1d5ce7743e821d3026647df486f517946ef1342a1ae93c96e4a8016eab7"
2222
EXPECTED_MAP = {
2323
MAINNET_TXN_HASH: (MAINNET_TRACE_FIRST_10_LINES, MAINNET_TRACE_LAST_10_LINES),
@@ -45,7 +45,8 @@ def read_trace(self, expected_start: str, file=None):
4545
@pytest.fixture(autouse=True, scope="module")
4646
def full_contracts_cache(config):
4747
destination = config.DATA_FOLDER / "ethereum"
48-
shutil.copytree(BASE_CONTRACTS_PATH, destination)
48+
assert BASE_CONTRACTS_PATH.is_dir()
49+
shutil.copytree(BASE_CONTRACTS_PATH, destination, dirs_exist_ok=True)
4950

5051

5152
@pytest.fixture(
@@ -116,7 +117,7 @@ def test_mainnet_transaction_traces(mainnet_receipt, captrace):
116117
assert_rich_output(actual_ending, expected_ending)
117118

118119

119-
def assert_rich_output(rich_capture: List[str], expected: str):
120+
def assert_rich_output(rich_capture: list[str], expected: str):
120121
expected_lines = [x.rstrip() for x in expected.splitlines() if x.rstrip()]
121122
actual_lines = [x.rstrip() for x in rich_capture if x.rstrip()]
122123
assert actual_lines, "No output."
@@ -178,4 +179,4 @@ def trace(txn_hash: str, *args, **kwargs):
178179
assert actual == ""
179180

180181
# Show failure was tracked
181-
assert tracker[0] == HexBytes(tx.txn_hash).hex()
182+
assert tracker[0] == to_hex(HexBytes(tx.txn_hash))

0 commit comments

Comments
 (0)