Module 0xd63343bff7977cb364b17dcdcdeb64e77b7bc9b4ec252f481ace51b6de3bbd96::math_utils
@title Math Utilities @author Aave @notice Utility functions for mathematical operations and calculations
- Constants
- Function
u256_max
- Function
get_percentage_factor
- Function
calculate_linear_interest
- Function
calculate_compounded_interest
- Function
calculate_compounded_interest_now
- Function
percent_mul
- Function
percent_div
- Function
pow
- Function
get_seconds_per_year
use 0x1::timestamp;
use 0xa888dd108ea636a234c973b8b72b9cadf0c7ae94d6f355c64dde9fdc275724ee::error_config;
use 0xd63343bff7977cb364b17dcdcdeb64e77b7bc9b4ec252f481ace51b6de3bbd96::wad_ray_math;
Constants
@notice Maximum value for u256
const U256_MAX: u256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935;
@notice Half percentage factor (50.00%)
const HALF_PERCENTAGE_FACTOR: u256 = 5000;
@notice Maximum percentage factor (100.00%)
const PERCENTAGE_FACTOR: u256 = 10000;
@notice Seconds per year (ignoring leap years)
const SECONDS_PER_YEAR: u256 = 31536000;
Function u256_max
@notice Returns the maximum value for u256 @return The maximum u256 value
public fun u256_max(): u256
Function get_percentage_factor
@notice Returns the percentage factor (10000) @return The percentage factor
public fun get_percentage_factor(): u256
Function calculate_linear_interest
@notice Calculates the interest accumulated using a linear interest rate formula @param rate The interest rate, in ray @param last_update_timestamp The timestamp of the last update of the interest @return The interest rate linearly accumulated during the timeDelta, in ray
public fun calculate_linear_interest(rate: u256, last_update_timestamp: u64): u256
Function calculate_compounded_interest
@notice Calculates the interest using a compounded interest rate formula @dev To avoid expensive exponentiation, the calculation is performed using a taylor approximation: time_elapsed_in_seconds s = nt t = s/n, where n is the number of seconds in a year r = rate, annual rate as input parameter (1+r/n)^(nt) = e^(rt) e^(rt) = 1 + rt + (rt)^2/2 + (r*t)^3/6 + ...
The approximation slightly underpays liquidity providers and undercharges borrowers, with the advantage of great gas cost reductions. The whitepaper contains reference to the approximation and a table showing the margin of error per different time periods
@param rate The interest rate, in ray @param last_update_timestamp The timestamp of the last update of the interest @param current_timestamp The current timestamp at the time of calling the method @return The interest rate compounded during the timeDelta, in ray
public fun calculate_compounded_interest(rate: u256, last_update_timestamp: u64, current_timestamp: u64): u256
Function calculate_compounded_interest_now
@notice Calculates the compounded interest between the timestamp of the last update and the current block timestamp @param rate The interest rate (in ray) @param last_update_timestamp The timestamp from which the interest accumulation needs to be calculated @return The interest rate compounded between lastUpdateTimestamp and current block timestamp, in ray
public fun calculate_compounded_interest_now(rate: u256, last_update_timestamp: u64): u256
Function percent_mul
@notice Executes a percentage multiplication @param value The value of which the percentage needs to be calculated @param percentage The percentage of the value to be calculated @return result The value multiplied by the percentage and divided by the percentage factor, rounded half up to the nearest unit
public fun percent_mul(value: u256, percentage: u256): u256
Function percent_div
@notice Executes a percentage division @param value The value of which the percentage needs to be calculated @param percentage The percentage of the value to be calculated @return result The value multiplied by the percentage factor and divided by the percentage, rounded half up to the nearest unit
public fun percent_div(value: u256, percentage: u256): u256
Function pow
@notice Calculates the power of a base to an exponent @param base The base value @param exponent The exponent value @return The result of base raised to the power of exponent
public fun pow(base: u256, exponent: u256): u256
Function get_seconds_per_year
@dev Returns the seconds per year value @return Seconds per year constant
public fun get_seconds_per_year(): u256