-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement worst case scenario for price algorithm v1 (#2219)
## Linked Issues/PRs Closes #2203 ## Description This PR adds the `worst_case()` calculation to the `V1` price algorithm. ## Checklist - [X] New behavior is reflected in tests ### Before requesting review - [X] I have reviewed the code myself --------- Co-authored-by: Mitchell Turner <james.mitchell.turner@gmail.com>
- Loading branch information
1 parent
79ca0d0
commit 43c1676
Showing
7 changed files
with
184 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
#![deny(clippy::cast_possible_truncation)] | ||
#![deny(warnings)] | ||
|
||
mod utils; | ||
pub mod v0; | ||
pub mod v1; |
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,20 @@ | ||
#[allow(clippy::cast_possible_truncation)] | ||
pub(crate) fn cumulative_percentage_change( | ||
new_exec_price: u64, | ||
for_height: u32, | ||
percentage: u64, | ||
height: u32, | ||
) -> u64 { | ||
let blocks = height.saturating_sub(for_height) as f64; | ||
let percentage_as_decimal = percentage as f64 / 100.0; | ||
let multiple = (1.0f64 + percentage_as_decimal).powf(blocks); | ||
let mut approx = new_exec_price as f64 * multiple; | ||
// account for rounding errors and take a slightly higher value | ||
const ROUNDING_ERROR_CUTOFF: f64 = 16948547188989277.0; | ||
if approx > ROUNDING_ERROR_CUTOFF { | ||
const ROUNDING_ERROR_COMPENSATION: f64 = 2000.0; | ||
approx += ROUNDING_ERROR_COMPENSATION; | ||
} | ||
// `f64` over `u64::MAX` are cast to `u64::MAX` | ||
approx.ceil() as u64 | ||
} |
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
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