From 2fc371f93e0e7f2b66dd21256e822da4024a69b1 Mon Sep 17 00:00:00 2001 From: padraic Date: Tue, 7 Feb 2023 18:47:20 +0000 Subject: [PATCH 1/2] Invert tstretch when using YieldSpaceMath --- contracts/libraries/HyperdriveMath.sol | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/contracts/libraries/HyperdriveMath.sol b/contracts/libraries/HyperdriveMath.sol index a7a506b58..941b65191 100644 --- a/contracts/libraries/HyperdriveMath.sol +++ b/contracts/libraries/HyperdriveMath.sol @@ -129,6 +129,9 @@ library HyperdriveMath { if (_isBondOut && _timeRemaining < 1) { revert Errors.HyperdriveMath_BaseWithNonzeroTime(); } + + uint256 oneDivT = FixedPointMath.ONE_18.divDown(_timeStretch); + if (_isBondOut) { // If bonds are being purchased, then the entire trade occurs on the // curved portion since t = 1. @@ -138,7 +141,7 @@ library HyperdriveMath { _bondReserveAdjustment, _amountIn, FixedPointMath.ONE_18, - _timeStretch, + oneDivT, _sharePrice, _initialSharePrice, _isBondOut @@ -164,7 +167,7 @@ library HyperdriveMath { _bondReserveAdjustment, curveIn, FixedPointMath.ONE_18, - _timeStretch, + oneDivT, _sharePrice, _initialSharePrice, _isBondOut @@ -221,6 +224,7 @@ library HyperdriveMath { .mulDown(FixedPointMath.ONE_18.sub(_timeRemaining)) .divDown(_sharePrice); uint256 curveOut = _amountOut.mulDown(_timeRemaining); + uint256 oneDivT = FixedPointMath.ONE_18.divDown(_timeStretch); uint256 curveIn = YieldSpaceMath.calculateInGivenOut( // Credit the share reserves by the flat trade. _shareReserves.add(flat.divDown(_sharePrice)), @@ -229,7 +233,7 @@ library HyperdriveMath { _bondReserveAdjustment, curveOut, FixedPointMath.ONE_18, - _timeStretch, + oneDivT, _sharePrice, _initialSharePrice, false From d2a1a90356554a5c626b4a8abd5c3c6d0787ca17 Mon Sep 17 00:00:00 2001 From: padraic Date: Tue, 7 Feb 2023 18:52:00 +0000 Subject: [PATCH 2/2] comment --- contracts/libraries/HyperdriveMath.sol | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contracts/libraries/HyperdriveMath.sol b/contracts/libraries/HyperdriveMath.sol index 941b65191..fa5eb08c2 100644 --- a/contracts/libraries/HyperdriveMath.sol +++ b/contracts/libraries/HyperdriveMath.sol @@ -130,6 +130,7 @@ library HyperdriveMath { revert Errors.HyperdriveMath_BaseWithNonzeroTime(); } + // 1/timestretch necessary for YieldSpaceMath uint256 oneDivT = FixedPointMath.ONE_18.divDown(_timeStretch); if (_isBondOut) { @@ -224,6 +225,7 @@ library HyperdriveMath { .mulDown(FixedPointMath.ONE_18.sub(_timeRemaining)) .divDown(_sharePrice); uint256 curveOut = _amountOut.mulDown(_timeRemaining); + // 1/timestretch necessary for YieldSpaceMath uint256 oneDivT = FixedPointMath.ONE_18.divDown(_timeStretch); uint256 curveIn = YieldSpaceMath.calculateInGivenOut( // Credit the share reserves by the flat trade.