diff --git a/src/pricing/pricing_utils.cairo b/src/pricing/pricing_utils.cairo index 9dca8069..f3641ed9 100644 --- a/src/pricing/pricing_utils.cairo +++ b/src/pricing/pricing_utils.cairo @@ -1,5 +1,4 @@ //! Library for pricing functions. - // ************************************************************************* // IMPORTS // ************************************************************************* diff --git a/src/utils/precision.cairo b/src/utils/precision.cairo index da19ba88..758f1d1a 100644 --- a/src/utils/precision.cairo +++ b/src/utils/precision.cairo @@ -2,7 +2,6 @@ // IMPORTS // ************************************************************************* // Core lib imports. -use alexandria_math::pow; use integer::{ u128_to_felt252, u256_wide_mul, u512_safe_div_rem_by_u256, BoundedU256, u256_try_as_non_zero }; @@ -17,7 +16,7 @@ const FLOAT_PRECISION_SQRT: u128 = 10_000_000_000; // 10^10 const WEI_PRECISION: u128 = 1_000_000_000_000_000_000; // 10^18 const BASIS_POINTS_DIVISOR: u128 = 10000; -const FLOAT_TO_WEI_DIVISOR: u128 = 1_000_000_000_000; // 10^12 +const FLOAT_TO_WEI_DIVISOR: u128 = 10_000_000_000_000_000; // 10^16 /// Applies the given factor to the given value and returns the result. /// # Arguments @@ -156,17 +155,6 @@ fn mul_div_roundup( /// * `value` - The value to the exponent is applied to. /// * `divisor` - The exponent applied. fn apply_exponent_factor(float_value: u128, exponent_factor: u128) -> u128 { // TODO - // if float_value < FLOAT_PRECISION { - // return 0; - // } - // if exponent_factor == FLOAT_PRECISION { - // return float_value; - // } - // let wei_value = float_to_wei(float_value); - // let exponent_wei = float_to_wei(exponent_factor); - // let wei_result = pow(wei_value, exponent_wei); - // let float_result = wei_to_float(wei_result); - // float_result 0 } diff --git a/tests/lib.cairo b/tests/lib.cairo index 69c2f902..e46cd8bf 100644 --- a/tests/lib.cairo +++ b/tests/lib.cairo @@ -80,6 +80,7 @@ mod price { mod pricing { mod test_position_pricing_utils; mod test_swap_pricing_utils; + mod test_pricing_utils; } mod reader { mod test_reader; diff --git a/tests/pricing/test_pricing_utils.cairo b/tests/pricing/test_pricing_utils.cairo new file mode 100644 index 00000000..68acff95 --- /dev/null +++ b/tests/pricing/test_pricing_utils.cairo @@ -0,0 +1,138 @@ +use satoru::pricing::pricing_utils; + +const E20: u128 = 100_000_000_000_000_000_000; +const _3: u128 = 300_000_000_000_000_000_000; +const _2: u128 = 200_000_000_000_000_000_000; +const _1_5: u128 = 150_000_000_000_000_000_000; +const _1_75: u128 = 175_000_000_000_000_000_000; +const _0_001: u128 = 100_000_000_000_000_000; +const _0_0001: u128 = 10_000_000_000_000_000; +const _0_000001: u128 = 1_000_000_000_000; +const _0_0000000000001: u128 = 100_000_000; + +#[test] +fn given_good_parameters_when_apply_impact_factor_then_works() { + // make sure it works for really big values + assert( + pricing_utils::apply_impact_factor( + 10000 * E20, 1 * _0_0000000000001, _3 + ) == 100000000000000000000, + 'wrong impact factor 1' + ); + assert( + pricing_utils::apply_impact_factor( + 100000 * E20, _0_0000000000001, _3 + ) == 100000000000000000000000, + 'wrong impact factor 2' + ); + assert( + pricing_utils::apply_impact_factor( + 1000000 * E20, _0_0000000000001, _3 + ) == 100000000000000000000000000, + 'wrong impact factor 3' + ); + assert( + pricing_utils::apply_impact_factor( + 1000000 * E20, E20, _3 + ) == 100000000000000000000000000000000000000, + 'wrong impact factor 4' + ); + + assert( + pricing_utils::apply_impact_factor(10000 * E20, _0_000001, _2) == 100000000000000000000, + 'wrong impact factor 5' + ); + assert( + pricing_utils::apply_impact_factor(100000 * E20, _0_000001, _2) == 10000000000000000000000, + 'wrong impact factor 6' + ); + + assert( + pricing_utils::apply_impact_factor( + 1000000 * E20, _0_000001, _2 + ) == 1000000000000000000000000, + 'wrong impact factor 7' + ); + assert( + pricing_utils::apply_impact_factor( + 10000000 * E20, _0_000001, _2 + ) == 100000000000000000000000000, + 'wrong impact factor 8' + ); + + assert( + pricing_utils::apply_impact_factor(10000 * E20, _0_000001, _1_75) == 9999999516460977028, + 'wrong impact factor 11' + ); + assert( + pricing_utils::apply_impact_factor(100000 * E20, _0_000001, _1_75) == 562341305085252697579, + 'wrong impact factor 12' + ); + assert( + pricing_utils::apply_impact_factor( + 1000000 * E20, _0_000001, _1_75 + ) == 31622775559765743614174, + 'wrong impact factor 13' + ); + assert( + pricing_utils::apply_impact_factor( + 10000000 * E20, _0_000001, _1_75 + ) == 1778279359983305772602558, + 'wrong impact factor 14' + ); + + // and for small values + assert( + pricing_utils::apply_impact_factor(_0_0000000000001, _0_000001, _1_5) == 0, + 'wrong impact factor 15' + ); + assert( + pricing_utils::apply_impact_factor(_0_001, _0_000001, _1_5) == 0, 'wrong impact factor 16' + ); + assert( + pricing_utils::apply_impact_factor(1 * E20, _0_000001, _1_5) == 1000000000000, + 'wrong impact factor 17' + ); + assert( + pricing_utils::apply_impact_factor(1000 * E20, _0_000001, _1_5) == 31622777689150255, + 'wrong impact factor 18' + ); + assert( + pricing_utils::apply_impact_factor(10000 * E20, _0_000001, _1_5) == 999999958558642276, + 'wrong impact factor 19' + ); + assert( + pricing_utils::apply_impact_factor(100000 * E20, _0_000001, _1_5) == 31622775633373054686, + 'wrong impact factor 20' + ); + assert( + pricing_utils::apply_impact_factor(1000000 * E20, _0_000001, _1_5) == 999999971754659821919, + 'wrong impact factor 21' + ); + assert( + pricing_utils::apply_impact_factor( + 10000000 * E20, _0_000001, _1_5 + ) == 31622775838897949974052, + 'wrong impact factor 22' + ); + + assert( + pricing_utils::apply_impact_factor(10000 * E20, _0_0001, E20) == 100000000000000000000, + 'wrong impact factor 23' + ); + assert( + pricing_utils::apply_impact_factor(100000 * E20, _0_0001, E20) == 1000000000000000000000, + 'wrong impact factor 24' + ); + assert( + pricing_utils::apply_impact_factor(1000000 * E20, _0_0001, E20) == 10000000000000000000000, + 'wrong impact factor 25' + ); + assert( + pricing_utils::apply_impact_factor( + 10000000 * E20, _0_0001, E20 + ) == 100000000000000000000000, + 'wrong impact factor 26' + ); +} + diff --git a/tests/utils/test_precision.cairo b/tests/utils/test_precision.cairo index 198c762e..02fdb68b 100644 --- a/tests/utils/test_precision.cairo +++ b/tests/utils/test_precision.cairo @@ -119,16 +119,16 @@ fn test_to_factor_ival_negative() { #[test] fn test_float_to_wei() { - let float_value: u128 = 1_000_000_000_000_000; + let float_value: u128 = 10_000_000_000_000_000_000; let result = precision::float_to_wei(float_value); assert(result == 1000, 'should be 10^3'); } #[test] fn test_wei_to_float() { - let wei_value: u128 = 10_000_000_000_000_000_000_000_000; //10^25 + let wei_value: u128 = 10_000_000_000_000_000_000_000; //10^22 let result = precision::wei_to_float(wei_value); - assert(result == 10_000_000_000_000_000_000_000_000_000_000_000_000, 'should be 10^37'); + assert(result == 100_000_000_000_000_000_000_000_000_000_000_000_000, 'should be 10^38'); } #[test]