Skip to content

Commit

Permalink
Correct implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
axiongsupra committed Dec 19, 2024
1 parent f0b7605 commit 88bb981
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
14 changes: 14 additions & 0 deletions aptos-move/framework/aptos-stdlib/sources/fixed_point64.move
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ module aptos_std::fixed_point64 {
ensures result.value == x.value + y.value;
}

/// Returns x / y. The result cannot be greater than MAX_U128.
public fun divide(x: FixedPoint64, y: FixedPoint64): FixedPoint64 {
let x_raw = get_raw_value(x);
let y_raw = get_raw_value(y);
// If it is divisable, return the result. If not, return the result + 1.
let result = (x_raw as u256) / (y_raw as u256);
if ((x_raw as u256) % (y_raw as u256) != 0) {
result = result + 1;
};
assert!(result <= MAX_U128, ERATIO_OUT_OF_RANGE);
create_from_raw_value((result as u128))
}

/// Multiply a u128 integer by a fixed-point number, truncating any
/// fractional part of the product. This will abort if the product
/// overflows.
Expand Down Expand Up @@ -95,6 +108,7 @@ module aptos_std::fixed_point64 {
create_from_raw_value((unscaled_product as u128))
}


/// Divide a u128 integer by a fixed-point number, truncating any
/// fractional part of the quotient. This will abort if the divisor
/// is zero or if the quotient overflows.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ module supra_framework::pbo_delegation_pool {
use aptos_std::table::{Self, Table};
use aptos_std::smart_table::{Self, SmartTable};
use aptos_std::fixed_point64::{Self, FixedPoint64};
use aptos_std::math64::min;

use supra_framework::coin::Coin;
use supra_framework::account;
Expand Down Expand Up @@ -1778,12 +1779,15 @@ module supra_framework::pbo_delegation_pool {
cfraction = fixed_point64::add(cfraction, next_fraction);
last_unlocked_period = last_unlocked_period + 1;
};

let final_fraction= *vector::borrow(&unlock_schedule.schedule, schedule_length - 1);
// Fast foward calculation to current period and don't update last_unlocked_period since it is not used anymore
cfraction = fixed_point64::add(cfraction, fixed_point64::multiply_u128_return_fixpoint64((unlock_periods_passed - last_unlocked_period as u128), final_fraction));
cfraction = fixed_point64::min(cfraction, one);

if (last_unlocked_period < unlock_periods_passed && fixed_point64::less(cfraction, one)) {
let final_fraction= *vector::borrow(&unlock_schedule.schedule, schedule_length - 1);
// Determine how many periods is needed
let periods_needed = min(unlock_periods_passed - last_unlocked_period,
((fixed_point64::get_raw_value(fixed_point64::divide(fixed_point64::sub(one, cfraction), final_fraction)) as u64) )
);
// Acclerate calculation to current period and don't update last_unlocked_period since it is not used anymore
cfraction = fixed_point64::add(cfraction, fixed_point64::multiply_u128_return_fixpoint64((periods_needed as u128), final_fraction));
};
unlock_schedule.cumulative_unlocked_fraction = cfraction;
unlock_schedule.last_unlock_period = unlock_periods_passed;
let unlockable_amount = cached_unlockable_balance(delegator_addr, pool_address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ module supra_framework::vesting_without_staking {

// Index is 0-based while period is 1-based so we need to subtract 1.
while (last_completed_period >= next_period_to_vest && vesting_record.left_amount > 0 && next_period_to_vest <= vector::length(schedule)) {
// let schedule_index = next_period_to_vest - 1;
let vesting_fraction = *vector::borrow(schedule, vector::length(schedule) - 1);
let schedule_index = next_period_to_vest - 1;
let vesting_fraction = *vector::borrow(schedule, schedule_index);
vest_transfer(vesting_record, signer_cap, beneficiary, vesting_fraction);
emit_event(&mut vesting_contract.vest_events,
VestEvent {
Expand All @@ -510,19 +510,29 @@ module supra_framework::vesting_without_staking {
next_period_to_vest = next_period_to_vest + 1;
};

let final_fraction = *vector::borrow(schedule, vector::length(schedule) - 1);
let total_fraction = fixed_point32::multiply_u64_return_fixpoint32((last_completed_period - next_period_to_vest ), final_fraction);
// We don't need to check vesting_record.left_amount > 0 because vest_transfer will handle that.
vest_transfer(vesting_record, signer_cap, beneficiary, total_fraction);
next_period_to_vest = last_completed_period + 1;
emit_event(&mut vesting_contract.vest_events,
VestEvent {
admin: vesting_contract.admin,
shareholder_address: shareholder_address,
vesting_contract_address: contract_address,
period_vested: next_period_to_vest,
},
);
if(last_completed_period >= next_period_to_vest && vesting_record.left_amount > 0) {
let final_fraction = *vector::borrow(schedule, vector::length(schedule) - 1);
// Determine how many periods is needed based on the left_amount
let periods_need_used_up_amount = if (vesting_record.left_amount % fixed_point32::multiply_u64(vesting_record.init_amount, final_fraction) == 0) {
vesting_record.left_amount / fixed_point32::multiply_u64(vesting_record.init_amount, final_fraction)
} else {
vesting_record.left_amount / fixed_point32::multiply_u64(vesting_record.init_amount, final_fraction) + 1
};
let periods_needed =
min(periods_need_used_up_amount, last_completed_period - next_period_to_vest + 1);
let total_fraction = fixed_point32::multiply_u64_return_fixpoint32(periods_needed, final_fraction);
// We don't need to check vesting_record.left_amount > 0 because vest_transfer will handle that.
vest_transfer(vesting_record, signer_cap, beneficiary, total_fraction);
next_period_to_vest = next_period_to_vest + periods_needed;
emit_event(&mut vesting_contract.vest_events,
VestEvent {
admin: vesting_contract.admin,
shareholder_address: shareholder_address,
vesting_contract_address: contract_address,
period_vested: next_period_to_vest,
},
);
};

//update last_vested_period for the shareholder
vesting_record.last_vested_period = next_period_to_vest - 1;
Expand Down

0 comments on commit 88bb981

Please sign in to comment.