Skip to content

Commit

Permalink
Updated PoolInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
jalextowle committed Jul 27, 2023
1 parent 2324420 commit 966be7b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 71 deletions.
40 changes: 0 additions & 40 deletions contracts/src/HyperdriveBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -217,46 +217,6 @@ abstract contract HyperdriveBase is MultiToken, HyperdriveStorage {

/// Helpers ///

/// @dev Calculates the normalized time remaining of a position.
/// @param _maturityTime The maturity time of the position.
/// @return timeRemaining The normalized time remaining (in [0, 1]).
function _calculateTimeRemaining(
uint256 _maturityTime
) internal view returns (uint256 timeRemaining) {
uint256 latestCheckpoint = _latestCheckpoint();
timeRemaining = _maturityTime > latestCheckpoint
? _maturityTime - latestCheckpoint
: 0;
timeRemaining = (timeRemaining).divDown(_positionDuration);
}

/// @dev Calculates the normalized time remaining of a position when the
/// maturity time is scaled up 18 decimals.
/// @param _maturityTime The maturity time of the position.
function _calculateTimeRemainingScaled(
uint256 _maturityTime
) internal view returns (uint256 timeRemaining) {
uint256 latestCheckpoint = _latestCheckpoint() * FixedPointMath.ONE_18;
timeRemaining = _maturityTime > latestCheckpoint
? _maturityTime - latestCheckpoint
: 0;
timeRemaining = (timeRemaining).divDown(
_positionDuration * FixedPointMath.ONE_18
);
}

/// @dev Gets the most recent checkpoint time.
/// @return latestCheckpoint The latest checkpoint.
function _latestCheckpoint()
internal
view
returns (uint256 latestCheckpoint)
{
latestCheckpoint =
block.timestamp -
(block.timestamp % _checkpointDuration);
}

/// @dev Calculates the fees that go to the LPs and governance.
/// @param _amountIn Amount in shares.
/// @param _spotPrice The price without slippage of bonds in terms of base (base/bonds).
Expand Down
15 changes: 13 additions & 2 deletions contracts/src/HyperdriveDataProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HyperdriveStorage } from "./HyperdriveStorage.sol";
import { IHyperdrive } from "./interfaces/IHyperdrive.sol";
import { AssetId } from "./libraries/AssetId.sol";
import { FixedPointMath } from "./libraries/FixedPointMath.sol";
import { HyperdriveMath } from "./libraries/HyperdriveMath.sol";
import { MultiTokenDataProvider } from "./token/MultiTokenDataProvider.sol";

/// @author DELV
Expand Down Expand Up @@ -85,16 +86,26 @@ abstract contract HyperdriveDataProvider is
/// important to evaluate potential trades.
/// @return The PoolInfo struct.
function getPoolInfo() external view returns (IHyperdrive.PoolInfo memory) {
uint256 sharePrice = _pricePerShare();
uint256 lpTotalSupply = _totalSupply[AssetId._LP_ASSET_ID] +
_totalSupply[AssetId._WITHDRAWAL_SHARE_ASSET_ID] -
_withdrawPool.readyToWithdraw;
uint256 presentValue = HyperdriveMath
.calculatePresentValue(_getPresentValueParams(sharePrice))
.mulDown(sharePrice);
IHyperdrive.PoolInfo memory poolInfo = IHyperdrive.PoolInfo({
shareReserves: _marketState.shareReserves,
bondReserves: _marketState.bondReserves,
lpTotalSupply: _totalSupply[AssetId._LP_ASSET_ID],
sharePrice: _pricePerShare(),
sharePrice: sharePrice,
longsOutstanding: _marketState.longsOutstanding,
longAverageMaturityTime: _marketState.longAverageMaturityTime,
shortsOutstanding: _marketState.shortsOutstanding,
shortAverageMaturityTime: _marketState.shortAverageMaturityTime,
shortBaseVolume: _marketState.shortBaseVolume,
lpTotalSupply: lpTotalSupply,
lpSharePrice: lpTotalSupply == 0
? 0
: presentValue.divDown(lpTotalSupply),
withdrawalSharesReadyToWithdraw: _withdrawPool.readyToWithdraw,
withdrawalSharesProceeds: _withdrawPool.proceeds
});
Expand Down
29 changes: 0 additions & 29 deletions contracts/src/HyperdriveLP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -557,33 +557,4 @@ abstract contract HyperdriveLP is HyperdriveTWAP {
// Remove the withdrawal pool proceeds from the reserves.
_updateLiquidity(-int256(withdrawalPoolProceeds));
}

/// @dev Gets the present value parameters from the current state.
/// @param _sharePrice The current share price.
/// @return presentValue The present value parameters.
function _getPresentValueParams(
uint256 _sharePrice
)
internal
view
returns (HyperdriveMath.PresentValueParams memory presentValue)
{
presentValue = HyperdriveMath.PresentValueParams({
shareReserves: _marketState.shareReserves,
bondReserves: _marketState.bondReserves,
sharePrice: _sharePrice,
initialSharePrice: _initialSharePrice,
minimumShareReserves: _minimumShareReserves,
timeStretch: _timeStretch,
longsOutstanding: _marketState.longsOutstanding,
longAverageTimeRemaining: _calculateTimeRemainingScaled(
_marketState.longAverageMaturityTime
),
shortsOutstanding: _marketState.shortsOutstanding,
shortAverageTimeRemaining: _calculateTimeRemainingScaled(
_marketState.shortAverageMaturityTime
),
shortBaseVolume: _marketState.shortBaseVolume
});
}
}
75 changes: 75 additions & 0 deletions contracts/src/HyperdriveStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity 0.8.19;
import { ReentrancyGuard } from "solmate/utils/ReentrancyGuard.sol";
import { IERC20 } from "./interfaces/IERC20.sol";
import { IHyperdrive } from "./interfaces/IHyperdrive.sol";
import { FixedPointMath } from "./libraries/FixedPointMath.sol";
import { HyperdriveMath } from "./libraries/HyperdriveMath.sol";
import { MultiTokenStorage } from "./token/MultiTokenStorage.sol";

