Skip to content

Commit

Permalink
replace ganache with anvil for v2 & v3 buy integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Elnaril committed Feb 21, 2025
1 parent 3c66f78 commit 65f7582
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions integration_tests/main.py → integration_tests/v2_v3_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
import time

from eth_utils import keccak
from eth_account.account import LocalAccount
from web3 import (
Account,
Web3,
Expand All @@ -17,12 +17,13 @@

web3_provider = os.environ['WEB3_HTTP_PROVIDER_URL_ETHEREUM_MAINNET']
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545"))
chain_id = 1
block_number = 16853693

account = Account.from_key(keccak(text="moo"))
assert account.address == "0xcd7328a5D376D5530f054EAF0B9D235a4Fd36059"
init_amount = 100 * 10**18
account: LocalAccount = Account.from_key("0xf7e96bcf6b5223c240ec308d8374ff01a753b00743b3a0127791f37f00c56514")
assert account.address == "0x1e46c294f20bC7C27D93a9b5f45039751D8BCc3e"

chain_id = 1
initial_block_number = 21893982
initial_eth_amount = 10000 * 10**18

erc20_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
weth_abi = '[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","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":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint256"}],"name":"withdraw","outputs":[],"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":"","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":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"guy","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Withdrawal","type":"event"}]' # noqa
Expand All @@ -31,25 +32,24 @@
usdc_address = Web3.to_checksum_address("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")
usdc_contract = w3.eth.contract(address=usdc_address, abi=erc20_abi)

ur_address = Web3.to_checksum_address("0xEf1c6E67703c7BD7107eed8303Fbe6EC2554BF6B") # old address

codec = RouterCodec()


def launch_ganache():
ganache_process = subprocess.Popen(
f"""ganache
--chain.chainId='{chain_id}'
--chain.networkId='{chain_id}'
--logging.quiet='true'
--fork.url='{web3_provider}'
--fork.blockNumber='{block_number - 1}'
--wallet.accounts='{account.key.hex()}','{init_amount}'
""".replace("\n", " "),
# Tests
def launch_anvil():
anvil_process = subprocess.Popen(
" ".join(
[
"anvil -vvvvv",
f"--fork-url {web3_provider}",
f"--fork-block-number {initial_block_number}",
"--mnemonic-seed-unsafe 8721345628937456298",
]
),
shell=True,
)
time.sleep(3)
parent_id = ganache_process.pid
time.sleep(2)
parent_id = anvil_process.pid
return parent_id


Expand All @@ -60,25 +60,26 @@ def kill_processes(parent_id):
).stdout.strip("\n")
children_ids = pgrep_process.split("\n") if len(pgrep_process) > 0 else []
processes.extend(children_ids)
print(f"Killing processes: {' '.join(processes)}")
subprocess.run(f"kill {' '.join(processes)}", shell=True, text=True, capture_output=True)


def check_initialization():
assert w3.eth.chain_id == chain_id # 1
assert w3.eth.block_number == block_number
assert w3.eth.get_balance(account.address) == init_amount
assert w3.eth.chain_id == chain_id
assert w3.eth.block_number == initial_block_number
assert w3.eth.get_balance(account.address) == initial_eth_amount
assert usdc_contract.functions.balanceOf(account.address).call() == 0
print(" => Initialization: OK")


def buy_usdc_from_v2_and_v3():
# Buying for 1 eth of usdc from v2 and v3 pools
v2_path = [weth_address, usdc_address]
v2_in_amount = 3 * 10**17
v2_out_amount = 536724856 # with slippage
v2_in_amount = Wei(3 * 10**17)
v2_out_amount = Wei(817968342) # with slippage
v3_path = [weth_address, 500, usdc_address]
v3_in_amount = 7 * 10 ** 17
v3_out_amount = 1252357999 # with slippage
v3_in_amount = Wei(7 * 10 ** 17)
v3_out_amount = Wei(1908592798) # with slippage
total_in_amount = Wei(v2_in_amount + v3_in_amount)
trx_params = (
codec
Expand All @@ -90,18 +91,20 @@ def buy_usdc_from_v2_and_v3():
.build_transaction(
account.address,
total_in_amount,
ur_address=ur_address, # because test is on old UR address
block_identifier=block_number - 1 # because test is on local Ganache fork
block_identifier=w3.eth.block_number # because test is on local Anvil fork
)
)

raw_transaction = w3.eth.account.sign_transaction(trx_params, account.key).rawTransaction
raw_transaction = w3.eth.account.sign_transaction(trx_params, account.key).raw_transaction
trx_hash = w3.eth.send_raw_transaction(raw_transaction)

receipt = w3.eth.wait_for_transaction_receipt(trx_hash)
assert receipt["status"] == 1 # trx success
assert w3.eth.get_balance(account.address) < init_amount - total_in_amount
assert usdc_contract.functions.balanceOf(account.address).call() == 1807154400
assert w3.eth.get_balance(account.address) < initial_eth_amount - total_in_amount

usdc_balance = usdc_contract.functions.balanceOf(account.address).call()
print(f"{usdc_balance=}", usdc_balance / 10**6)
assert usdc_contract.functions.balanceOf(account.address).call() == 2782205245

print(" => BUY USDC: OK")

Expand All @@ -121,12 +124,12 @@ def print_success_message():


def main():
ganache_pid = launch_ganache()
anvil_pid = launch_anvil()
try:
launch_integration_tests()
print_success_message()
finally:
kill_processes(ganache_pid)
kill_processes(anvil_pid)


if __name__ == "__main__":
Expand Down

0 comments on commit 65f7582

Please sign in to comment.