Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/dynamic library rounding #106

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading