Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tradingstrategy-ai/web3-ethereum-…
Browse files Browse the repository at this point in the history
…defi
  • Loading branch information
miohtama committed Mar 22, 2024
2 parents 3748c2a + 242ed92 commit b00d54a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-5b7e4cb3c882b28f3c32ba580de27ce7381f415a'
version: 'nightly-de33b6af53005037b463318d2628b5cfcaf39916'

# We also work around race condition for setting up Aave NPM packages.
- name: Setup Aave v3 for tests
Expand Down
29 changes: 10 additions & 19 deletions tests/aave_v3/test_aave_v3_loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 17 additions & 14 deletions tests/enzyme/test_price_feed.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tests/enzyme/test_vault_controlled_wallet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test vault's wallet like interface.
"""

import secrets
from decimal import Decimal

Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions tests/guard/test_guard_simple_vault_uniswap_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -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})

Expand Down

0 comments on commit b00d54a

Please sign in to comment.