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
2 changes: 1 addition & 1 deletion contracts/src/external/Hyperdrive.sol
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ abstract contract Hyperdrive is
uint256,
IHyperdrive.Options calldata
) external payable returns (uint256) {
_delegate(target1);
_delegate(target3);
}

/// @inheritdoc IHyperdriveCore
Expand Down
7 changes: 7 additions & 0 deletions contracts/src/external/HyperdriveTarget0.sol
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,13 @@ abstract contract HyperdriveTarget0 is
_revert(abi.encode(_perTokenApprovals[tokenId][account][spender]));
}

/// @notice Gets the decimals of the MultiToken. This is the same as the
/// decimals used by the base token.
/// @return The decimals of the MultiToken.
function decimals() external view virtual returns (uint8) {
_revert(abi.encode(_baseToken.decimals()));
}

/// @notice Gets the name of a sub-token.
/// @param tokenId The sub-token id.
/// @return The name.
Expand Down
13 changes: 0 additions & 13 deletions contracts/src/external/HyperdriveTarget1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ abstract contract HyperdriveTarget1 is

/// LPs ///

/// @notice Allows the first LP to initialize the market with a target APR.
/// @param _contribution The amount of base to supply.
/// @param _apr The target APR.
/// @param _options The options that configure how the operation is settled.
/// @return lpShares The initial number of LP shares created.
function initialize(
uint256 _contribution,
uint256 _apr,
IHyperdrive.Options calldata _options
) external payable returns (uint256 lpShares) {
return _initialize(_contribution, _apr, _options);
}

/// @notice Allows LPs to supply liquidity for LP shares.
/// @param _contribution The amount of base to supply.
/// @param _minLpSharePrice The minimum LP share price the LP is willing
Expand Down
15 changes: 15 additions & 0 deletions contracts/src/external/HyperdriveTarget3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ abstract contract HyperdriveTarget3 is
IHyperdrive.PoolConfig memory _config
) HyperdriveStorage(_config) {}

/// LPs ///

/// @notice Allows the first LP to initialize the market with a target APR.
/// @param _contribution The amount of base to supply.
/// @param _apr The target APR.
/// @param _options The options that configure how the operation is settled.
/// @return lpShares The initial number of LP shares created.
function initialize(
uint256 _contribution,
uint256 _apr,
IHyperdrive.Options calldata _options
) external payable returns (uint256 lpShares) {
return _initialize(_contribution, _apr, _options);
}

/// Longs ///

/// @notice Opens a long position.
Expand Down
6 changes: 6 additions & 0 deletions contracts/src/instances/steth/StETHTarget0.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ contract StETHTarget0 is HyperdriveTarget0, StETHBase {
function lido() external view returns (ILido) {
_revert(abi.encode(_lido));
}

/// @notice Returns the MultiToken's decimals.
/// @return The MultiToken's decimals.
function decimals() external pure override returns (uint8) {
_revert(abi.encode(uint8(18)));
}
}
24 changes: 16 additions & 8 deletions contracts/src/interfaces/IHyperdriveEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ interface IHyperdriveEvents is IMultiTokenEvents {
address indexed provider,
uint256 lpAmount,
uint256 baseAmount,
uint256 vaultSharePrice,
uint256 vaultShareAmount,
bool asBase,
uint256 apr
);

Expand All @@ -18,7 +19,8 @@ interface IHyperdriveEvents is IMultiTokenEvents {
address indexed provider,
uint256 lpAmount,
uint256 baseAmount,
uint256 vaultSharePrice,
uint256 vaultShareAmount,
bool asBase,
uint256 lpSharePrice
);

Expand All @@ -27,7 +29,8 @@ interface IHyperdriveEvents is IMultiTokenEvents {
address indexed provider,
uint256 lpAmount,
uint256 baseAmount,
uint256 vaultSharePrice,
uint256 vaultShareAmount,
bool asBase,
uint256 withdrawalShareAmount,
uint256 lpSharePrice
);
Expand All @@ -37,7 +40,8 @@ interface IHyperdriveEvents is IMultiTokenEvents {
address indexed provider,
uint256 withdrawalShareAmount,
uint256 baseAmount,
uint256 vaultSharePrice
uint256 vaultShareAmount,
bool asBase
);

/// @notice Emitted when a long position is opened.
Expand All @@ -46,7 +50,8 @@ interface IHyperdriveEvents is IMultiTokenEvents {
uint256 indexed assetId,
uint256 maturityTime,
uint256 baseAmount,
uint256 vaultSharePrice,
uint256 vaultShareAmount,
bool asBase,
uint256 bondAmount
);

Expand All @@ -56,7 +61,8 @@ interface IHyperdriveEvents is IMultiTokenEvents {
uint256 indexed assetId,
uint256 maturityTime,
uint256 baseAmount,
uint256 vaultSharePrice,
uint256 vaultShareAmount,
bool asBase,
uint256 bondAmount
);

