22import subprocess
33import time
44
5- from eth_utils import keccak
5+ from eth_account . account import LocalAccount
66from web3 import (
77 Account ,
88 Web3 ,
1717
1818web3_provider = os .environ ['QUICKNODE_BSC_MAINNET' ]
1919w3 = Web3 (Web3 .HTTPProvider ("http://127.0.0.1:8545" ))
20- chain_id = 1337
21- block_number = 31096415
20+ chain_id = 56
21+ block_number = 63929588
2222gas_limit = 800_000
2323
24- account = Account .from_key (keccak ( text = "moo" ) )
25- assert account .address == "0xcd7328a5D376D5530f054EAF0B9D235a4Fd36059 "
26- init_amount = 100 * 10 ** 18
24+ account : LocalAccount = Account .from_key ("0xf7e96bcf6b5223c240ec308d8374ff01a753b00743b3a0127791f37f00c56514" )
25+ assert account .address == "0x1e46c294f20bC7C27D93a9b5f45039751D8BCc3e "
26+ init_amount = 10000 * 10 ** 18
2727transient_eth_balance = init_amount
2828
2929erc20_abi = '[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}]' # noqa
3434usdt_address = Web3 .to_checksum_address ("0x55d398326f99059fF775485246999027B3197955" )
3535usdt_contract = w3 .eth .contract (address = usdt_address , abi = erc20_abi )
3636
37- ur_address = Web3 .to_checksum_address ("0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD " )
37+ ur_address = Web3 .to_checksum_address ("0x1906c1d672b88cd1b9ac7593301ca990f94eae07 " )
3838permit2_address = Web3 .to_checksum_address ("0x000000000022D473030F116dDEE9F6B43aC78BA3" )
3939
4040codec = RouterCodec ()
4141
4242
43- def launch_ganache ():
44- ganache_process = subprocess .Popen (
45- f"""ganache
46- --logging.quiet='true'
47- --fork.url='{ web3_provider } '
48- --fork.blockNumber='{ block_number } '
49- --miner.defaultGasPrice='15000000000'
50- --wallet.accounts='{ account .key .hex ()} ','{ init_amount } '
51- """ .replace ("\n " , " " ),
43+ def launch_anvil ():
44+ anvil_process = subprocess .Popen (
45+ " " .join (
46+ [
47+ "anvil -vvvvv" ,
48+ f"--fork-url { web3_provider } " ,
49+ f"--fork-block-number { block_number } " ,
50+ "--mnemonic-seed-unsafe 8721345628937456298" ,
51+ ]
52+ ),
5253 shell = True ,
5354 )
5455 time .sleep (3 )
55- parent_id = ganache_process .pid
56+ parent_id = anvil_process .pid
5657 return parent_id
5758
5859
@@ -63,12 +64,13 @@ def kill_processes(parent_id):
6364 ).stdout .strip ("\n " )
6465 children_ids = pgrep_process .split ("\n " ) if len (pgrep_process ) > 0 else []
6566 processes .extend (children_ids )
67+ print (f"Killing processes: { ' ' .join (processes )} " )
6668 subprocess .run (f"kill { ' ' .join (processes )} " , shell = True , text = True , capture_output = True )
6769
6870
6971def check_initialization ():
70- assert w3 .eth .chain_id == chain_id # 1337
71- assert w3 .eth .block_number == block_number + 1
72+ assert w3 .eth .chain_id == chain_id # 56
73+ assert w3 .eth .block_number == block_number
7274 assert w3 .eth .get_balance (account .address ) == init_amount
7375 assert usdt_contract .functions .balanceOf (account .address ).call () == 0
7476 print (" => Initialization: OK" )
@@ -87,7 +89,7 @@ def send_transaction(value, encoded_data):
8789 "nonce" : w3 .eth .get_transaction_count (account .address ),
8890 "data" : encoded_data ,
8991 }
90- raw_transaction = w3 .eth .account .sign_transaction (trx_params , account .key ).rawTransaction
92+ raw_transaction = w3 .eth .account .sign_transaction (trx_params , account .key ).raw_transaction
9193 trx_hash = w3 .eth .send_raw_transaction (raw_transaction )
9294 return trx_hash
9395
@@ -112,7 +114,7 @@ def buy_usdt():
112114 assert receipt ["status" ] == 1 , f'receipt["status"] is actually { receipt ["status" ]} ' # trx success
113115
114116 usdt_balance = usdt_contract .functions .balanceOf (account .address ).call ()
115- assert usdt_balance == 213931846540111575874 , f"USDT balance was actually: { usdt_balance } "
117+ assert usdt_balance == 1309572675460077202940 , f"USDT balance was actually: { usdt_balance } "
116118
117119 print (" => BUY USDT: OK" )
118120
@@ -131,7 +133,7 @@ def approve_permit2_for_usdt():
131133 "nonce" : w3 .eth .get_transaction_count (account .address ),
132134 }
133135 )
134- raw_transaction = w3 .eth .account .sign_transaction (trx_params , account .key ).rawTransaction
136+ raw_transaction = w3 .eth .account .sign_transaction (trx_params , account .key ).raw_transaction
135137 trx_hash = w3 .eth .send_raw_transaction (raw_transaction )
136138
137139 receipt = w3 .eth .wait_for_transaction_receipt (trx_hash )
@@ -174,10 +176,10 @@ def sell_usdt_part_1():
174176 assert receipt ["status" ] == 1 , f'receipt["status"] is actually { receipt ["status" ]} ' # trx success
175177
176178 usdt_balance = usdt_contract .functions .balanceOf (account .address ).call ()
177- assert usdt_balance == 113931846540111575874 , f"USDT balance was actually: { usdt_balance } "
179+ assert usdt_balance == 1209572675460077202940 , f"USDT balance was actually: { usdt_balance } "
178180
179181 wbnb_balance = wbnb_contract .functions .balanceOf (account .address ).call ()
180- assert wbnb_balance == 466978799145556691 , f"WBNB balance was actually: { wbnb_balance } "
182+ assert wbnb_balance == 76395672864257517 , f"WBNB balance was actually: { wbnb_balance } "
181183
182184 print (" => SELL USDT for WBNB PART 1: OK" )
183185
@@ -187,7 +189,7 @@ def sell_usdt_part_2():
187189 amount_out_min = 0
188190 v3_path = [usdt_address , 500 , wbnb_address ]
189191
190- amount , expiration , nonce = codec .fetch_permit2_allowance (account .address , usdt_address )
192+ amount , expiration , nonce = codec .fetch_permit2_allowance (account .address , usdt_address , ur_address )
191193 assert amount == 0 , "Wrong Permit2 allowance amount" # allowance fully used in sell_usdc_part_1()
192194 assert expiration > 0 , "Wrong Permit2 allowance expiration"
193195 assert nonce == 1 , "Wrong Permit2 allowance nonce"
@@ -219,12 +221,12 @@ def sell_usdt_part_2():
219221 assert receipt ["status" ] == 1 , f'receipt["status"] is actually { receipt ["status" ]} ' # trx success
220222
221223 usdt_balance = usdt_contract .functions .balanceOf (account .address ).call ()
222- assert usdt_balance == 13931846540111575874 , f"USDT balance was actually: { usdt_balance } "
224+ assert usdt_balance == 1109572675460077202940 , f"USDT balance was actually: { usdt_balance } "
223225
224226 wbnb_balance = wbnb_contract .functions .balanceOf (account .address ).call ()
225- assert wbnb_balance == 933944384948957177 , f"WBNB balance was actually: { wbnb_balance } "
227+ assert wbnb_balance == 152772939867854785 , f"WBNB balance was actually: { wbnb_balance } "
226228
227- amount , expiration , nonce = codec .fetch_permit2_allowance (account .address , usdt_address )
229+ amount , expiration , nonce = codec .fetch_permit2_allowance (account .address , usdt_address , ur_address )
228230 assert amount == 2 ** 160 - 1 , "Wrong Permit2 allowance amount" # infinite allowance
229231 assert expiration > 0 , "Wrong Permit2 allowance expiration"
230232 assert nonce == 2 , "Wrong Permit2 allowance nonce"
@@ -251,12 +253,12 @@ def print_success_message():
251253
252254
253255def main ():
254- ganache_pid = launch_ganache ()
256+ anvil_pid = launch_anvil ()
255257 try :
256258 launch_integration_tests ()
257259 print_success_message ()
258260 finally :
259- kill_processes (ganache_pid )
261+ kill_processes (anvil_pid )
260262
261263
262264if __name__ == "__main__" :
0 commit comments