diff --git a/contracts/libraries/TradingLimits.sol b/contracts/libraries/TradingLimits.sol index 557d23a..e9c410d 100644 --- a/contracts/libraries/TradingLimits.sol +++ b/contracts/libraries/TradingLimits.sol @@ -172,7 +172,7 @@ library TradingLimits { */ function safeINT48Add(int48 a, int48 b) internal pure returns (int48) { int256 c = int256(a) + int256(b); - require(c >= -1 * MAX_INT48 && c <= MAX_INT48, "int48 addition overflow"); + require(c >= MIN_INT48 && c <= MAX_INT48, "int48 addition overflow"); return int48(c); } } diff --git a/test/unit/libraries/TradingLimits.t.sol b/test/unit/libraries/TradingLimits.t.sol index 66432d6..e9681f7 100644 --- a/test/unit/libraries/TradingLimits.t.sol +++ b/test/unit/libraries/TradingLimits.t.sol @@ -316,10 +316,23 @@ contract TradingLimitsTest is Test { function test_update_withOverflowOnAdd_reverts() public { ITradingLimits.Config memory config = configLG(int48(uint48(2 ** 47))); - int256 maxFlow = int256(uint256(type(uint48).max / 2)); + int256 maxFlow = int256(type(int48).max); state = harness.update(state, config, (maxFlow - 1000) * 1e18, 18); + state = harness.update(state, config, 1000 * 1e18, 18); + + vm.expectRevert(bytes("int48 addition overflow")); + state = harness.update(state, config, 1 * 1e18, 18); + } + + function test_update_withUnderflowOnAdd_reverts() public { + ITradingLimits.Config memory config = configLG(int48(uint48(2 ** 47))); + int256 minFlow = int256(type(int48).min); + + state = harness.update(state, config, (minFlow + 1000) * 1e18, 18); + state = harness.update(state, config, -1000 * 1e18, 18); + vm.expectRevert(bytes("int48 addition overflow")); - state = harness.update(state, config, 1002 * 10e18, 18); + state = harness.update(state, config, -1 * 1e18, 18); } }