-
Notifications
You must be signed in to change notification settings - Fork 4
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
Conversation
Pull Request Test Coverage Report for Build 11962859845Details
💛 - Coveralls |
4e97edf
to
45f3c90
Compare
There was a problem hiding this 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 :)
54ee5d1
to
ac28b01
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
* 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
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:
matchOrders
with the order intents and specifies some other configuration parameters, including the amount of liquidity to flash borrow.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 likeaddLiquidity
usingasBase
astrue
andremoveLiquidity
usingasBase
asfalse
.