From b3859e82de18f7014bab193cf87674d9a50e204d Mon Sep 17 00:00:00 2001 From: mootz12 Date: Tue, 2 Jan 2024 15:08:41 -0500 Subject: [PATCH] chore: remove scalar hardcoding for constants for non-tests --- backstop/src/backstop/pool.rs | 2 +- pool-factory/src/pool_factory.rs | 4 ++- pool/src/auctions/user_liquidation_auction.rs | 6 ++--- pool/src/emissions/manager.rs | 2 +- pool/src/pool/config.rs | 22 ++++++++------- pool/src/pool/health_factor.rs | 2 +- pool/src/pool/interest.rs | 12 +++++---- pool/src/pool/user.rs | 27 ++++++++++--------- 8 files changed, 42 insertions(+), 35 deletions(-) diff --git a/backstop/src/backstop/pool.rs b/backstop/src/backstop/pool.rs index 82b7d0aa..c7448351 100644 --- a/backstop/src/backstop/pool.rs +++ b/backstop/src/backstop/pool.rs @@ -86,7 +86,7 @@ pub fn require_pool_above_threshold(pool_backstop_data: &PoolBackstopData) -> bo .saturating_mul(bal_blnd) .saturating_mul(bal_usdc) .saturating_mul(SCALAR_7); // 10^7 * 10^7 - saturating_pool_pc / threshold_pc >= 1_0000000 + saturating_pool_pc / threshold_pc >= SCALAR_7 } /// The pool's backstop balances diff --git a/pool-factory/src/pool_factory.rs b/pool-factory/src/pool_factory.rs index 12d6bedd..9ec846f5 100644 --- a/pool-factory/src/pool_factory.rs +++ b/pool-factory/src/pool_factory.rs @@ -7,6 +7,8 @@ use soroban_sdk::{ Symbol, Val, Vec, }; +const SCALAR_9: u64 = 1_000_000_000; + #[contract] pub struct PoolFactoryContract; @@ -69,7 +71,7 @@ impl PoolFactory for PoolFactoryContract { let pool_init_meta = storage::get_pool_init_meta(&e); // verify backstop take rate is within [0,1) with 9 decimals - if backstop_take_rate >= 1_000_000_000 { + if backstop_take_rate >= SCALAR_9 { panic_with_error!(&e, PoolFactoryError::InvalidPoolInitArgs); } diff --git a/pool/src/auctions/user_liquidation_auction.rs b/pool/src/auctions/user_liquidation_auction.rs index 5a866bbf..9c8c34e0 100644 --- a/pool/src/auctions/user_liquidation_auction.rs +++ b/pool/src/auctions/user_liquidation_auction.rs @@ -52,7 +52,7 @@ pub fn create_user_liq_auction_data( .fixed_div_floor(position_data.liability_raw, oracle_scalar) .unwrap_optimized(); let est_incentive = (SCALAR_7 - avg_cf.fixed_div_ceil(avg_lf, SCALAR_7).unwrap_optimized()) - .fixed_div_ceil(2_0000000, SCALAR_7) + .fixed_div_ceil(2 * SCALAR_7, SCALAR_7) .unwrap_optimized() + SCALAR_7; @@ -65,8 +65,8 @@ pub fn create_user_liq_auction_data( let mut est_withdrawn_collateral_pct = est_withdrawn_collateral .fixed_div_ceil(position_data.collateral_raw, oracle_scalar) .unwrap_optimized(); - if est_withdrawn_collateral_pct > 1_0000000 { - est_withdrawn_collateral_pct = 1_0000000; + if est_withdrawn_collateral_pct > SCALAR_7 { + est_withdrawn_collateral_pct = SCALAR_7; } for (asset, amount) in user_state.positions.collateral.iter() { diff --git a/pool/src/emissions/manager.rs b/pool/src/emissions/manager.rs index 0fe5efaf..a4fccafb 100644 --- a/pool/src/emissions/manager.rs +++ b/pool/src/emissions/manager.rs @@ -46,7 +46,7 @@ pub fn set_pool_emissions(e: &Env, res_emission_metadata: Vec 1_0000000 { + if total_share > SCALAR_7 as u64 { panic_with_error!(e, PoolError::BadRequest); } diff --git a/pool/src/pool/config.rs b/pool/src/pool/config.rs index 828d026e..d1533cf7 100644 --- a/pool/src/pool/config.rs +++ b/pool/src/pool/config.rs @@ -1,5 +1,5 @@ use crate::{ - constants::SECONDS_PER_WEEK, + constants::{SCALAR_7, SCALAR_9, SECONDS_PER_WEEK}, errors::PoolError, storage::{self, PoolConfig, QueuedReserveInit, ReserveConfig, ReserveData}, }; @@ -27,7 +27,7 @@ pub fn execute_initialize( } // ensure backstop is [0,1) - if *bstop_rate >= 1_000_000_000 { + if *bstop_rate >= SCALAR_9 as u64 { panic_with_error!(e, PoolError::InvalidPoolInitArgs); } @@ -50,7 +50,7 @@ pub fn execute_initialize( /// Update the pool pub fn execute_update_pool(e: &Env, backstop_take_rate: u64, max_positions: u32) { // ensure backstop is [0,1) - if backstop_take_rate >= 1_000_000_000 { + if backstop_take_rate >= SCALAR_9 as u64 { panic_with_error!(e, PoolError::BadRequest); } let mut pool_config = storage::get_pool_config(e); @@ -117,15 +117,15 @@ fn initialize_reserve(e: &Env, asset: &Address, config: &ReserveConfig) -> u32 { || reserve_config.r_three != config.r_three || reserve_config.util != config.util { - reserve.ir_mod = 1_000_000_000; + reserve.ir_mod = SCALAR_9; } reserve.store(e); } else { index = storage::push_res_list(e, asset); let init_data = ReserveData { - b_rate: 1_000_000_000, - d_rate: 1_000_000_000, - ir_mod: 1_000_000_000, + b_rate: SCALAR_9, + d_rate: SCALAR_9, + ir_mod: SCALAR_9, d_supply: 0, b_supply: 0, last_time: e.ledger().timestamp(), @@ -153,11 +153,12 @@ fn initialize_reserve(e: &Env, asset: &Address, config: &ReserveConfig) -> u32 { #[allow(clippy::zero_prefixed_literal)] fn require_valid_reserve_metadata(e: &Env, metadata: &ReserveConfig) { + const SCALAR_7_U32: u32 = SCALAR_7 as u32; if metadata.decimals > 18 - || metadata.c_factor > 1_0000000 - || metadata.l_factor > 1_0000000 + || metadata.c_factor > SCALAR_7_U32 + || metadata.l_factor > SCALAR_7_U32 || metadata.util > 0_9500000 - || (metadata.max_util > 1_0000000 || metadata.max_util <= metadata.util) + || (metadata.max_util > SCALAR_7_U32 || metadata.max_util <= metadata.util) || (metadata.r_one > metadata.r_two || metadata.r_two > metadata.r_three) || (metadata.reactivity > 0_0005000) { @@ -253,6 +254,7 @@ mod tests { execute_update_pool(&e, 1_000_000_000u64, 4u32); }); } + #[test] fn test_queue_initial_reserve() { let e = Env::default(); diff --git a/pool/src/pool/health_factor.rs b/pool/src/pool/health_factor.rs index 4d129f1e..5d376402 100644 --- a/pool/src/pool/health_factor.rs +++ b/pool/src/pool/health_factor.rs @@ -196,7 +196,7 @@ mod tests { assert_eq!(position_data.liability_base, 185_2368827); assert_eq!(position_data.collateral_raw, 350_3984567); assert_eq!(position_data.liability_raw, 148_0895061); - assert_eq!(position_data.scalar, 1_0000000); + assert_eq!(position_data.scalar, SCALAR_7); }); } diff --git a/pool/src/pool/interest.rs b/pool/src/pool/interest.rs index 4bb57743..33a52a4a 100644 --- a/pool/src/pool/interest.rs +++ b/pool/src/pool/interest.rs @@ -81,8 +81,9 @@ pub fn calc_accrual( .fixed_mul_floor(i128(config.reactivity), SCALAR_9) .unwrap_optimized(); let next_ir_mod = ir_mod + rate_dif; - if next_ir_mod > 10_000_000_000 { - new_ir_mod = 10_000_000_000; + let ir_mod_max = 10 * SCALAR_9; + if next_ir_mod > ir_mod_max { + new_ir_mod = ir_mod_max; } else { new_ir_mod = next_ir_mod; } @@ -95,8 +96,9 @@ pub fn calc_accrual( .fixed_mul_ceil(i128(config.reactivity), SCALAR_9) .unwrap_optimized(); let next_ir_mod = ir_mod + rate_dif; - if next_ir_mod < 0_100_000_000 { - new_ir_mod = 0_100_000_000; + let ir_mod_min = SCALAR_9 / 10; + if next_ir_mod < ir_mod_min { + new_ir_mod = ir_mod_min; } else { new_ir_mod = next_ir_mod; } @@ -105,7 +107,7 @@ pub fn calc_accrual( // calc accrual amount over blocks let time_weight = delta_time_scaled / SECONDS_PER_YEAR; ( - 1_000_000_000 + SCALAR_9 + time_weight .fixed_mul_ceil(cur_ir * 100, SCALAR_9) .unwrap_optimized(), diff --git a/pool/src/pool/user.rs b/pool/src/pool/user.rs index 0244150b..d7ba61ee 100644 --- a/pool/src/pool/user.rs +++ b/pool/src/pool/user.rs @@ -217,7 +217,8 @@ impl User { mod tests { use super::*; use crate::{ - storage, testutils, ReserveEmissionsConfig, ReserveEmissionsData, UserEmissionData, + constants::SCALAR_7, storage, testutils, ReserveEmissionsConfig, ReserveEmissionsData, + UserEmissionData, }; use soroban_fixed_point_math::FixedPoint; use soroban_sdk::{ @@ -345,7 +346,7 @@ mod tests { let new_emis_res_data = storage::get_res_emis_data(&e, &res_0_d_token_index).unwrap(); let new_index = 1000 + (1000i128 * 0_1000000) - .fixed_div_floor(starting_d_supply_0, 1_0000000) + .fixed_div_floor(starting_d_supply_0, SCALAR_7) .unwrap(); assert_eq!(new_emis_res_data.last_time, 10001000); assert_eq!(new_emis_res_data.index, new_index); @@ -353,7 +354,7 @@ mod tests { storage::get_user_emissions(&e, &samwise, &res_0_d_token_index).unwrap(); let new_accrual = 0 + (new_index - emis_user_data.index) - .fixed_mul_floor(1000, 1_0000000) + .fixed_mul_floor(1000, SCALAR_7) .unwrap(); assert_eq!(user_emis_data.accrued, new_accrual); }); @@ -412,7 +413,7 @@ mod tests { let new_emis_res_data = storage::get_res_emis_data(&e, &res_0_d_token_index).unwrap(); let new_index = 1000 + (1000i128 * 0_1000000) - .fixed_div_floor(starting_d_supply_0, 1_0000000) + .fixed_div_floor(starting_d_supply_0, SCALAR_7) .unwrap(); assert_eq!(new_emis_res_data.last_time, 10001000); assert_eq!(new_emis_res_data.index, new_index); @@ -420,7 +421,7 @@ mod tests { storage::get_user_emissions(&e, &samwise, &res_0_d_token_index).unwrap(); let new_accrual = 0 + (new_index - emis_user_data.index) - .fixed_mul_floor(1000, 1_0000000) + .fixed_mul_floor(1000, SCALAR_7) .unwrap(); assert_eq!(user_emis_data.accrued, new_accrual); }); @@ -540,7 +541,7 @@ mod tests { let new_emis_res_data = storage::get_res_emis_data(&e, &res_0_d_token_index).unwrap(); let new_index = 1000 + (1000i128 * 0_1000000) - .fixed_div_floor(starting_b_token_supply, 1_0000000) + .fixed_div_floor(starting_b_token_supply, SCALAR_7) .unwrap(); assert_eq!(new_emis_res_data.last_time, 10001000); assert_eq!(new_emis_res_data.index, new_index); @@ -548,7 +549,7 @@ mod tests { storage::get_user_emissions(&e, &samwise, &res_0_d_token_index).unwrap(); let new_accrual = 0 + (new_index - emis_user_data.index) - .fixed_mul_floor(1000, 1_0000000) + .fixed_mul_floor(1000, SCALAR_7) .unwrap(); assert_eq!(user_emis_data.accrued, new_accrual); }); @@ -608,7 +609,7 @@ mod tests { let new_emis_res_data = storage::get_res_emis_data(&e, &res_0_d_token_index).unwrap(); let new_index = 1000 + (1000i128 * 0_1000000) - .fixed_div_floor(starting_b_token_supply, 1_0000000) + .fixed_div_floor(starting_b_token_supply, SCALAR_7) .unwrap(); assert_eq!(new_emis_res_data.last_time, 10001000); assert_eq!(new_emis_res_data.index, new_index); @@ -616,7 +617,7 @@ mod tests { storage::get_user_emissions(&e, &samwise, &res_0_d_token_index).unwrap(); let new_accrual = 0 + (new_index - emis_user_data.index) - .fixed_mul_floor(1000, 1_0000000) + .fixed_mul_floor(1000, SCALAR_7) .unwrap(); assert_eq!(user_emis_data.accrued, new_accrual); }); @@ -737,7 +738,7 @@ mod tests { let new_emis_res_data = storage::get_res_emis_data(&e, &res_0_d_token_index).unwrap(); let new_index = 1000 + (1000i128 * 0_1000000) - .fixed_div_floor(starting_b_token_supply, 1_0000000) + .fixed_div_floor(starting_b_token_supply, SCALAR_7) .unwrap(); assert_eq!(new_emis_res_data.last_time, 10001000); assert_eq!(new_emis_res_data.index, new_index); @@ -745,7 +746,7 @@ mod tests { storage::get_user_emissions(&e, &samwise, &res_0_d_token_index).unwrap(); let new_accrual = 0 + (new_index - emis_user_data.index) - .fixed_mul_floor(1000, 1_0000000) + .fixed_mul_floor(1000, SCALAR_7) .unwrap(); assert_eq!(user_emis_data.accrued, new_accrual); }); @@ -805,7 +806,7 @@ mod tests { let new_emis_res_data = storage::get_res_emis_data(&e, &res_0_d_token_index).unwrap(); let new_index = 1000 + (1000i128 * 0_1000000) - .fixed_div_floor(starting_b_token_supply, 1_0000000) + .fixed_div_floor(starting_b_token_supply, SCALAR_7) .unwrap(); assert_eq!(new_emis_res_data.last_time, 10001000); assert_eq!(new_emis_res_data.index, new_index); @@ -813,7 +814,7 @@ mod tests { storage::get_user_emissions(&e, &samwise, &res_0_d_token_index).unwrap(); let new_accrual = 0 + (new_index - emis_user_data.index) - .fixed_mul_floor(1000, 1_0000000) + .fixed_mul_floor(1000, SCALAR_7) .unwrap(); assert_eq!(user_emis_data.accrued, new_accrual); });