Median by Position (#533)

* use median price instead of the highest sustained

* add test for lexicographically reversing a byte slice

* fix pr comments

* fix CI fail

* fix dex tests

* Use a fuzz-tested list of prices

* Working median algorithm based on position + lints

---------

Co-authored-by: akildemir <aeg_asd@hotmail.com>
This commit is contained in:
Luke Parker
2024-02-19 20:50:04 -05:00
committed by GitHub
parent 34b93b882c
commit 6f5d794f10
12 changed files with 334 additions and 92 deletions

View File

@@ -50,7 +50,7 @@ use sp_runtime::{
use primitives::{PublicKey, AccountLookup, SubstrateAmount};
use support::{
traits::{ConstU8, ConstU32, ConstU64, Contains},
traits::{ConstU8, ConstU16, ConstU32, ConstU64, Contains},
weights::{
constants::{RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND},
IdentityFee, Weight,
@@ -124,6 +124,16 @@ pub const DAYS: BlockNumber = HOURS * 24;
pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
/// This needs to be long enough for arbitrage to occur and make holding any fake price up
/// sufficiently unrealistic.
#[allow(clippy::cast_possible_truncation)]
pub const ARBITRAGE_TIME: u16 = (2 * HOURS) as u16;
/// Since we use the median price, double the window length.
///
/// We additionally +1 so there is a true median.
pub const MEDIAN_PRICE_WINDOW_LENGTH: u16 = (2 * ARBITRAGE_TIME) + 1;
pub const BABE_GENESIS_EPOCH_CONFIG: sp_consensus_babe::BabeEpochConfiguration =
sp_consensus_babe::BabeEpochConfiguration {
c: PRIMARY_PROBABILITY,
@@ -246,6 +256,8 @@ impl dex::Config for Runtime {
type MaxSwapPathLength = ConstU32<3>; // coin1 -> SRI -> coin2
type MedianPriceWindowLength = ConstU16<{ MEDIAN_PRICE_WINDOW_LENGTH }>;
type WeightInfo = dex::weights::SubstrateWeight<Runtime>;
}