Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions aave-core/aave-math/sources/math_utils.move
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ module aave_math::math_utils {
result
}

/// @notice Performs division with ceiling (rounding up)
/// @dev This function rounds up the result of division, ensuring that any remainder
/// is accounted for by adding 1 to the quotient
/// @param numerator The numerator value
/// @param denominator The denominator value
/// @return The result of division rounded up
public fun ceil_div(numerator: u256, denominator: u256): u256 {
assert!(denominator > 0, error_config::get_edivision_by_zero());
if (numerator == 0) {
return 0
};
// Add denominator - 1 to numerator before division to achieve ceiling division
(numerator + denominator - 1) / denominator
}

/// @dev Returns the seconds per year value
/// @return Seconds per year constant
public fun get_seconds_per_year(): u256 {
Expand Down
25 changes: 25 additions & 0 deletions aave-core/aave-math/tests/math_utils_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module aave_math::math_utils_tests {
use aave_math::math_utils::{
calculate_compounded_interest_now,
calculate_linear_interest,
ceil_div,
get_half_percentage_factor_for_testing,
get_percentage_factor,
get_percentage_factor_for_testing,
Expand Down Expand Up @@ -230,4 +231,28 @@ module aave_math::math_utils_tests {
fun test_calculate_compounded_interest_with_current_time_less_than_last_update_timestamp() {
calculate_compounded_interest(ray(), 1000000008, 1000000000);
}

#[test]
fun test_ceil_div() {
// Test basic cases
assert!(ceil_div(10, 3) == 4, TEST_SUCCESS); // 10/3 = 3.33... -> 4
assert!(ceil_div(10, 5) == 2, TEST_SUCCESS); // 10/5 = 2.0 -> 2
assert!(ceil_div(10, 7) == 2, TEST_SUCCESS); // 10/7 = 1.42... -> 2

// Test edge cases
assert!(ceil_div(0, 5) == 0, TEST_SUCCESS); // 0/5 = 0 -> 0
assert!(ceil_div(1, 1) == 1, TEST_SUCCESS); // 1/1 = 1 -> 1
assert!(ceil_div(1, 2) == 1, TEST_SUCCESS); // 1/2 = 0.5 -> 1

// Test large numbers
assert!(ceil_div(1000000, 999999) == 2, TEST_SUCCESS); // 1000000/999999 = 1.000001... -> 2
assert!(ceil_div(1000000, 1000000) == 1, TEST_SUCCESS); // 1000000/1000000 = 1.0 -> 1
assert!(ceil_div(1000000, 1000001) == 1, TEST_SUCCESS); // 1000000/1000001 = 0.999999... -> 1
}

#[test]
#[expected_failure(abort_code = EDIVISION_BY_ZERO, location = aave_math::math_utils)]
fun test_ceil_div_by_zero() {
ceil_div(10, 0);
}
}
2 changes: 1 addition & 1 deletion aave-core/sources/aave-logic/borrow_logic.move
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ module aave_pool::borrow_logic {

let next_isolation_mode_total_debt =
(isolation_mode_total_debt as u256)
+ (amount / math_utils::pow(10, decimals));
+ (math_utils::ceil_div(amount, math_utils::pow(10, decimals)));
// Update isolation_mode_total_debt
pool::set_reserve_isolation_mode_total_debt(
isolation_mode_collateral_reserve_data,
Expand Down
3 changes: 2 additions & 1 deletion aave-core/sources/aave-logic/isolation_mode_logic.move
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ module aave_pool::isolation_mode_logic {
reserve_config::get_decimals(&reserve_config_map)
- reserve_config::get_debt_ceiling_decimals();

let isolated_debt_repaid = (repay_amount / math_utils::pow(10, debt_decimals));
let isolated_debt_repaid =
(math_utils::ceil_div(repay_amount, math_utils::pow(10, debt_decimals)));

// since the debt ceiling does not take into account the interest accrued, it might happen that amount
// repaid > debt in isolation mode
Expand Down
6 changes: 4 additions & 2 deletions aave-core/sources/aave-logic/validation_logic.move
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,16 @@ module aave_pool::validation_logic {
let total_debt =
isolation_mode_total_debt
+ (
amount
/ math_utils::pow(
math_utils::ceil_div(
amount,
math_utils::pow(
10,
(
reserve_decimals
- reserve_config::get_debt_ceiling_decimals()
)
)
)
);
assert!(
total_debt <= isolation_mode_debt_ceiling,
Expand Down
Loading
Loading