From 169d5c489fd7cf027cb537acdb0aa3c60162ace6 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 2 Jan 2024 18:34:31 +0000 Subject: [PATCH 1/5] Refactor open interest and oi shares reduction --- contracts/OverlayV1Market.sol | 43 ++++++++++++++++------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/contracts/OverlayV1Market.sol b/contracts/OverlayV1Market.sol index 6da34c96..f76f0afa 100644 --- a/contracts/OverlayV1Market.sol +++ b/contracts/OverlayV1Market.sol @@ -342,18 +342,7 @@ contract OverlayV1Market is IOverlayV1Market { // 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 @@ -444,18 +433,7 @@ contract OverlayV1Market is IOverlayV1Market { // 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)); @@ -748,6 +726,23 @@ contract OverlayV1Market is IOverlayV1Market { 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, oiTotalOnSide, oiTotalSharesOnSide) + ); + oiLongShares -= pos.oiSharesCurrent(fraction); + } else { + oiShort = oiShort.subFloor( + pos.oiCurrent(fraction, oiTotalOnSide, oiTotalSharesOnSide) + ); + 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 { From 788c5b1cc89d8fe96a611b734d6457414c0dd55d Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 2 Jan 2024 18:57:53 +0000 Subject: [PATCH 2/5] Refactor _reduceOIAndOIShares to include additional parameters --- contracts/OverlayV1Market.sol | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/contracts/OverlayV1Market.sol b/contracts/OverlayV1Market.sol index f76f0afa..7ec073f0 100644 --- a/contracts/OverlayV1Market.sol +++ b/contracts/OverlayV1Market.sol @@ -342,7 +342,7 @@ contract OverlayV1Market is IOverlayV1Market { // subtract unwound open interest from the side's aggregate oi value // and decrease number of oi shares issued - _reduceOIAndOIShares(pos, fraction); + _reduceOIAndOIShares(pos, fraction, oiTotalOnSide, oiTotalSharesOnSide); // register the amount to be minted/burned // capPayoff prevents overflow reverts with int256 cast @@ -433,7 +433,7 @@ contract OverlayV1Market is IOverlayV1Market { // subtract liquidated open interest from the side's aggregate oi value // and decrease number of oi shares issued - _reduceOIAndOIShares(pos, fraction); + _reduceOIAndOIShares(pos, fraction, oiTotalOnSide, oiTotalSharesOnSide); // register the amount to be burned _registerMintOrBurn(int256(value) - int256(cost) - int256(marginToBurn)); @@ -728,7 +728,12 @@ contract OverlayV1Market is IOverlayV1Market { /// @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 { + function _reduceOIAndOIShares( + Position.Info memory pos, + uint256 fraction, + uint256 oiTotalOnSide, + uint256 oiTotalSharesOnSide + ) internal { // NOTE: use subFloor to avoid reverts with oi rounding issues if (pos.isLong) { oiLong = oiLong.subFloor( From b161f87f239418081e9793cdbf01b3aa24694a29 Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 4 Jan 2024 00:25:09 +0000 Subject: [PATCH 3/5] Refactor _reduceOIAndOIShares function --- contracts/OverlayV1Market.sol | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/contracts/OverlayV1Market.sol b/contracts/OverlayV1Market.sol index 7ec073f0..b2a47d2e 100644 --- a/contracts/OverlayV1Market.sol +++ b/contracts/OverlayV1Market.sol @@ -342,7 +342,7 @@ contract OverlayV1Market is IOverlayV1Market { // subtract unwound open interest from the side's aggregate oi value // and decrease number of oi shares issued - _reduceOIAndOIShares(pos, fraction, oiTotalOnSide, oiTotalSharesOnSide); + _reduceOIAndOIShares(pos, fraction); // register the amount to be minted/burned // capPayoff prevents overflow reverts with int256 cast @@ -433,7 +433,7 @@ contract OverlayV1Market is IOverlayV1Market { // subtract liquidated open interest from the side's aggregate oi value // and decrease number of oi shares issued - _reduceOIAndOIShares(pos, fraction, oiTotalOnSide, oiTotalSharesOnSide); + _reduceOIAndOIShares(pos, fraction); // register the amount to be burned _registerMintOrBurn(int256(value) - int256(cost) - int256(marginToBurn)); @@ -731,19 +731,13 @@ contract OverlayV1Market is IOverlayV1Market { function _reduceOIAndOIShares( Position.Info memory pos, uint256 fraction, - uint256 oiTotalOnSide, - uint256 oiTotalSharesOnSide ) internal { // NOTE: use subFloor to avoid reverts with oi rounding issues if (pos.isLong) { - oiLong = oiLong.subFloor( - pos.oiCurrent(fraction, oiTotalOnSide, oiTotalSharesOnSide) - ); + oiLong = oiLong.subFloor(pos.oiCurrent(fraction, oiLong, oiLongShares)); oiLongShares -= pos.oiSharesCurrent(fraction); } else { - oiShort = oiShort.subFloor( - pos.oiCurrent(fraction, oiTotalOnSide, oiTotalSharesOnSide) - ); + oiShort = oiShort.subFloor(pos.oiCurrent(fraction, oiShort, oiShortShares)); oiShortShares -= pos.oiSharesCurrent(fraction); } } From b28099e41e1c4081306d63208d839b99a88fde98 Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 13 Jan 2024 14:18:09 +0000 Subject: [PATCH 4/5] Fix parameter formatting in _reduceOIAndOIShares function --- contracts/OverlayV1Market.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/OverlayV1Market.sol b/contracts/OverlayV1Market.sol index 6da52ea9..4dffcc75 100644 --- a/contracts/OverlayV1Market.sol +++ b/contracts/OverlayV1Market.sol @@ -731,7 +731,7 @@ contract OverlayV1Market is IOverlayV1Market, Pausable { /// @notice and decrease number of oi shares issued function _reduceOIAndOIShares( Position.Info memory pos, - uint256 fraction, + uint256 fraction ) internal { // NOTE: use subFloor to avoid reverts with oi rounding issues if (pos.isLong) { From b4e69a3b3f044f985e593d2b88e0125a6154dc5b Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 16 Jan 2024 20:20:05 +0000 Subject: [PATCH 5/5] Fix assert --- tests/markets/test_conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/markets/test_conftest.py b/tests/markets/test_conftest.py index 0c2d51ad..050a5bef 100644 --- a/tests/markets/test_conftest.py +++ b/tests/markets/test_conftest.py @@ -24,14 +24,14 @@ def test_feed_fixture(feed): def test_mock_feed_fixture(mock_feed): assert mock_feed.microWindow() == 600 - assert mock_feed.macroWindow() == 1500 + assert mock_feed.macroWindow() == 1800 assert mock_feed.price() == 1000000000000000000 assert mock_feed.reserve() == 2000000000000000000000000 def test_fake_feed_fixture(fake_feed): assert fake_feed.microWindow() == 600 - assert fake_feed.macroWindow() == 1500 + assert fake_feed.macroWindow() == 1800 assert fake_feed.price() == 1000000000000000000 assert fake_feed.reserve() == 2000000000000000000000000