Skip to content

Commit 6a2b16a

Browse files
committed
feat(pool-token-logic): use ray_mul_down for transfer balance checks
Apply directional rounding for balance consistency in transfer function. Changes in transfer() function: - Calculate from_balance_before using ray_mul_down instead of ray_mul - Calculate to_balance_before using ray_mul_down instead of ray_mul - Reuses pre-fetched index for atomic snapshot guarantee Rationale: - Matches a_token_factory::balance_of behavior (uses ray_mul_down) - Ensures atomic consistency: all balance calculations use same index value - Prevents race conditions where index changes between balance calculations - Enables correct detection of full withdrawal and first-time receipt scenarios - Manual calculation reuses pre-fetched index (also used by token_base::transfer and balance transfer event), ensuring consistent state snapshot Impact: - Fixes collateral flag updates for full withdrawal and first receipt - Guarantees atomic snapshot of user balances (critical for HF validation) - Consistent with user-facing balance_of query results - Gas optimized: single index fetch reused across all operations - Aligns with Solidity Aave V3 implementation pattern Security: Prevents non-atomic balance calculations that could lead to inconsistent state and incorrect health factor validation
1 parent 3fe1067 commit 6a2b16a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,22 @@ module aave_pool::pool_token_logic {
132132
let underlying_asset =
133133
a_token_factory::get_underlying_asset_address(a_token_address);
134134
let index = pool::get_reserve_normalized_income(underlying_asset);
135+
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)
135140
let from_balance_before =
136-
wad_ray_math::ray_mul(
141+
wad_ray_math::ray_mul_down(
137142
a_token_factory::scaled_balance_of(sender_address, a_token_address),
138143
index
139144
);
145+
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
140149
let to_balance_before =
141-
wad_ray_math::ray_mul(
150+
wad_ray_math::ray_mul_down(
142151
a_token_factory::scaled_balance_of(recipient, a_token_address),
143152
index
144153
);

0 commit comments

Comments
 (0)