diff --git a/src/lib/DynamicParamLib.sol b/src/lib/DynamicParamLib.sol index 33a1c115..365ce3db 100644 --- a/src/lib/DynamicParamLib.sol +++ b/src/lib/DynamicParamLib.sol @@ -53,6 +53,15 @@ library DynamicParamLib { uint256 timeDelta = updateEnd - block.timestamp; int256 delta = int256(target) - int256(param.lastComputedValue); int256 deltaPerSecond = delta / int256(timeDelta); + + int256 remainder = delta % int256(timeDelta); + + if (remainder > 0) { + param.lastComputedValue += uint256(remainder); + } else { + param.lastComputedValue -= uint256(-remainder); + } + param.updateEnd = updateEnd; param.updatePerSecond = deltaPerSecond; } diff --git a/test/lib/DynamicParamLib.t.sol b/test/lib/DynamicParamLib.t.sol index dea32839..bf5dd94c 100644 --- a/test/lib/DynamicParamLib.t.sol +++ b/test/lib/DynamicParamLib.t.sol @@ -100,7 +100,8 @@ contract DynamicParamLibTest is Test { function test_DynamicParamLib_set_SetsValueIncrease() public { initStoredParam(10, 0, 0, 0); storedParam.set(20, 10); - assertEq(storedParam.lastComputedValue, 10); + // immediately updates `lastComputedValue` to 11 + assertEq(storedParam.lastComputedValue, 11); assertEq(storedParam.updateEnd, 10); assertEq(storedParam.lastUpdateAt, block.timestamp); assertEq(storedParam.updatePerSecond, 1); @@ -109,7 +110,8 @@ contract DynamicParamLibTest is Test { function test_DynamicParamLib_set_SetsValueDecrease() public { initStoredParam(20, 0, 0, 0); storedParam.set(10, 10); - assertEq(storedParam.lastComputedValue, 20); + // immediately updates `lastComputedValue` to 19 + assertEq(storedParam.lastComputedValue, 19); assertEq(storedParam.updateEnd, 10); assertEq(storedParam.lastUpdateAt, block.timestamp); assertEq(storedParam.updatePerSecond, -1);