delvtech / hyperdrive

An automated market maker for fixed and variable yield with on-demand terms.
Apache License 2.0
30 stars 3 forks source link

[CRASH REPORT] can't close this long #705

Open wakamex opened 10 months ago

wakamex commented 10 months ago

Crash Report

Description

While running an econ experiment, I managed to end up with a long position that crashed both a smart contract preview transaction to close it and a call to the rust sdk's close long function.

Expected Behavior

Was expecting to be able to value this as part of pnl calculations.

Actual Behavior

Crash on both smart contract and rust sdk.

Steps to Reproduce

  1. Run lib/agent0/examples/interactive_econ.py on this branch: https://github.com/delvtech/agent0/tree/bug_1

Possible Solution

I can investigate what may have caused this degenerate state. No liquidity should have been withdrawn in this setup.

Log

day 19: OpenShort(trader='0x5ff7e882985fF060501200EafEEBae62Cf249182', asset_id=904625697166532776746648320380374280103671755200316906558262375063567728512, maturity_time=1746403200, base_amount=FixedPoint("0.000429146259790096"), share_price=FixedPoint("1.001823584397715158"), bond_amount=FixedPoint("0.012563787352138755")) WARNING:root:Retry attempt 1 out of 5: Function <bound method ContractFunction.call of <Function closeLong(uint256,uint256,uint256,(address,bool,bytes)) bound to (1746316800, 16921016709460082767275, 0, ('0x5ff7e882985fF060501200EafEEBae62Cf249182', True, b''))>> called from smart_contract_preview_transaction failed with ContractPanicError('Panic error 0x11: Arithmetic operation results in underflow or overflow.') WARNING:root:Retry attempt 2 out of 5: Function <bound method ContractFunction.call of <Function closeLong(uint256,uint256,uint256,(address,bool,bytes)) bound to (1746316800, 16921016709460082767275, 0, ('0x5ff7e882985fF060501200EafEEBae62Cf249182', True, b''))>> called from smart_contract_preview_transaction failed with ContractPanicError('Panic error 0x11: Arithmetic operation results in underflow or overflow.') WARNING:root:Retry attempt 3 out of 5: Function <bound method ContractFunction.call of <Function closeLong(uint256,uint256,uint256,(address,bool,bytes)) bound to (1746316800, 16921016709460082767275, 0, ('0x5ff7e882985fF060501200EafEEBae62Cf249182', True, b''))>> called from smart_contract_preview_transaction failed with ContractPanicError('Panic error 0x11: Arithmetic operation results in underflow or overflow.') WARNING:root:Retry attempt 4 out of 5: Function <bound method ContractFunction.call of <Function closeLong(uint256,uint256,uint256,(address,bool,bytes)) bound to (1746316800, 16921016709460082767275, 0, ('0x5ff7e882985fF060501200EafEEBae62Cf249182', True, b''))>> called from smart_contract_preview_transaction failed with ContractPanicError('Panic error 0x11: Arithmetic operation results in underflow or overflow.') WARNING:root:Retry attempt 5 out of 5: Function <bound method ContractFunction.call of <Function closeLong(uint256,uint256,uint256,(address,bool,bytes)) bound to (1746316800, 16921016709460082767275, 0, ('0x5ff7e882985fF060501200EafEEBae62Cf249182', True, b''))>> called from smart_contract_preview_transaction failed with ContractPanicError('Panic error 0x11: Arithmetic operation results in underflow or overflow.') Backend TkAgg is interactive backend. Turning interactive mode on. thread '' panicked at /home/runner/work/hyperdrive-sdk/hyperdrive-sdk/hyperdrive/crates/hyperdrive-math/src/yield_space.rs:120:14:

Smart contract error

