Skip to content

Conversation

@Sean329
Copy link
Contributor

@Sean329 Sean329 commented Dec 9, 2024

resolves #1222

The matchOrders function will be called by a 3rd party when he finds two hanging orders can actually be matched. So, both of these 2 orders are actually the maker, and the surplus goes to the 3rd party caller (frontrun competition possible).

The fillOrders function will be called by the taker directly. The take does not need to sign a full orderIntent. He only grabs the maker's orderIntent and provide some other particular inputs along to the function call.

Checklist before finalization:

  1. the contract needs to support asBase==false as the fund token as well;
  2. apply the style: https://github.com/delvtech/hyperdrive/blob/main/STYLE_GUIDE.md.

hyperdrive);

// Update order bond amount used again to be accurate
orderBondAmountUsed[order1Hash] += bondAmount - bondMatchAmount;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this just be:

Suggested change
orderBondAmountUsed[order1Hash] += bondAmount - bondMatchAmount;
orderBondAmountUsed[order1Hash] += bondAmount;

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. This is correcting for what was done earlier. Is there a reason not to just do this once? The bondAmount will probably be lower than the bondMatchAmount, so I think that this could fail due to a lack of liquidity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Alex, in lines# 90 and 91 I've already updated the mapping like

        orderBondAmountUsed[order1Hash] += bondMatchAmount;
        orderBondAmountUsed[order2Hash] += bondMatchAmount;

And, I do this here before the execution really goes into the if(xxx){_handle...} blocks mainly to follow the CEI pattern to prevent reentrancy. I know there is a reentrancy guard modifier for this function, but the guard won't for cross-contract reentrancy. So I tend to use both the guard and the CEI patter. Therefore, if we have the Line#90 and 91, then later I will need something like this orderBondAmountUsed[order1Hash] += bondAmount - bondMatchAmount; to revise the update (account for the differences, and ofc the real bondAmount must always be larger than bondMatchAmount as the mint checks the min output. So there shouldn't be underflow risk here)

Copy link
Contributor Author

@Sean329 Sean329 Jan 13, 2025

Choose a reason for hiding this comment

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

I see. This is correcting for what was done earlier. Is there a reason not to just do this once? The bondAmount will probably be lower than the bondMatchAmount, so I think that this could fail due to a lack of liquidity.

Can it be? The bondMatchAmount is passed into the mint function call as the min output guard, so can the output of bondAmount still be lower than bondMatchAmount? I thought it couldn't, otherwise the mint just fails?

Copy link
Contributor

Choose a reason for hiding this comment

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

I need to think more about this second point. It’s late here, but I’ll take another look in the morning.

@Sean329 Sean329 changed the title WIP PR, NO REVIEW REQUESTED ATM -- Hyperdrive Matching Engine on top of mint() & burn() WIP PR -- Hyperdrive Matching Engine V2 on top of mint() & burn() Jan 13, 2025
@Sean329 Sean329 changed the base branch from main to jalextowle/feature/mint January 21, 2025 18:30
Base automatically changed from jalextowle/feature/mint to main January 23, 2025 22:08
@Sean329 Sean329 force-pushed the sean/feature/match-engine branch from 08059ba to a3de4b1 Compare January 24, 2025 08:19
@github-actions
Copy link

github-actions bot commented Jan 24, 2025

Hyperdrive Gas Benchmark

