@@ -4,6 +4,8 @@ pragma solidity 0.8.19;
44import { ReentrancyGuard } from "solmate/utils/ReentrancyGuard.sol " ;
55import { IERC20 } from "./interfaces/IERC20.sol " ;
66import { IHyperdrive } from "./interfaces/IHyperdrive.sol " ;
7+ import { FixedPointMath } from "./libraries/FixedPointMath.sol " ;
8+ import { HyperdriveMath } from "./libraries/HyperdriveMath.sol " ;
79import { MultiTokenStorage } from "./token/MultiTokenStorage.sol " ;
810
911/// @author DELV
@@ -13,6 +15,8 @@ import { MultiTokenStorage } from "./token/MultiTokenStorage.sol";
1315/// only, and is not intended to, and does not, have any
1416/// particular legal or regulatory significance.
1517abstract contract HyperdriveStorage is ReentrancyGuard , MultiTokenStorage {
18+ using FixedPointMath for uint256 ;
19+
1620 /// Tokens ///
1721
1822 /// @notice The base asset.
@@ -143,4 +147,75 @@ abstract contract HyperdriveStorage is ReentrancyGuard, MultiTokenStorage {
143147 // Initialize the oracle.
144148 _updateGap = _config.updateGap;
145149 }
150+
151+ /// Helpers ///
152+
153+ /// @dev Calculates the normalized time remaining of a position.
154+ /// @param _maturityTime The maturity time of the position.
155+ /// @return timeRemaining The normalized time remaining (in [0, 1]).
156+ function _calculateTimeRemaining (
157+ uint256 _maturityTime
158+ ) internal view returns (uint256 timeRemaining ) {
159+ uint256 latestCheckpoint = _latestCheckpoint ();
160+ timeRemaining = _maturityTime > latestCheckpoint
161+ ? _maturityTime - latestCheckpoint
162+ : 0 ;
163+ timeRemaining = (timeRemaining).divDown (_positionDuration);
164+ }
165+
166+ /// @dev Calculates the normalized time remaining of a position when the
167+ /// maturity time is scaled up 18 decimals.
168+ /// @param _maturityTime The maturity time of the position.
169+ function _calculateTimeRemainingScaled (
170+ uint256 _maturityTime
171+ ) internal view returns (uint256 timeRemaining ) {
172+ uint256 latestCheckpoint = _latestCheckpoint () * FixedPointMath.ONE_18;
173+ timeRemaining = _maturityTime > latestCheckpoint
174+ ? _maturityTime - latestCheckpoint
175+ : 0 ;
176+ timeRemaining = (timeRemaining).divDown (
177+ _positionDuration * FixedPointMath.ONE_18
178+ );
179+ }
180+
181+ /// @dev Gets the most recent checkpoint time.
182+ /// @return latestCheckpoint The latest checkpoint.
183+ function _latestCheckpoint ()
184+ internal
185+ view
186+ returns (uint256 latestCheckpoint )
187+ {
188+ latestCheckpoint =
189+ block .timestamp -
190+ (block .timestamp % _checkpointDuration);
191+ }
192+
193+ /// @dev Gets the present value parameters from the current state.
194+ /// @param _sharePrice The current share price.
195+ /// @return presentValue The present value parameters.
196+ function _getPresentValueParams (
197+ uint256 _sharePrice
198+ )
199+ internal
200+ view
201+ returns (HyperdriveMath.PresentValueParams memory presentValue )
202+ {
203+ presentValue = HyperdriveMath.PresentValueParams ({
204+ shareReserves: _marketState.shareReserves,
205+ bondReserves: _marketState.bondReserves,
206+ sharePrice: _sharePrice,
207+ initialSharePrice: _initialSharePrice,
208+ minimumShareReserves: _minimumShareReserves,
209+ timeStretch: _timeStretch,
210+ longsOutstanding: _marketState.longsOutstanding,
211+ longAverageTimeRemaining: _calculateTimeRemainingScaled (
212+ _marketState.longAverageMaturityTime
213+ ),
214+ shortsOutstanding: _marketState.shortsOutstanding,
215+ shortAverageTimeRemaining: _calculateTimeRemainingScaled (
216+ _marketState.shortAverageMaturityTime
217+ ),
218+ shortBaseVolume: _marketState.shortBaseVolume
219+ });
220+ }
146221}
0 commit comments