delvtech / hyperdrive-rs

Rust SDK for the Hyperdrive AMM.
https://docs.rs/crate/hyperdrive-math/latest
Apache License 2.0
3 stars 0 forks source link

fix `long_trade_needed_given_reserves` and `reserves_given_rate_ignoring_exposure` #180

Closed dpaiton closed 4 months ago

dpaiton commented 4 months ago

Resolved Issues

working towards https://github.com/delvtech/hyperdrive-rs/issues/21

Description

In #167 I wrote a test for long_trade_needed_given_reserves and reserves_given_rate_ignoring_exposure, which we will need to translate to the short variety for the targeted short flow. For the tests to pass I had to set fees, zeta, and exposure to zero. This was because the test itself used other components that required this, although the function of interest (long_trade_needed_given_reserves) does not have the same requirements.

In this PR I rewrote the test to be simpler and more direct, and found that the function was not passing with any sort of reasonable tolerance. I figured out the bug (we were not accounting for fees correctly) and fixed it.

Part of the fix for long_trade_needed_given_reserves included adding a sanity check on reserve levels, which revealed a bug in reserves_given_rate_ignoring_exposure, which was returning invalid reserve amounts due to non-zero exposure or zeta. Adding zeta to the equation fixed that problem, so all tests are passing again.

I had to increase the tolerance for the targeted long test. I'm not entirely sure why, but I think it's still reasonable so I will not investigate it further at this time.