Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions contracts/src/matching/HyperdriveMatchingEngineV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ contract HyperdriveMatchingEngineV2 is
// Calculate costs and parameters.
(uint256 maturityTime, uint256 cost) = _calculateMintCost(
hyperdrive,
bondMatchAmount
bondMatchAmount,
_order1.options.asBase
);

// Check if the maturity time is within the range.
Expand Down Expand Up @@ -442,7 +443,8 @@ contract HyperdriveMatchingEngineV2 is
// Calculate costs and parameters.
(uint256 maturityTime, uint256 cost) = _calculateMintCost(
hyperdrive,
bondMatchAmount
bondMatchAmount,
_makerOrder.options.asBase
);

// Check if the maturity time is within the range.
Expand Down Expand Up @@ -552,7 +554,8 @@ contract HyperdriveMatchingEngineV2 is
// Calculate costs and parameters.
(uint256 maturityTime, uint256 cost) = _calculateMintCost(
hyperdrive,
bondMatchAmount
bondMatchAmount,
_makerOrder.options.asBase
);

// Check if the maturity time is within the range.
Expand Down Expand Up @@ -1415,11 +1418,13 @@ contract HyperdriveMatchingEngineV2 is
/// @dev Calculates the cost and parameters for minting positions.
/// @param _hyperdrive The Hyperdrive contract instance.
/// @param _bondMatchAmount The amount of bonds to mint.
/// @param _asBase Whether the cost is in terms of base.
/// @return maturityTime The maturity time for new positions.
/// @return cost The total cost including fees.
function _calculateMintCost(
IHyperdrive _hyperdrive,
uint256 _bondMatchAmount
uint256 _bondMatchAmount,
bool _asBase
) internal view returns (uint256 maturityTime, uint256 cost) {
// Get pool configuration.
IHyperdrive.PoolConfig memory config = _hyperdrive.getPoolConfig();
Expand Down Expand Up @@ -1453,6 +1458,26 @@ contract HyperdriveMatchingEngineV2 is
// NOTE: Round the governance fee calculation down to match other flows.
uint256 governanceFee = 2 * flatFee.mulDown(config.fees.governanceLP);
cost += governanceFee;

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

/// @dev Updates either the bond amount or fund amount used for a given order.
Expand Down
Loading