Skip to content

Commit 1f99a4f

Browse files
committed
implemented test for apply_impact_factor + refacto relative function
1 parent 3367de4 commit 1f99a4f

File tree

6 files changed

+159
-18
lines changed

6 files changed

+159
-18
lines changed

Scarb.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ alexandria_data_structures = { git = "https://github.com/keep-starknet-strange/a
2323
alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "a3052ff" }
2424
alexandria_storage = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "a3052ff" }
2525
alexandria_sorting = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "a3052ff" }
26+
cubit = {git ="https://github.com/influenceth/cubit.git", rev="41bdf71"}
2627
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.7.0" }
2728

2829

src/pricing/pricing_utils.cairo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Library for pricing functions.
2-
32
// *************************************************************************
43
// IMPORTS
54
// *************************************************************************

src/utils/precision.cairo

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// IMPORTS
33
// *************************************************************************
44
// Core lib imports.
5-
use alexandria_math::pow;
65
use integer::{
76
i128_to_felt252, u128_to_felt252, u256_wide_mul, u512_safe_div_rem_by_u256, BoundedU256,
87
u256_try_as_non_zero
98
};
109
use core::traits::TryInto;
1110
use core::option::Option;
11+
use cubit::f128::types::fixed::{Fixed, FixedTrait, ONE_u128};
1212
use satoru::utils::calc::{roundup_division, roundup_magnitude_division};
1313

1414
const FLOAT_PRECISION: u128 = 100_000_000_000_000_000_000; // 10^20
@@ -17,7 +17,7 @@ const FLOAT_PRECISION_SQRT: u128 = 10_000_000_000; // 10^10
1717
const WEI_PRECISION: u128 = 1_000_000_000_000_000_000; // 10^18
1818
const BASIS_POINTS_DIVISOR: u128 = 10000;
1919

20-
const FLOAT_TO_WEI_DIVISOR: u128 = 1_000_000_000_000; // 10^12
20+
const FLOAT_TO_WEI_DIVISOR: u128 = 10_000_000_000_000_000; // 10^16
2121