Expand All @@ -66,7 +72,8 @@ interface IHyperdriveEvents is IMultiTokenEvents {
uint256 indexed assetId,
uint256 maturityTime,
uint256 baseAmount,
uint256 vaultSharePrice,
uint256 vaultShareAmount,
bool asBase,
uint256 bondAmount
);

Expand All @@ -76,7 +83,8 @@ interface IHyperdriveEvents is IMultiTokenEvents {
uint256 indexed assetId,
uint256 maturityTime,
uint256 baseAmount,
uint256 vaultSharePrice,
uint256 vaultShareAmount,
bool asBase,
uint256 bondAmount
);

Expand Down
4 changes: 4 additions & 0 deletions contracts/src/interfaces/IMultiTokenRead.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
pragma solidity 0.8.20;

interface IMultiTokenRead {
/// @notice Gets the decimals of the MultiToken.
/// @return The decimals of the MultiToken.
function decimals() external view returns (uint8);

/// @notice Gets the name of the MultiToken.
/// @return The name of the MultiToken.
function name(uint256 id) external view returns (string memory);
Expand Down
19 changes: 19 additions & 0 deletions contracts/src/internal/HyperdriveBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,25 @@ abstract contract HyperdriveBase is IHyperdriveEvents, HyperdriveStorage {
}
}

/// @dev Converts input to vault shares if necessary according to what is
/// specified in options.
/// @param _amount The amount to convert.
/// @param _vaultSharePrice The current vault share price.
/// @param _options The options that configure the conversion.
/// @return The converted amount.
function _convertToVaultSharesFromOption(
uint256 _amount,
uint256 _vaultSharePrice,
IHyperdrive.Options calldata _options
) internal pure returns (uint256) {
if (_options.asBase) {
// NOTE: Round down to underestimate the vault share amount.
return _amount.divDown(_vaultSharePrice);
} else {
return _amount;
}
}

/// @dev Converts input to what is specified in the options from base.
/// @param _amount The amount to convert.
/// @param _vaultSharePrice The current vault share price.
Expand Down
34 changes: 18 additions & 16 deletions contracts/src/internal/HyperdriveLP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ abstract contract HyperdriveLP is
_options.destination,
lpShares,
baseContribution,
vaultSharePrice,
vaultShares,
_options.asBase,
_apr
);

Expand Down Expand Up @@ -239,7 +240,8 @@ abstract contract HyperdriveLP is
_options.destination,
lpShares,
baseContribution,
vaultSharePrice,
vaultShares,
_options.asBase,
lpSharePrice
);
}
Expand Down Expand Up @@ -297,16 +299,16 @@ abstract contract HyperdriveLP is
withdrawalShares = _lpShares - withdrawalSharesRedeemed;

// Emit a RemoveLiquidity event.
uint256 baseProceeds = _convertToBaseFromOption(
proceeds,
vaultSharePrice,
_options
);
emit RemoveLiquidity(
_options.destination,
_lpShares,
baseProceeds,
vaultSharePrice,
_convertToBaseFromOption(proceeds, vaultSharePrice, _options),
_convertToVaultSharesFromOption(
proceeds,
vaultSharePrice,
_options
),
_options.asBase,
uint256(withdrawalShares),
_calculateLPSharePrice(vaultSharePrice)
);
Expand Down Expand Up @@ -351,16 +353,16 @@ abstract contract HyperdriveLP is
);

// Emit a RedeemWithdrawalShares event.
uint256 baseProceeds = _convertToBaseFromOption(
proceeds,
vaultSharePrice,
_options
);
emit RedeemWithdrawalShares(
_options.destination,
withdrawalSharesRedeemed,
baseProceeds,
vaultSharePrice
_convertToBaseFromOption(proceeds, vaultSharePrice, _options),
_convertToVaultSharesFromOption(
proceeds,
vaultSharePrice,
_options
),
_options.asBase
);

return (proceeds, withdrawalSharesRedeemed);
Expand Down
28 changes: 19 additions & 9 deletions contracts/src/internal/HyperdriveLong.sol
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,21 @@ abstract contract HyperdriveLong is IHyperdriveEvents, HyperdriveLP {

// Emit an OpenLong event.
uint256 amount = _amount; // Avoid stack too deep error.
uint256 maturityTime_ = maturityTime; // Avoid stack too deep error.
uint256 bondProceeds_ = bondProceeds; // Avoid stack too deep error.
uint256 vaultSharePrice_ = vaultSharePrice; // Avoid stack too deep error.
IHyperdrive.Options calldata options = _options; // Avoid stack too deep error.
uint256 _bondProceeds = bondProceeds; // Avoid stack too deep error.
emit OpenLong(
_options.destination,
options.destination,
assetId,
maturityTime,
_convertToBaseFromOption(amount, vaultSharePrice, options),
vaultSharePrice,
_bondProceeds
maturityTime_,
_convertToBaseFromOption(amount, vaultSharePrice_, options),
_convertToVaultSharesFromOption(amount, vaultSharePrice_, options),
options.asBase,
bondProceeds_
);

return (maturityTime, _bondProceeds);
return (maturityTime, bondProceeds_);
}

