Skip to content

Commit 7a51119

Browse files
authored
fix: close imbalance check (#606)
* fix: close imbalance check * fix: close imbalance check
1 parent a28d228 commit 7a51119

File tree

5 files changed

+21
-37
lines changed

5 files changed

+21
-37
lines changed

src/UsdnProtocol/libraries/UsdnProtocolActionsUtilsLibrary.sol

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,9 @@ library UsdnProtocolActionsUtilsLibrary {
202202
data_.tempPositionValue =
203203
_assetToRemove(balanceLong, data_.lastPrice, liqPriceWithoutPenalty, data_.totalExpoToClose);
204204

205-
uint128 priceAfterFees =
206-
(data_.lastPrice - data_.lastPrice * s._positionFeeBps / Constants.BPS_DIVISOR).toUint128();
207-
208-
uint256 posValueAfterFees =
209-
_assetToRemove(balanceLong, priceAfterFees, liqPriceWithoutPenalty, data_.totalExpoToClose);
210-
211-
// we perform the imbalance check with the position value after fees
212-
// the position value after fees is smaller than the position value before fees so the subtraction is safe
213-
_checkImbalanceLimitClose(
214-
s, data_.totalExpoToClose, posValueAfterFees, data_.tempPositionValue - posValueAfterFees
215-
);
205+
// we perform the imbalance check with the full position value subtracted from the long side, which is
206+
// representative of the state of the balances after this initiate action
207+
_checkImbalanceLimitClose(s, data_.totalExpoToClose, data_.tempPositionValue);
216208
}
217209

218210
/**
@@ -341,15 +333,12 @@ library UsdnProtocolActionsUtilsLibrary {
341333
* the close limit on the vault side, otherwise revert
342334
* @param s The storage of the protocol
343335
* @param posTotalExpoToClose The total expo to remove position
344-
* @param posValueToCloseAfterFees The value to remove from the position after the fees are applied
345-
* @param fees The fees applied to the position, going to the vault
336+
* @param posValueToClose The value to remove from the position (and the long balance)
346337
*/
347-
function _checkImbalanceLimitClose(
348-
Types.Storage storage s,
349-
uint256 posTotalExpoToClose,
350-
uint256 posValueToCloseAfterFees,
351-
uint256 fees
352-
) internal view {
338+
function _checkImbalanceLimitClose(Types.Storage storage s, uint256 posTotalExpoToClose, uint256 posValueToClose)
339+
internal
340+
view
341+
{
353342
int256 closeExpoImbalanceLimitBps;
354343
if (msg.sender == address(s._rebalancer)) {
355344
closeExpoImbalanceLimitBps = s._rebalancerCloseExpoImbalanceLimitBps;
@@ -362,9 +351,9 @@ library UsdnProtocolActionsUtilsLibrary {
362351
return;
363352
}
364353

365-
int256 newLongBalance = s._balanceLong.toInt256().safeSub(posValueToCloseAfterFees.toInt256());
354+
int256 newLongBalance = s._balanceLong.toInt256().safeSub(posValueToClose.toInt256());
366355
uint256 newTotalExpo = s._totalExpo - posTotalExpoToClose;
367-
int256 currentVaultExpo = s._balanceVault.toInt256().safeAdd(s._pendingBalanceVault + fees.toInt256());
356+
int256 currentVaultExpo = s._balanceVault.toInt256().safeAdd(s._pendingBalanceVault);
368357

369358
int256 imbalanceBps = Utils._calcImbalanceCloseBps(currentVaultExpo, newLongBalance, newTotalExpo);
370359

test/unit/UsdnProtocol/Actions/_ImbalanceLimitClose.t.sol

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ contract TestImbalanceLimitClose is UsdnProtocolBaseFixture {
2828
*/
2929
function test_checkImbalanceLimitClose() public view {
3030
(, uint256 longAmount, uint256 totalExpoValueToLimit) = _getCloseLimitValues(false);
31-
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit / 2, longAmount, 0);
31+
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit / 2, longAmount);
3232
}
3333

3434
/**
@@ -40,7 +40,7 @@ contract TestImbalanceLimitClose is UsdnProtocolBaseFixture {
4040
*/
4141
function test_checkImbalanceLimitCloseOnLimit() public view {
4242
(, uint256 longAmount, uint256 totalExpoValueToLimit) = _getCloseLimitValues(false);
43-
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit - 1, longAmount, 0);
43+
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit - 1, longAmount);
4444
}
4545

4646
/**
@@ -55,7 +55,7 @@ contract TestImbalanceLimitClose is UsdnProtocolBaseFixture {
5555
vm.expectRevert(
5656
abi.encodeWithSelector(IUsdnProtocolErrors.UsdnProtocolImbalanceLimitReached.selector, closeLimitBps)
5757
);
58-
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit, longAmount, 0);
58+
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit, longAmount);
5959
}
6060

6161
/**
@@ -75,7 +75,7 @@ contract TestImbalanceLimitClose is UsdnProtocolBaseFixture {
7575
abi.encodeWithSelector(IUsdnProtocolErrors.UsdnProtocolImbalanceLimitReached.selector, closeLimitBps)
7676
);
7777

78-
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit, longAmount, 0);
78+
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit, longAmount);
7979
}
8080

8181
/**
@@ -90,7 +90,7 @@ contract TestImbalanceLimitClose is UsdnProtocolBaseFixture {
9090
// disable close limit
9191
protocol.setExpoImbalanceLimits(200, 200, 600, 0, 0, 0);
9292

93-
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit + 1, longAmount, 0);
93+
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit + 1, longAmount);
9494
}
9595

9696
/**
@@ -149,7 +149,7 @@ contract TestImbalanceLimitClose is UsdnProtocolBaseFixture {
149149
vm.expectRevert(
150150
abi.encodeWithSelector(IUsdnProtocolErrors.UsdnProtocolImbalanceLimitReached.selector, type(int256).max)
151151
);
152-
protocol.i_checkImbalanceLimitClose(0, 0, 0);
152+
protocol.i_checkImbalanceLimitClose(0, 0);
153153
}
154154

155155
/**
@@ -174,7 +174,7 @@ contract TestImbalanceLimitClose is UsdnProtocolBaseFixture {
174174
IUsdnProtocolErrors.UsdnProtocolImbalanceLimitReached.selector, uint256(expectedImbalance)
175175
)
176176
);
177-
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit, longAmount, 0);
177+
protocol.i_checkImbalanceLimitClose(totalExpoValueToLimit, longAmount);
178178
}
179179

180180
/**

test/unit/UsdnProtocol/Actions/fuzzing/_ImbalanceLimitClose.fuzzing.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ contract TestImbalanceLimitCloseFuzzing is UsdnProtocolBaseFixture {
5454
);
5555
}
5656

57-
protocol.i_checkImbalanceLimitClose(totalExpoToRemove, closeAmount, 0);
57+
protocol.i_checkImbalanceLimitClose(totalExpoToRemove, closeAmount);
5858
}
5959
}

test/unit/UsdnProtocol/utils/Handler.sol

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,8 @@ contract UsdnProtocolHandler is UsdnProtocolImpl, Test {
404404
Long._checkImbalanceLimitOpen(s, openTotalExpoValue, openCollatValue);
405405
}
406406

407-
function i_checkImbalanceLimitClose(uint256 posTotalExpoToClose, uint256 posValueToClose, uint256 fees)
408-
external
409-
view
410-
{
411-
ActionsUtils._checkImbalanceLimitClose(s, posTotalExpoToClose, posValueToClose, fees);
407+
function i_checkImbalanceLimitClose(uint256 posTotalExpoToClose, uint256 posValueToClose) external view {
408+
ActionsUtils._checkImbalanceLimitClose(s, posTotalExpoToClose, posValueToClose);
412409
}
413410

414411
function i_getLeverage(uint128 price, uint128 liqPrice) external pure returns (uint256) {

test/utils/IUsdnProtocolHandler.sol

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ interface IUsdnProtocolHandler is IUsdnProtocol {
202202

203203
function i_checkImbalanceLimitOpen(uint256 openTotalExpoValue, uint256 openCollatValue) external view;
204204

205-
function i_checkImbalanceLimitClose(uint256 posTotalExpoToClose, uint256 posValueToCloseAfterFees, uint256 fees)
206-
external
207-
view;
205+
function i_checkImbalanceLimitClose(uint256 posTotalExpoToClose, uint256 posValueToClose) external view;
208206

209207
function i_getLeverage(uint128 price, uint128 liqPrice) external pure returns (uint256);
210208

0 commit comments

Comments
 (0)