delvtech / agent0

Analysis & simulation repo for Delv
https://agent0.readthedocs.io/en/latest/
Apache License 2.0
49 stars 21 forks source link

bug: crash in calc_targeted_long after max short #1670

Open wakamex opened 2 weeks ago

wakamex commented 2 weeks ago

this script was working, so this is a recent change. I tried previous versions, but they result in a different error.

reproduced in this script on a branch: https://github.com/delvtech/agent0/blob/af73b4dcb2380dd39e3de2a864258fbef8fa43ce/fail_calc_targeted_long.py

steps are:

uv pip install -e .
python fail_calc_targeted_long.py

output:

Warning: failed to convert bytecode for stethtarget3deployer to HexBytes: Non-hexadecimal digit found
Warning: failed to convert bytecode for erc4626target0deployer to HexBytes: Non-hexadecimal digit found
Warning: failed to convert bytecode for erc4626target3deployer to HexBytes: Non-hexadecimal digit found
Warning: failed to convert bytecode for stethtarget0deployer to HexBytes: Non-hexadecimal digit found
Warning: failed to convert bytecode for erc4626target4deployer to HexBytes: Non-hexadecimal digit found
Warning: failed to convert bytecode for erc4626target1deployer to HexBytes: Non-hexadecimal digit found
Warning: failed to convert bytecode for stethtarget2deployer to HexBytes: Non-hexadecimal digit found
Warning: failed to convert bytecode for stethtarget1deployer to HexBytes: Non-hexadecimal digit found
Warning: failed to convert bytecode for erc4626target2deployer to HexBytes: Non-hexadecimal digit found
Warning: failed to convert bytecode for stethtarget4deployer to HexBytes: Non-hexadecimal digit found
=== TIME_STRETCH_APR=0.1 DELTA=1e+18 ===

24-08-29 15:40:14: INFO: interface.py:88::interface::initialize_engine:
Database agent0_db does not exist, creating

24-08-29 15:40:14: WARNING: interface.py:93::interface::initialize_engine:
No postgres connection, retrying

24-08-29 15:40:15: INFO: interface.py:88::interface::initialize_engine:
Database agent0_db does not exist, creating
max long  rate = 0.00000%
max short rate = 401.67863%
=== POOL INFO ===
 bond_reserves = 113485.98289020474650654
 long_average_maturity_time = 0.0
 long_exposure = 0.0
 longs_outstanding = 0.0
 lp_share_price = 1.000000190258751902
 lp_total_supply = 99999.999
 share_adjustment = 72640.8983086062206679
 share_reserves = 72640.899816826822935958
 short_average_maturity_time = 1756494000.0
 shorts_outstanding = 33580.99475073790377984
 vault_share_price = 1.000000190258751902
 withdrawal_shares_proceeds = 0.0
 withdrawal_shares_ready_to_withdraw = 0.0
 zombie_base_proceeds = 0.0
 zombie_share_reserves = 0.0
