Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

268 potencial fixes contracts from review #81

Merged
merged 13 commits into from
Nov 21, 2024
24 changes: 6 additions & 18 deletions contracts/APRCalculator/APRCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import {RSIndex} from "./modules/RSI/RSIndex.sol";
import {IAPRCalculator} from "./IAPRCalculator.sol";

contract APRCalculator is IAPRCalculator, MacroFactor, RSIndex {
uint256 public constant INITIAL_BASE_APR = 500;
uint256 public constant BASE_APR = 500;

uint256 public base;
uint256[52] public vestingBonus;

// _______________ Initializer _______________
Expand All @@ -23,27 +22,16 @@ contract APRCalculator is IAPRCalculator, MacroFactor, RSIndex {
__MacroFactor_init();
__RSIndex_init();

base = INITIAL_BASE_APR;

initializeVestingBonus();
}

// _______________ External functions _______________

/**
* @inheritdoc IAPRCalculator
*/
function setBase(uint256 newBase) external onlyRole(MANAGER_ROLE) {
base = newBase;
}

// _______________ Public functions _______________

/**
* @inheritdoc IAPRCalculator
*/
function getBaseAPR() public view returns (uint256) {
return base;
function getBaseAPR() public pure returns (uint256) {
return BASE_APR;
}

/**
Expand All @@ -66,7 +54,7 @@ contract APRCalculator is IAPRCalculator, MacroFactor, RSIndex {
function getMaxAPR() public view returns (uint256 nominator, uint256 denominator) {
uint256 vestBonus = getVestingBonus(52);

nominator = (base + vestBonus) * MAX_MACRO_FACTOR * MAX_RSI_BONUS;
nominator = (BASE_APR + vestBonus) * MAX_MACRO_FACTOR * MAX_RSI_BONUS;
denominator = 10000 ** 3;
}

Expand All @@ -89,8 +77,8 @@ contract APRCalculator is IAPRCalculator, MacroFactor, RSIndex {
/**
* @inheritdoc IAPRCalculator
*/
function applyBaseAPR(uint256 amount) public view returns (uint256) {
return (amount * base) / DENOMINATOR;
function applyBaseAPR(uint256 amount) public pure returns (uint256) {
return (amount * BASE_APR) / DENOMINATOR;
}

// _______________ Internal functions _______________
Expand Down
7 changes: 0 additions & 7 deletions contracts/APRCalculator/IAPRCalculator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import {IMacroFactor} from "./modules/MacroFactor/IMacroFactor.sol";
interface IAPRCalculator is IMacroFactor, IRSIndex, IPrice {
error InvalidRSI();

/**
* @notice Sets new base APR
* @dev only owner can call this function
* @param newBase new base APR
*/
function setBase(uint256 newBase) external;

// _______________ Public functions _______________

/**
Expand Down
3 changes: 3 additions & 0 deletions contracts/APRCalculator/modules/Price/Price.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ abstract contract Price is IPrice, Initializable, System, Governed, HydraChainCo
* @inheritdoc IPrice
*/
function updatePrice(uint256 _price, uint256 _day) external onlyPriceOracle {
assert(_price != 0);
assert(pricePerDay[_day] == 0);

latestDailyPrice = _price;
pricePerDay[_day] = _price;

Expand Down
4 changes: 4 additions & 0 deletions contracts/HydraChain/modules/Inspector/Inspector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ abstract contract Inspector is IInspector, ValidatorManager {
* @inheritdoc IInspector
*/
function isSubjectToFinishBan(address account) public view returns (bool) {
if (validators[account].status == ValidatorStatus.Banned) {
return false;
}
R-Santev marked this conversation as resolved.
Show resolved Hide resolved

// check if the owner (governance) is calling
if (msg.sender == owner()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ abstract contract ValidatorManager is
* @inheritdoc IValidatorManager
*/
function deactivateValidator(address account) external onlyHydraStaking {
assert(validators[account].status == ValidatorStatus.Active);
validators[account].status = ValidatorStatus.Registered;
activeValidatorsCount--;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ struct ValidatorPower {
}

interface IValidatorsData {
event ValidatorsDataSynced(ValidatorPower[] validatorsPower);

/**
* @notice Syncs the validators voting power with the provided data
* @param validatorsPower Array of ValidatorPower struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ abstract contract ValidatorsData is IValidatorsData, System, Initializable {
*/
function syncValidatorsData(ValidatorPower[] calldata validatorsPower) external onlySystemCall {
uint256 arrLength = validatorsPower.length;
if (arrLength == 0) {
return;
}

uint256 totalNewPower = 0;
uint256 totalOldPower = 0;
for (uint256 i = 0; i < arrLength; i++) {
Expand All @@ -35,6 +39,8 @@ abstract contract ValidatorsData is IValidatorsData, System, Initializable {
} else {
totalVotingPower -= totalOldPower - totalNewPower;
}

emit ValidatorsDataSynced(validatorsPower);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/HydraDelegation/Delegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract Delegation is
uint256 _initialCommission
) internal onlyInitializing {
__Governed_init(_governance);
__Withdrawal_init(_governance);
__Withdrawal_init();
__RewardWalletConnector_init(_rewardWalletAddr);
__Delegation_init_unchained(_initialStakers, _initialCommission);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import {IVesting} from "../../../common/Vesting/IVesting.sol";
import {IDelegation} from "../../IDelegation.sol";
import {RPS, DelegationPoolDelegatorParams} from "../DelegationPoolLib/IDelegationPoolLib.sol";

interface IVestedDelegation is IDelegation {
interface IVestedDelegation is IVesting, IDelegation {
event PositionOpened(
address indexed manager,
address indexed staker,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import {Governed} from "../../../common/Governed/Governed.sol";
import {Withdrawal} from "../../../common/Withdrawal/Withdrawal.sol";
import {VestedPositionLib} from "../../../common/Vesting/VestedPositionLib.sol";
import {VestingPosition} from "../../../common/Vesting/IVesting.sol";
import {Vesting} from "../../../common/Vesting/Vesting.sol";
Expand All @@ -17,13 +15,11 @@ import {IVestedDelegation, RPS, DelegationPoolDelegatorParams} from "./IVestedDe

abstract contract VestedDelegation is
IVestedDelegation,
Governed,
Withdrawal,
Vesting,
HydraChainConnector,
RewardWalletConnector,
Delegation,
VestingManagerFactoryConnector,
Vesting
VestingManagerFactoryConnector
{
using DelegationPoolLib for DelegationPool;
using VestedPositionLib for VestingPosition;
Expand Down
3 changes: 2 additions & 1 deletion contracts/HydraStaking/IHydraStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import {Uptime} from "../HydraChain/modules/ValidatorManager/IValidatorManager.s
import {ILiquidStaking} from "./modules/LiquidStaking/ILiquidStaking.sol";
import {IDelegatedStaking} from "./modules/DelegatedStaking/IDelegatedStaking.sol";
import {IPenalizeableStaking} from "./modules/PenalizeableStaking/IPenalizeableStaking.sol";
import {IVestedStaking} from "./modules/VestedStaking/IVestedStaking.sol";
import {IStaking} from "./IStaking.sol";

struct StakerInit {
address addr;
uint256 stake;
}

interface IHydraStaking is IDelegatedStaking, IStaking, ILiquidStaking, IPenalizeableStaking {
interface IHydraStaking is IDelegatedStaking, IStaking, ILiquidStaking, IPenalizeableStaking, IVestedStaking {
error DistributeRewardFailed(string message);

/**
Expand Down
4 changes: 2 additions & 2 deletions contracts/HydraStaking/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ contract Staking is IStaking, Governed, Withdrawal, HydraChainConnector, APRCalc
address _governance
) internal onlyInitializing {
__Governed_init(_governance);
__Withdrawal_init(_governance);
__Withdrawal_init();
__HydraChainConnector_init(_hydraChainAddr);
__APRCalculatorConnector_init(_aprCalculatorAddr);
__RewardWalletConnector_init(_rewardWalletAddr);
Expand Down Expand Up @@ -78,7 +78,7 @@ contract Staking is IStaking, Governed, Withdrawal, HydraChainConnector, APRCalc
/**
* @inheritdoc IStaking
*/
function changeMinStake(uint256 newMinStake) external onlyOwner {
function changeMinStake(uint256 newMinStake) external onlyRole(DEFAULT_ADMIN_ROLE) {
_changeMinStake(newMinStake);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import {IVesting} from "../../../common/Vesting/IVesting.sol";
import {IStaking} from "../../Staking.sol";

struct StakingRewardsHistory {
uint256 totalReward;
uint256 epoch;
uint256 timestamp;
}

interface IVestedStaking {
interface IVestedStaking is IVesting, IStaking {
/**
* @notice Stakes sent amount with vesting period.
* @param durationWeeks Duration of the vesting in weeks. Must be between 1 and 52.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {IVestedStaking, StakingRewardsHistory} from "./IVestedStaking.sol";
* @title VestedStaking
* @notice An extension of the Staking contract that enables vesting the stake for a higher APY
*/
abstract contract VestedStaking is IVestedStaking, Staking, Vesting {
abstract contract VestedStaking is IVestedStaking, Vesting, Staking {
using VestedPositionLib for VestingPosition;

/**
Expand Down
3 changes: 2 additions & 1 deletion contracts/PriceOracle/PriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ contract PriceOracle is IPriceOracle, System, Initializable, HydraChainConnector
mapping(uint256 => uint256) public pricePerDay;
mapping(uint256 => List) public priceVotesForDay;

uint256 public constant MAX_UINT224 = type(uint224).max;
uint256 public constant VOTING_POWER_PERCENTAGE_NEEDED = 61;
uint256 public constant DAILY_VOTING_START_TIME = 36 minutes;
uint256 public constant DAILY_VOTING_END_TIME = DAILY_VOTING_START_TIME + 3 hours;
Expand All @@ -37,7 +38,7 @@ contract PriceOracle is IPriceOracle, System, Initializable, HydraChainConnector
* @inheritdoc IPriceOracle
*/
function vote(uint256 price) external {
if (price == 0) {
if (price == 0 || price > MAX_UINT224) {
revert InvalidPrice();
}

Expand Down
19 changes: 10 additions & 9 deletions contracts/VestingManager/VestingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ contract VestingManager is IVestingManager, Initializable, OwnableUpgradeable {

/// @notice The hydra delegation contract
IHydraDelegation public immutable HYDRA_DELEGATION;
IERC20 public immutable LIQUIDITY_TOKEN;

// _______________ Constructor _______________

constructor(address hydraDelegationAddr) {
constructor(address hydraDelegationAddr, address liquidityTokenAddr) {
// Set the HydraDelegation contract as part of the code (because immutable) when implementation is deployed.
// That way, we don't have to set it separately in every proxy we create later.
HYDRA_DELEGATION = IHydraDelegation(hydraDelegationAddr);
LIQUIDITY_TOKEN = IERC20(liquidityTokenAddr);
_disableInitializers();
}

Expand All @@ -37,8 +39,7 @@ contract VestingManager is IVestingManager, Initializable, OwnableUpgradeable {
*/
function openVestedDelegatePosition(address staker, uint256 durationWeeks) external payable onlyOwner {
HYDRA_DELEGATION.delegateWithVesting{value: msg.value}(staker, durationWeeks);
address liquidToken = HYDRA_DELEGATION.liquidToken();
_sendLiquidTokens(msg.sender, IERC20(liquidToken).balanceOf(address(this)));
_sendLiquidTokens(msg.sender, LIQUIDITY_TOKEN.balanceOf(address(this)));
}

/**
Expand All @@ -59,9 +60,8 @@ contract VestingManager is IVestingManager, Initializable, OwnableUpgradeable {
bytes32 r,
bytes32 s
) external payable onlyOwner {
address liquidToken = HYDRA_DELEGATION.liquidToken();
uint256 owedLiquidTokens = HYDRA_DELEGATION.calculateOwedLiquidTokens(address(this), amount);
IERC20Permit(liquidToken).permit(msg.sender, address(this), owedLiquidTokens, deadline, v, r, s);
IERC20Permit(address(LIQUIDITY_TOKEN)).permit(msg.sender, address(this), owedLiquidTokens, deadline, v, r, s);
_cutVestedPosition(staker, amount, owedLiquidTokens);
}

Expand Down Expand Up @@ -111,8 +111,7 @@ contract VestingManager is IVestingManager, Initializable, OwnableUpgradeable {
* @param amount staked amount
*/
function _sendLiquidTokens(address positionOwner, uint256 amount) private onlyOwner {
address liquidToken = HYDRA_DELEGATION.liquidToken();
IERC20(liquidToken).safeTransfer(positionOwner, amount);
LIQUIDITY_TOKEN.safeTransfer(positionOwner, amount);
}

/**
Expand All @@ -121,7 +120,9 @@ contract VestingManager is IVestingManager, Initializable, OwnableUpgradeable {
* @param amount Amount to be unstaked
*/
function _fulfillLiquidTokens(address positionOwner, uint256 amount) private onlyOwner {
address liquidToken = HYDRA_DELEGATION.liquidToken();
IERC20(liquidToken).safeTransferFrom(positionOwner, address(this), amount);
LIQUIDITY_TOKEN.safeTransferFrom(positionOwner, address(this), amount);
}

// slither-disable-next-line unused-state,naming-convention
uint256[50] private __gap;
}
18 changes: 9 additions & 9 deletions contracts/VestingManager/VestingManagerFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ contract VestingManagerFactory is IVestingManagerFactory, System, Initializable

// _______________ Initializer _______________

function initialize(address hydraDelegationAddr) external initializer onlySystemCall {
_initialize(hydraDelegationAddr);
function initialize(address hydraDelegationAddr, address liquidityTokenAddr) external initializer onlySystemCall {
_initialize(hydraDelegationAddr, liquidityTokenAddr);
}

function _initialize(address hydraDelegationAddr) internal onlyInitializing {
address implementation = address(new VestingManager(hydraDelegationAddr));
function _initialize(address hydraDelegationAddr, address liquidityTokenAddr) internal onlyInitializing {
address implementation = address(new VestingManager(hydraDelegationAddr, liquidityTokenAddr));
beacon = new UpgradeableBeacon(implementation);
}

Expand All @@ -43,7 +43,7 @@ contract VestingManagerFactory is IVestingManagerFactory, System, Initializable
abi.encodeWithSelector(VestingManager(address(0)).initialize.selector, msg.sender)
);

_storeVestManagerData(address(manager), msg.sender);
_storeVestingManagerData(address(manager), msg.sender);

emit NewVestingManager(msg.sender, address(manager));
}
Expand All @@ -66,12 +66,12 @@ contract VestingManagerFactory is IVestingManagerFactory, System, Initializable

/**
* @notice Stores the vesting manager data
* @param vestManager Address of the vesting manager
* @param vestingManager Address of the vesting manager
* @param owner Address of the vest manager owner
*/
function _storeVestManagerData(address vestManager, address owner) private {
vestingManagerOwner[vestManager] = owner;
userVestingManagers[owner].push(vestManager);
function _storeVestingManagerData(address vestingManager, address owner) private {
vestingManagerOwner[vestingManager] = owner;
userVestingManagers[owner].push(vestingManager);
}

// slither-disable-next-line unused-state,naming-convention
Expand Down
13 changes: 13 additions & 0 deletions contracts/common/Vesting/IVesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@ struct VestingPosition {
uint256 rsiBonus;
uint256 commission;
}

interface IVesting {
error FailedToBurnAmount();
error PenaltyRateOutOfRange();

/**
* @notice sets a new penalty rate
* @param newRate the new penalty rate
* @dev Only callable by the admin
* @dev the rate should be between 10 and 150 (0.1% and 1.5%)
*/
function setPenaltyDecreasePerWeek(uint256 newRate) external;
}
Loading
Loading