Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 18 additions & 29 deletions contracts/src/internal/HyperdriveBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,15 @@ abstract contract HyperdriveBase is IHyperdriveEvents, HyperdriveStorage {
// of longs and the amount of shorts with a given maturity time. If the
// difference is negative, the amount of non-netted longs is zero.
return
int256(
_totalSupply[
AssetId.encodeAssetId(
AssetId.AssetIdPrefix.Long,
_maturityTime
)
]
) -
int256(
_totalSupply[
AssetId.encodeAssetId(
AssetId.AssetIdPrefix.Short,
_maturityTime
)
]
);
_totalSupply[
AssetId.encodeAssetId(AssetId.AssetIdPrefix.Long, _maturityTime)
].toInt256() -
_totalSupply[
AssetId.encodeAssetId(
AssetId.AssetIdPrefix.Short,
_maturityTime
)
].toInt256();
}

/// @dev Gets the distribute excess idle parameters from the current state.
Expand Down Expand Up @@ -220,16 +213,14 @@ abstract contract HyperdriveBase is IHyperdriveEvents, HyperdriveStorage {

// NOTE: For consistency with the present value calculation, we round
// up the long side and round down the short side.
int256 netCurveTrade = int256(
presentValueParams.longsOutstanding.mulUp(
presentValueParams.longAverageTimeRemaining
)
) -
int256(
presentValueParams.shortsOutstanding.mulDown(
presentValueParams.shortAverageTimeRemaining
)
);
int256 netCurveTrade = presentValueParams
.longsOutstanding
.mulUp(presentValueParams.longAverageTimeRemaining)
.toInt256() -
presentValueParams
.shortsOutstanding
.mulDown(presentValueParams.shortAverageTimeRemaining)
.toInt256();
params = LPMath.DistributeExcessIdleParams({
presentValueParams: presentValueParams,
startingPresentValue: startingPresentValue,
Expand Down Expand Up @@ -428,9 +419,7 @@ abstract contract HyperdriveBase is IHyperdriveEvents, HyperdriveStorage {
// k invariant.
zombieInterestShares -= governanceZombieFeeCollected;
_marketState.shareReserves += zombieInterestShares.toUint128();
_marketState.shareAdjustment += int128(
zombieInterestShares.toUint128()
);
_marketState.shareAdjustment += zombieInterestShares.toInt128();

// After collecting the interest, the zombie base reserves are
// equal to the zombie base proceeds.
Expand Down
6 changes: 3 additions & 3 deletions contracts/src/internal/HyperdriveCheckpoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ abstract contract HyperdriveCheckpoint is
maturedShortsAmount,
0,
shareProceeds,
int256(shareProceeds), // keep the effective share reserves constant
shareProceeds.toInt256(), // keep the effective share reserves constant
_checkpointTime
);

Expand Down Expand Up @@ -194,7 +194,7 @@ abstract contract HyperdriveCheckpoint is
maturedLongsAmount,
0,
shareProceeds,
int256(shareProceeds), // keep the effective share reserves constant
shareProceeds.toInt256(), // keep the effective share reserves constant
checkpointTime
);

Expand All @@ -219,7 +219,7 @@ abstract contract HyperdriveCheckpoint is
// positions, we can reduce the long exposure for the matured
// checkpoint to zero.
_updateLongExposure(
int256(maturedLongsAmount) - int256(maturedShortsAmount),
maturedLongsAmount.toInt256() - maturedShortsAmount.toInt256(),
0
);

Expand Down
4 changes: 2 additions & 2 deletions contracts/src/internal/HyperdriveLP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ abstract contract HyperdriveLP is

// Add the liquidity to the pool's reserves and calculate the new
// present value.
_updateLiquidity(int256(shareContribution));
_updateLiquidity(shareContribution.toInt256());
params.shareReserves = _marketState.shareReserves;
params.shareAdjustment = _marketState.shareAdjustment;
params.bondReserves = _marketState.bondReserves;
Expand Down Expand Up @@ -525,7 +525,7 @@ abstract contract HyperdriveLP is
_withdrawPool.proceeds += shareProceeds.toUint128();

// Remove the withdrawal pool proceeds from the reserves.
_updateLiquidity(-int256(shareProceeds));
_updateLiquidity(-shareProceeds.toInt256());

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/src/internal/HyperdriveLong.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ abstract contract HyperdriveLong is IHyperdriveEvents, HyperdriveLP {
// number of non-netted longs decreases by the bond amount.
int256 nonNettedLongs = _nonNettedLongs(maturityTime);
_updateLongExposure(
nonNettedLongs + int256(_bondAmount),
nonNettedLongs + _bondAmount.toInt256(),
nonNettedLongs
);

Expand Down Expand Up @@ -297,7 +297,7 @@ abstract contract HyperdriveLong is IHyperdriveEvents, HyperdriveLP {
int256 nonNettedLongs = _nonNettedLongs(_maturityTime);
_updateLongExposure(
nonNettedLongs,
nonNettedLongs + int256(_bondReservesDelta)
nonNettedLongs + _bondReservesDelta.toInt256()
);

// We need to check solvency because longs increase the system's exposure.
Expand Down Expand Up @@ -353,7 +353,7 @@ abstract contract HyperdriveLong is IHyperdriveEvents, HyperdriveLP {
int256 shareAdjustment = _marketState.shareAdjustment;
shareAdjustment -= _shareAdjustmentDelta;
if (
int256(_shareReservesDelta) > _shareAdjustmentDelta &&
_shareReservesDelta.toInt256() > _shareAdjustmentDelta &&
HyperdriveMath.calculateEffectiveShareReserves(
shareReserves,
shareAdjustment
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/internal/HyperdriveShort.sol
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ abstract contract HyperdriveShort is IHyperdriveEvents, HyperdriveLP {
// number of non-netted longs increases by the bond amount.
int256 nonNettedLongs = _nonNettedLongs(_maturityTime);
_updateLongExposure(
nonNettedLongs - int256(_bondAmount),
nonNettedLongs - _bondAmount.toInt256(),
nonNettedLongs
);

Expand Down Expand Up @@ -350,7 +350,7 @@ abstract contract HyperdriveShort is IHyperdriveEvents, HyperdriveLP {
int256 nonNettedLongs = _nonNettedLongs(_maturityTime);
_updateLongExposure(
nonNettedLongs,
nonNettedLongs - int256(_bondAmount)
nonNettedLongs - _bondAmount.toInt256()
);

// Opening a short decreases the system's exposure because the short's
Expand Down
16 changes: 8 additions & 8 deletions contracts/src/libraries/FixedPointMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.20;

import { IHyperdrive } from "../interfaces/IHyperdrive.sol";
import { SafeCast } from "./SafeCast.sol";

uint256 constant ONE = 1e18;

Expand All @@ -13,6 +14,7 @@ uint256 constant ONE = 1e18;
/// particular legal or regulatory significance.
library FixedPointMath {
using FixedPointMath for uint256;
using SafeCast for uint256;

uint256 internal constant MAX_UINT256 = 2 ** 256 - 1;

Expand Down Expand Up @@ -112,11 +114,11 @@ library FixedPointMath {
// Using properties of logarithms we calculate x^y:
// -> ln(x^y) = y * ln(x)
// -> e^(y * ln(x)) = x^y
int256 y_int256 = int256(y); // solhint-disable-line var-name-mixedcase
int256 y_int256 = y.toInt256(); // solhint-disable-line var-name-mixedcase

// Compute y*ln(x)
// Any overflow for x will be caught in ln() in the initial bounds check
int256 lnx = ln(int256(x));
int256 lnx = ln(x.toInt256());
int256 ylnx;
assembly ("memory-safe") {
ylnx := mul(y_int256, lnx)
Expand Down Expand Up @@ -189,11 +191,9 @@ library FixedPointMath {
// * the 1e18 / 2**96 factor for base conversion.
// We do this all at once, with an intermediate result in 2**213
// basis, so the final right shift is always by a positive amount.
r = int256(
(uint256(r) *
3822833074963236453042738258902158003155416615667) >>
uint256(195 - k)
);
r = ((uint256(r) *
3822833074963236453042738258902158003155416615667) >>
uint256(195 - k)).toInt256();
}
}

Expand Down Expand Up @@ -228,7 +228,7 @@ library FixedPointMath {
// ln(2^k * x) = k * ln(2) + ln(x)
int256 k = r - 96;
x <<= uint256(159 - k);
x = int256(uint256(x) >> 159);
x = (uint256(x) >> 159).toInt256();

// Evaluate using a (8, 8)-term rational approximation.
// p is made monic, we will multiply by a scale factor later.
Expand Down
10 changes: 5 additions & 5 deletions contracts/src/libraries/HyperdriveMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ library HyperdriveMath {
uint256 _shareReserves,
int256 _shareAdjustment
) internal pure returns (uint256) {
int256 effectiveShareReserves = int256(_shareReserves) -
int256 effectiveShareReserves = _shareReserves.toInt256() -
_shareAdjustment;
if (effectiveShareReserves < 0) {
Errors.throwInsufficientLiquidityError(
Expand Down Expand Up @@ -664,8 +664,8 @@ library HyperdriveMath {
);
// NOTE: Using unscaled `shareCurveDelta`.
shareAdjustmentDelta =
int256(_shareReservesDelta) -
int256(_shareCurveDelta);
_shareReservesDelta.toInt256() -
_shareCurveDelta.toInt256();
_shareCurveDelta = _shareCurveDelta.mulDivDown(
_closeVaultSharePrice,
_openVaultSharePrice
Expand All @@ -676,8 +676,8 @@ library HyperdriveMath {
);
} else {
shareAdjustmentDelta =
int256(_shareReservesDelta) -
int256(_shareCurveDelta);
_shareReservesDelta.toInt256() -
_shareCurveDelta.toInt256();
}

return (
Expand Down
Loading