Skip to content

Commit

Permalink
implemented test for apply_impact_factor + refacto relative function
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Oct 18, 2023
1 parent 3a1dfdd commit 0fcfe82
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/pricing/pricing_utils.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Library for pricing functions.

// *************************************************************************
// IMPORTS
// *************************************************************************
Expand Down
14 changes: 1 addition & 13 deletions src/utils/precision.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions tests/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
138 changes: 138 additions & 0 deletions tests/pricing/test_pricing_utils.cairo
Original file line number Diff line number Diff line change
@@ -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'
);
}

6 changes: 3 additions & 3 deletions tests/utils/test_precision.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 0fcfe82

Please sign in to comment.