@@ -5,8 +5,7 @@ import {WadRayMath} from './WadRayMath.sol';
55
66/**
77 * @title MathUtils library
8- * @author Aave
9- * @notice Provides functions to perform linear and compounded interest calculations
8+ * @author Aave Labs
109 */
1110library MathUtils {
1211 using WadRayMath for uint256 ;
@@ -24,7 +23,6 @@ library MathUtils {
2423 uint256 rate ,
2524 uint40 lastUpdateTimestamp
2625 ) internal view returns (uint256 ) {
27- //solium-disable-next-line
2826 uint256 result = rate * (block .timestamp - uint256 (lastUpdateTimestamp));
2927 unchecked {
3028 result = result / SECONDS_PER_YEAR;
@@ -33,81 +31,14 @@ library MathUtils {
3331 return WadRayMath.RAY + result;
3432 }
3533
36- /**
37- * @notice Calculates the new weighted average given a current weighted average, the sum of the weights subtracted with a new value, weight.
38- * @dev Add precision to weighted average & new value before calling this method.
39- * @param currentWeightedAvg The base weighted average.
40- * @param currentSumWeights The base sum of weights.
41- * @param newValue The new value to add or subtract.
42- * @param newValueWeight The weight of the new value.
43- * @return newWeightedAvg The weighted average after the operation.
44- * @return newSumWeights The sum of weights after operation, cannot be less than 0.
45- */
46- function addToWeightedAverage (
47- uint256 currentWeightedAvg ,
48- uint256 currentSumWeights ,
49- uint256 newValue ,
50- uint256 newValueWeight
51- ) internal pure returns (uint256 , uint256 ) {
52- // newWeightedAvg, newSumWeights
53-
54- if (newValueWeight == 0 ) {
55- return (currentWeightedAvg, currentSumWeights);
56- }
57- if (currentSumWeights == 0 ) {
58- return (newValue, newValueWeight);
59- }
60-
61- uint256 newSumWeights = currentSumWeights + newValueWeight;
62- uint256 newWeightedAvg = ((currentWeightedAvg * currentSumWeights) +
63- (newValue * newValueWeight)) / newSumWeights; // newSumWeights cannot be zero when execution reaches here
64-
65- return (newWeightedAvg, newSumWeights);
66- }
67-
68- /**
69- * @notice Calculates the new weighted average given a current weighted average, the sum of the weights added with a new value, weight.
70- * @dev Add precision to weighted average & new value before calling this method.
71- * @param currentWeightedAvg The base weighted average.
72- * @param currentSumWeights The base sum of weights.
73- * @param newValue The new value to add or subtract.
74- * @param newValueWeight The weight of the new value.
75- * @return newWeightedAvg The weighted average after the operation.
76- * @return newSumWeights The sum of weights after operation, cannot be less than 0.
77- * @dev Reverts when newValueWeight is greater than currentSumWeights.
78- * @dev Reverts when the newWeightedValue (weight * value) is greater than currentWeightedSum (currentSumWeights * currentWeightedAvg).
79- */
80- function subtractFromWeightedAverage (
81- uint256 currentWeightedAvg ,
82- uint256 currentSumWeights ,
83- uint256 newValue ,
84- uint256 newValueWeight
85- ) internal pure returns (uint256 , uint256 ) {
86- // newWeightedAvg, newSumWeights
87- if (newValueWeight == 0 ) return (currentWeightedAvg, currentSumWeights);
88-
89- if (currentSumWeights == newValueWeight) return (0 , 0 ); // no change
90- if (currentSumWeights < newValueWeight) revert ();
91-
92- uint256 newWeightedValue = newValue * newValueWeight;
93- uint256 currentWeightedSum = currentWeightedAvg * currentSumWeights;
94-
95- if (currentWeightedSum < newWeightedValue) revert ();
96-
97- uint256 newSumWeights = currentSumWeights - newValueWeight;
98- uint256 newWeightedAvg = (currentWeightedSum - newWeightedValue) / newSumWeights;
99-
100- return (newWeightedAvg, newSumWeights);
101- }
102-
10334 /**
10435 * @notice Returns the minimum of two values.
10536 * @param a The first value to compare.
10637 * @param b The second value to compare.
10738 * @return result The minimum of the two values.
10839 */
10940 function min (uint256 a , uint256 b ) internal pure returns (uint256 result ) {
110- assembly {
41+ assembly ( ' memory-safe ' ) {
11142 result := xor (b, mul (xor (a, b), lt (a, b)))
11243 }
11344 }
0 commit comments