Skip to content

Commit bde0843

Browse files
committed
Resolve PR feedback
1 parent e3f4822 commit bde0843

File tree

3 files changed

+16
-55
lines changed

3 files changed

+16
-55
lines changed

contracts/src/matching/HyperdriveMatchingEngineV2.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,18 @@ contract HyperdriveMatchingEngineV2 is
14591459
uint256 governanceFee = 2 * flatFee.mulDown(config.fees.governanceLP);
14601460
cost += governanceFee;
14611461

1462+
// @dev we can use hyperdrive.convertToBase and hyperdrive.convertToShares
1463+
// to simulate the conversions made within the yield source. When we
1464+
// are using base, what happens under the hood is that the calculations
1465+
// will flow as:
1466+
// 1. Deposit base into yield source.
1467+
// 2. Calculate the shares minted with hyperdrive.convertToShares(baseAmount)
1468+
// 3. Calculate the final base amount with
1469+
// finalBaseAmount =
1470+
// hyperdrive.convertToShares(baseAmount).mulDown(vaultSharePrice)
1471+
// With this in mind, we should be able to work back to this input
1472+
// base amount with high precision if we do:
1473+
// baseAmount = hyperdrive.convertToBase(finalBaseAmount.divDown(vaultSharePrice)
14621474
if (_asBase) {
14631475
// NOTE: Round up to overestimate the cost.
14641476
cost = _hyperdrive.convertToBase(cost.divUp(vaultSharePrice));

test/integrations/matching/MatchingEngineV2IntegrationTest.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
pragma solidity 0.8.24;
33

4-
import { IMorpho, Market, MarketParams, Id } from "morpho-blue/src/interfaces/IMorpho.sol";
4+
import { IMorpho, MarketParams } from "morpho-blue/src/interfaces/IMorpho.sol";
55
import { MarketParamsLib } from "morpho-blue/src/libraries/MarketParamsLib.sol";
66
import { MorphoBlueConversions } from "../../../contracts/src/instances/morpho-blue/MorphoBlueConversions.sol";
77
import { MorphoBlueHyperdrive } from "../../../contracts/src/instances/morpho-blue/MorphoBlueHyperdrive.sol";

test/units/matching/HyperdriveMatchingEngineV2Test.t.sol

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,9 @@ contract HyperdriveMatchingEngineV2Test is HyperdriveTest {
575575
/// @dev Fuzzing test to verify TOKEN_AMOUNT_BUFFER is sufficient
576576
function testFuzz_tokenAmountBuffer(uint256 bondAmount) public {
577577
bondAmount = bound(bondAmount, 100e18, 1_000_000e18);
578-
uint256 fundAmount1 = bondAmount / 2;
579-
(, uint256 cost) = _calculateMintCost(bondAmount, true);
580-
uint256 fundAmount2 = cost + 10 - fundAmount1;
578+
// Ensure the fund is way more than sufficient
579+
uint256 fundAmount1 = bondAmount * 2;
580+
uint256 fundAmount2 = fundAmount1;
581581

582582
// Create orders
583583
IHyperdriveMatchingEngineV2.OrderIntent
@@ -1063,55 +1063,4 @@ contract HyperdriveMatchingEngineV2Test is HyperdriveTest {
10631063
(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, digest);
10641064
return abi.encodePacked(r, s, v);
10651065
}
1066-
1067-
/// @dev Calculates the cost and parameters for minting positions.
1068-
/// @param _bondMatchAmount The amount of bonds to mint.
1069-
/// @param _asBase Whether the cost is in terms of base.
1070-
/// @return maturityTime The maturity time for new positions.
1071-
/// @return cost The total cost including fees.
1072-
function _calculateMintCost(
1073-
uint256 _bondMatchAmount,
1074-
bool _asBase
1075-
) internal view returns (uint256 maturityTime, uint256 cost) {
1076-
// Get pool configuration.
1077-
IHyperdrive.PoolConfig memory config = hyperdrive.getPoolConfig();
1078-
1079-
// Calculate checkpoint and maturity time.
1080-
uint256 latestCheckpoint = hyperdrive.latestCheckpoint();
1081-
maturityTime = latestCheckpoint + config.positionDuration;
1082-
1083-
// Get vault share prices.
1084-
uint256 vaultSharePrice = hyperdrive.convertToBase(1e18);
1085-
uint256 openVaultSharePrice = hyperdrive
1086-
.getCheckpoint(latestCheckpoint)
1087-
.vaultSharePrice;
1088-
if (openVaultSharePrice == 0) {
1089-
openVaultSharePrice = vaultSharePrice;
1090-
}
1091-
1092-
// Calculate the required fund amount.
1093-
// NOTE: Round the required fund amount up to overestimate the cost.
1094-
cost = _bondMatchAmount.mulDivUp(
1095-
vaultSharePrice.max(openVaultSharePrice),
1096-
openVaultSharePrice
1097-
);
1098-
1099-
// Add flat fee.
1100-
// NOTE: Round the flat fee calculation up to match other flows.
1101-
uint256 flatFee = _bondMatchAmount.mulUp(config.fees.flat);
1102-
cost += flatFee;
1103-
1104-
// Add governance fee.
1105-
// NOTE: Round the governance fee calculation down to match other flows.
1106-
uint256 governanceFee = 2 * flatFee.mulDown(config.fees.governanceLP);
1107-
cost += governanceFee;
1108-
1109-
if (_asBase) {
1110-
// NOTE: Round up to overestimate the cost.
1111-
cost = hyperdrive.convertToBase(cost.divUp(vaultSharePrice));
1112-
} else {
1113-
// NOTE: Round up to overestimate the cost.
1114-
cost = cost.divUp(vaultSharePrice);
1115-
}
1116-
}
11171066
}

0 commit comments

Comments
 (0)