@@ -135,21 +135,45 @@ library HyperdriveMath {
135135 /// flat+curve trades.
136136 /// @param _shareReserves The pool's share reserves.
137137 /// @param _shareAdjustment The pool's share adjustment.
138- /// @return The effective share reserves.
138+ /// @return effectiveShareReserves The effective share reserves.
139139 function calculateEffectiveShareReserves (
140140 uint256 _shareReserves ,
141141 int256 _shareAdjustment
142- ) internal pure returns (uint256 ) {
143- int256 effectiveShareReserves = _shareReserves.toInt256 () -
144- _shareAdjustment;
145- if (effectiveShareReserves < 0 ) {
142+ ) internal pure returns (uint256 effectiveShareReserves ) {
143+ bool success;
144+ (effectiveShareReserves, success) = calculateEffectiveShareReservesSafe (
145+ _shareReserves,
146+ _shareAdjustment
147+ );
148+ if (! success) {
146149 Errors.throwInsufficientLiquidityError (
147150 IHyperdrive
148151 .InsufficientLiquidityReason
149152 .InvalidEffectiveShareReserves
150153 );
151154 }
152- return uint256 (effectiveShareReserves);
155+ }
156+
157+ /// @dev Calculates the effective share reserves. The effective share
158+ /// reserves are the share reserves minus the share adjustment or
159+ /// z - zeta. We use the effective share reserves as the z-parameter
160+ /// to the YieldSpace pricing model. The share adjustment is used to
161+ /// hold the pricing mechanism invariant under the flat component of
162+ /// flat+curve trades.
163+ /// @param _shareReserves The pool's share reserves.
164+ /// @param _shareAdjustment The pool's share adjustment.
165+ /// @return The effective share reserves.
166+ /// @return A flag indicating if the calculation succeeded.
167+ function calculateEffectiveShareReservesSafe (
168+ uint256 _shareReserves ,
169+ int256 _shareAdjustment
170+ ) internal pure returns (uint256 , bool ) {
171+ int256 effectiveShareReserves = _shareReserves.toInt256 () -
172+ _shareAdjustment;
173+ if (effectiveShareReserves < 0 ) {
174+ return (0 , false );
175+ }
176+ return (uint256 (effectiveShareReserves), true );
153177 }
154178
155179 /// @dev Calculates the initial bond reserves assuming that the initial LP
0 commit comments