Skip to content

Commit e441357

Browse files
committed
test(wad_ray_math): add test_directional_rounding_protocol_safety test
- Add protocol safety test for directional rounding functions - Test small debt calculation scenario (should not round to zero) - Test collateral calculation scenario (should not overestimate) - Test interest calculation precision with exact rate multiplication - Verify directional rounding provides protocol safety benefits in real-world scenarios
1 parent 14b95fd commit e441357

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

aave-core/aave-math/tests/wad_ray_math_tests.move

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,36 @@ module aave_math::wad_ray_math_tests {
413413
assert!(mul_down == 210000000000000000000000000, TEST_SUCCESS); // Same result for exact multiplication
414414
assert!(mul_up == mul_down, TEST_SUCCESS); // Should be equal for exact results
415415
}
416+
417+
#[test]
418+
fun test_directional_rounding_protocol_safety() {
419+
// Test scenarios that demonstrate protocol safety benefits
420+
// These tests simulate real-world scenarios where rounding direction matters
421+
422+
// Scenario 1: Small debt calculation (should not round to zero)
423+
let small_debt = 1; // 1 wei
424+
let high_index = 2000000000000000000000000000; // 2000 RAY (high index)
425+
let debt_up = ray_div_up(small_debt, high_index);
426+
// (1 * RAY + 2000000000000000000000000000 - 1) / 2000000000000000000000000000
427+
// = (1000000000000000000000000000 + 1999999999999999999999999999) / 2000000000000000000000000000
428+
// = 2999999999999999999999999999 / 2000000000000000000000000000 = 1 (rounded up)
429+
assert!(debt_up == 1, TEST_SUCCESS);
430+
431+
// Scenario 2: Collateral calculation (should not overestimate)
432+
let collateral = 999999999999999999999999999; // Just under 1000 RAY
433+
let index = 1000000000000000000000000000; // 1000 RAY
434+
let collateral_down = ray_div_down(collateral, index);
435+
// (999999999999999999999999999 * 1000000000000000000000000000) / 1000000000000000000000000000
436+
// = 999999999999999999999999999 (exact)
437+
assert!(collateral_down == 999999999999999999999999999, TEST_SUCCESS);
438+
439+
// Scenario 3: Interest calculation precision
440+
let principal = 1000000000000000000000000000; // 1000 RAY
441+
let rate = 1050000000000000000000000000; // 1.05 RAY (5% annual rate)
442+
let interest_up = ray_mul_up(principal, rate);
443+
let interest_down = ray_mul_down(principal, rate);
444+
// Interest should be 1050 RAY exactly, but test rounding behavior
445+
assert!(interest_up == 1050000000000000000000000000, TEST_SUCCESS);
446+
assert!(interest_down == 1050000000000000000000000000, TEST_SUCCESS);
447+
}
416448
}

0 commit comments

Comments
 (0)