Skip to content

Commit 7deaf4c

Browse files
lb_libraries test coverage ----> 94%
1 parent 03dbeae commit 7deaf4c

File tree

17 files changed

+304
-23
lines changed

17 files changed

+304
-23
lines changed

contracts/liquidity_book/tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ default = []
1818
backtraces = ["cosmwasm-std/backtraces"]
1919

2020
[dependencies]
21-
shade-protocol = { version = "0.1.0", path = "../../../packages/shade_protocol", features = ["liquidity_book_impl"] }
21+
shade-protocol = { version = "0.1.0", path = "../../../packages/shade_protocol", features = ["liquidity_book_impl","liquidity_book"] }
2222
schemars = "0.8.16"
2323
serde = { version = "1.0" }
2424
serde-json-wasm = { version = "1.0"}

packages/shade_protocol/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ required-features = [
2323
[lib]
2424
crate-type = ["cdylib", "rlib"]
2525

26+
27+
2628
[features]
27-
default = ["utils"]
29+
default = ["utils", "liquidity_book"]
30+
2831

2932
# Utils
3033
utils = ["chrono"]

packages/shade_protocol/src/contract_interfaces/liquidity_book/lb_libraries/bin_helper.rs

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,15 +565,11 @@ impl BinHelper {
565565
mod tests {
566566
use super::*;
567567

568+
use super::super::{math::encoded_sample::EncodedSample, types::StaticFeeParameters};
568569
use crate::c_std::StdResult;
569570
use ethnum::U256;
570-
use super::super::{
571-
math::encoded_sample::EncodedSample,
572-
types::StaticFeeParameters,
573-
};
574571
use std::str::FromStr;
575572

576-
577573
fn assert_approxeq_abs(a: U256, b: U256, max_diff: U256, msg: &str) {
578574
let diff = if a > b { a - b } else { b - a };
579575
assert!(diff <= max_diff, "{}: diff was {:?}", msg, diff);
@@ -692,6 +688,47 @@ mod tests {
692688
Ok(())
693689
}
694690

691+
#[test]
692+
fn test_get_shares_and_effective_amounts_in_delta_liquidity_adjustment() {
693+
// Assume these constants based on your SCALE and SCALE_OFFSET values
694+
let scale = U256::from(SCALE);
695+
let scale_offset = U256::from(SCALE_OFFSET);
696+
697+
// Sample input values to trigger the condition
698+
let bin_reserves = Bytes32::encode(1000, 2000); // bin reserves
699+
let price = U256::from(10u128); // price
700+
let total_supply = U256::from(10000u128); // total supply
701+
let user_liquidity = U256::from(1000u128); // user liquidity
702+
let bin_liquidity = U256::from(5000u128); // bin liquidity
703+
let shares =
704+
U256x256Math::mul_div_round_down(user_liquidity, total_supply, bin_liquidity).unwrap();
705+
let effective_liquidity =
706+
U256x256Math::mul_div_round_up(shares, bin_liquidity, total_supply).unwrap();
707+
708+
// Adjusting amounts_in to ensure delta_liquidity calculation triggers the specific condition
709+
let mut amounts_in = Bytes32::encode(500, 1000);
710+
if user_liquidity > effective_liquidity {
711+
let delta_liquidity = user_liquidity - effective_liquidity;
712+
amounts_in = Bytes32::encode(
713+
500,
714+
1000 + ((delta_liquidity >> scale_offset.as_u32()).as_u128()),
715+
);
716+
}
717+
718+
// Execute the method
719+
let result = BinHelper::get_shares_and_effective_amounts_in(
720+
bin_reserves,
721+
amounts_in,
722+
price,
723+
total_supply,
724+
)
725+
.unwrap();
726+
727+
// Assertions
728+
assert!(result.1.decode_y() == 1000);
729+
assert!(result.1.decode_x() == 500);
730+
}
731+
695732
#[test]
696733
fn test_get_shares_and_effective_amounts_in() -> StdResult<()> {
697734
let mut total_supply = U256::from_str("0").unwrap();
@@ -962,4 +999,57 @@ mod tests {
962999

9631000
Ok(())
9641001
}
1002+
1003+
#[test]
1004+
fn test_verify_amounts_with_flawed_factor_id_less_than_active_id() {
1005+
let amounts = Bytes32::encode(1, 0); // Right-side 128 bits greater than zero
1006+
let active_id = 2;
1007+
let id = 1;
1008+
let result = BinHelper::verify_amounts(amounts, active_id, id);
1009+
1010+
match result {
1011+
Err(BinError::CompositionFactorFlawed(error_id)) => assert_eq!(error_id, id),
1012+
_ => panic!("Expected CompositionFactorFlawed error"),
1013+
}
1014+
}
1015+
1016+
#[test]
1017+
fn test_verify_amounts_with_flawed_factor_id_greater_than_active_id() {
1018+
let amounts = U256::from(u128::MAX) + U256::ONE; // Greater than u128::MAX
1019+
let active_id = 1;
1020+
let id = 2;
1021+
let result = BinHelper::verify_amounts(amounts.to_le_bytes(), active_id, id);
1022+
1023+
match result {
1024+
Err(BinError::CompositionFactorFlawed(error_id)) => assert_eq!(error_id, id),
1025+
_ => panic!("Expected CompositionFactorFlawed error"),
1026+
}
1027+
}
1028+
1029+
#[test]
1030+
fn test_verify_amounts_valid_case_id_less_than_active_id() {
1031+
let amounts = Bytes32::encode(0, 0); // Right-side 128 bits are zero
1032+
let active_id = 2;
1033+
let id = 1;
1034+
let result = BinHelper::verify_amounts(amounts, active_id, id);
1035+
assert!(result.is_ok());
1036+
}
1037+
1038+
#[test]
1039+
fn test_verify_amounts_valid_case_id_greater_than_active_id() {
1040+
let amounts = U256::from(u128::MAX).to_le_bytes(); // Not greater than u128::MAX
1041+
let active_id = 1;
1042+
let id = 2;
1043+
let result = BinHelper::verify_amounts(amounts, active_id, id);
1044+
assert!(result.is_ok());
1045+
}
1046+
1047+
#[test]
1048+
fn test_verify_amounts_valid_case_id_equals_active_id() {
1049+
let amounts = Bytes32::encode(0, 0); // Any value is fine, as id == active_id
1050+
let active_id = 1;
1051+
let id = 1;
1052+
let result = BinHelper::verify_amounts(amounts, active_id, id);
1053+
assert!(result.is_ok());
1054+
}
9651055
}

packages/shade_protocol/src/contract_interfaces/liquidity_book/lb_libraries/math/encoded_sample.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//!
44
//! Helper library used for setting and decoding parts of encoded Bytes32.
55
6-
use crate::lb_libraries::types::Bytes32;
6+
use crate::liquidity_book::lb_libraries::types::Bytes32;
77
use cosmwasm_schema::cw_serde;
88
use ethnum::U256;
99

packages/shade_protocol/src/contract_interfaces/liquidity_book/lb_libraries/math/liquidity_configurations.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
use cosmwasm_schema::cw_serde;
77
use ethnum::U256;
88

9-
use crate::lb_libraries::types::Bytes32;
9+
use crate::liquidity_book::lb_libraries::types::Bytes32;
1010

1111
use super::packed_u128_math::PackedUint128Math;
1212

1313
pub const PRECISION: u64 = 1_000_000_000_000_000_000; // 1e18
1414

15-
#[derive(thiserror::Error, Debug)]
15+
#[derive(thiserror::Error, Debug, PartialEq)]
1616
pub enum LiquidityConfigurationsError {
1717
#[error("Liquidity Configurations Error: Distribution must be less than PRECISION")]
1818
InvalidConfig,
@@ -132,4 +132,71 @@ mod tests {
132132

133133
assert_eq!(result, (expected_amounts, 0));
134134
}
135+
136+
#[test]
137+
fn test_new_valid_config() {
138+
let lc = LiquidityConfigurations::new(500_000_000_000_000_000, 500_000_000_000_000_000, 1);
139+
assert!(lc.is_ok());
140+
}
141+
142+
#[test]
143+
fn test_new_invalid_config_x() {
144+
let lc = LiquidityConfigurations::new(PRECISION + 1, 500_000_000_000_000_000, 1);
145+
assert_eq!(lc, Err(LiquidityConfigurationsError::InvalidConfig));
146+
}
147+
148+
#[test]
149+
fn test_new_invalid_config_y() {
150+
let lc = LiquidityConfigurations::new(500_000_000_000_000_000, PRECISION + 1, 1);
151+
assert_eq!(lc, Err(LiquidityConfigurationsError::InvalidConfig));
152+
}
153+
154+
#[test]
155+
fn test_update_distribution_valid() {
156+
let mut lc =
157+
LiquidityConfigurations::new(300_000_000_000_000_000, 300_000_000_000_000_000, 1)
158+
.unwrap();
159+
let result = lc.update_distribution(400_000_000_000_000_000, 400_000_000_000_000_000);
160+
assert!(result.is_ok());
161+
assert_eq!(lc.distribution_x, 400_000_000_000_000_000);
162+
assert_eq!(lc.distribution_y, 400_000_000_000_000_000);
163+
}
164+
165+
#[test]
166+
fn test_update_distribution_invalid_x() {
167+
let mut lc =
168+
LiquidityConfigurations::new(300_000_000_000_000_000, 300_000_000_000_000_000, 1)
169+
.unwrap();
170+
let result = lc.update_distribution(PRECISION + 1, 400_000_000_000_000_000);
171+
assert_eq!(result, Err(LiquidityConfigurationsError::InvalidConfig));
172+
}
173+
174+
#[test]
175+
fn test_update_distribution_invalid_y() {
176+
let mut lc =
177+
LiquidityConfigurations::new(300_000_000_000_000_000, 300_000_000_000_000_000, 1)
178+
.unwrap();
179+
let result = lc.update_distribution(400_000_000_000_000_000, PRECISION + 1);
180+
assert_eq!(result, Err(LiquidityConfigurationsError::InvalidConfig));
181+
}
182+
#[test]
183+
fn test_equality() {
184+
let config1 = LiquidityConfigurations::new(100, 200, 1).unwrap();
185+
let config2 = LiquidityConfigurations::new(100, 200, 1).unwrap();
186+
assert_eq!(config1, config2);
187+
}
188+
189+
#[test]
190+
fn test_debug_format() {
191+
let config = LiquidityConfigurations::new(100, 200, 1).unwrap();
192+
let debug_string = format!("{:?}", config);
193+
assert!(!debug_string.is_empty());
194+
}
195+
196+
#[test]
197+
fn test_clone() {
198+
let config = LiquidityConfigurations::new(100, 200, 1).unwrap();
199+
let cloned_config = config.clone();
200+
assert_eq!(config, cloned_config);
201+
}
135202
}

packages/shade_protocol/src/contract_interfaces/liquidity_book/lb_libraries/math/packed_u128_math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! u128 is a 128-bit unsigned integer type, which means that its little-endian byte representation is 16 bytes long.
88
//! A `Bytes32` value is a `[u8; 32]` and can hold 256 bits, or two `u128` values.
99
10-
use crate::{c_std::StdError, lb_libraries::types::Bytes32};
10+
use crate::{c_std::StdError, liquidity_book::lb_libraries::types::Bytes32};
1111

1212
pub const BASIS_POINT_MAX: u128 = 10_000;
1313

packages/shade_protocol/src/contract_interfaces/liquidity_book/lb_libraries/math/tree_math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use ethnum::U256;
88
use std::collections::HashMap;
99

1010
use super::{bit_math::BitMath, u24::U24};
11-
use crate::lb_libraries::types::Bytes32;
11+
use crate::liquidity_book::lb_libraries::types::Bytes32;
1212

1313
// TODO - This module is likely inefficient because we don't have bit ops for Bytes32.
1414
// - Other libraries could benefit from Bytes32 bit ops also...

packages/shade_protocol/src/contract_interfaces/liquidity_book/lb_libraries/math/u128x128_math.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use ethnum::{I256, U256};
88

9-
use crate::lb_libraries::constants::*;
9+
use crate::liquidity_book::lb_libraries::constants::*;
1010

1111
use super::bit_math::BitMath;
1212

@@ -165,6 +165,7 @@ impl U128x128Math {
165165

166166
#[cfg(test)]
167167
mod tests {
168+
use core::panic;
168169
use std::str::FromStr;
169170

170171
use super::*;
@@ -188,12 +189,9 @@ mod tests {
188189

189190
#[test]
190191
fn test_pow_and_log() {
191-
println!("{}", LOG_SCALE);
192-
println!("{}", LOG_SCALE_SQUARED);
193192
let x = (U256::from((1.0001 * PRECISION as f64) as u128) << 128) / PRECISION;
194193
let y = 100_000;
195194
let res = U128x128Math::pow(x, y.into()).unwrap();
196-
// let expected = U256::from(7491471493045233295460405875225305845649644);
197195
let tolerance = 10 ^ 12;
198196

199197
let expected = U256::from_str("7491471493045233295460405875225305845649644").unwrap();
@@ -220,4 +218,56 @@ mod tests {
220218
"test_pow_and_log::1 failed"
221219
);
222220
}
221+
#[test]
222+
fn test_log2_x_equals_1() {
223+
let x = U256::ONE;
224+
let result = U128x128Math::log2(x).unwrap();
225+
assert_eq!(result, I256::from(-128));
226+
}
227+
228+
#[test]
229+
fn test_log2_x_equals_0() {
230+
let x = U256::ZERO;
231+
let result = U128x128Math::log2(x);
232+
233+
assert!(matches!(result, Err(U128x128MathError::LogUnderflow)));
234+
}
235+
236+
#[test]
237+
fn test_pow_y_equals_0() {
238+
let x = U256::from(123456789u128);
239+
let y = I256::ZERO;
240+
let result = U128x128Math::pow(x, y).unwrap();
241+
assert_eq!(result, SCALE);
242+
}
243+
244+
#[test]
245+
fn test_pow_y_negative() {
246+
let x = U256::from(123456789u128);
247+
let y = I256::from(-1);
248+
let _result = U128x128Math::pow(x, y).unwrap();
249+
assert_eq!(
250+
_result.to_string(),
251+
"937915931357296158282320018939484225960793332034907890237268231623237"
252+
);
253+
}
254+
255+
#[test]
256+
fn test_pow_y_positive() {
257+
let x = U256::from(123456789u128);
258+
let y = I256::from(1);
259+
let _result = U128x128Math::pow(x, y).unwrap();
260+
assert_eq!(_result.to_string(), "123456789");
261+
}
262+
263+
#[test]
264+
fn test_pow_invert_result() {
265+
let x = U256::from(0xffffffffffffffffffffffffffffffffu128);
266+
let y = I256::from(-1);
267+
let _result = U128x128Math::pow(x, y).unwrap();
268+
assert_eq!(
269+
_result.to_string(),
270+
"340282366920938463463374607431768211457"
271+
);
272+
}
223273
}

0 commit comments

Comments
 (0)