Skip to content

Conversation

Sean329
Copy link
Contributor

@Sean329 Sean329 commented Feb 4, 2025

HyperdriveMatchingEngineV2 PR Description

Overview

This PR introduces HyperdriveMatchingEngineV2, an enhanced matching engine for Hyperdrive that facilitates efficient order matching and position trading. The engine supports direct position minting/burning and position transfers between traders.

Key Features

Order Types Support

  • OpenLong: Open a long position
  • OpenShort: Open a short position
  • CloseLong: Close an existing long position
  • CloseShort: Close an existing short position

Core Functionalities

  1. Order Matching (matchOrders)

    • Matches complementary orders (e.g., OpenLong + OpenShort)
    • Supports position transfers between traders
    • Handles partial fills
    • Includes surplus distribution mechanism
  2. Direct Order Filling (fillOrder)

    • Allows immediate order execution by takers
    • Supports maker-taker model
    • Maintains order state tracking
  3. EIP-712 Signature Support

    • Secure order signing and verification
    • Support for both EOA and contract signatures

Safety Features

  • Reentrancy protection
  • Order expiration checks
  • Counterparty validation
  • Maturity time validation
  • Fund amount validation
  • Token amount buffer for gas optimization

Technical Improvements

  • Direct mint/burn operations instead of flash loans
  • Optimized gas usage with TOKEN_AMOUNT_BUFFER
  • Enhanced order state tracking
  • Comprehensive error handling

Testing Coverage

The implementation includes extensive test coverage:

  • Basic order matching scenarios
  • Position transfer cases
  • Partial fill scenarios
  • Edge cases and failure conditions
  • Fuzzing tests for TOKEN_AMOUNT_BUFFER validation

Interface Changes

The IHyperdriveMatchingEngineV2 interface provides:

  • Clear error definitions
  • Comprehensive event logging
  • Structured order intent definition
  • Order amount tracking

Security Considerations

  • All monetary operations use SafeERC20
  • Strict validation of order parameters
  • Protected against common attack vectors
  • Clear access control mechanisms

Breaking Changes

None. This is a new contract implementation that can coexist with previous versions.

Next Steps

  • Complete security audit
  • Add integration tests
  • Update documentation
  • Deploy to testnet

Related Issues

  • Closes #[issue-number]
  • References #[related-issue-number]

resolves #1222

Copy link

github-actions bot commented Feb 4, 2025

Hyperdrive Gas Benchmark

Benchmark suite Current: c65c98e Previous: 3497650 Deviation Status
addLiquidity: min 33877 gas 33899 gas -0.0649%
addLiquidity: avg 203454 gas 203380 gas 0.0364% 🚨
addLiquidity: max 187754 gas 191291 gas -1.8490%
burn: min 31471 gas 31493 gas -0.0699%
burn: avg 135785 gas 136107 gas -0.2366%
burn: max 111163 gas 111180 gas -0.0153%
checkpoint: min 40267 gas 40289 gas -0.0546%
checkpoint: avg 148332 gas 148356 gas -0.0162%
checkpoint: max 149197 gas 149219 gas -0.0147%
closeLong: min 31459 gas 31481 gas -0.0699%
closeLong: avg 146544 gas 146214 gas 0.2257% 🚨
closeLong: max 126667 gas 126689 gas -0.0174%
closeShort: min 31425 gas 31336 gas 0.2840% 🚨
closeShort: avg 137776 gas 137443 gas 0.2423% 🚨
closeShort: max 132669 gas 132580 gas 0.0671% 🚨
initialize: min 31291 gas 31313 gas -0.0703%
initialize: avg 355249 gas 355025 gas 0.0631% 🚨
initialize: max 355631 gas 355601 gas 0.0084% 🚨
openLong: min 33440 gas 33330 gas 0.3300% 🚨
openLong: avg 179731 gas 179602 gas 0.0718% 🚨
openLong: max 191544 gas 191434 gas 0.0575% 🚨
openShort: min 33874 gas 33896 gas -0.0649%
openShort: avg 180154 gas 180333 gas -0.0993%
openShort: max 170046 gas 170068 gas -0.0129%
redeemWithdrawalShares: min 31259 gas 31259 gas 0% 🟰
redeemWithdrawalShares: avg 77114 gas 77780 gas -0.8563%
redeemWithdrawalShares: max 67460 gas 67460 gas 0% 🟰
removeLiquidity: min 31243 gas 31265 gas -0.0704%
removeLiquidity: avg 214072 gas 214089 gas -0.0079%
removeLiquidity: max 220553 gas 218337 gas 1.0149% 🚨

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

@Sean329 Sean329 force-pushed the sean/feature/match-engine-newPR branch from 8a88d79 to 6c61031 Compare February 10, 2025 17:17
@Sean329 Sean329 force-pushed the sean/feature/match-engine branch from 932d6b5 to fdc3d30 Compare February 10, 2025 17:19
@Sean329 Sean329 changed the base branch from sean/feature/match-engine to main February 10, 2025 17:45
@Sean329 Sean329 changed the title WIP PR -- Matching Engine V2 new PR on top of the part 1 PR with already approved code Matching Engine V2 new PR on top of the part 1 PR#1225 with already approved code Feb 11, 2025
@Sean329 Sean329 added this pull request to the merge queue Feb 14, 2025
@Sean329 Sean329 removed this pull request from the merge queue due to a manual request Feb 14, 2025
@Sean329 Sean329 enabled auto-merge February 14, 2025 03:26
@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 13321830238

Details

  • 196 of 334 (58.68%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-2.7%) to 86.513%

Changes Missing Coverage Covered Lines Changed/Added Lines %
contracts/src/matching/HyperdriveMatchingEngineV2.sol 196 334 58.68%
Totals Coverage Status
Change from base Build 13148119986: -2.7%
Covered Lines: 3233
Relevant Lines: 3737

💛 - Coveralls

@Sean329 Sean329 added this pull request to the merge queue Feb 14, 2025
Merged via the queue into main with commit 8d8554d Feb 14, 2025
33 of 35 checks passed
@Sean329 Sean329 deleted the sean/feature/match-engine-newPR branch February 14, 2025 05:40
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.

Hyperdrive Matching Engine

3 participants