From b3859e82de18f7014bab193cf87674d9a50e204d Mon Sep 17 00:00:00 2001 From: mootz12 Date: Tue, 2 Jan 2024 15:08:41 -0500 Subject: [PATCH 1/2] 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); }); From 88a9440980dc9af292317ab56d24975719c9a09d Mon Sep 17 00:00:00 2001 From: mootz12 Date: Tue, 2 Jan 2024 15:52:15 -0500 Subject: [PATCH 2/2] pool: chore: use an enum for RequestType --- pool/src/auctions/auction.rs | 8 +- pool/src/lib.rs | 2 +- pool/src/pool/actions.rs | 105 +++++++++++++++------- pool/src/pool/mod.rs | 2 +- pool/src/pool/submit.rs | 16 ++-- test-suites/fuzz/lib.rs | 8 +- test-suites/src/setup.rs | 14 +-- test-suites/tests/test_liquidation.rs | 74 +++++++-------- test-suites/tests/test_overflow_flag.rs | 10 +-- test-suites/tests/test_pool.rs | 14 +-- test-suites/tests/test_wasm_happy_path.rs | 26 +++--- 11 files changed, 158 insertions(+), 121 deletions(-) diff --git a/pool/src/auctions/auction.rs b/pool/src/auctions/auction.rs index 8c1e3e6e..2e4170ae 100644 --- a/pool/src/auctions/auction.rs +++ b/pool/src/auctions/auction.rs @@ -25,12 +25,12 @@ pub enum AuctionType { } impl AuctionType { - pub fn from_u32(value: u32) -> Self { + pub fn from_u32(e: &Env, value: u32) -> Self { match value { 0 => AuctionType::UserLiquidation, 1 => AuctionType::BadDebtAuction, 2 => AuctionType::InterestAuction, - _ => panic!("internal error"), + _ => panic_with_error!(e, PoolError::BadRequest), } } } @@ -54,7 +54,7 @@ pub struct AuctionData { /// If the auction is unable to be created pub fn create(e: &Env, auction_type: u32) -> AuctionData { let backstop = storage::get_backstop(e); - let auction_data = match AuctionType::from_u32(auction_type) { + let auction_data = match AuctionType::from_u32(e, auction_type) { AuctionType::UserLiquidation => { panic_with_error!(e, PoolError::BadRequest); } @@ -131,7 +131,7 @@ pub fn fill( ) { let auction_data = storage::get_auction(e, &auction_type, user); let (to_fill_auction, remaining_auction) = scale_auction(e, &auction_data, percent_filled); - match AuctionType::from_u32(auction_type) { + match AuctionType::from_u32(e, auction_type) { AuctionType::UserLiquidation => { fill_user_liq_auction(e, pool, &to_fill_auction, user, filler_state) } diff --git a/pool/src/lib.rs b/pool/src/lib.rs index 8501da91..03b857d3 100644 --- a/pool/src/lib.rs +++ b/pool/src/lib.rs @@ -20,7 +20,7 @@ pub use auctions::{AuctionData, AuctionType}; pub use contract::*; pub use emissions::ReserveEmissionMetadata; pub use errors::PoolError; -pub use pool::{Positions, Request}; +pub use pool::{Positions, Request, RequestType}; pub use storage::{ AuctionKey, PoolConfig, PoolDataKey, PoolEmissionConfig, ReserveConfig, ReserveData, ReserveEmissionsConfig, ReserveEmissionsData, UserEmissionData, UserReserveKey, diff --git a/pool/src/pool/actions.rs b/pool/src/pool/actions.rs index 17cc192a..23e23acf 100644 --- a/pool/src/pool/actions.rs +++ b/pool/src/pool/actions.rs @@ -6,7 +6,7 @@ use crate::{auctions, errors::PoolError, validator::require_nonnegative}; use super::pool::Pool; use super::User; -/// An request a user makes against the pool +/// A request a user makes against the pool #[derive(Clone)] #[contracttype] pub struct Request { @@ -15,6 +15,44 @@ pub struct Request { pub amount: i128, } +/// The type of request to be made against the pool +#[derive(Clone, PartialEq)] +#[repr(u32)] +pub enum RequestType { + Supply = 0, + Withdraw = 1, + SupplyCollateral = 2, + WithdrawCollateral = 3, + Borrow = 4, + Repay = 5, + FillUserLiquidationAuction = 6, + FillBadDebtAuction = 7, + FillInterestAuction = 8, + DeleteLiquidationAuction = 9, +} + +impl RequestType { + /// Convert a u32 to a RequestType + /// + /// ### Panics + /// If the value is not a valid RequestType + pub fn from_u32(e: &Env, value: u32) -> Self { + match value { + 0 => RequestType::Supply, + 1 => RequestType::Withdraw, + 2 => RequestType::SupplyCollateral, + 3 => RequestType::WithdrawCollateral, + 4 => RequestType::Borrow, + 5 => RequestType::Repay, + 6 => RequestType::FillUserLiquidationAuction, + 7 => RequestType::FillBadDebtAuction, + 8 => RequestType::FillInterestAuction, + 9 => RequestType::DeleteLiquidationAuction, + _ => panic_with_error!(e, PoolError::BadRequest), + } + } +} + /// Transfer actions to be taken by the sender and pool pub struct Actions { pub spender_transfer: Map, @@ -77,8 +115,8 @@ pub fn build_actions_from_request( // verify the request is allowed require_nonnegative(e, &request.amount); pool.require_action_allowed(e, request.request_type); - match request.request_type { - 0 => { + match RequestType::from_u32(e, request.request_type) { + RequestType::Supply => { // supply let mut reserve = pool.load_reserve(e, &request.address); let b_tokens_minted = reserve.to_b_token_down(request.amount); @@ -94,7 +132,7 @@ pub fn build_actions_from_request( (request.amount, b_tokens_minted), ); } - 1 => { + RequestType::Withdraw => { // withdraw let mut reserve = pool.load_reserve(e, &request.address); let cur_b_tokens = from_state.get_supply(reserve.index); @@ -116,7 +154,7 @@ pub fn build_actions_from_request( (tokens_out, to_burn), ); } - 2 => { + RequestType::SupplyCollateral => { // supply collateral let mut reserve = pool.load_reserve(e, &request.address); let b_tokens_minted = reserve.to_b_token_down(request.amount); @@ -132,7 +170,7 @@ pub fn build_actions_from_request( (request.amount, b_tokens_minted), ); } - 3 => { + RequestType::WithdrawCollateral => { // withdraw collateral let mut reserve = pool.load_reserve(e, &request.address); let cur_b_tokens = from_state.get_collateral(reserve.index); @@ -155,7 +193,7 @@ pub fn build_actions_from_request( (tokens_out, to_burn), ); } - 4 => { + RequestType::Borrow => { // borrow let mut reserve = pool.load_reserve(e, &request.address); let d_tokens_minted = reserve.to_d_token_up(request.amount); @@ -173,7 +211,7 @@ pub fn build_actions_from_request( (request.amount, d_tokens_minted), ); } - 5 => { + RequestType::Repay => { // repay let mut reserve = pool.load_reserve(e, &request.address); let cur_d_tokens = from_state.get_liabilities(reserve.index); @@ -206,7 +244,7 @@ pub fn build_actions_from_request( } pool.cache_reserve(reserve, true); } - 6 => { + RequestType::FillUserLiquidationAuction => { // fill user liquidation auction auctions::fill( e, @@ -227,7 +265,7 @@ pub fn build_actions_from_request( (from.clone(), request.amount), ); } - 7 => { + RequestType::FillBadDebtAuction => { // fill bad debt auction // Note: will fail if input address is not the backstop since there cannot be a bad debt auction for a different address in storage auctions::fill( @@ -249,7 +287,7 @@ pub fn build_actions_from_request( (from.clone(), request.amount), ); } - 8 => { + RequestType::FillInterestAuction => { // fill interest auction // Note: will fail if input address is not the backstop since there cannot be an interest auction for a different address in storage auctions::fill( @@ -269,7 +307,7 @@ pub fn build_actions_from_request( (from.clone(), request.amount), ); } - 9 => { + RequestType::DeleteLiquidationAuction => { // delete liquidation auction // Note: request object is ignored besides type auctions::delete_liquidation(e, &from); @@ -279,7 +317,6 @@ pub fn build_actions_from_request( (), ); } - _ => panic_with_error!(e, PoolError::BadRequest), } } @@ -347,7 +384,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 0, + request_type: RequestType::Supply as u32, address: underlying.clone(), amount: 10_1234567, }, @@ -423,7 +460,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 1, + request_type: RequestType::Withdraw as u32, address: underlying.clone(), amount: 10_1234567, }, @@ -496,7 +533,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 1, + request_type: RequestType::Withdraw as u32, address: underlying.clone(), amount: 21_0000000, }, @@ -561,7 +598,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: underlying.clone(), amount: 10_1234567, }, @@ -639,7 +676,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 3, + request_type: RequestType::WithdrawCollateral as u32, address: underlying.clone(), amount: 10_1234567, }, @@ -712,7 +749,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 3, + request_type: RequestType::WithdrawCollateral as u32, address: underlying.clone(), amount: 21_0000000, }, @@ -776,7 +813,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: underlying.clone(), amount: 10_1234567, }, @@ -847,7 +884,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: underlying.clone(), amount: 10_1234567, }, @@ -921,7 +958,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: underlying.clone(), amount: 21_0000000, }, @@ -997,32 +1034,32 @@ mod tests { let requests = vec![ &e, Request { - request_type: 0, + request_type: RequestType::Supply as u32, address: underlying.clone(), amount: 10_0000000, }, Request { - request_type: 1, + request_type: RequestType::Withdraw as u32, address: underlying.clone(), amount: 5_0000000, }, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: underlying.clone(), amount: 10_0000000, }, Request { - request_type: 3, + request_type: RequestType::WithdrawCollateral as u32, address: underlying.clone(), amount: 5_0000000, }, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: underlying.clone(), amount: 20_0000000, }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: underlying.clone(), amount: 21_0000000, }, @@ -1162,7 +1199,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 6, + request_type: RequestType::FillUserLiquidationAuction as u32, address: samwise.clone(), amount: 50, }, @@ -1294,7 +1331,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 7, + request_type: RequestType::FillBadDebtAuction as u32, address: backstop_address.clone(), amount: 100, }, @@ -1418,7 +1455,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 8, + request_type: RequestType::FillInterestAuction as u32, address: backstop_address.clone(), amount: 100, }, @@ -1501,7 +1538,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 9, + request_type: RequestType::DeleteLiquidationAuction as u32, address: Address::generate(&e), amount: 0, }, @@ -1570,7 +1607,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 3, + request_type: RequestType::WithdrawCollateral as u32, address: underlying_1.clone(), amount: 20, }, @@ -1627,7 +1664,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: underlying.clone(), amount: 1_0000000, }, diff --git a/pool/src/pool/mod.rs b/pool/src/pool/mod.rs index 01c7b3dc..b07cbe64 100644 --- a/pool/src/pool/mod.rs +++ b/pool/src/pool/mod.rs @@ -1,5 +1,5 @@ mod actions; -pub use actions::Request; +pub use actions::{Request, RequestType}; mod bad_debt; pub use bad_debt::transfer_bad_debt_to_backstop; diff --git a/pool/src/pool/submit.rs b/pool/src/pool/submit.rs index f6dc075d..0ef7c219 100644 --- a/pool/src/pool/submit.rs +++ b/pool/src/pool/submit.rs @@ -65,7 +65,7 @@ pub fn execute_submit( mod tests { use crate::{ storage::{self, PoolConfig}, - testutils, + testutils, RequestType, }; use super::*; @@ -138,12 +138,12 @@ mod tests { let requests = vec![ &e, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: underlying_0, amount: 15_0000000, }, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: underlying_1, amount: 1_5000000, }, @@ -228,12 +228,12 @@ mod tests { let requests = vec![ &e, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: underlying_0, amount: 15_0000000, }, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: underlying_1, amount: 1_7500000, }, @@ -293,7 +293,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: underlying_0, amount: 15_0000000, }, @@ -353,7 +353,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: underlying_0, amount: 15_0000000, }, @@ -413,7 +413,7 @@ mod tests { let requests = vec![ &e, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: underlying_0, amount: 15_0000000, }, diff --git a/test-suites/fuzz/lib.rs b/test-suites/fuzz/lib.rs index 18de93d0..9cfdd2aa 100644 --- a/test-suites/fuzz/lib.rs +++ b/test-suites/fuzz/lib.rs @@ -137,7 +137,7 @@ impl Supply { &vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: token, amount: self.amount, }, @@ -164,7 +164,7 @@ impl Withdraw { &vec![ &fixture.env, Request { - request_type: 3, + request_type: RequestType::WithdrawCollateral as u32, address: token, amount: self.amount, }, @@ -191,7 +191,7 @@ impl Borrow { &vec![ &fixture.env, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: token, amount: self.amount, }, @@ -218,7 +218,7 @@ impl Repay { &vec![ &fixture.env, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: token, amount: self.amount, }, diff --git a/test-suites/src/setup.rs b/test-suites/src/setup.rs index b26ca669..edf3b732 100644 --- a/test-suites/src/setup.rs +++ b/test-suites/src/setup.rs @@ -1,4 +1,4 @@ -use pool::{Request, ReserveEmissionMetadata}; +use pool::{Request, RequestType, ReserveEmissionMetadata}; use soroban_sdk::{testutils::Address as _, vec as svec, Address, Symbol, Vec as SVec}; use crate::{ @@ -109,12 +109,12 @@ pub fn create_fixture_with_data<'a>(wasm: bool) -> TestFixture<'a> { let requests: SVec = svec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 10_000 * 10i128.pow(6), }, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 8_000 * 10i128.pow(6), }, @@ -125,12 +125,12 @@ pub fn create_fixture_with_data<'a>(wasm: bool) -> TestFixture<'a> { let requests: SVec = svec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::WETH].address.clone(), amount: 10 * 10i128.pow(9), }, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::WETH].address.clone(), amount: 5 * 10i128.pow(9), }, @@ -141,12 +141,12 @@ pub fn create_fixture_with_data<'a>(wasm: bool) -> TestFixture<'a> { let requests: SVec = svec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: 100_000 * SCALAR_7, }, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: 65_000 * SCALAR_7, }, diff --git a/test-suites/tests/test_liquidation.rs b/test-suites/tests/test_liquidation.rs index 6a7ce85b..f353bca6 100644 --- a/test-suites/tests/test_liquidation.rs +++ b/test-suites/tests/test_liquidation.rs @@ -1,6 +1,6 @@ #![cfg(test)] use cast::i128; -use pool::{PoolDataKey, Positions, Request, ReserveConfig, ReserveData}; +use pool::{PoolDataKey, Positions, Request, RequestType, ReserveConfig, ReserveData}; use soroban_fixed_point_math::FixedPoint; use soroban_sdk::{ testutils::{Address as AddressTestTrait, Events}, @@ -22,32 +22,32 @@ fn test_liquidations() { let requests: Vec = vec![ &fixture.env, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 1, }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 1, }, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: 1, }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: 1, }, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::WETH].address.clone(), amount: 1, }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: fixture.tokens[TokenIndex::WETH].address.clone(), amount: 1, }, @@ -95,7 +95,7 @@ fn test_liquidations() { let frodo_requests: Vec = vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 30_000 * 10i128.pow(6), }, @@ -108,23 +108,23 @@ fn test_liquidations() { let sam_requests: Vec = vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: 160_000 * SCALAR_7, }, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::WETH].address.clone(), amount: 17 * 10i128.pow(9), }, // Sam's max borrow is 39_200 STABLE Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 28_000 * 10i128.pow(6), }, // reduces Sam's max borrow to 14_526.31579 STABLE Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: 65_000 * SCALAR_7, }, @@ -252,27 +252,27 @@ fn test_liquidations() { let fill_requests = vec![ &fixture.env, Request { - request_type: 6, + request_type: RequestType::FillUserLiquidationAuction as u32, address: samwise.clone(), amount: 25, }, Request { - request_type: 6, + request_type: RequestType::FillUserLiquidationAuction as u32, address: samwise.clone(), amount: 100, }, Request { - request_type: 8, + request_type: RequestType::FillInterestAuction as u32, address: fixture.backstop.address.clone(), //address shouldn't matter amount: 99, }, Request { - request_type: 8, + request_type: RequestType::FillInterestAuction as u32, address: fixture.backstop.address.clone(), //address shouldn't matter amount: 100, }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: usdc_bid_amount, }, @@ -463,19 +463,19 @@ fn test_liquidations() { let fill_requests = vec![ &fixture.env, Request { - request_type: 6, + request_type: RequestType::FillUserLiquidationAuction as u32, address: samwise.clone(), amount: 100, }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: usdc_bid_amount .fixed_div_floor(2_0000000, SCALAR_7) .unwrap(), }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: xlm_bid_amount.fixed_div_floor(2_0000000, SCALAR_7).unwrap(), }, @@ -595,7 +595,7 @@ fn test_liquidations() { let bad_debt_fill_request = vec![ &fixture.env, Request { - request_type: 7, + request_type: RequestType::FillBadDebtAuction as u32, address: fixture.backstop.address.clone(), amount: 20, }, @@ -704,7 +704,7 @@ fn test_liquidations() { let bad_debt_fill_request = vec![ &fixture.env, Request { - request_type: 7, + request_type: RequestType::FillBadDebtAuction as u32, address: fixture.backstop.address.clone(), amount: 100, }, @@ -784,13 +784,13 @@ fn test_liquidations() { let sam_requests: Vec = vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::WETH].address.clone(), amount: 1 * 10i128.pow(9), }, // Sam's max borrow is 39_200 STABLE Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 100 * 10i128.pow(6), }, // reduces Sam's max borrow to 14_526.31579 STABLE @@ -849,7 +849,7 @@ fn test_liquidations() { let bad_debt_fill_request = vec![ &fixture.env, Request { - request_type: 6, + request_type: RequestType::FillUserLiquidationAuction as u32, address: samwise.clone(), amount: 100, }, @@ -895,7 +895,7 @@ fn test_liquidations() { let bump_usdc = vec![ &fixture.env, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 1, }, @@ -928,7 +928,7 @@ fn test_liquidations() { let bad_debt_fill_request = vec![ &fixture.env, Request { - request_type: 7, + request_type: RequestType::FillBadDebtAuction as u32, address: fixture.backstop.address.clone(), amount: 100, }, @@ -998,12 +998,12 @@ fn test_user_restore_position_and_delete_liquidation() { let setup_request: Vec = vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 1000 * 10i128.pow(6), }, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: 6075 * SCALAR_7, }, @@ -1030,7 +1030,7 @@ fn test_user_restore_position_and_delete_liquidation() { let delete_only_request: Vec = vec![ &fixture.env, Request { - request_type: 9, + request_type: RequestType::DeleteLiquidationAuction as u32, address: Address::generate(&fixture.env), amount: i128::MAX, }, @@ -1045,12 +1045,12 @@ fn test_user_restore_position_and_delete_liquidation() { let short_supply_delete_request: Vec = vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 79 * 10i128.pow(6), // need $80 more collateral }, Request { - request_type: 9, + request_type: RequestType::DeleteLiquidationAuction as u32, address: Address::generate(&fixture.env), amount: i128::MAX, }, @@ -1067,12 +1067,12 @@ fn test_user_restore_position_and_delete_liquidation() { let short_repay_delete_request: Vec = vec![ &fixture.env, Request { - request_type: 9, + request_type: RequestType::DeleteLiquidationAuction as u32, address: Address::generate(&fixture.env), amount: i128::MAX, }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: 449 * SCALAR_7, // need to repay 450 XLM }, @@ -1090,17 +1090,17 @@ fn test_user_restore_position_and_delete_liquidation() { let delete_request: Vec = vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 41 * 10i128.pow(6), }, Request { - request_type: 9, + request_type: RequestType::DeleteLiquidationAuction as u32, address: Address::generate(&fixture.env), amount: i128::MAX, }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: 226 * SCALAR_7, }, diff --git a/test-suites/tests/test_overflow_flag.rs b/test-suites/tests/test_overflow_flag.rs index e8be5865..5c991b4d 100644 --- a/test-suites/tests/test_overflow_flag.rs +++ b/test-suites/tests/test_overflow_flag.rs @@ -1,5 +1,5 @@ #![cfg(test)] -use pool::Request; +use pool::{Request, RequestType}; use soroban_sdk::{testutils::Address as AddressTestTrait, vec, Address, Vec}; use test_suites::{ create_fixture_with_data, @@ -47,12 +47,12 @@ fn test_auction_underflow_panics() { let sam_requests: Vec = vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: fixture.tokens[TokenIndex::XLM].address.clone(), amount: 6_000 * SCALAR_7, }, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: 200 * 10i128.pow(6), }, @@ -84,12 +84,12 @@ fn test_auction_underflow_panics() { let fill_requests = vec![ &fixture.env, Request { - request_type: 6, + request_type: RequestType::FillUserLiquidationAuction as u32, address: samwise.clone(), amount: 1, }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: fixture.tokens[TokenIndex::STABLE].address.clone(), amount: usdc_bid_amount, }, diff --git a/test-suites/tests/test_pool.rs b/test-suites/tests/test_pool.rs index 9bd8866e..b18397a6 100644 --- a/test-suites/tests/test_pool.rs +++ b/test-suites/tests/test_pool.rs @@ -1,6 +1,6 @@ #![cfg(test)] -use pool::{Request, ReserveEmissionMetadata}; +use pool::{Request, RequestType, ReserveEmissionMetadata}; use soroban_fixed_point_math::FixedPoint; use soroban_sdk::{ testutils::{Address as _, AuthorizedFunction, AuthorizedInvocation, Events}, @@ -45,7 +45,7 @@ fn test_pool_user() { let requests = vec![ &fixture.env, Request { - request_type: 0, + request_type: RequestType::Supply as u32, address: weth.address.clone(), amount, }, @@ -131,7 +131,7 @@ fn test_pool_user() { let requests = vec![ &fixture.env, Request { - request_type: 1, + request_type: RequestType::Withdraw as u32, address: weth.address.clone(), amount, }, @@ -201,7 +201,7 @@ fn test_pool_user() { let requests = vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: xlm.address.clone(), amount, }, @@ -284,7 +284,7 @@ fn test_pool_user() { let requests = vec![ &fixture.env, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: weth.address.clone(), amount, }, @@ -367,12 +367,12 @@ fn test_pool_user() { let requests = vec![ &fixture.env, Request { - request_type: 3, + request_type: RequestType::WithdrawCollateral as u32, address: xlm.address.clone(), amount: amount_withdrawal, }, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: weth.address.clone(), amount: amount_repay, }, diff --git a/test-suites/tests/test_wasm_happy_path.rs b/test-suites/tests/test_wasm_happy_path.rs index 8897951e..3686f6ea 100644 --- a/test-suites/tests/test_wasm_happy_path.rs +++ b/test-suites/tests/test_wasm_happy_path.rs @@ -1,6 +1,6 @@ #![cfg(test)] -use pool::Request; +use pool::{Request, RequestType}; use soroban_fixed_point_math::FixedPoint; use soroban_sdk::{testutils::Address as _, vec, Address}; use test_suites::{ @@ -51,7 +51,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: stable.address.clone(), amount, }, @@ -83,7 +83,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 2, + request_type: RequestType::SupplyCollateral as u32, address: xlm.address.clone(), amount, }, @@ -112,7 +112,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: stable.address.clone(), amount, }, @@ -144,7 +144,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 4, + request_type: RequestType::Borrow as u32, address: xlm.address.clone(), amount, }, @@ -247,7 +247,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: stable.address.clone(), amount, }, @@ -279,7 +279,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: xlm.address.clone(), amount, }, @@ -308,7 +308,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 3, + request_type: RequestType::WithdrawCollateral as u32, address: xlm.address.clone(), amount, }, @@ -337,7 +337,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 3, + request_type: RequestType::WithdrawCollateral as u32, address: stable.address.clone(), amount, }, @@ -473,7 +473,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: stable.address.clone(), amount: amount, }, @@ -505,7 +505,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 5, + request_type: RequestType::Repay as u32, address: xlm.address.clone(), amount: amount, }, @@ -538,7 +538,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 3, + request_type: RequestType::WithdrawCollateral as u32, address: xlm.address.clone(), amount: amount, }, @@ -566,7 +566,7 @@ fn test_wasm_happy_path() { &vec![ &fixture.env, Request { - request_type: 3, + request_type: RequestType::WithdrawCollateral as u32, address: stable.address.clone(), amount: amount, },