Traceback (most recent call last):
  File "/code/elfpy/lib/ethpy/ethpy/base/transactions.py", line 197, in smart_contract_preview_transaction
    return_values = retry_call(
  File "/code/elfpy/lib/ethpy/ethpy/base/retry_utils.py", line 68, in retry_call
    raise exception
  File "/code/elfpy/lib/ethpy/ethpy/base/retry_utils.py", line 48, in retry_call
    out = func(*args, **kwargs)
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/web3/contract/contract.py", line 283, in call
    return call_contract_function(
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/web3/contract/utils.py", line 96, in call_contract_function
    return_data = w3.eth.call(
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/web3/eth/eth.py", line 256, in call
    return self._durin_call(transaction, block_identifier, state_override)
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/web3/eth/eth.py", line 275, in _durin_call
    return self._call(transaction, block_identifier, state_override)
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/web3/module.py", line 73, in caller
    result = w3.manager.request_blocking(
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/web3/manager.py", line 250, in request_blocking
    return self.formatted_response(
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/web3/manager.py", line 209, in formatted_response
    apply_error_formatters(error_formatters, response)
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/web3/manager.py", line 85, in apply_error_formatters
    formatted_resp = pipe(response, error_formatters)
  File "cytoolz/functoolz.pyx", line 680, in cytoolz.functoolz.pipe
  File "cytoolz/functoolz.pyx", line 655, in cytoolz.functoolz.c_pipe
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/web3/_utils/contract_error_handling.py", line 126, in raise_contract_logic_error_on_revert
    raise ContractPanicError(PANIC_ERROR_CODES[panic_error_code], data=data)
web3.exceptions.ContractPanicError: Panic error 0x11: Arithmetic operation results in underflow or overflow.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/mihai/.pyenv/versions/3.10.11/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/mihai/.pyenv/versions/3.10.11/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "lib/agent0/examples/interactive_econ.py", line 224, in <module>
    event = rob.open_long(trade_size_base)
  File "/code/elfpy/lib/agent0/agent0/hyperdrive/interactive/interactive_hyperdrive_agent.py", line 112, in open_long
    return self._pool._open_long(self.agent, base)
  File "/code/elfpy/lib/agent0/agent0/hyperdrive/interactive/interactive_hyperdrive.py", line 934, in _open_long
    self._run_blocking_data_pipeline()
  File "/code/elfpy/lib/agent0/agent0/hyperdrive/interactive/interactive_hyperdrive.py", line 355, in _run_blocking_data_pipeline
    data_analysis(
  File "/code/elfpy/lib/chainsync/chainsync/exec/data_analysis.py", line 129, in data_analysis
    data_to_analysis(
  File "/code/elfpy/lib/chainsync/chainsync/analysis/data_to_analysis.py", line 197, in data_to_analysis
    pnl_df = calc_closeout_pnl(wallet_pnl, pool_info, hyperdrive_contract, hyperdrive_interface)
  File "/code/elfpy/lib/chainsync/chainsync/analysis/calc_pnl.py", line 255, in calc_closeout_pnl
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/pandas/core/frame.py", line 9423, in apply
    return op.apply().__finalize__(self, method="apply")
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/pandas/core/apply.py", line 678, in apply
    return self.apply_standard()
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/pandas/core/apply.py", line 798, in apply_standard
    results, res_index = self.apply_series_generator()
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/pandas/core/apply.py", line 814, in apply_series_generator
    results[i] = self.f(v)
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/pandas/core/apply.py", line 133, in f
    return func(x, *args, **kwargs)
  File "/code/elfpy/lib/chainsync/chainsync/analysis/calc_pnl.py", line 192, in calc_single_closeout
  File "/code/elfpy/lib/ethpy/ethpy/base/transactions.py", line 220, in smart_contract_preview_transaction
    raise ContractCallException(
ethpy.base.errors.errors.ContractCallException: Error in preview transaction

Rust sdk error

called `Result::unwrap()` on an `Err` value: calculate_shares_out_given_bonds_in_down_safe: k = 23736090.763233754451666878 < 3431052297604105489462.567022729897272670 = y

Location:
    /home/runner/work/hyperdrive-sdk/hyperdrive-sdk/hyperdrive/crates/hyperdrive-math/src/yield_space.rs:137:24
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
98.19s - Error processing internal command.
Traceback (most recent call last):
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 1783, in process_internal_commands
    int_cmd.do_it(self)
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 528, in do_it
    self.method(dbg, *self.args, **self.kwargs)
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 1195, in internal_evaluate_expression_json
    pydevd_vars.evaluate_expression(py_db, frame, expression, is_exec=True)
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 370, in new_func
    return _run_with_unblock_threads(original_func, py_db, curr_thread, frame, expression, is_exec)
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 334, in _run_with_unblock_threads
    return _run_with_interrupt_thread(original_func, py_db, curr_thread, frame, expression, is_exec)
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 305, in _run_with_interrupt_thread
    return original_func(py_db, frame, expression, is_exec)
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 558, in evaluate_expression
    Exec(compiled, updated_globals, updated_locals)
  File "/home/mihai/.vscode/extensions/ms-python.python-2023.22.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_exec2.py", line 5, in Exec
    exec(exp, global_vars)
  File "<string>", line 2, in <module>
  File "/code/elfpy/lib/chainsync/chainsync/analysis/calc_pnl.py", line 45, in calculate_close_long_flat_plus_curve
    curve_part_shares = interface.calc_shares_out_given_bonds_in_down(abs(curve_part_bonds), pool_state)
  File "/code/elfpy/lib/ethpy/ethpy/hyperdrive/interface/interface.py", line 934, in calc_shares_out_given_bonds_in_down
    return _calc_shares_out_given_bonds_in_down(pool_state, amount_in)
  File "/code/elfpy/lib/ethpy/ethpy/hyperdrive/interface/_mock_contract.py", line 133, in _calc_shares_out_given_bonds_in_down
    amount_out = hyperdrivepy.calculate_shares_out_given_bonds_in_down(
  File "/home/mihai/.pyenv/versions/3.10.11/envs/elf-env/lib/python3.10/site-packages/hyperdrivepy/hyperdrive_state.py", line 403, in calculate_shares_out_given_bonds_in_down
    return _get_interface(pool_config, pool_info).calculate_shares_out_given_bonds_in_down(amount_in)
pyo3_runtime.PanicException: called `Result::unwrap()` on an `Err` value: calculate_shares_out_given_bonds_in_down_safe: k = 23736090.763233754451666878 < 3431052297604105489462.567022729897272670 = y

Location:
    /home/runner/work/hyperdrive-sdk/hyperdrive-sdk/hyperdrive/crates/hyperdrive-math/src/yield_space.rs:137:24

Environment

jrhea commented 8 months ago

The only time you are guaranteed to close longs/shorts is at maturity