trying to calculate targeted long, target=401.00000%, current rate=401.67863%
Traceback (most recent call last):
  File "/code/experiments/fail_calc_targeted_long.py", line 92, in <module>
    calc_shorts(chain=chain, interface=interface)
  File "/code/experiments/fail_calc_targeted_long.py", line 62, in calc_shorts
    trade_size = interface.calc_targeted_long(budget=FixedPoint(1e18), target_rate=FixedPoint(short_target))
  File "/code/agent0/src/agent0/ethpy/hyperdrive/interface/read_interface.py", line 1354, in calc_targeted_long
    return _calc_targeted_long(pool_state, budget, target_rate, max_iterations, allowable_error)
  File "/code/agent0/src/agent0/ethpy/hyperdrive/interface/_mock_contract.py", line 199, in _calc_targeted_long
    hyperdrivepy.calculate_targeted_long(
  File "/code/experiments/.venv/lib64/python3.10/site-packages/hyperdrivepy/hyperdrive_state.py", line 514, in calculate_targeted_long
    return _get_interface(pool_config, pool_info).calculate_targeted_long_with_budget(
ValueError: calculate_targeted_long_with_budget: MinimumTransactionAmount: Input amount too low

Location:
    crates/hyperdrive-math/src/long/open.rs:34:24

using agent0 v0.21.7:

Traceback (most recent call last):
  File "/code/agent0/fail_calc_targeted_long.py", line 4, in <module>
    from agent0.core.hyperdrive.interactive import LocalChain, LocalHyperdrive
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/__init__.py", line 12, in <module>
    from agent0.core.hyperdrive.interactive import Chain, Hyperdrive, LocalChain, LocalHyperdrive
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/core/hyperdrive/interactive/__init__.py", line 3, in <module>
    from .chain import Chain
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/core/hyperdrive/interactive/chain.py", line 22, in <module>
    from agent0.chainsync.dashboard.usernames import build_user_mapping
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/chainsync/dashboard/__init__.py", line 3, in <module>
    from .build_dashboard_dfs import build_pool_dashboard, build_wallet_dashboard
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/chainsync/dashboard/build_dashboard_dfs.py", line 9, in <module>
    from agent0.chainsync.db.hyperdrive import (
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/chainsync/db/hyperdrive/__init__.py", line 3, in <module>
    from .chain_to_db import checkpoint_events_to_db, data_chain_to_db, init_data_chain_to_db, trade_events_to_db
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/chainsync/db/hyperdrive/chain_to_db.py", line 13, in <module>
    from agent0.ethpy.hyperdrive import HyperdriveReadInterface
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/ethpy/hyperdrive/__init__.py", line 3, in <module>
    from .addresses import (
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/ethpy/hyperdrive/addresses.py", line 13, in <module>
    from agent0.hypertypes import HyperdriveRegistryContract, IHyperdriveContract, MockERC4626Contract
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/hypertypes/__init__.py", line 4, in <module>
    from .types import *
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/hypertypes/types/__init__.py", line 10, in <module>
    from .IHyperdriveContract import *
  File "/code/agent0/.venv/lib64/python3.10/site-packages/agent0/hypertypes/types/IHyperdriveContract.py", line 47, in <module>
    from web3.exceptions import FallbackNotFound
ImportError: cannot import name 'FallbackNotFound' from 'web3.exceptions' (/code/agent0/.venv/lib64/python3.10/site-packages/web3/exceptions.py)
dpaiton commented 1 week ago

Using the earlier version of agent0 is tricky; you have to backdate your hyperdrivetypes, pypechain, and possibly web3 installs as well. That's where the FallbackNotFound error is coming from. Your best bet for this is to look at earlier release that have the frozen dependency versions attached, like v0.21.8 and install the dependencies from that file.

Regarding the initial error, my guess is your target rate is too close to the actual rate, and the trade required to hit it is too low. Early on in the targeted rate calculation we do this:

// Estimate the long that achieves a target rate.
let (target_pool_share_reserves, target_pool_bond_reserves) =
    self.reserves_given_rate_ignoring_exposure(target_rate)?;
let (mut target_user_base_delta, target_user_bond_delta) = self
    .long_trade_needed_given_reserves(
        target_pool_share_reserves,
        target_pool_bond_reserves,
    )?;
// Determine what rate was achieved.
let resulting_rate = self
    .calculate_spot_rate_after_long(target_user_base_delta, Some(target_user_bond_delta))?;

That calculate_spot_rate_after_long function eventually calls calculate_open_long, which has this check at the beginning:

if base_amount < self.minimum_transaction_amount() {
    return Err(eyre!("MinimumTransactionAmount: Input amount too low",));
}

That's the error you're seeing. It is possible that there is a bug -- for example maybe long_trade_needed_given_reserves is producing an invalid base amount when there should be a valid base amount. But I think the more likely answer is your rates are too close together.

You could test this manually by opening a long for the minimum trade amount on this market and seeing how much it moves the fixed rate. If it's more than 0.67863% then you know that is your problem