Skip to content

Commit

Permalink
Merge pull request #106 from primitivefinance/fix/dynamic-library-rou…
Browse files Browse the repository at this point in the history
…nding

Fix/dynamic library rounding
  • Loading branch information
clemlak authored Apr 2, 2024
2 parents f597df3 + 4fd811a commit bf3c7bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/lib/DynamicParamLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions test/lib/DynamicParamLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit bf3c7bf

Please sign in to comment.