Benchmark suite Current: 932d6b5 Previous: 3497650 Deviation Status
addLiquidity: min 33899 gas 33899 gas 0% 🟰
addLiquidity: avg 202963 gas 203380 gas -0.2050%
addLiquidity: max 191291 gas 191291 gas 0% 🟰
burn: min 31493 gas 31493 gas 0% 🟰
burn: avg 136995 gas 136107 gas 0.6524% 🚨
burn: max 111180 gas 111180 gas 0% 🟰
checkpoint: min 40289 gas 40289 gas 0% 🟰
checkpoint: avg 148341 gas 148356 gas -0.0101%
checkpoint: max 149219 gas 149219 gas 0% 🟰
closeLong: min 31481 gas 31481 gas 0% 🟰
closeLong: avg 146560 gas 146214 gas 0.2366% 🚨
closeLong: max 126677 gas 126689 gas -0.0095%
closeShort: min 31336 gas 31336 gas 0% 🟰
closeShort: avg 137858 gas 137443 gas 0.3019% 🚨
closeShort: max 132580 gas 132580 gas 0% 🟰
initialize: min 31313 gas 31313 gas 0% 🟰
initialize: avg 355142 gas 355025 gas 0.0330% 🚨
initialize: max 355601 gas 355601 gas 0% 🟰
openLong: min 33330 gas 33330 gas 0% 🟰
openLong: avg 179315 gas 179602 gas -0.1598%
openLong: max 191398 gas 191434 gas -0.0188%
openShort: min 33896 gas 33896 gas 0% 🟰
openShort: avg 180518 gas 180333 gas 0.1026% 🚨
openShort: max 170068 gas 170068 gas 0% 🟰
redeemWithdrawalShares: min 31259 gas 31259 gas 0% 🟰
redeemWithdrawalShares: avg 77710 gas 77780 gas -0.0900%
redeemWithdrawalShares: max 67460 gas 67460 gas 0% 🟰
removeLiquidity: min 31265 gas 31265 gas 0% 🟰
removeLiquidity: avg 213881 gas 214089 gas -0.0972%
removeLiquidity: max 220575 gas 218337 gas 1.0250% 🚨

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

Comment on lines +120 to +117
/// @dev The amount to be used in the trade. In the case of `OpenLong` or
/// `OpenShort`, this is the amount of funds to deposit; and in the
/// case of `CloseLong` or `CloseShort`, this is the min amount of
/// funds to receive.
uint256 fundAmount;
/// @dev The minimum output amount expected from the trade. In the case of
/// `OpenLong` or `OpenShort`, this is the min amount of bonds to
/// receive; and in the case of `CloseLong` or `CloseShort`, this is
/// the amount of bonds to close.
uint256 bondAmount;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we talked about framing this in terms of price (or rate) rather than in terms of the fund amount and bond amount. It's fine to do this later, but I am curious on your thoughts on this change. What are the pros and cons from the implementation perspective as you see it.

Copy link
Contributor Author

@Sean329 Sean329 Jan 27, 2025

Choose a reason for hiding this comment

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

I thought about it for a while and asking the traders to directly provide fundAmount and bondAmount makes it easier for the matching engine to code. The price/rate is the consideration in the traders' mind, and, in the end for whatever decision they've made, the price/rate can all be reflected in the combination of these 3 params:

  1. input amount (money or positions)
  2. min output amount (positions or money)
  3. maturity time of the output or input positions

So, I put fundAmount , bondAmount , minMaturityTime and maxMaturityTime these four elements in the orderIntent. With them, the matching engine code can be straight forward; and, by carefully selecting the values for these four elements, the trader should be able to convey their price expectations.

Let me know if this makes sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Works for me. Would be good to poke Mihai about it again.

Copy link
Contributor

@jalextowle jalextowle left a comment

Choose a reason for hiding this comment

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

LGTM once the comments are addressed and CI is passing

@Sean329 Sean329 force-pushed the sean/feature/match-engine branch from 2cda1a2 to 932d6b5 Compare February 4, 2025 20:59
@Sean329 Sean329 force-pushed the sean/feature/match-engine branch from 932d6b5 to fdc3d30 Compare February 10, 2025 17:19
@Sean329
Copy link
Contributor Author

Sean329 commented Feb 14, 2025

Another more comprehensive PR #1239 will be merged into main. This PR and its branch shall be discarded.

@Sean329 Sean329 closed this Feb 14, 2025
@Sean329 Sean329 deleted the sean/feature/match-engine branch February 19, 2025 18:41
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