Skip to content

Commit

Permalink
Renamings (#725)
Browse files Browse the repository at this point in the history
* Renamed `pool` => `vault`

* Renamed all variants of "share price" to "vault share price"

* Addressed review feedback from @jrhea

* Update contracts/src/instances/erc4626/ERC4626Base.sol

Co-authored-by: Jonny Rhea <[email protected]>

* Update contracts/src/instances/erc4626/ERC4626Base.sol

Co-authored-by: Jonny Rhea <[email protected]>

* Addressed issues after the merge

---------

Co-authored-by: Jonny Rhea <[email protected]>
  • Loading branch information
jalextowle and jrhea authored Jan 17, 2024
1 parent 10691c2 commit 2a8c81f
Show file tree
Hide file tree
Showing 83 changed files with 1,891 additions and 1,697 deletions.
10 changes: 5 additions & 5 deletions contracts/src/deployers/HyperdriveDeployerCoordinator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ abstract contract HyperdriveDeployerCoordinator is IHyperdriveDeployer {
bytes memory _extraData
) external override returns (address) {
// Convert the deploy config into the pool config and set the initial
// share price.
// vault share price.
IHyperdrive.PoolConfig memory _config = _copyPoolConfig(_deployConfig);
_config.initialSharePrice = _getInitialSharePrice(_extraData);
_config.initialVaultSharePrice = _getInitialVaultSharePrice(_extraData);

// Deploy the target0 contract.
address target0 = IHyperdriveTargetDeployer(target0Deployer).deploy(
Expand Down Expand Up @@ -93,10 +93,10 @@ abstract contract HyperdriveDeployerCoordinator is IHyperdriveDeployer {
);
}

/// @dev Gets the initial share price of the Hyperdrive pool.
/// @dev Gets the initial vault share price of the Hyperdrive pool.
/// @param _extraData The extra data passed to the child deployers.
/// @return The initial share price of the Hyperdrive pool.
function _getInitialSharePrice(
/// @return The initial vault share price of the Hyperdrive pool.
function _getInitialVaultSharePrice(
bytes memory _extraData
) internal view virtual returns (uint256);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract ERC4626HyperdriveCoreDeployer is IHyperdriveCoreDeployer {
address target3
) external override returns (address) {
// Deploy the ERC4626Hyperdrive instance.
address pool = abi.decode(_extraData, (address));
address vault = abi.decode(_extraData, (address));
return (
address(
new ERC4626Hyperdrive(
Expand All @@ -39,7 +39,7 @@ contract ERC4626HyperdriveCoreDeployer is IHyperdriveCoreDeployer {
target1,
target2,
target3,
IERC4626(pool)
IERC4626(vault)
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ contract ERC4626HyperdriveDeployerCoordinator is HyperdriveDeployerCoordinator {
)
{}

/// @dev Gets the initial share price of the Hyperdrive pool.
/// @dev Gets the initial vault share price of the Hyperdrive pool.
/// @param _extraData The extra data passed to the child deployers.
/// @return The initial share price of the Hyperdrive pool.
function _getInitialSharePrice(
/// @return The initial vault share price of the Hyperdrive pool.
function _getInitialVaultSharePrice(
bytes memory _extraData
) internal view override returns (uint256) {
// Return the vault's current share price.
IERC4626 pool = IERC4626(abi.decode(_extraData, (address)));
return pool.convertToAssets(ONE);
IERC4626 vault = IERC4626(abi.decode(_extraData, (address)));
return vault.convertToAssets(ONE);
}
}
4 changes: 2 additions & 2 deletions contracts/src/deployers/erc4626/ERC4626Target0Deployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ contract ERC4626Target0Deployer is IHyperdriveTargetDeployer {
bytes memory _extraData
) external override returns (address) {
// Deploy the ERC4626Target0 instance.
IERC4626 pool = IERC4626(abi.decode(_extraData, (address)));
return address(new ERC4626Target0(_config, pool));
IERC4626 vault = IERC4626(abi.decode(_extraData, (address)));
return address(new ERC4626Target0(_config, vault));
}
}
4 changes: 2 additions & 2 deletions contracts/src/deployers/erc4626/ERC4626Target1Deployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ contract ERC4626Target1Deployer is IHyperdriveTargetDeployer {
bytes memory _extraData
) external override returns (address) {
// Deploy the ERC4626Target1 instance.
IERC4626 pool = IERC4626(abi.decode(_extraData, (address)));
return address(new ERC4626Target1(_config, pool));
IERC4626 vault = IERC4626(abi.decode(_extraData, (address)));
return address(new ERC4626Target1(_config, vault));
}
}
4 changes: 2 additions & 2 deletions contracts/src/deployers/erc4626/ERC4626Target2Deployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ contract ERC4626Target2Deployer is IHyperdriveTargetDeployer {
bytes memory _extraData
) external override returns (address) {
// Deploy the ERC4626Target2 instance.
IERC4626 pool = IERC4626(abi.decode(_extraData, (address)));
return address(new ERC4626Target2(_config, pool));
IERC4626 vault = IERC4626(abi.decode(_extraData, (address)));
return address(new ERC4626Target2(_config, vault));
}
}
4 changes: 2 additions & 2 deletions contracts/src/deployers/erc4626/ERC4626Target3Deployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ contract ERC4626Target3Deployer is IHyperdriveTargetDeployer {
bytes memory _extraData
) external override returns (address) {
// Deploy the ERC4626Target3 instance.
IERC4626 pool = IERC4626(abi.decode(_extraData, (address)));
return address(new ERC4626Target3(_config, pool));
IERC4626 vault = IERC4626(abi.decode(_extraData, (address)));
return address(new ERC4626Target3(_config, vault));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ contract StETHHyperdriveDeployerCoordinator is HyperdriveDeployerCoordinator {
lido = _lido;
}

/// @dev Gets the initial share price of the Hyperdrive pool.
/// @return The initial share price of the Hyperdrive pool.
function _getInitialSharePrice(
/// @dev Gets the initial vault share price of the Hyperdrive pool.
/// @return The initial vault share price of the Hyperdrive pool.
function _getInitialVaultSharePrice(
bytes memory // unused extra data
) internal view override returns (uint256) {
// Return the stETH's current share price.
// Return stETH's current vault share price.
return lido.getPooledEthByShares(ONE);
}
}
12 changes: 6 additions & 6 deletions contracts/src/external/HyperdriveTarget0.sol
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ abstract contract HyperdriveTarget0 is
baseToken: _baseToken,
linkerFactory: _linkerFactory,
linkerCodeHash: _linkerCodeHash,
initialSharePrice: _initialSharePrice,
initialVaultSharePrice: _initialVaultSharePrice,
minimumShareReserves: _minimumShareReserves,
minimumTransactionAmount: _minimumTransactionAmount,
positionDuration: _positionDuration,
Expand All @@ -267,22 +267,22 @@ abstract contract HyperdriveTarget0 is
/// important to evaluate potential trades.
/// @return The PoolInfo struct.
function getPoolInfo() external view returns (IHyperdrive.PoolInfo memory) {
uint256 sharePrice = _pricePerShare();
uint256 vaultSharePrice = _pricePerVaultShare();
uint256 lpTotalSupply = _totalSupply[AssetId._LP_ASSET_ID] +
_totalSupply[AssetId._WITHDRAWAL_SHARE_ASSET_ID] -
_withdrawPool.readyToWithdraw;
uint256 presentValue = sharePrice > 0
uint256 presentValue = vaultSharePrice > 0
? LPMath
.calculatePresentValue(_getPresentValueParams(sharePrice))
.mulDown(sharePrice)
.calculatePresentValue(_getPresentValueParams(vaultSharePrice))
.mulDown(vaultSharePrice)
: 0;
IHyperdrive.PoolInfo memory poolInfo = IHyperdrive.PoolInfo({
shareReserves: _marketState.shareReserves,
shareAdjustment: _marketState.shareAdjustment,
zombieBaseProceeds: _marketState.zombieBaseProceeds,
zombieShareReserves: _marketState.zombieShareReserves,
bondReserves: _marketState.bondReserves,
sharePrice: sharePrice,
vaultSharePrice: vaultSharePrice,
longsOutstanding: _marketState.longsOutstanding,
longAverageMaturityTime: _marketState.longAverageMaturityTime,
shortsOutstanding: _marketState.shortsOutstanding,
Expand Down
14 changes: 8 additions & 6 deletions contracts/src/external/HyperdriveTarget2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract contract HyperdriveTarget2 is
/// @notice Opens a long position.
/// @param _baseAmount The amount of base to use when trading.
/// @param _minOutput The minium number of bonds to receive.
/// @param _minSharePrice The minium share price at which to open the long.
/// @param _minVaultSharePrice The minium share price at which to open the long.
/// This allows traders to protect themselves from opening a long in
/// a checkpoint where negative interest has accrued.
/// @param _options The options that configure how the trade is settled.
Expand All @@ -44,18 +44,19 @@ abstract contract HyperdriveTarget2 is
function openLong(
uint256 _baseAmount,
uint256 _minOutput,
uint256 _minSharePrice,
uint256 _minVaultSharePrice,
IHyperdrive.Options calldata _options
) external payable returns (uint256 maturityTime, uint256 bondProceeds) {
return _openLong(_baseAmount, _minOutput, _minSharePrice, _options);
return
_openLong(_baseAmount, _minOutput, _minVaultSharePrice, _options);
}

/// Shorts ///

/// @notice Opens a short position.
/// @param _bondAmount The amount of bonds to short.
/// @param _maxDeposit The most the user expects to deposit for this trade
/// @param _minSharePrice The minium share price at which to open the long.
/// @param _minVaultSharePrice The minium share price at which to open the long.
/// This allows traders to protect themselves from opening a long in
/// a checkpoint where negative interest has accrued.
/// @param _options The options that configure how the trade is settled.
Expand All @@ -64,9 +65,10 @@ abstract contract HyperdriveTarget2 is
function openShort(
uint256 _bondAmount,
uint256 _maxDeposit,
uint256 _minSharePrice,
uint256 _minVaultSharePrice,
IHyperdrive.Options calldata _options
) external payable returns (uint256 maturityTime, uint256 traderDeposit) {
return _openShort(_bondAmount, _maxDeposit, _minSharePrice, _options);
return
_openShort(_bondAmount, _maxDeposit, _minVaultSharePrice, _options);
}
}
40 changes: 22 additions & 18 deletions contracts/src/instances/erc4626/ERC4626Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ abstract contract ERC4626Base is HyperdriveBase {
using FixedPointMath for uint256;
using SafeTransferLib for ERC20;

/// @dev The yield source contract for this hyperdrive.
IERC4626 internal immutable _pool;
/// @dev The ERC4626 vault that this pool uses as a yield source.
IERC4626 internal immutable _vault;

/// @notice Instantiates the ERC4626 Hyperdrive base contract.
/// @param __pool The ERC4626 compatible yield source.
constructor(IERC4626 __pool) {
/// @param __vault The ERC4626 compatible vault.
constructor(IERC4626 __vault) {
// Initialize the pool immutable.
_pool = __pool;
_vault = __vault;
}

/// Yield Source ///
Expand All @@ -40,11 +40,15 @@ abstract contract ERC4626Base is HyperdriveBase {
/// used in this implementation is "asBase" which determines if
/// the deposit is settled in base or vault shares.
/// @return sharesMinted The shares this deposit creates.
/// @return sharePrice The share price at time of deposit.
/// @return vaultSharePrice The vault share price at time of deposit.
function _deposit(
uint256 _amount,
IHyperdrive.Options calldata _options
) internal override returns (uint256 sharesMinted, uint256 sharePrice) {
)
internal
override
returns (uint256 sharesMinted, uint256 vaultSharePrice)
{
if (_options.asBase) {
// Take custody of the deposit in base.
ERC20(address(_baseToken)).safeTransferFrom(
Expand All @@ -54,22 +58,22 @@ abstract contract ERC4626Base is HyperdriveBase {
);

// Deposit the base into the yield source.
ERC20(address(_baseToken)).safeApprove(address(_pool), _amount);
sharesMinted = _pool.deposit(_amount, address(this));
ERC20(address(_baseToken)).safeApprove(address(_vault), _amount);
sharesMinted = _vault.deposit(_amount, address(this));
} else {
// WARN: This logic doesn't account for slippage in the conversion
// from base to shares. If deposits to the yield source incur
// slippage, this logic will be incorrect.
sharesMinted = _amount;

// Take custody of the deposit in vault shares.
ERC20(address(_pool)).safeTransferFrom(
ERC20(address(_vault)).safeTransferFrom(
msg.sender,
address(this),
sharesMinted
);
}
sharePrice = _pricePerShare();
vaultSharePrice = _pricePerVaultShare();
}

/// @notice Processes a trader's withdrawal in either base or vault shares.
Expand All @@ -93,7 +97,7 @@ abstract contract ERC4626Base is HyperdriveBase {
// amount by converting the shares to base and then back to shares
// using the vault's share conversion logic.
uint256 baseAmount = _shares.mulDown(_sharePrice);
_shares = _pool.convertToShares(baseAmount);
_shares = _vault.convertToShares(baseAmount);

// If we're withdrawing zero shares, short circuit and return 0.
if (_shares == 0) {
Expand All @@ -105,7 +109,7 @@ abstract contract ERC4626Base is HyperdriveBase {
if (_options.asBase) {
// Redeem from the yield source and transfer the
// resulting base to the destination address.
amountWithdrawn = _pool.redeem(
amountWithdrawn = _vault.redeem(
_shares,
_options.destination,
address(this)
Expand All @@ -115,16 +119,16 @@ abstract contract ERC4626Base is HyperdriveBase {
// shares to the destination.
else {
// Transfer vault shares to the destination.
ERC20(address(_pool)).safeTransfer(_options.destination, _shares);
ERC20(address(_vault)).safeTransfer(_options.destination, _shares);
amountWithdrawn = _shares;
}
}

/// @notice Loads the share price from the yield source.
/// @return The current share price.
/// @notice Loads the vault share price from the yield source.
/// @return The current vault share price.
/// @dev must remain consistent with the impl inside of the DataProvider
function _pricePerShare() internal view override returns (uint256) {
return _pool.convertToAssets(ONE);
function _pricePerVaultShare() internal view override returns (uint256) {
return _vault.convertToAssets(ONE);
}

/// @dev Ensure that ether wasn't sent because ERC4626 vaults don't support
Expand Down
16 changes: 8 additions & 8 deletions contracts/src/instances/erc4626/ERC4626Hyperdrive.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,39 @@ contract ERC4626Hyperdrive is Hyperdrive, ERC4626Base {
/// @param _target1 The target1 address.
/// @param _target2 The target2 address.
/// @param _target3 The target3 address.
/// @param __pool The ERC4626 compatible yield source.
/// @param __vault The ERC4626 compatible yield source.
constructor(
IHyperdrive.PoolConfig memory _config,
address _target0,
address _target1,
address _target2,
address _target3,
IERC4626 __pool
IERC4626 __vault
)
Hyperdrive(_config, _target0, _target1, _target2, _target3)
ERC4626Base(__pool)
ERC4626Base(__vault)
{
// Ensure that the initial share price is properly configured.
// Ensure that the initial vault share price is properly configured.
//
// WARN: ERC4626 implementations should be checked that if they use an
// asset with decimals less than 18 that the preview deposit is scale
// invariant. EG - because this line uses a very large query to load
// price for USDC if the price per share changes based on size of
// price for USDC if the price per vault share changes based on size of
// deposit then this line will read an incorrect and possibly dangerous
// price.
if (_config.initialSharePrice != _pricePerShare()) {
if (_config.initialVaultSharePrice != _pricePerVaultShare()) {
revert IHyperdrive.InvalidInitialSharePrice();
}

// Ensure that the base token is the same as the vault's underlying
// asset.
if (address(_config.baseToken) != IERC4626(_pool).asset()) {
if (address(_config.baseToken) != IERC4626(_vault).asset()) {
revert IHyperdrive.InvalidBaseToken();
}

// Approve the base token with 1 wei. This ensures that all of the
// subsequent approvals will be writing to a dirty storage slot.
ERC20(address(_config.baseToken)).safeApprove(address(_pool), 1);
ERC20(address(_config.baseToken)).safeApprove(address(_vault), 1);
}

/// @notice Some yield sources [eg Morpho] pay rewards directly to this
Expand Down
Loading

0 comments on commit 2a8c81f

Please sign in to comment.