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

Hyperdrive Matching Engine #1201

Merged
merged 16 commits into from
Nov 26, 2024
Merged

Hyperdrive Matching Engine #1201

merged 16 commits into from
Nov 26, 2024

Conversation

jalextowle
Copy link
Contributor

@jalextowle jalextowle commented Oct 26, 2024

Description

This PR adds a matching engine that can be used to match traders on Hyperdrive directly. Due to the way it was designed, this matching engine allows a long and short trader to trade directly with each other without needing to worry about whether or not there is enough LP provided liquidity in the pool. The system uses Morpho as a flash lender since Morpho provides zero-cost flash loans on the funds that it holds.

In order for two users to be directly matched, one needs to open a long and the other needs to open a short. Because of the way that Hyperdrive's intra-checkpoint netting system works, a long and a short that have the same size will offset each other. This is the core insight that enables direct matching to work because it means that directly matching a long against a short doesn't impact liquidity if they are sized properly. The way that the users are directly matched follows this flow:

  1. The long buyer and short buyer create order intents and approve the matching engine to spend their base tokens.
  2. The executor of the match calls matchOrders with the order intents and specifies some other configuration parameters, including the amount of liquidity to flash borrow.
  3. The requested funds are flash borrowed into the matching engine.
  4. The flash borrowed funds are LP'd into Hyperdrive.
  5. One of the trades is executed on Hyperdrive.
  6. The opposing trade is executed on Hyperdrive.
  7. The liquidity is removed from Hyperdrive.
  8. Any surplus base and withdrawal shares in the matching engine are sent to the fee recipient.
  9. The flash loan is repaid.

A few simplifications were made to speed up the development process and reduce complexity:

  • asBase must be true for both orders and for the add and remove liquidity options. This simplifies the logic and avoids having to deal with edge cases like addLiquidity using asBase as true and removeLiquidity using asBase as false.
  • ETH is not a supported deposit or withdrawal asset.
  • There are no partial fills. This logic is challenging to design for this kind of matching engine because Hyperdrive trades aren't linear. This means that chopping up an order and executing it in pieces could have very different results (and different slippage properties) than executing it in one shot.

@jalextowle jalextowle changed the title Hyperdrive OTC WIP: Hyperdrive OTC Oct 26, 2024
@coveralls
Copy link
Collaborator

coveralls commented Oct 26, 2024

Pull Request Test Coverage Report for Build 11962859845

Details

  • 53 of 88 (60.23%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.5%) to 88.354%

Changes Missing Coverage Covered Lines Changed/Added Lines %
contracts/src/matching/HyperdriveMatchingEngine.sol 53 88 60.23%
Totals Coverage Status
Change from base Build 11507869923: -0.5%
Covered Lines: 2769
Relevant Lines: 3134

💛 - Coveralls

@jalextowle jalextowle changed the title WIP: Hyperdrive OTC Hyperdrive Matching Engine Nov 22, 2024
@jalextowle jalextowle force-pushed the jalextowle/feature/hyperdrive-otc branch from 4e97edf to 45f3c90 Compare November 22, 2024 01:23
Copy link
Contributor

@mcclurejt mcclurejt left a comment

Choose a reason for hiding this comment

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

Awesome work dude! As a whole it looks awesome.

Lots of nits + questions for my own understanding...

Want to give it one more pass before approve just to make sure I'm not missing anything :)

contracts/src/instances/morpho-blue/MorphoBlueBase.sol Outdated Show resolved Hide resolved
contracts/src/interfaces/IHyperdriveMatchingEngine.sol Outdated Show resolved Hide resolved
contracts/src/interfaces/IHyperdriveMatchingEngine.sol Outdated Show resolved Hide resolved
contracts/src/matching/HyperdriveMatchingEngine.sol Outdated Show resolved Hide resolved
test/units/matching/HyperdriveMatchingEngineTest.t.sol Outdated Show resolved Hide resolved
@jalextowle jalextowle force-pushed the jalextowle/feature/hyperdrive-otc branch from 54ee5d1 to ac28b01 Compare November 25, 2024 21:01
Copy link
Contributor

@mcclurejt mcclurejt left a comment

Choose a reason for hiding this comment

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

Looks great!

@jalextowle jalextowle added this pull request to the merge queue Nov 26, 2024
Merged via the queue into main with commit d9e0a07 Nov 26, 2024
33 of 35 checks passed
@jalextowle jalextowle deleted the jalextowle/feature/hyperdrive-otc branch November 26, 2024 02:59
mcclurejt pushed a commit that referenced this pull request Nov 26, 2024
* Progress checkpoint

* Checkpoint #2

* Wrote a working example of the OTC trade on cbBTC/USDC using flash loans

* Committed temporary progress

* Added the gap calculation

* Started building the real matching engine

* Implemented a MCP matching engine

* Updated the test to utilize the new matching engine

* Wired up the new matching engine with the integration test

* Reverted typo in `contracts/src/internal/HyperdriveShort.sol`

* Added a bunch of unit tests for the matching engine

* Wrote a full set of unit tests for the matching engine

* Fixed linter errors

* Addressed some review feedback from @mcclurejt

* Removed outdated comment

* Addressed remaining feedback from @mcclurejt
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.

3 participants