22Test suite for `ethereum_test.helpers` module.
33"""
44
5- from ..common import to_address
5+ import pytest
6+
7+ from ..common import (
8+ compute_create2_address ,
9+ compute_create_address ,
10+ to_address ,
11+ )
612
713
814def test_to_address ():
@@ -18,3 +24,127 @@ def test_to_address():
1824 to_address (2 ** (20 * 8 ) - 1 )
1925 == "0xffffffffffffffffffffffffffffffffffffffff"
2026 )
27+
28+
29+ @pytest .mark .parametrize (
30+ "address,nonce,expected_contract_address" ,
31+ [
32+ pytest .param (
33+ "0x00caa64684700d2825da7cac6ba0c6ed9fd2a1bb" ,
34+ 0 ,
35+ "0x863df6bfa4469f3ead0be8f9f2aae51c91a907b4" ,
36+ id = "zero-nonce-0x-str-address" ,
37+ ),
38+ pytest .param (
39+ "00caa64684700d2825da7cac6ba0c6ed9fd2a1bb" ,
40+ 0 ,
41+ "0x863df6bfa4469f3ead0be8f9f2aae51c91a907b4" ,
42+ id = "zero-nonce-str-address" ,
43+ ),
44+ pytest .param (
45+ int ("0x00caa64684700d2825da7cac6ba0c6ed9fd2a1bb" , 16 ),
46+ 0 ,
47+ "0x863df6bfa4469f3ead0be8f9f2aae51c91a907b4" ,
48+ id = "zero-nonce-int-address" ,
49+ ),
50+ pytest .param (
51+ "0x9c33eacc2f50e39940d3afaf2c7b8246b681a374" ,
52+ 3 ,
53+ "0x7a250d5630b4cf539739df2c5dacb4c659f2488d" ,
54+ id = "non-zero-nonce-0x-str-address" ,
55+ ),
56+ pytest .param (
57+ "0xba52c75764d6f594735dc735be7f1830cdf58ddf" ,
58+ 3515 ,
59+ "0x06012c8cf97bead5deae237070f9587f8e7a266d" ,
60+ id = "large-nonce-0x-str-address" ,
61+ marks = pytest .mark .xfail (
62+ reason = "Nonce too large to convert with hard-coded to_bytes "
63+ "length of 1"
64+ ),
65+ ),
66+ ],
67+ )
68+ def test_compute_create_address (
69+ address : str | int , nonce : int , expected_contract_address : str
70+ ):
71+ """
72+ Test `ethereum_test.helpers.compute_create_address` with some famous
73+ contracts:
74+ - https://etherscan.io/address/0x863df6bfa4469f3ead0be8f9f2aae51c91a907b4
75+ - https://etherscan.io/address/0x7a250d5630b4cf539739df2c5dacb4c659f2488d
76+ - https://etherscan.io/address/0x06012c8cf97bead5deae237070f9587f8e7a266d
77+
78+ """
79+ assert compute_create_address (address , nonce ) == expected_contract_address
80+
81+
82+ @pytest .mark .parametrize (
83+ "address,salt,initcode,expected_contract_address" ,
84+ [
85+ pytest .param (
86+ "0x0000000000000000000000000000000000000000" ,
87+ "0x0000000000000000000000000000000000000000" ,
88+ "0x00" ,
89+ "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38" ,
90+ ),
91+ pytest .param (
92+ "0xdeadbeef00000000000000000000000000000000" ,
93+ "0x0000000000000000000000000000000000000000" ,
94+ "0x00" ,
95+ "0xB928f69Bb1D91Cd65274e3c79d8986362984fDA3" ,
96+ ),
97+ pytest .param (
98+ "0xdeadbeef00000000000000000000000000000000" ,
99+ "0xfeed000000000000000000000000000000000000" ,
100+ "0x00" ,
101+ "0xD04116cDd17beBE565EB2422F2497E06cC1C9833" ,
102+ ),
103+ pytest .param (
104+ "0x0000000000000000000000000000000000000000" ,
105+ "0x0000000000000000000000000000000000000000" ,
106+ "0xdeadbeef" ,
107+ "0x70f2b2914A2a4b783FaEFb75f459A580616Fcb5e" ,
108+ ),
109+ pytest .param (
110+ "0x00000000000000000000000000000000deadbeef" ,
111+ "0xcafebabe" ,
112+ "0xdeadbeef" ,
113+ "0x60f3f640a8508fC6a86d45DF051962668E1e8AC7" ,
114+ ),
115+ pytest .param (
116+ "0x00000000000000000000000000000000deadbeef" ,
117+ "0xcafebabe" ,
118+ (
119+ "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
120+ "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
121+ ),
122+ "0x1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C" ,
123+ ),
124+ pytest .param (
125+ "0x0000000000000000000000000000000000000000" ,
126+ "0x0000000000000000000000000000000000000000" ,
127+ "0x" ,
128+ "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0" ,
129+ ),
130+ ],
131+ )
132+ def test_compute_create2_address (
133+ address : str | int ,
134+ salt : str ,
135+ initcode : str ,
136+ expected_contract_address : str ,
137+ ):
138+ """
139+ Test `ethereum_test.helpers.compute_create2_address` using the CREATE2 geth
140+ test cases from:
141+ https://github.com/ethereum/go-ethereum/blob/2189773093b2fe6d161b6477589f964470ff5bce/core/vm/instructions_test.go
142+
143+ Note: `compute_create2_address` does not generate checksum addresses; s
144+ """
145+ salt_as_int = int (salt , 16 )
146+ initcode_as_bytes = bytes .fromhex (initcode [2 :])
147+ assert (
148+ compute_create2_address (address , salt_as_int , initcode_as_bytes )
149+ == expected_contract_address .lower ()
150+ )
0 commit comments