Skip to content

Commit f34fb33

Browse files
committed
feat(token_base): add directional rounding support to transfer function
- Add rounding_up: bool parameter to token_base::transfer - Update a_token_factory::transfer_on_liquidation to use rounding parameter - Update pool_token_logic::transfer to pass rounding_up=false for user transfers - Update liquidation calls to use appropriate rounding: - Collateral transfer: rounding_up=true (favor protocol, consistent with burn) - Protocol fee transfer: rounding_up=false (conservative charging) - Align with existing mint_scaled/burn_scaled API patterns - Ensure complete liquidation without dust accumulation
1 parent 5f87000 commit f34fb33

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ module aave_pool::pool_token_logic {
156156
recipient,
157157
amount,
158158
index,
159-
a_token_address
159+
a_token_address,
160+
false // Use round half up for user-to-user transfers
160161
);
161162

162163
finalize_transfer(

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,14 @@ module aave_pool::a_token_factory {
640640
// Dust amounts are acceptable to skip as they are too small to be meaningful
641641
if (amount_scaled == 0) { return };
642642

643-
token_base::transfer(from, to, amount, index, metadata_address);
643+
token_base::transfer(
644+
from,
645+
to,
646+
amount,
647+
index,
648+
metadata_address,
649+
rounding_up
650+
);
644651

645652
// Emit event with the actual transferred scaled amount
646653
// This ensures event accurately reflects the on-chain state change

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,19 +506,26 @@ module aave_pool::token_base {
506506
/// @param amount The amount being transferred
507507
/// @param index The next liquidity index of the reserve
508508
/// @param metadata_address The address of the token
509+
/// @param rounding_up If true, rounds up the scaled amount; if false, rounds half up
509510
public(friend) fun transfer(
510511
sender: address,
511512
recipient: address,
512513
amount: u256,
513514
index: u256,
514-
metadata_address: address
515+
metadata_address: address,
516+
rounding_up: bool
515517
) acquires ManagedFungibleAsset, TokenBaseState {
516518
if (amount == 0) {
517519
return;
518520
};
519521
// NOTE: in `ray_div`, while `amount` can be less precision than Ray
520522
// precision, `index` must be expressed in Ray precision.
521-
let amount_scaled = wad_ray_math::ray_div(amount, index);
523+
let amount_scaled =
524+
if (rounding_up) {
525+
wad_ray_math::ray_div_up(amount, index)
526+
} else {
527+
wad_ray_math::ray_div(amount, index)
528+
};
522529
assert!(amount_scaled != 0, error_config::get_einvalid_transfer_amount());
523530

524531
// update sender balance

0 commit comments

Comments
 (0)