2222
/// Applies the given factor to the given value and returns the result.
2323
/// # Arguments
@@ -156,18 +156,20 @@ fn mul_div_roundup(
156156
/// * `value` - The value to the exponent is applied to.
157157
/// * `divisor` - The exponent applied.
158158
fn apply_exponent_factor(float_value: u128, exponent_factor: u128) -> u128 { // TODO
159-
// if float_value < FLOAT_PRECISION {
160-
// return 0;
161-
// }
162-
// if exponent_factor == FLOAT_PRECISION {
163-
// return float_value;
164-
// }
165-
// let wei_value = float_to_wei(float_value);
166-
// let exponent_wei = float_to_wei(exponent_factor);
167-
// let wei_result = pow(wei_value, exponent_wei);
168-
// let float_result = wei_to_float(wei_result);
169-
// float_result
170-
0
159+
if float_value < FLOAT_PRECISION {
160+
return 0;
161+
}
162+
if exponent_factor == FLOAT_PRECISION {
163+
return float_value;
164+
}
165+
let wei_value = float_to_wei(float_value);
166+
let value_scaled = mul_div(wei_value, ONE_u128, BASIS_POINTS_DIVISOR);
167+
let exponent_wei = float_to_wei(exponent_factor);
168+
let exponent_scaled = mul_div(exponent_wei, ONE_u128, BASIS_POINTS_DIVISOR);
169+
let value_fp = FixedTrait::new(value_scaled, false);
170+
let exponent_fp = FixedTrait::new(exponent_scaled, false);
171+
let wei_result_fp = value_fp.pow(exponent_fp);
172+
mul_div(wei_result_fp.mag, FLOAT_PRECISION, ONE_u128)
171173
}
172174

173175
/// Compute factor from value and divisor with a roundup.

tests/lib.cairo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ mod price {
7878
mod pricing {
7979
mod test_position_pricing_utils;
8080
mod test_swap_pricing_utils;
81+
mod test_pricing_utils;
8182
}
8283
mod reader {
8384
mod test_reader;
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
use satoru::pricing::pricing_utils;
2+
3+
const E20: u128 = 100_000_000_000_000_000_000;
4+
const _3: u128 = 300_000_000_000_000_000_000;
5+
const _2: u128 = 200_000_000_000_000_000_000;
6+
const _1_5: u128 = 150_000_000_000_000_000_000;
7+
const _1_75: u128 = 175_000_000_000_000_000_000;
8+
const _0_001: u128 = 100_000_000_000_000_000;
9+
const _0_0001: u128 = 10_000_000_000_000_000;
10+
const _0_000001: u128 = 1_000_000_000_000;
11+
const _0_0000000000001: u128 = 100_000_000;
12+
13+
#[test]
14+
fn given_good_parameters_when_apply_impact_factor_then_works() {
15+
// make sure it works for really big values
16+
assert(
17+
pricing_utils::apply_impact_factor(
18+
10000 * E20, 1 * _0_0000000000001, _3
19+
) == 100000000000000000000,
20+
'wrong impact factor 1'
21+
);
22+
assert(
23+
pricing_utils::apply_impact_factor(
24+
100000 * E20, _0_0000000000001, _3
25+
) == 100000000000000000000000,
26+
'wrong impact factor 2'
27+
);
28+
assert(
29+
pricing_utils::apply_impact_factor(
30+
1000000 * E20, _0_0000000000001, _3
31+
) == 100000000000000000000000000,
32+
'wrong impact factor 3'
33+
);
34+
assert(
35+
pricing_utils::apply_impact_factor(
36+
1000000 * E20, E20, _3
37+
) == 100000000000000000000000000000000000000,
38+
'wrong impact factor 4'
39+
);
40+
41+
assert(
42+
pricing_utils::apply_impact_factor(10000 * E20, _0_000001, _2) == 100000000000000000000,
43+
'wrong impact factor 5'
44+
);
45+
assert(
46+
pricing_utils::apply_impact_factor(100000 * E20, _0_000001, _2) == 10000000000000000000000,
47+
'wrong impact factor 6'
48+
);
49+
50+
assert(
51+
pricing_utils::apply_impact_factor(
52+
1000000 * E20, _0_000001, _2
53+
) == 1000000000000000000000000,
54+
'wrong impact factor 7'
55+
);
56+
assert(
57+
pricing_utils::apply_impact_factor(
58+
10000000 * E20, _0_000001, _2
59+
) == 100000000000000000000000000,
60+
'wrong impact factor 8'
61+
);
62+
63+
assert(
64+
pricing_utils::apply_impact_factor(10000 * E20, _0_000001, _1_75) == 9999999516460977028,
65+
'wrong impact factor 11'
66+
);
67+
assert(
68+
pricing_utils::apply_impact_factor(100000 * E20, _0_000001, _1_75) == 562341305085252697579,
69+
'wrong impact factor 12'
70+
);
71+
assert(
72+
pricing_utils::apply_impact_factor(
73+
1000000 * E20, _0_000001, _1_75
74+
) == 31622775559765743614174,
75+
'wrong impact factor 13'
76+
);
77+
assert(
78+
pricing_utils::apply_impact_factor(
79+
10000000 * E20, _0_000001, _1_75
80+
) == 1778279359983305772602558,
81+
'wrong impact factor 14'
82+
);
83+
84+
// and for small values
85+
assert(
86+
pricing_utils::apply_impact_factor(_0_0000000000001, _0_000001, _1_5) == 0,
87+
'wrong impact factor 15'
88+
);
89+
assert(
90+
pricing_utils::apply_impact_factor(_0_001, _0_000001, _1_5) == 0, 'wrong impact factor 16'
91+
);
92+
assert(
93+
pricing_utils::apply_impact_factor(1 * E20, _0_000001, _1_5) == 1000000000000,
94+
'wrong impact factor 17'
95+
);
96+
assert(
97+
pricing_utils::apply_impact_factor(1000 * E20, _0_000001, _1_5) == 31622777689150255,
98+
'wrong impact factor 18'
99+
);
100+
assert(
101+
pricing_utils::apply_impact_factor(10000 * E20, _0_000001, _1_5) == 999999958558642276,
102+
'wrong impact factor 19'
103+
);
104+
assert(
105+
pricing_utils::apply_impact_factor(100000 * E20, _0_000001, _1_5) == 31622775633373054686,
106+
'wrong impact factor 20'
107+
);
108+
assert(
109+
pricing_utils::apply_impact_factor(1000000 * E20, _0_000001, _1_5) == 999999971754659821919,
110+
'wrong impact factor 21'
111+
);
112+
assert(
113+
pricing_utils::apply_impact_factor(
114+
10000000 * E20, _0_000001, _1_5
115+
) == 31622775838897949974052,
116+
'wrong impact factor 22'
117+
);
118+
119+
assert(
120+
pricing_utils::apply_impact_factor(10000 * E20, _0_0001, E20) == 100000000000000000000,
121+
'wrong impact factor 23'
122+
);
123+
assert(
124+
pricing_utils::apply_impact_factor(100000 * E20, _0_0001, E20) == 1000000000000000000000,
125+
'wrong impact factor 24'
126+
);
127+
assert(
128+
pricing_utils::apply_impact_factor(1000000 * E20, _0_0001, E20) == 10000000000000000000000,
129+
'wrong impact factor 25'
130+
);
131+
assert(
132+
pricing_utils::apply_impact_factor(
133+
10000000 * E20, _0_0001, E20
134+
) == 100000000000000000000000,
135+
'wrong impact factor 26'
136+
);
137+
}
138+

tests/utils/test_precision.cairo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,16 @@ fn test_to_factor_ival_negative() {
118118

119119
#[test]
120120
fn test_float_to_wei() {
121-
let float_value: u128 = 1_000_000_000_000_000;
121+
let float_value: u128 = 10_000_000_000_000_000_000;
122122
let result = precision::float_to_wei(float_value);
123123
assert(result == 1000, 'should be 10^3');
124124
}
125125

126126
#[test]
127127
fn test_wei_to_float() {
128-
let wei_value: u128 = 10_000_000_000_000_000_000_000_000; //10^25
128+
let wei_value: u128 = 10_000_000_000_000_000_000_000; //10^22
129129
let result = precision::wei_to_float(wei_value);
130-
assert(result == 10_000_000_000_000_000_000_000_000_000_000_000_000, 'should be 10^37');
130+
assert(result == 100_000_000_000_000_000_000_000_000_000_000_000_000, 'should be 10^38');
131131
}
132132

133133
#[test]

0 commit comments

Comments
 (0)