/// @dev Closes a long position with a specified maturity time.
Expand Down Expand Up @@ -218,12 +221,19 @@ abstract contract HyperdriveLong is IHyperdriveEvents, HyperdriveLP {

// Emit a CloseLong event.
uint256 bondAmount = _bondAmount; // Avoid stack too deep error.
uint256 vaultSharePrice_ = vaultSharePrice; // Avoid stack too deep error.
IHyperdrive.Options calldata options = _options; // Avoid stack too deep error.
emit CloseLong(
_options.destination,
options.destination,
AssetId.encodeAssetId(AssetId.AssetIdPrefix.Long, maturityTime),
maturityTime,
baseProceeds,
vaultSharePrice,
_convertToVaultSharesFromOption(
proceeds,
vaultSharePrice_,
options
),
options.asBase,
bondAmount
);

Expand Down
17 changes: 13 additions & 4 deletions contracts/src/internal/HyperdriveShort.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ abstract contract HyperdriveShort is IHyperdriveEvents, HyperdriveLP {
_mint(assetId, _options.destination, bondAmount);

// Emit an OpenShort event.
IHyperdrive.Options calldata options = _options;
emit OpenShort(
_options.destination,
options.destination,
assetId,
maturityTime,
baseDeposit,
vaultSharePrice,
baseDeposit.divDown(vaultSharePrice),
options.asBase,
bondAmount
);

Expand Down Expand Up @@ -223,12 +225,19 @@ abstract contract HyperdriveShort is IHyperdriveEvents, HyperdriveLP {
// Emit a CloseShort event.
uint256 bondAmount = _bondAmount; // Avoid stack too deep error.
uint256 maturityTime = _maturityTime; // Avoid stack too deep error.
uint256 vaultSharePrice_ = vaultSharePrice; // Avoid stack too deep error.
IHyperdrive.Options calldata options = _options; // Avoid stack too deep error.
emit CloseShort(
_options.destination,
options.destination,
AssetId.encodeAssetId(AssetId.AssetIdPrefix.Short, maturityTime),
maturityTime,
baseProceeds,
vaultSharePrice,
_convertToVaultSharesFromOption(
proceeds,
vaultSharePrice_,
options
),
options.asBase,
bondAmount
);

Expand Down
6 changes: 5 additions & 1 deletion test/instances/erc4626/ERC4626Validation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,11 @@ abstract contract ERC4626ValidationTest is HyperdriveTest {
bytes32(uint256(0xfade))
);

// Ensure minimumShareReserves were added, and lpTotalSupply increased
// Ensure that the decimals are set correctly.
assertEq(hyperdrive.decimals(), decimals);

// Ensure that the minimum share reserves was successfully capitalized
// and that the LP total supply was updated to the correct value.
assertEq(
hyperdrive.getPoolInfo().lpTotalSupply,
hyperdrive.getPoolInfo().shareReserves - config.minimumShareReserves
Expand Down
3 changes: 3 additions & 0 deletions test/instances/steth/StETHHyperdrive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ contract StETHHyperdriveTest is HyperdriveTest {
);
assertEq(address(bob).balance, bobBalanceBefore - contribution);

// Ensure that the decimals are set correctly.
assertEq(hyperdrive.decimals(), 18);

// Ensure that Bob received the correct amount of LP tokens. He should
// receive LP shares totaling the amount of shares that he contributed
// minus the shares set aside for the minimum share reserves and the
Expand Down
16 changes: 13 additions & 3 deletions test/units/hyperdrive/CloseLongTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -910,12 +910,22 @@ contract CloseLongTest is HyperdriveTest {
(
uint256 eventMaturityTime,
uint256 eventBaseAmount,
uint256 eventSharePrice,
uint256 eventVaultShareAmount,
bool eventAsBase,
uint256 eventBondAmount
) = abi.decode(log.data, (uint256, uint256, uint256, uint256));
) = abi.decode(
log.data,
(uint256, uint256, uint256, bool, uint256)
);
assertEq(eventMaturityTime, testCase.maturityTime);
assertEq(eventBaseAmount, testCase.baseProceeds);
assertEq(eventSharePrice, hyperdrive.getPoolInfo().vaultSharePrice);
assertEq(
eventVaultShareAmount,
testCase.baseProceeds.divDown(
hyperdrive.getPoolInfo().vaultSharePrice
)
);
assertEq(eventAsBase, true);
assertEq(eventBondAmount, testCase.bondAmount);
}

Expand Down
Loading