Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Short Sandwich #537

Merged
merged 44 commits into from
Sep 26, 2023
Merged

fix: Short Sandwich #537

merged 44 commits into from
Sep 26, 2023

Conversation

jalextowle
Copy link
Contributor

@jalextowle jalextowle commented Jul 28, 2023

Fixes: #426.

Aside from implementing the zeta adjustment, this PR also includes significant improvements to both get_max_long and get_max_short.

@jalextowle jalextowle changed the title Started implementing a potential fix for the short sandwich fix: Short Sandwich Jul 28, 2023
@jalextowle jalextowle marked this pull request as draft July 28, 2023 01:52
@coveralls
Copy link
Collaborator

coveralls commented Jul 28, 2023

Coverage Status

coverage: 92.612% (-0.6%) from 93.182% when pulling dc2cf66 on jalextowle/bug/short-sandwich into 0cb80cc on main.

@github-actions
Copy link

github-actions bot commented Jul 30, 2023

Hyperdrive Gas Benchmark

Benchmark suite Current: dc2cf66 Previous: 0cb80cc Deviation Status
addLiquidity: min 785 gas 785 gas 0% 🟰
addLiquidity: avg 48716 gas 46879 gas 3.9186% 🚨
addLiquidity: max 88517 gas 80516 gas 9.9372% 🚨
checkpoint: min 514 gas 514 gas 0% 🟰
checkpoint: avg 29384 gas 29372 gas 0.0409% 🚨
checkpoint: max 84935 gas 82072 gas 3.4884% 🚨
closeLong: min 852 gas 852 gas 0% 🟰
closeLong: avg 22844 gas 21498 gas 6.2610% 🚨
closeLong: max 126080 gas 103566 gas 21.7388% 🚨
closeShort: min 765 gas 765 gas 0% 🟰
closeShort: avg 23834 gas 22248 gas 7.1287% 🚨
closeShort: max 104852 gas 87175 gas 20.2776% 🚨
initialize: min 803 gas 803 gas 0% 🟰
initialize: avg 159708 gas 159627 gas 0.0507% 🚨
initialize: max 233251 gas 233222 gas 0.0124% 🚨
openLong: min 740 gas 740 gas 0% 🟰
openLong: avg 42051 gas 42003 gas 0.1143% 🚨
openLong: max 205672 gas 224368 gas -8.3327%
openShort: min 782 gas 782 gas 0% 🟰
openShort: avg 50883 gas 49097 gas 3.6377% 🚨
openShort: max 219690 gas 218455 gas 0.5653% 🚨
removeLiquidity: min 762 gas 762 gas 0% 🟰
removeLiquidity: avg 65484 gas 63388 gas 3.3066% 🚨
removeLiquidity: max 140728 gas 123036 gas 14.3795% 🚨

This comment was automatically generated by workflow using github-action-benchmark.

@jalextowle jalextowle force-pushed the jalextowle/bug/short-sandwich branch from 6110827 to e4334e4 Compare August 1, 2023 02:23
@jalextowle jalextowle marked this pull request as ready for review September 5, 2023 18:10
contracts/src/HyperdriveLong.sol Show resolved Hide resolved
contracts/src/HyperdriveStorage.sol Show resolved Hide resolved
contracts/src/libraries/HyperdriveMath.sol Outdated Show resolved Hide resolved
contracts/src/libraries/HyperdriveMath.sol Outdated Show resolved Hide resolved
- For `test__calculateMaxLong__matureShort`, I ultimately settled for a
  relatively large bound. There are a few strategies that can be used to
  increase it's performance. First, we should see if adding a stricter
  constraint on the spot price improves the worst-case performance of
  `calculateMaxLong`. Second, we can try some of the tricks outlined in
  this thread:
  https://math.stackexchange.com/questions/508199/newtons-method-why-is-there-slow-convergence-with-a-high-multiplicity.
- For `test_long_multiblock_round_trip_end_of_checkpoint`, the failure
  occurs rarely, so we can merge without fixing it in this PR. This
  issue is tracked here:
  #582. I'll address this
  issue in an immediate follow-up PR.
Copy link
Contributor

@jrhea jrhea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really good. Just a few comments. On the testing side, i think it would be good to add a path independence test that shows that you can close trades in a different order and still get the same result. i actually tested a few cases when i reviewed this the first time around.

Also, here is a desmos that shows the short sandwhich problem:

https://www.desmos.com/calculator/trao96oozf

and here is the zeta adjustment:

https://www.desmos.com/calculator/6b2coffqsj

Makefile Outdated Show resolved Hide resolved
contracts/src/libraries/HyperdriveMath.sol Show resolved Hide resolved
contracts/src/libraries/YieldSpaceMath.sol Show resolved Hide resolved
test/integrations/hyperdrive/RoundTripTest.t.sol Outdated Show resolved Hide resolved
test/units/hyperdrive/CloseLongTest.t.sol Show resolved Hide resolved
test/units/hyperdrive/CloseLongTest.t.sol Show resolved Hide resolved
crates/hyperdrive-math/src/lib.rs Outdated Show resolved Hide resolved
@jalextowle jalextowle merged commit bdac1aa into main Sep 26, 2023
7 checks passed
@jalextowle jalextowle deleted the jalextowle/bug/short-sandwich branch September 26, 2023 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Drain pool by sandwiching matured shorts
3 participants