From e7ff3944d029cabe45f7763220c1c34645d7892c Mon Sep 17 00:00:00 2001 From: Hieu Nguyen Date: Fri, 22 Mar 2024 11:52:29 +0200 Subject: [PATCH] Make sure tests work with newer Foundry Change Foundry version --- .github/workflows/test.yml | 2 +- tests/aave_v3/test_aave_v3_loan.py | 29 ++++++----------- tests/enzyme/test_price_feed.py | 31 ++++++++++--------- tests/enzyme/test_vault_controlled_wallet.py | 3 +- .../test_guard_simple_vault_uniswap_v2.py | 6 ++-- 5 files changed, 33 insertions(+), 38 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 714083df..320a440e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -59,7 +59,7 @@ jobs: uses: foundry-rs/foundry-toolchain@v1 with: # pick a nightly release from: https://github.com/foundry-rs/foundry/releases - version: 'nightly-0026488754512acf0fd902a0d2c90cf8a09367b0' + version: 'nightly-de33b6af53005037b463318d2628b5cfcaf39916' # We also work around race condition for setting up Aave NPM packages. - name: Setup Aave v3 for tests diff --git a/tests/aave_v3/test_aave_v3_loan.py b/tests/aave_v3/test_aave_v3_loan.py index 279bc8f7..80f1fc2d 100644 --- a/tests/aave_v3/test_aave_v3_loan.py +++ b/tests/aave_v3/test_aave_v3_loan.py @@ -163,7 +163,7 @@ def test_aave_v3_supply( # over withdraw should fail # error code 32 = 'User cannot withdraw more than the available balance' # https://github.com/aave/aave-v3-core/blob/e0bfed13240adeb7f05cb6cbe5e7ce78657f0621/contracts/protocol/libraries/helpers/Errors.sol#L41 - (1.0001, TransactionAssertionError("execution reverted: 32")), + (1.0001, TransactionAssertionError("32")), ], ) def test_aave_v3_withdraw( @@ -208,13 +208,10 @@ def test_aave_v3_withdraw( tx_hash = web3.eth.send_raw_transaction(signed.rawTransaction) if isinstance(expected_exception, Exception): - with pytest.raises(type(expected_exception)) as e: + with pytest.raises(type(expected_exception), match=str(expected_exception)) as e: assert_transaction_success_with_explanation(web3, tx_hash) - assert str(expected_exception) == e.value.revert_reason - - # revert reason should be in message as well - assert str(expected_exception) in str(e.value) + assert str(expected_exception) in e.value.revert_reason else: # withdraw successfully assert_transaction_success_with_explanation(web3, tx_hash) @@ -274,12 +271,12 @@ def test_aave_v3_oracle( # try to borrow 8001 USDC should fail as collateral is 10k USDC and reserve LTV is 80% # error code 36 = 'There is not enough collateral to cover a new borrow' # https://github.com/aave/aave-v3-core/blob/e0bfed13240adeb7f05cb6cbe5e7ce78657f0621/contracts/protocol/libraries/helpers/Errors.sol#L45 - ("usdc", 8_001 * 10**6, TransactionAssertionError("execution reverted: 36"), None), + ("usdc", 8_001 * 10**6, TransactionAssertionError("36"), None), # TODO: more test case for borrowing ETH # 1 WETH = 4000 USDC ("weth", 1 * 10**18, None, 2125000000000000000), # 2.1 WETH (8400 USDC) should fail - ("weth", int(2.1 * 10**18), TransactionAssertionError("execution reverted: 36"), None), + ("weth", int(2.1 * 10**18), TransactionAssertionError("36"), None), ], ) def test_aave_v3_borrow( @@ -328,13 +325,10 @@ def test_aave_v3_borrow( tx_hash = web3.eth.send_raw_transaction(signed.rawTransaction) if isinstance(expected_exception, Exception): - with pytest.raises(type(expected_exception)) as e: + with pytest.raises(type(expected_exception), match=str(expected_exception)) as e: assert_transaction_success_with_explanation(web3, tx_hash) - assert str(expected_exception) == e.value.revert_reason - - # revert reason should be in message as well - assert str(expected_exception) in str(e.value) + assert str(expected_exception) in e.value.revert_reason else: # borrow successfully assert_transaction_success_with_explanation(web3, tx_hash) @@ -364,7 +358,7 @@ def test_aave_v3_borrow( ("usdc", 8_000 * 10**6, MAX_AMOUNT, 1_000 * 10**6, None, 0), # repay everything: capital + interest # currently set to fail since hot wallet doesn't have enough to repay interest - ("usdc", 8_000 * 10**6, MAX_AMOUNT, 0, TransactionAssertionError("execution reverted: ERC20: transfer amount exceeds balance"), None), + ("usdc", 8_000 * 10**6, MAX_AMOUNT, 0, TransactionAssertionError("ERC20: transfer amount exceeds balance"), None), ], ) def test_aave_v3_repay( @@ -447,13 +441,10 @@ def test_aave_v3_repay( tx_hash = web3.eth.send_raw_transaction(signed.rawTransaction) if isinstance(expected_exception, Exception): - with pytest.raises(type(expected_exception)) as e: + with pytest.raises(type(expected_exception), match=str(expected_exception)) as e: assert_transaction_success_with_explanation(web3, tx_hash) - assert str(expected_exception) == e.value.revert_reason - - # revert reason should be in message as well - assert str(expected_exception) in str(e.value) + assert str(expected_exception) in e.value.revert_reason else: # repay successfully assert_transaction_success_with_explanation(web3, tx_hash) diff --git a/tests/enzyme/test_price_feed.py b/tests/enzyme/test_price_feed.py index 49c01922..0ef93ae0 100644 --- a/tests/enzyme/test_price_feed.py +++ b/tests/enzyme/test_price_feed.py @@ -1,27 +1,36 @@ """Fetch enzyme price feeds. """ -from functools import partial -from typing import cast, List + from decimal import Decimal +from functools import partial +from typing import List, cast import pytest from eth.constants import ZERO_ADDRESS from eth_typing import HexAddress -from web3 import Web3, HTTPProvider +from web3 import HTTPProvider, Web3 from web3.contract import Contract from web3.exceptions import ContractLogicError from eth_defi.deploy import deploy_contract from eth_defi.enzyme.deployment import EnzymeDeployment, RateAsset -from eth_defi.enzyme.events import fetch_vault_balance_events, Deposit, Redemption -from eth_defi.enzyme.price_feed import fetch_price_feeds, EnzymePriceFeed, fetch_updated_price_feed +from eth_defi.enzyme.events import Deposit, Redemption, fetch_vault_balance_events +from eth_defi.enzyme.price_feed import ( + EnzymePriceFeed, + fetch_price_feeds, + fetch_updated_price_feed, +) from eth_defi.enzyme.uniswap_v2 import prepare_swap from eth_defi.enzyme.vault import Vault from eth_defi.event_reader.multithread import MultithreadEventReader -from eth_defi.event_reader.reader import extract_events, Web3EventReader +from eth_defi.event_reader.reader import Web3EventReader, extract_events from eth_defi.token import fetch_erc20_details -from eth_defi.trace import assert_transaction_success_with_explanation, TransactionAssertionError, assert_call_success_with_explanation +from eth_defi.trace import ( + TransactionAssertionError, + assert_call_success_with_explanation, + assert_transaction_success_with_explanation, +) from eth_defi.uniswap_v2.deployment import UniswapV2Deployment @@ -99,19 +108,13 @@ def test_unsupported_base_asset(web3: Web3, deployment: EnzymeDeployment, weth: # and print a Solidity stack trace of errors if any value_interpreter = deployment.contracts.value_interpreter raw_amount = 10**18 - with pytest.raises((ContractLogicError, ValueError)) as e: + with pytest.raises((ContractLogicError, ValueError), match="__calcAssetValue: Unsupported _baseAsset") as e: result = value_interpreter.functions.calcCanonicalAssetValue( ZERO_ADDRESS, raw_amount, usdc.address, ).call() - if isinstance(e, ContractLogicError): - assert e.value.args[0] == "execution reverted: __calcAssetValue: Unsupported _baseAsset" - else: - # ETHEREUM JSON RPC LOVELY - assert e.value.args[0]["message"] == "execution reverted: __calcAssetValue: Unsupported _baseAsset" - def test_manipulate_price( web3: Web3, diff --git a/tests/enzyme/test_vault_controlled_wallet.py b/tests/enzyme/test_vault_controlled_wallet.py index 6742852a..bb574933 100644 --- a/tests/enzyme/test_vault_controlled_wallet.py +++ b/tests/enzyme/test_vault_controlled_wallet.py @@ -1,6 +1,7 @@ """Test vault's wallet like interface. """ + import secrets from decimal import Decimal @@ -270,4 +271,4 @@ def test_vault_controlled_wallet_make_unauthorised( signed, bound_func = vault_wallet.sign_transaction_with_new_nonce(approve_tx) tx_hash = web3.eth.send_raw_transaction(signed.raw_transaction) reason = fetch_transaction_revert_reason(web3, tx_hash) - assert reason == "execution reverted: receiveCallFromComptroller: Unauthorized" + assert "receiveCallFromComptroller: Unauthorized" in reason diff --git a/tests/guard/test_guard_simple_vault_uniswap_v2.py b/tests/guard/test_guard_simple_vault_uniswap_v2.py index 5b8d9cde..65f65b6b 100644 --- a/tests/guard/test_guard_simple_vault_uniswap_v2.py +++ b/tests/guard/test_guard_simple_vault_uniswap_v2.py @@ -262,7 +262,7 @@ def test_guard_token_in_not_approved( FOREVER_DEADLINE, ) - with pytest.raises(TransactionFailed, match="execution reverted: TransferHelper: TRANSFER_FROM_FAILED"): + with pytest.raises(TransactionFailed, match="TransferHelper: TRANSFER_FROM_FAILED"): target, call_data = encode_simple_vault_transaction(trade_call) vault.functions.performCall(target, call_data).transact({"from": asset_manager}) @@ -292,7 +292,7 @@ def test_guard_token_in_not_approved( FOREVER_DEADLINE, ) - with pytest.raises(TransactionFailed, match="execution reverted: TransferHelper: TRANSFER_FROM_FAILED"): + with pytest.raises(TransactionFailed, match="TransferHelper: TRANSFER_FROM_FAILED"): target, call_data = encode_simple_vault_transaction(trade_call) vault.functions.performCall(target, call_data).transact({"from": asset_manager}) @@ -348,7 +348,7 @@ def test_guard_pair_not_approved( FOREVER_DEADLINE, ) - with pytest.raises(TransactionFailed, match="execution reverted: Token not allowed"): + with pytest.raises(TransactionFailed, match="Token not allowed"): target, call_data = encode_simple_vault_transaction(trade_call) vault.functions.performCall(target, call_data).transact({"from": asset_manager})