-
Notifications
You must be signed in to change notification settings - Fork 7
Use two decimals for delayed scale factor #114
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
Open
nikeshnazareth
wants to merge
3
commits into
price-delayed-publications
Choose a base branch
from
delayed-publications/configurable-precision
base: price-delayed-publications
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.28; | ||
|
||
import "@openzeppelin/contracts/utils/math/SafeCast.sol"; | ||
|
||
library LibPercentage { | ||
using SafeCast for uint256; | ||
|
||
// COMMON PRECISION AMOUNTS (https://muens.io/solidity-percentages) | ||
uint256 constant BASIS_POINTS = 10_000; | ||
uint256 constant PERCENT = 100; | ||
|
||
/// @dev Calculates the percentage of a given value scaling by `precision` to limit rounding loss | ||
/// @param value The number to scale | ||
/// @param percentage The percentage expressed in `precision` units. | ||
/// @param precision The precision of `percentage` (e.g. percentage 5000 with BASIS_POINTS precision is 50%). | ||
/// @return _ The scaled value | ||
function scaleBy(uint256 value, uint16 percentage, uint256 precision) internal pure returns (uint96) { | ||
return (value * percentage / precision).toUint96(); | ||
} | ||
|
||
/// @dev Calculates the percentage (represented in basis points) of a given value | ||
/// @param value The number to scale | ||
/// @param percentage The percentage expressed in basis points | ||
/// @return _ The scaled value | ||
function scaleBy(uint256 value, uint16 percentage) internal pure returns (uint96) { | ||
return scaleBy(value, percentage, BASIS_POINTS); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ uint96 constant LIVENESS_BOND = 1 ether; | |
uint16 constant EVICTOR_INCENTIVE_PERCENTAGE = 500; // 5% | ||
uint16 constant REWARD_PERCENTAGE = 9000; // 90% | ||
uint96 constant INITIAL_FEE = 0.1 ether; | ||
uint16 constant DELAYED_FEE_PERCENTAGE = 15_000; // 150% | ||
uint16 constant DELAYED_FEE_PERCENTAGE = 150; // 150% | ||
uint256 constant INITIAL_PERIOD = 1; | ||
|
||
abstract contract BaseProverManagerTest is Test { | ||
|
@@ -218,7 +218,7 @@ abstract contract BaseProverManagerTest is Test { | |
// Capture current period stake before eviction | ||
BaseProverManager.Period memory periodBefore = proverManager.getPeriod(1); | ||
uint256 stakeBefore = periodBefore.stake; | ||
uint256 incentive = _calculatePercentage(stakeBefore, EVICTOR_INCENTIVE_PERCENTAGE); | ||
uint256 incentive = _calculatePercentageBPS(stakeBefore, EVICTOR_INCENTIVE_PERCENTAGE); | ||
|
||
// Evict the prover | ||
vm.warp(vm.getBlockTimestamp() + LIVENESS_WINDOW + 1); | ||
|
@@ -848,7 +848,7 @@ abstract contract BaseProverManagerTest is Test { | |
|
||
uint256 initialProverBalanceAfter = proverManager.balances(initialProver); | ||
uint256 prover1BalanceAfter = proverManager.balances(prover1); | ||
uint256 stakeReward = _calculatePercentage(stakeBefore, REWARD_PERCENTAGE); | ||
uint256 stakeReward = _calculatePercentageBPS(stakeBefore, REWARD_PERCENTAGE); | ||
assertEq(prover1BalanceAfter, prover1BalanceBefore + stakeReward, "Prover1 should receive the remaining stake"); | ||
assertEq(initialProverBalanceAfter, initialProverBalanceBefore, "Initial prover should receive nothing"); | ||
} | ||
|
@@ -974,13 +974,18 @@ abstract contract BaseProverManagerTest is Test { | |
function _deposit(address user, uint256 amount) internal virtual; | ||
|
||
function _maxAllowedFee(uint96 fee) internal pure returns (uint96) { | ||
return uint96(_calculatePercentage(fee, MAX_BID_PERCENTAGE)); | ||
return uint96(_calculatePercentageBPS(fee, MAX_BID_PERCENTAGE)); | ||
} | ||
|
||
function _calculatePercentage(uint256 amount, uint16 percentage) internal pure returns (uint256) { | ||
function _calculatePercentageBPS(uint256 amount, uint16 percentage) internal pure returns (uint256) { | ||
return amount * percentage / 10_000; | ||
} | ||
|
||
function _calculatePercentage(uint256 amount, uint16 percentage) internal pure returns (uint256) { | ||
return amount * percentage / 100; | ||
} | ||
|
||
|
||
Comment on lines
+980
to
+988
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the tests just use the library? I don't think we are trying to check if the library works properly on these tests(we can create a new unit test for |
||
function _exit(address prover) internal { | ||
vm.prank(prover); | ||
proverManager.exit(); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hesitant to name the two functions the same. it might be easy to get confused by anyone using the library.
What do you think about this approach?
scaleBy
remains the common function that takes aprecision
scaleByBps
is the bps implementationscaleByPercentage
is the percentage implementationIt is more verbose, but I believe it is less error prone