From 48f8f86795e838ae0abe3f6c240df553aab38069 Mon Sep 17 00:00:00 2001 From: mootz12 Date: Thu, 7 Mar 2024 09:36:19 -0500 Subject: [PATCH] backstop: optimize backstop threshold calculation (#211) --- backstop/src/backstop/pool.rs | 16 ++++------------ pool/src/pool/status.rs | 5 +++-- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/backstop/src/backstop/pool.rs b/backstop/src/backstop/pool.rs index e54cebc0..6f5b1e6a 100644 --- a/backstop/src/backstop/pool.rs +++ b/backstop/src/backstop/pool.rs @@ -60,13 +60,6 @@ pub fn require_is_from_pool_factory(e: &Env, address: &Address, balance: i128) { /// Calculate the threshold for the pool's backstop balance /// /// Returns true if the pool's backstop balance is above the threshold -/// NOTE: The calculation is the percentage^5 to simplify the calculation of the pools product constant. -/// Some useful calculation results: -/// - greater than 1 = 100+% -/// - 1_0000000 = 100% -/// - 0_0000100 = ~10% -/// - 0_0000003 = ~5% -/// - 0_0000000 = ~0-4% pub fn require_pool_above_threshold(pool_backstop_data: &PoolBackstopData) -> bool { // @dev: Calculation for pools product constant of underlying will often overflow i128 // so saturating mul is used. This is safe because the threshold is below i128::MAX and the @@ -74,17 +67,16 @@ pub fn require_pool_above_threshold(pool_backstop_data: &PoolBackstopData) -> bo // The calculation is: // - Threshold % = (bal_blnd^4 * bal_usdc) / PC^5 such that PC is 200k let threshold_pc = 320_000_000_000_000_000_000_000_000i128; // 3.2e26 (200k^5) - // floor balances to nearest full unit and calculate saturated pool product constant - // and scale to SCALAR_7 to get final division result in SCALAR_7 points + + // floor balances to nearest full unit and calculate saturated pool product constant let bal_blnd = pool_backstop_data.blnd / SCALAR_7; let bal_usdc = pool_backstop_data.usdc / SCALAR_7; let saturating_pool_pc = bal_blnd .saturating_mul(bal_blnd) .saturating_mul(bal_blnd) .saturating_mul(bal_blnd) - .saturating_mul(bal_usdc) - .saturating_mul(SCALAR_7); // 10^7 * 10^7 - saturating_pool_pc / threshold_pc >= SCALAR_7 + .saturating_mul(bal_usdc); + saturating_pool_pc >= threshold_pc } /// The pool's backstop balances diff --git a/pool/src/pool/status.rs b/pool/src/pool/status.rs index 0a3b040b..1b939e27 100644 --- a/pool/src/pool/status.rs +++ b/pool/src/pool/status.rs @@ -133,8 +133,9 @@ pub fn calc_pool_backstop_threshold(pool_backstop_data: &PoolBackstopData) -> i1 // The calculation is: // - Threshold % = (bal_blnd^4 * bal_usdc) / PC^5 such that PC is 200k let threshold_pc = 320_000_000_000_000_000_000_000_000i128; // 3.2e26 (200k^5) - // floor balances to nearest full unit and calculate saturated pool product constant - // and scale to SCALAR_7 to get final division result in SCALAR_7 points + + // floor balances to nearest full unit and calculate saturated pool product constant + // and scale to SCALAR_7 to get final division result in SCALAR_7 points let bal_blnd = pool_backstop_data.blnd / SCALAR_7; let bal_usdc = pool_backstop_data.usdc / SCALAR_7; let saturating_pool_pc = bal_blnd