Skip to content

Commit

Permalink
Merge pull request #212 from blend-capital/optimize-threshold-check
Browse files Browse the repository at this point in the history
backstop: optimize backstop threshold calculation (#211)
  • Loading branch information
mootz12 authored Mar 14, 2024
2 parents ea26698 + 48f8f86 commit 943c8c7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
16 changes: 4 additions & 12 deletions backstop/src/backstop/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,23 @@ 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
// protocol does not need to differentiate between pools over the threshold product constant.
// 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
Expand Down
5 changes: 3 additions & 2 deletions pool/src/pool/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 943c8c7

Please sign in to comment.