/// @author DELV
Expand All @@ -13,6 +15,8 @@ import { MultiTokenStorage } from "./token/MultiTokenStorage.sol";
/// only, and is not intended to, and does not, have any
/// particular legal or regulatory significance.
abstract contract HyperdriveStorage is ReentrancyGuard, MultiTokenStorage {
using FixedPointMath for uint256;

/// Tokens ///

/// @notice The base asset.
Expand Down Expand Up @@ -143,4 +147,75 @@ abstract contract HyperdriveStorage is ReentrancyGuard, MultiTokenStorage {
// Initialize the oracle.
_updateGap = _config.updateGap;
}

/// Helpers ///

/// @dev Calculates the normalized time remaining of a position.
/// @param _maturityTime The maturity time of the position.
/// @return timeRemaining The normalized time remaining (in [0, 1]).
function _calculateTimeRemaining(
uint256 _maturityTime
) internal view returns (uint256 timeRemaining) {
uint256 latestCheckpoint = _latestCheckpoint();
timeRemaining = _maturityTime > latestCheckpoint
? _maturityTime - latestCheckpoint
: 0;
timeRemaining = (timeRemaining).divDown(_positionDuration);
}

/// @dev Calculates the normalized time remaining of a position when the
/// maturity time is scaled up 18 decimals.
/// @param _maturityTime The maturity time of the position.
function _calculateTimeRemainingScaled(
uint256 _maturityTime
) internal view returns (uint256 timeRemaining) {
uint256 latestCheckpoint = _latestCheckpoint() * FixedPointMath.ONE_18;
timeRemaining = _maturityTime > latestCheckpoint
? _maturityTime - latestCheckpoint
: 0;
timeRemaining = (timeRemaining).divDown(
_positionDuration * FixedPointMath.ONE_18
);
}

/// @dev Gets the most recent checkpoint time.
/// @return latestCheckpoint The latest checkpoint.
function _latestCheckpoint()
internal
view
returns (uint256 latestCheckpoint)
{
latestCheckpoint =
block.timestamp -
(block.timestamp % _checkpointDuration);
}

/// @dev Gets the present value parameters from the current state.
/// @param _sharePrice The current share price.
/// @return presentValue The present value parameters.
function _getPresentValueParams(
uint256 _sharePrice
)
internal
view
returns (HyperdriveMath.PresentValueParams memory presentValue)
{
presentValue = HyperdriveMath.PresentValueParams({
shareReserves: _marketState.shareReserves,
bondReserves: _marketState.bondReserves,
sharePrice: _sharePrice,
initialSharePrice: _initialSharePrice,
minimumShareReserves: _minimumShareReserves,
timeStretch: _timeStretch,
longsOutstanding: _marketState.longsOutstanding,
longAverageTimeRemaining: _calculateTimeRemainingScaled(
_marketState.longAverageMaturityTime
),
shortsOutstanding: _marketState.shortsOutstanding,
shortAverageTimeRemaining: _calculateTimeRemainingScaled(
_marketState.shortAverageMaturityTime
),
shortBaseVolume: _marketState.shortBaseVolume
});
}
}
3 changes: 3 additions & 0 deletions contracts/src/interfaces/IHyperdrive.sol
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ interface IHyperdrive is IHyperdriveRead, IHyperdriveWrite, IMultiToken {
uint256 withdrawalSharesReadyToWithdraw;
/// @dev The proceeds recovered by the withdrawal pool.
uint256 withdrawalSharesProceeds;
/// @dev The share price of LP shares. This can be used to mark LP
/// shares to market.
uint256 lpSharePrice;
}

struct OracleState {
Expand Down

0 comments on commit 966be7b

Please sign in to comment.