Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor open interest and oi shares reduction #120

Merged
merged 12 commits into from
Jan 18, 2024
42 changes: 18 additions & 24 deletions contracts/OverlayV1Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,7 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {

// subtract unwound open interest from the side's aggregate oi value
// and decrease number of oi shares issued
// NOTE: use subFloor to avoid reverts with oi rounding issues
if (pos.isLong) {
oiLong = oiLong.subFloor(
pos.oiCurrent(fraction, oiTotalOnSide, oiTotalSharesOnSide)
);
oiLongShares -= pos.oiSharesCurrent(fraction);
} else {
oiShort = oiShort.subFloor(
pos.oiCurrent(fraction, oiTotalOnSide, oiTotalSharesOnSide)
);
oiShortShares -= pos.oiSharesCurrent(fraction);
}
_reduceOIAndOIShares(pos, fraction);

// register the amount to be minted/burned
// capPayoff prevents overflow reverts with int256 cast
Expand Down Expand Up @@ -445,18 +434,7 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {

// subtract liquidated open interest from the side's aggregate oi value
// and decrease number of oi shares issued
// NOTE: use subFloor to avoid reverts with oi rounding issues
if (pos.isLong) {
oiLong = oiLong.subFloor(
pos.oiCurrent(fraction, oiTotalOnSide, oiTotalSharesOnSide)
);
oiLongShares -= pos.oiSharesCurrent(fraction);
} else {
oiShort = oiShort.subFloor(
pos.oiCurrent(fraction, oiTotalOnSide, oiTotalSharesOnSide)
);
oiShortShares -= pos.oiSharesCurrent(fraction);
}
_reduceOIAndOIShares(pos, fraction);

// register the amount to be burned
_registerMintOrBurn(int256(value) - int256(cost) - int256(marginToBurn));
Expand Down Expand Up @@ -749,6 +727,22 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
return minted;
}

/// @notice subtract open interest from the side's aggregate oi value
/// @notice and decrease number of oi shares issued
function _reduceOIAndOIShares(
Position.Info memory pos,
uint256 fraction
) internal {
// NOTE: use subFloor to avoid reverts with oi rounding issues
if (pos.isLong) {
oiLong = oiLong.subFloor(pos.oiCurrent(fraction, oiLong, oiLongShares));
oiLongShares -= pos.oiSharesCurrent(fraction);
} else {
oiShort = oiShort.subFloor(pos.oiCurrent(fraction, oiShort, oiShortShares));
oiShortShares -= pos.oiSharesCurrent(fraction);
}
}

/// @notice Updates the market for funding changes to open interest
/// @notice since last time market was interacted with
function _payFunding() private {
Expand Down