From ff94f5e46c9dc430fb4c1f7fa8525721d17d7b74 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Mon, 25 Nov 2024 12:22:27 -0300 Subject: [PATCH] chore(contracts): Normalize function signatures --- contracts/EpochMath.sol | 6 +-- contracts/MultiplierPointMath.sol | 54 +++++++++++--------------- contracts/StakeManager.sol | 6 +-- contracts/StakeMath.sol | 8 ++-- contracts/interfaces/IStakeManager.sol | 2 +- test/DynamicTest.t.sol | 14 +++---- test/StakeManager.t.sol | 10 ++--- 7 files changed, 46 insertions(+), 54 deletions(-) diff --git a/contracts/EpochMath.sol b/contracts/EpochMath.sol index f503362..85211b9 100644 --- a/contracts/EpochMath.sol +++ b/contracts/EpochMath.sol @@ -172,10 +172,10 @@ abstract contract EpochMath is MultiplierPointMath { pure returns (uint256 mpRate, uint256 mpFractional, uint256 epochTarget1, uint256 mpRemainder) { - mpRate = _accruedMP(_balance, ACCURE_RATE); - mpFractional = mpRate - _accruedMP(_balance, _deltaTime); + mpRate = _accrueMP(_balance, ACCURE_RATE); + mpFractional = mpRate - _accrueMP(_balance, _deltaTime); - uint256 mpTarget = _accruedMaxMP(_balance) + mpFractional; + uint256 mpTarget = _maxAccrueMP(_balance) + mpFractional; uint256 deltaEpochTarget1 = mpTarget / mpRate; epochTarget1 = _accountEpoch + deltaEpochTarget1; diff --git a/contracts/MultiplierPointMath.sol b/contracts/MultiplierPointMath.sol index bc5a18f..8062980 100644 --- a/contracts/MultiplierPointMath.sol +++ b/contracts/MultiplierPointMath.sol @@ -31,9 +31,9 @@ abstract contract MultiplierPointMath is IStakeConstants { * @param _balance Represents the current account balance * @param _deltaTime The time difference or the duration over which the multiplier points are accrued, expressed in * seconds - * @return _accruedMP points accrued for given `_amount` and `_seconds` + * @return _accruedMP points accrued for given `_balance` and `_seconds` */ - function _accruedMP(uint256 _balance, uint256 _deltaTime) internal pure returns (uint256 _accruedMP) { + function _accrueMP(uint256 _balance, uint256 _deltaTime) internal pure returns (uint256 _accruedMP) { return Math.mulDiv(_balance, _deltaTime * MP_APY, YEAR * 100); } @@ -42,21 +42,21 @@ abstract contract MultiplierPointMath is IStakeConstants { * t_lock. * It is equivalent to the accrued multiplier points function but specifically applied in the context of a locked * balance. - * @param _amount quantity of tokens + * @param _balance quantity of tokens * @param _lockedSeconds time in seconds locked - * @return _bonusMP bonus multiplier points for given `_amount` and `_lockedSeconds` + * @return _bonusMP bonus multiplier points for given `_balance` and `_lockedSeconds` */ - function _bonusMP(uint256 _amount, uint256 _lockedSeconds) internal pure returns (uint256 _bonusMP) { - return _accruedMP(_amount, _lockedSeconds); + function _bonusMP(uint256 _balance, uint256 _lockedSeconds) internal pure returns (uint256 _bonusMP) { + return _accrueMP(_balance, _lockedSeconds); } /** * @notice Calculates the initial multiplier points (MPs) based on the balance change Δa. The result is equal to * the amount of balance added. - * @param _amount Represents the change in balance. + * @param _balance Represents the change in balance. */ - function _initialMP(uint256 _amount) internal pure returns (uint256 _initialMP) { - return _amount; + function _initialMP(uint256 _balance) internal pure returns (uint256 _initialMP) { + return _balance; } /** @@ -64,17 +64,17 @@ abstract contract MultiplierPointMath is IStakeConstants { * removed from the total balance a_bal `_balance`. * The reduction is proportional to the ratio of the removed balance to the total balance, applied to the current * multiplier points $mp$. - * @param _mp Represents the current multiplier points * @param _balance The total account balance before the removal of Δa `_reducedBalance` + * @param _mp Represents the current multiplier points * @param _reducedAmount reduced balance * @return _reducedMP Multiplier points to reduce from `_mp` */ - function _reducedMP( - uint256 _mp, + function _reduceMP( uint256 _balance, + uint256 _mp, uint256 _reducedAmount ) - public + internal pure returns (uint256 _reducedMP) { @@ -82,11 +82,11 @@ abstract contract MultiplierPointMath is IStakeConstants { } /** - * @notice Calculates maximum stake a given `_amount` can be generated with `MAX_MULTIPLIER` + * @notice Calculates maximum stake a given `_balance` can be generated with `MAX_MULTIPLIER` * @param _balance quantity of tokens - * @return _maxMPAccrued maximum quantity of muliplier points that can be generated for given `_amount` + * @return _maxMPAccrued maximum quantity of muliplier points that can be generated for given `_balance` */ - function _accruedMaxMP(uint256 _balance) internal pure returns (uint256 _maxMPAccrued) { + function _maxAccrueMP(uint256 _balance) internal pure returns (uint256 _maxMPAccrued) { return Math.mulDiv(_balance, MP_MPY, 100); } @@ -97,7 +97,7 @@ abstract contract MultiplierPointMath is IStakeConstants { * @param _lockTime The time duration for which the balance is locked * @return _maxMP Maximum Multiplier Points that can be generated for given `_balance` and `_lockTime` */ - function _totalMaxMP(uint256 _balance, uint256 _lockTime) internal pure returns (uint256 _maxMP) { + function _maxTotalMP(uint256 _balance, uint256 _lockTime) internal pure returns (uint256 _maxMP) { return _balance + Math.mulDiv(_balance * MP_APY, (MAX_MULTIPLIER * YEAR) + _lockTime, YEAR * 100); } @@ -107,29 +107,21 @@ abstract contract MultiplierPointMath is IStakeConstants { * @param _balance quantity of tokens * @return _maxMPAbsolute Absolute Maximum Multiplier Points */ - function _absoluteMaxMP(uint256 _balance) internal pure returns (uint256 _maxMPAbsolute) { + function _maxAbsoluteMP(uint256 _balance) internal pure returns (uint256 _maxMPAbsolute) { return Math.mulDiv(_balance, MP_MPY_ABSOLUTE, 100); } /** * @dev Caution: This value is estimated and can be incorrect due precision loss. * @notice Calculates the remaining lock time available for a given `_mpMax` and `_balance` - * @param _mpMax Maximum multiplier points calculated from the current balance. * @param _balance Current balance used to calculate the maximum multiplier points. + * @param _mpMax Maximum multiplier points calculated from the current balance. + * @return _lockTime Amount of lock time allowed to be increased */ - function _lockTimeAvailable(uint256 _mpMax, uint256 _balance) public pure returns (uint256 _lockTime) { + function _lockTimeAvailable(uint256 _balance, uint256 _mpMax) internal pure returns (uint256 _lockTime) { return Math.mulDiv((_balance * MP_MPY_ABSOLUTE) - _mpMax, YEAR, _balance * 100); } - /** - * @notice Calculates the lock time for a given bonus multiplier points and current balance. - * @param _bonusMP bonus multiplier points intended to be generated - * @param _balance current balance - */ - function _lockTime(uint256 _bonusMP, uint256 _balance) internal pure returns (uint256 _lockTime) { - return Math.mulDiv(_bonusMP * 100, YEAR, _balance * MP_APY); - } - /** * @notice Calculates the time required to accrue a specific multiplier point value. * @param _balance The current balance. @@ -147,7 +139,7 @@ abstract contract MultiplierPointMath is IStakeConstants { * @return _bonusMP The calculated bonus multiplier points. */ function _retrieveBonusMP(uint256 _balance, uint256 _maxMP) internal pure returns (uint256 _bonusMP) { - return _maxMP - (_balance + _accruedMaxMP(_balance)); + return _maxMP - (_balance + _maxAccrueMP(_balance)); } /** @@ -166,6 +158,6 @@ abstract contract MultiplierPointMath is IStakeConstants { pure returns (uint256 _accruedMP) { - return _totalMP + _accruedMaxMP(_balance) - _maxMP; + return _totalMP + _maxAccrueMP(_balance) - _maxMP; } } diff --git a/contracts/StakeManager.sol b/contracts/StakeManager.sol index 3ca8f5f..d474873 100644 --- a/contracts/StakeManager.sol +++ b/contracts/StakeManager.sol @@ -179,7 +179,7 @@ contract StakeManager is StakeMath, EpochMath, TrustedCodehashAccess, IStakeMana revert StakeManager__InvalidLockTime(); } //mints bonus multiplier points for seconds increased - uint256 bonusMP = _accruedMP(account.balance, _secondsIncrease); + uint256 bonusMP = _accrueMP(account.balance, _secondsIncrease); //update account storage account.lockUntil = lockUntil; @@ -339,7 +339,7 @@ contract StakeManager is StakeMath, EpochMath, TrustedCodehashAccess, IStakeMana * @param epoch Epoch to increment total supply */ function _mintMP(Account storage account, uint256 processTime, Epoch storage epoch) private { - uint256 accruedMP = _accruedMP(account.balance, processTime - account.lastMint); + uint256 accruedMP = _accrueMP(account.balance, processTime - account.lastMint); if (accruedMP + account.totalMP > account.maxMP) { accruedMP = account.maxMP - account.totalMP; //how much left to reach cap } @@ -369,7 +369,7 @@ contract StakeManager is StakeMath, EpochMath, TrustedCodehashAccess, IStakeMana * @return mp multiplier points to mint */ function calculateMP(uint256 _balance, uint256 _deltaTime) public pure returns (uint256 mp) { - return _accruedMP(_balance, _deltaTime); + return _accrueMP(_balance, _deltaTime); } /** diff --git a/contracts/StakeMath.sol b/contracts/StakeMath.sol index fad7409..39c6a59 100644 --- a/contracts/StakeMath.sol +++ b/contracts/StakeMath.sol @@ -61,7 +61,7 @@ abstract contract StakeMath is MultiplierPointMath { } _deltaMpTotal = _initialMP(_increasedAmount) + deltaMpBonus; - _newMaxMP = _currentMaxMP + _deltaMpTotal + _accruedMP(_increasedAmount, MAX_MULTIPLIER * YEAR); + _newMaxMP = _currentMaxMP + _deltaMpTotal + _accrueMP(_increasedAmount, MAX_MULTIPLIER * YEAR); require(_newMaxMP <= MP_MPY_ABSOLUTE * (_balance + _increasedAmount), "StakeMath: max multiplier exceeded"); } @@ -136,8 +136,8 @@ abstract contract StakeMath is MultiplierPointMath { if (newBalance < MIN_BALANCE) { revert StakeManager__StakeIsTooLow(); } - _deltaMpTotal = _reducedMP(_currentTotalMP, _balance, _reducedAmount); - _deltaMpMax = _reducedMP(_currentMaxMP, _balance, _reducedAmount); + _deltaMpTotal = _reduceMP(_balance, _currentTotalMP, _reducedAmount); + _deltaMpMax = _reduceMP(_balance, _currentMaxMP, _reducedAmount); } /** @@ -165,7 +165,7 @@ abstract contract StakeMath is MultiplierPointMath { revert StakeManager__AccrueTimeNotReached(); } if (_currentTotalMP < _currentMaxMP) { - _deltaMpTotal = Math.min(_accruedMP(_balance, dt), _currentMaxMP - _currentTotalMP); + _deltaMpTotal = Math.min(_accrueMP(_balance, dt), _currentMaxMP - _currentTotalMP); } } diff --git a/contracts/interfaces/IStakeManager.sol b/contracts/interfaces/IStakeManager.sol index 1c47b0b..2fefa6f 100644 --- a/contracts/interfaces/IStakeManager.sol +++ b/contracts/interfaces/IStakeManager.sol @@ -15,7 +15,7 @@ interface IStakeManager is IStakeConstants, ITrustedCodehashAccess { function totalStaked() external view returns (uint256); function totalMP() external view returns (uint256); - //function totalMaxMP() external view returns (uint256); + //function maxTotalMP() external view returns (uint256); function getStakedBalance(address _vault) external view returns (uint256 _balance); struct Account { diff --git a/test/DynamicTest.t.sol b/test/DynamicTest.t.sol index 9877799..ba59eae 100644 --- a/test/DynamicTest.t.sol +++ b/test/DynamicTest.t.sol @@ -185,7 +185,7 @@ contract DynamicTest is StakeMath, Test { require(action.args.length == 1, "Incorrect number of arguments"); output.stakeAmount = input.stakeAmount; output.predictedBonusMP = input.predictedBonusMP; - output.increasedAccruedMP = _accruedMP(input.stakeAmount, action.args[0]); + output.increasedAccruedMP = _accrueMP(input.stakeAmount, action.args[0]); output.predictedAccruedMP = input.predictedAccruedMP + output.increasedAccruedMP; } } @@ -340,8 +340,8 @@ contract DynamicTest is StakeMath, Test { } assertEq(streamer.totalStaked(), globalParams[stage].stakeAmount, "wrong total staked"); assertEq(streamer.totalMP(), globalParams[stage].predictedBonusMP, "wrong total MP"); - assertEq(streamer.totalMaxMP(), globalParams[stage].stakeAmount * MAX_MULTIPLIER + - globalParams[stage].predictedBonusMP, "wrong totalMaxMP MP"); + assertEq(streamer.maxTotalMP(), globalParams[stage].stakeAmount * MAX_MULTIPLIER + + globalParams[stage].predictedBonusMP, "wrong maxTotalMP MP"); } stage++; // second stage = progress in time @@ -362,8 +362,8 @@ contract DynamicTest is StakeMath, Test { } assertEq(streamer.totalStaked(), globalParams[stage].stakeAmount, "wrong total staked"); assertEq(streamer.totalMP(), globalParams[stage].predictedBonusMP, "wrong total MP"); - assertEq(streamer.totalMaxMP(), globalParams[stage].stakeAmount * MAX_MULTIPLIER + - globalParams[stage].predictedBonusMP, "wrong totalMaxMP MP"); + assertEq(streamer.maxTotalMP(), globalParams[stage].stakeAmount * MAX_MULTIPLIER + + globalParams[stage].predictedBonusMP, "wrong maxTotalMP MP"); } stage++; // third stage = reduce stake @@ -384,8 +384,8 @@ contract DynamicTest is StakeMath, Test { } assertEq(streamer.totalStaked(), globalParams[stage].stakeAmount, "wrong total staked"); assertEq(streamer.totalMP(), globalParams[stage].predictedBonusMP, "wrong total MP"); - assertEq(streamer.totalMaxMP(), globalParams[stage].stakeAmount * MAX_MULTIPLIER + - globalParams[stage].predictedBonusMP, "wrong totalMaxMP MP"); + assertEq(streamer.maxTotalMP(), globalParams[stage].stakeAmount * MAX_MULTIPLIER + + globalParams[stage].predictedBonusMP, "wrong maxTotalMP MP"); } }*/ } diff --git a/test/StakeManager.t.sol b/test/StakeManager.t.sol index f9d53c7..cd3e73f 100644 --- a/test/StakeManager.t.sol +++ b/test/StakeManager.t.sol @@ -137,7 +137,7 @@ contract StakeTest is StakeManagerTest { assertEq(balance, stakeAmount, "balance of user vault should be equal to stake amount after stake"); assertEq( maxMP, - _totalMaxMP(stakeAmount, 0), + _maxTotalMP(stakeAmount, 0), "maxMP of user vault should be equal to stake amount after stake if no lock" ); assertEq( @@ -146,7 +146,7 @@ contract StakeTest is StakeManagerTest { vm.prank(testUser); userVault.lock(lockTime); - uint256 estimatedMaxMP = maxMP + _accruedMP(stakeAmount, lockTime); + uint256 estimatedMaxMP = maxMP + _accrueMP(stakeAmount, lockTime); uint256 estimatedTotalMP = _initialMP(stakeAmount) + _bonusMP(stakeAmount, lockTime); (, balance, maxMP, totalMP,,,,) = stakeManager.accounts(address(userVault)); assertEq(balance, stakeAmount, "balance of user vault should be equal to stake amount after lock"); @@ -790,11 +790,11 @@ contract UserFlowsTest is StakeManagerTest { ); uint256 epochTarget1; - uint256 mpRate = _accruedMP(_stakeAmount, ACCURE_RATE); - uint256 mpFractional = mpRate - _accruedMP(_stakeAmount, ACCURE_RATE - _stakeWarpTime); + uint256 mpRate = _accrueMP(_stakeAmount, ACCURE_RATE); + uint256 mpFractional = mpRate - _accrueMP(_stakeAmount, ACCURE_RATE - _stakeWarpTime); uint256 mpRemainder; { - uint256 mpTarget = _accruedMaxMP(_stakeAmount) + mpFractional; + uint256 mpTarget = _maxAccrueMP(_stakeAmount) + mpFractional; uint256 deltaEpochTarget1 = (mpTarget / mpRate); epochTarget1 = _startEpoch + deltaEpochTarget1; if (mpTarget % mpRate > 0) {