@@ -51,6 +51,7 @@ contract HyperdriveMatchingEngineV2 is
5151 string public constant version = VERSION;
5252
5353 /// @notice The buffer amount used for cost related calculations.
54+ /// @dev TODO: The buffer amount needs more testing.
5455 uint256 public constant TOKEN_AMOUNT_BUFFER = 10 ;
5556
5657 /// @notice Mapping to track cancelled orders.
@@ -87,12 +88,11 @@ contract HyperdriveMatchingEngineV2 is
8788 IHyperdrive hyperdrive = _order1.hyperdrive;
8889
8990 // Handle different order type combinations.
91+ // Case 1: Long + Short creation using mint().
9092 if (_order1.orderType == OrderType.OpenLong &&
9193 _order2.orderType == OrderType.OpenShort) {
92- // Case 1: Long + Short creation using mint().
93-
9494 // Get necessary pool parameters.
95- IHyperdrive.PoolConfig memory config = _getHyperdriveDurationsAndFees ( hyperdrive);
95+ IHyperdrive.PoolConfig memory config = hyperdrive. getPoolConfig ( );
9696
9797 uint256 latestCheckpoint = _latestCheckpoint (config.checkpointDuration);
9898 // @dev TODO: there is another way to get the info without calling
@@ -125,7 +125,7 @@ contract HyperdriveMatchingEngineV2 is
125125
126126
127127 // Get the sufficient funding amount to mint the bonds.
128- // NOTE: Round the requred fund amount up to overestimate the cost.
128+ // NOTE: Round the required fund amount up to overestimate the cost.
129129 // Round the flat fee calculation up and the governance fee
130130 // calculation down to match the rounding used in the other flows.
131131 uint256 cost = bondMatchAmount.mulDivUp (
@@ -136,7 +136,7 @@ contract HyperdriveMatchingEngineV2 is
136136
137137 // Calculate the amount of base tokens to transfer based on the
138138 // bondMatchAmount.
139- // NOTE: Round the requred fund amount down to prevent overspending
139+ // NOTE: Round the required fund amount down to prevent overspending
140140 // and possible reverting at a later step.
141141 uint256 baseTokenAmountOrder1 = order1.fundAmount.mulDivDown (bondMatchAmount, order1.bondAmount);
142142 uint256 baseTokenAmountOrder2 = order2.fundAmount.mulDivDown (bondMatchAmount, order2.bondAmount);
@@ -195,11 +195,9 @@ contract HyperdriveMatchingEngineV2 is
195195 );
196196 }
197197
198-
198+ // Case 2: Long + Short closing using burn().
199199 else if (_order1.orderType == OrderType.CloseLong &&
200200 _order2.orderType == OrderType.CloseShort) {
201- // Case 2: Long + Short closing using burn().
202-
203201 // Verify both orders have the same maturity time.
204202 if (_order1.maxMaturityTime != _order2.maxMaturityTime) {
205203 revert InvalidMaturityTime ();
@@ -221,7 +219,7 @@ contract HyperdriveMatchingEngineV2 is
221219 orderBondAmountUsed[order2Hash] += bondMatchAmount;
222220
223221 // Get the min fund output according to the bondMatchAmount.
224- // NOTE: Round the requred fund amount up to respect the order specified
222+ // NOTE: Round the required fund amount up to respect the order specified
225223 // min fund output.
226224 uint256 minFundAmountOrder1 = (_order1.fundAmount - orderFundAmountUsed[order1Hash]).mulDivUp (bondMatchAmount, _order1.bondAmount);
227225 uint256 minFundAmountOrder2 = (_order2.fundAmount - orderFundAmountUsed[order2Hash]).mulDivUp (bondMatchAmount, _order2.bondAmount);
@@ -261,16 +259,19 @@ contract HyperdriveMatchingEngineV2 is
261259 );
262260 }
263261
262+ // Case 3: Long transfer between traders.
264263 else if (_order1.orderType == OrderType.OpenLong &&
265264 _order2.orderType == OrderType.CloseLong) {
266- // Case 3: Long transfer between traders.
267265 _handleLongTransfer ();
268266 }
267+
268+ // Case 4: Short transfer between traders.
269269 else if (_order1.orderType == OrderType.OpenShort &&
270270 _order2.orderType == OrderType.CloseShort) {
271- // Case 4: Short transfer between traders.
272271 _handleShortTransfer ();
273272 }
273+
274+ // All other cases are invalid.
274275 else {
275276 revert InvalidOrderCombination ();
276277 }
@@ -382,8 +383,6 @@ contract HyperdriveMatchingEngineV2 is
382383 OrderIntent calldata _order1 ,
383384 OrderIntent calldata _order2
384385 ) internal view returns (bytes32 order1Hash , bytes32 order2Hash ) {
385-
386-
387386 // Verify counterparties.
388387 if (
389388 (_order1.counterparty != address (0 ) &&
@@ -451,7 +450,6 @@ contract HyperdriveMatchingEngineV2 is
451450 revert AlreadyCancelled ();
452451 }
453452
454-
455453 // Verify signatures.
456454 if (
457455 ! verifySignature (
@@ -467,7 +465,6 @@ contract HyperdriveMatchingEngineV2 is
467465 ) {
468466 revert InvalidSignature ();
469467 }
470-
471468 }
472469
473470 /// @dev Calculates the amount of bonds that can be matched between two orders.
@@ -535,14 +532,16 @@ contract HyperdriveMatchingEngineV2 is
535532
536533 // Approve Hyperdrive.
537534 // @dev Use balanceOf to get the total amount of base tokens instead of
538- // summing up the two amounts, in order to open the door for poential
539- // donation to help match orders.
535+ // summing up the two amounts, in order to open the door for any
536+ // potential donation to help match orders.
540537 uint256 totalBaseTokenAmount = _baseToken.balanceOf (address (this ));
541538 uint256 baseTokenAmountToUse = _cost + TOKEN_AMOUNT_BUFFER;
542539 if (totalBaseTokenAmount < baseTokenAmountToUse) {
543540 revert InsufficientFunding ();
544541 }
545- _baseToken.forceApprove (address (_hyperdrive), baseTokenAmountToUse);
542+
543+ // @dev Add 1 wei of approval so that the storage slot stays hot.
544+ _baseToken.forceApprove (address (_hyperdrive), baseTokenAmountToUse + 1 );
546545
547546 // Create PairOptions.
548547 IHyperdrive.PairOptions memory pairOptions = IHyperdrive.PairOptions ({
@@ -586,7 +585,6 @@ contract HyperdriveMatchingEngineV2 is
586585 ERC20 _baseToken ,
587586 IHyperdrive _hyperdrive
588587 ) internal {
589-
590588 // Get asset IDs for the long and short positions.
591589 uint256 longAssetId = AssetId.encodeAssetId (
592590 AssetId.AssetIdPrefix.Long,
@@ -631,22 +629,12 @@ contract HyperdriveMatchingEngineV2 is
631629 // Transfer proceeds to traders.
632630 _baseToken.safeTransfer (_longOrder.options.destination, _minFundAmountLongOrder);
633631 _baseToken.safeTransfer (_shortOrder.options.destination, _minFundAmountShortOrder);
634-
635632 }
636633
637634 // TODO: Implement these functions.
638635 function _handleLongTransfer () internal {}
639636 function _handleShortTransfer () internal {}
640637
641- /// @dev Get checkpoint and position durations from Hyperdrive contract.
642- /// @param _hyperdrive The Hyperdrive contract to query.
643- /// @return config The pool config.
644- function _getHyperdriveDurationsAndFees (IHyperdrive _hyperdrive ) internal view returns (
645- IHyperdrive.PoolConfig memory config
646- ) {
647- config = _hyperdrive.getPoolConfig ();
648- }
649-
650638 /// @dev Gets the most recent checkpoint time.
651639 /// @param _checkpointDuration The duration of the checkpoint.
652640 /// @return latestCheckpoint The latest checkpoint.
0 commit comments