-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement parameters for liquidity tournament control (in funding)
Closes #5013. I also added a little utility type for representing percentages. Not strictly necessary, but I think it's a good move towards type safety. The motivation here was all of the annoyance of dealing with keeping track of what u64s were bps or bps^2 or yadda yadda yadda in other parts of the codebase.
- Loading branch information
1 parent
881cd26
commit f20af27
Showing
8 changed files
with
377 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,36 @@ | ||
/// Represents a percentage value. | ||
/// | ||
/// Useful for more robust typesafety, versus just passing around a `u64` which | ||
/// is merely *understood* to only contain values in [0, 100]. | ||
/// | ||
/// Defaults to 0%. | ||
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct Percentage(u64); | ||
|
||
impl Percentage { | ||
/// 0% | ||
pub const fn zero() -> Self { | ||
Self(0) | ||
} | ||
|
||
/// Convert this value into a `u64` in [0, 100]; | ||
pub const fn to_percent(self) -> u64 { | ||
self.0 | ||
} | ||
|
||
/// Given an arbitrary `u64`, produce a percentage, *saturating* at 100. | ||
pub fn from_percent(p: u64) -> Self { | ||
Self(u64::min(p.into(), 100)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_percentage_operations() { | ||
assert_eq!(Percentage::from_percent(101), Percentage::from_percent(100)); | ||
assert_eq!(Percentage::from_percent(48).to_percent(), 48); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.