Skip to content

Commit 6463bfb

Browse files
authored
Merge pull request #79 from aave/mike/improve/rounding-comments
Mike/improve/rounding comments
2 parents 34d1aec + 38aa074 commit 6463bfb

File tree

11 files changed

+20
-673
lines changed

11 files changed

+20
-673
lines changed

aave-core/sources/aave-logic/flashloan_logic.move

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,6 @@ module aave_pool::flashloan_logic {
748748
let a_token_total_supply = a_token_factory::total_supply(a_token_address);
749749
let reserve_accrued_to_treasury =
750750
pool::get_reserve_accrued_to_treasury(reserve_data);
751-
// Note: Use ray_mul_down for conservative liquidity calculation
752-
// Ensures available flashloan liquidity is not overestimated
753-
// Prevents protocol from lending more than actually available
754751
let total_liquidity =
755752
a_token_total_supply
756753
+ wad_ray_math::ray_mul_down(
@@ -765,9 +762,6 @@ module aave_pool::flashloan_logic {
765762
pool_logic::set_next_liquidity_index(&mut reserve_cache, next_liquidity_index);
766763

767764
// update accrued to treasury
768-
// Note: Use ray_div_down for conservative treasury accrual
769-
// Ensures protocol treasury doesn't accumulate optimistic amounts
770-
// Aligns with mint_to_treasury's conservative minting approach
771765
let new_reserve_accrued_to_treasury =
772766
reserve_accrued_to_treasury
773767
+ wad_ray_math::ray_div_down(premium_to_protocol, next_liquidity_index);

aave-core/sources/aave-logic/generic_logic.move

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ module aave_pool::generic_logic {
3434

3535
if (user_total_debt != 0) {
3636
let normalized_debt = pool::get_normalized_debt_by_reserve_data(reserve_data);
37-
// Note: Use ray_mul_up for conservative debt calculation
38-
// Ensures user debt is never underestimated in health factor calculations
3937
user_total_debt = wad_ray_math::ray_mul_up(user_total_debt, normalized_debt);
4038
};
4139

42-
// Note: Use ceil_div for conservative debt conversion to base currency
43-
// Prevents rounding down small debt amounts to zero
44-
// Critical for accurate debt tracking in low-price or small-amount scenarios
4540
math_utils::ceil_div(asset_price * user_total_debt, asset_unit)
4641
}
4742

@@ -59,8 +54,6 @@ module aave_pool::generic_logic {
5954
asset_unit: u256
6055
): u256 {
6156
let normalized_income = pool::get_normalized_income_by_reserve_data(reserve_data);
62-
// Note: Use ray_mul_down for conservative collateral calculation
63-
// Ensures user collateral is never overestimated in health factor calculations
6457
let balance =
6558
wad_ray_math::ray_mul_down(
6659
a_token_factory::scaled_balance_of(

aave-core/sources/aave-logic/liquidation_logic.move

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ module aave_pool::liquidation_logic {
268268
vars.actual_collateral_to_liquidate,
269269
index,
270270
vars.collateral_a_token,
271-
true // Round up, consistent with burn path, ensure complete liquidation without dust
271+
true
272272
);
273273

274274
// For the special case of account_address == params.user (self-liquidation) the liquidator_previous_a_token_balance
@@ -625,16 +625,11 @@ module aave_pool::liquidation_logic {
625625
10, reserve_config::get_decimals(&debt_reserve_cache_config)
626626
);
627627

628-
// Note: Use ceil_div for conservative liquidation debt calculation
629-
// Ensures debt is never underestimated during liquidation
630-
// Critical for accurate liquidation bonus and collateral seizure calculations
631628
vars.user_reserve_debt_in_base_currency = math_utils::ceil_div(
632629
vars.user_reserve_debt * vars.debt_asset_price,
633630
vars.debt_asset_unit
634631
);
635632

636-
// Note: Keep normal division for collateral (rounds down conservatively)
637-
// Ensures collateral is never overestimated during liquidation
638633
vars.user_reserve_collateral_in_base_currency =
639634
(vars.user_collateral_balance * vars.collateral_asset_price)
640635
/ vars.collateral_asset_unit;

aave-core/sources/aave-logic/supply_logic.move

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,6 @@ module aave_pool::supply_logic {
196196
// update pool state
197197
pool_logic::update_state(reserve_data, &mut reserve_cache);
198198

199-
// Note: Use ray_mul_down for conservative withdrawal balance calculation
200-
// Ensures user cannot withdraw more than their actual balance
201-
// Consistent with a_token_factory::balance_of behavior
202199
let user_balance =
203200
wad_ray_math::ray_mul_down(
204201
a_token_factory::scaled_balance_of(account_address, a_token_address),

aave-core/sources/aave-pool/pool_logic.move

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ module aave_pool::pool_logic {
9292
liquidity_added: u256,
9393
liquidity_taken: u256
9494
) {
95-
// Note: Use ray_mul_up for conservative interest rate calculation input
96-
// Ensures debt is not underestimated when calculating utilization rate
97-
// Higher debt estimation → higher rates → encourages repayment → safer protocol
9895
let total_variable_debt =
9996
wad_ray_math::ray_mul_up(
10097
reserve_cache.next_scaled_variable_debt,
@@ -440,9 +437,6 @@ module aave_pool::pool_logic {
440437
math_utils::percent_mul(total_debt_accrued, reserve_cache.reserve_factor);
441438

442439
if (amount_to_mint != 0) {
443-
// Note: Use ray_div_down for conservative treasury accrual
444-
// Ensures protocol treasury doesn't accumulate optimistic amounts
445-
// Consistent with mint_to_treasury and flashloan treasury accrual
446440
let new_accrued_to_treasury =
447441
pool::get_reserve_accrued_to_treasury(reserve_data)
448442
+ wad_ray_math::ray_div_down(

aave-core/sources/aave-pool/pool_token_logic.move

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ module aave_pool::pool_token_logic {
9393

9494
let normalized_income =
9595
pool::get_reserve_normalized_income(asset_address);
96-
// Note: Use ray_mul_down for conservative treasury minting
97-
// When converting scaled treasury accrual to actual amount,
98-
// round down to ensure protocol doesn't overestimate mintable amount
99-
// This aligns with the principle of favoring protocol safety
10096
let amount_to_mint =
10197
wad_ray_math::ray_mul_down(accrued_to_treasury, normalized_income);
10298

@@ -133,19 +129,12 @@ module aave_pool::pool_token_logic {
133129
a_token_factory::get_underlying_asset_address(a_token_address);
134130
let index = pool::get_reserve_normalized_income(underlying_asset);
135131

136-
// Note: Calculate sender's actual balance using ray_mul_down (consistent with balance_of)
137-
// This ensures proper detection of "full withdrawal" scenario for collateral flag updates
138-
// Atomic snapshot: reuses pre-fetched index to guarantee all balance calculations
139-
// are based on the same reserve state (prevents inconsistency if index updates mid-execution)
140132
let from_balance_before =
141133
wad_ray_math::ray_mul_down(
142134
a_token_factory::scaled_balance_of(sender_address, a_token_address),
143135
index
144136
);
145137

146-
// Note: Calculate recipient's actual balance using ray_mul_down (consistent with balance_of)
147-
// This ensures proper detection of "first-time receipt" scenario for collateral flag enabling
148-
// Atomic snapshot: reuses pre-fetched index to guarantee consistency with sender balance calculation
149138
let to_balance_before =
150139
wad_ray_math::ray_mul_down(
151140
a_token_factory::scaled_balance_of(recipient, a_token_address),
@@ -157,7 +146,7 @@ module aave_pool::pool_token_logic {
157146
amount,
158147
index,
159148
a_token_address,
160-
false // Use round half up for user-to-user transfers
149+
false
161150
);
162151

163152
finalize_transfer(

aave-core/sources/aave-tokens/a_token_factory.move

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ module aave_pool::a_token_factory {
212212
};
213213
let underlying_token_address = get_underlying_asset_address(metadata_address);
214214

215-
wad_ray_math::ray_mul_down(// Round down: count less asset (conservative)
215+
wad_ray_math::ray_mul_down(
216216
current_scaled_balance,
217217
pool::get_reserve_normalized_income(underlying_token_address)
218218
)
@@ -240,7 +240,7 @@ module aave_pool::a_token_factory {
240240

241241
let underlying_token_address = get_underlying_asset_address(metadata_address);
242242

243-
wad_ray_math::ray_mul_down(// Round down: count less total supply (conservative)
243+
wad_ray_math::ray_mul_down(
244244
current_supply_scaled,
245245
pool::get_reserve_normalized_income(underlying_token_address)
246246
)
@@ -492,7 +492,7 @@ module aave_pool::a_token_factory {
492492
amount,
493493
index,
494494
metadata_address,
495-
false // Round down: mint less aToken (safer for protocol)
495+
false
496496
)
497497
}
498498

@@ -519,7 +519,7 @@ module aave_pool::a_token_factory {
519519
amount,
520520
index,
521521
metadata_address,
522-
true // Round up: burn more aToken scaled balance (safer for protocol)
522+
true
523523
);
524524

525525
let token_data = get_token_data(metadata_address);
@@ -550,9 +550,6 @@ module aave_pool::a_token_factory {
550550
// Early return if amount is 0 to avoid unnecessary computation
551551
if (amount == 0) { return };
552552

553-
// Pre-calculate scaled amount to check for dust
554-
// Treasury accrued fees can be tiny (1-2 octa), which round down to 0
555-
// We must skip these dust amounts to prevent assert failure in token_base::mint_scaled
556553
let amount_scaled = wad_ray_math::ray_div_down(amount, index);
557554

558555
if (amount_scaled != 0) {
@@ -566,7 +563,7 @@ module aave_pool::a_token_factory {
566563
amount,
567564
index,
568565
metadata_address,
569-
false // Round down: mint less to treasury (conservative)
566+
false
570567
);
571568
}
572569
}
@@ -621,23 +618,13 @@ module aave_pool::a_token_factory {
621618
) acquires TokenMap {
622619
assert_token_exists(metadata_address);
623620

624-
// Pre-calculate scaled amount using directional rounding based on rounding_up parameter
625-
// Reasons for pre-calculation:
626-
// 1. Event accuracy: Must match the actual transferred scaled amount for event consistency
627-
// 2. Dust handling: Liquidation protocol fees can be tiny (1-2 octa), which may round to 0
628-
// We must check and skip these dust transfers to prevent assert failure in token_base::transfer
629-
// 3. Directional rounding: rounding_up=true for collateral transfer (favor protocol, consistent with burn),
630-
// rounding_up=false for protocol fees (conservative charging, favor user)
631621
let amount_scaled =
632622
if (rounding_up) {
633-
wad_ray_math::ray_div_up(amount, index) // Round up, consistent with burn path
623+
wad_ray_math::ray_div_up(amount, index)
634624
} else {
635-
wad_ray_math::ray_div(amount, index) // Round half up, conservative charging
625+
wad_ray_math::ray_div(amount, index)
636626
};
637627

638-
// Skip transfer if scaled amount is 0 (dust)
639-
// This prevents assertion failure in token_base::transfer
640-
// Dust amounts are acceptable to skip as they are too small to be meaningful
641628
if (amount_scaled == 0) { return };
642629

643630
token_base::transfer(
@@ -649,8 +636,6 @@ module aave_pool::a_token_factory {
649636
rounding_up
650637
);
651638

652-
// Emit event with the actual transferred scaled amount
653-
// This ensures event accurately reflects the on-chain state change
654639
events::emit_balance_transfer(
655640
from,
656641
to,
@@ -772,6 +757,6 @@ module aave_pool::a_token_factory {
772757
index: u256,
773758
metadata_address: address
774759
) acquires TokenMap {
775-
transfer_on_liquidation(from, to, amount, index, metadata_address, false) // Use round half up, conservative handling
760+
transfer_on_liquidation(from, to, amount, index, metadata_address, false)
776761
}
777762
}

aave-core/sources/aave-tokens/variable_debt_token_factory.move

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ module aave_pool::variable_debt_token_factory {
131131
};
132132
let underlying_token_address = get_underlying_asset_address(metadata_address);
133133

134-
wad_ray_math::ray_mul_up(// Round up: count more debt (conservative)
134+
wad_ray_math::ray_mul_up(
135135
current_scaled_balance,
136136
pool::get_reserve_normalized_variable_debt(underlying_token_address)
137137
)
@@ -159,7 +159,7 @@ module aave_pool::variable_debt_token_factory {
159159

160160
let underlying_token_address = get_underlying_asset_address(metadata_address);
161161

162-
wad_ray_math::ray_mul_up(// Round up: count more total debt (conservative)
162+
wad_ray_math::ray_mul_up(
163163
current_supply_scaled,
164164
pool::get_reserve_normalized_variable_debt(underlying_token_address)
165165
)
@@ -345,7 +345,7 @@ module aave_pool::variable_debt_token_factory {
345345
amount,
346346
index,
347347
metadata_address,
348-
true // Round up: mint more debt token (safer for protocol)
348+
true
349349
)
350350
}
351351

@@ -383,7 +383,7 @@ module aave_pool::variable_debt_token_factory {
383383
amount,
384384
index,
385385
metadata_address,
386-
false // Round down: burn less debt token scaled balance (safer for protocol)
386+
false
387387
);
388388
}
389389

0 commit comments

Comments
 (0)