Skip to content

Commit

Permalink
Merge pull request #237 from SurfingNerd/i236-staked-amount-total
Browse files Browse the repository at this point in the history
I236 staked amount total
  • Loading branch information
SurfingNerd authored Jul 31, 2024
2 parents 2bbd749 + 2deb23c commit 6753487
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions contracts/StakingHbbft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ contract StakingHbbft is Initializable, OwnableUpgradeable, ReentrancyGuardUpgra
/// @dev The limit of the minimum delegator stake (DELEGATOR_MIN_STAKE).
uint256 public delegatorMinStake;

/// @dev current limit of how many funds can
/// be staked on a single validator.
uint256 public maxStakeAmount;

/// @dev The current amount of staking coins ordered for withdrawal from the specified
/// pool by the specified staker. Used by the `orderWithdraw`, `claimOrderedWithdraw` and other functions.
/// The first parameter is the pool staking address, the second one is the staker address.
Expand Down Expand Up @@ -102,6 +106,10 @@ contract StakingHbbft is Initializable, OwnableUpgradeable, ReentrancyGuardUpgra
/// The pool staking address is accepted as a parameter.
mapping(address => uint256) public stakeAmountTotal;

/// @dev Returns the total amount of staking coins currently staked on all pools.
/// Doesn't include the amount ordered for withdrawal.
uint256 public totalStakeAmount;

/// @dev The address of the `ValidatorSetHbbft` contract.
IValidatorSetHbbft public validatorSetContract;

Expand All @@ -113,9 +121,6 @@ contract StakingHbbft is Initializable, OwnableUpgradeable, ReentrancyGuardUpgra

mapping(address => PoolInfo) public poolInfo;

/// @dev current limit of how many funds can
/// be staked on a single validator.
uint256 public maxStakeAmount;

mapping(address => bool) public abandonedAndRemoved;

Expand Down Expand Up @@ -662,6 +667,7 @@ contract StakingHbbft is Initializable, OwnableUpgradeable, ReentrancyGuardUpgra

stakeAmount[_poolStakingAddress][_poolStakingAddress] += validatorReward;
stakeAmountTotal[_poolStakingAddress] += poolReward;
totalStakeAmount += poolReward;

_setLikelihood(_poolStakingAddress);

Expand Down Expand Up @@ -713,13 +719,15 @@ contract StakingHbbft is Initializable, OwnableUpgradeable, ReentrancyGuardUpgra
newOrderedAmountTotal = newOrderedAmountTotal + amount;
newStakeAmount = newStakeAmount - amount;
newStakeAmountTotal = newStakeAmountTotal - amount;
totalStakeAmount -= amount;
orderWithdrawEpoch[_poolStakingAddress][staker] = stakingEpoch;
} else {
uint256 amount = uint256(-_amount);
newOrderedAmount = newOrderedAmount - amount;
newOrderedAmountTotal = newOrderedAmountTotal - amount;
newStakeAmount = newStakeAmount + amount;
newStakeAmountTotal = newStakeAmountTotal + amount;
totalStakeAmount += amount;
}
orderedWithdrawAmount[_poolStakingAddress][staker] = newOrderedAmount;
orderedWithdrawAmountTotal[_poolStakingAddress] = newOrderedAmountTotal;
Expand Down Expand Up @@ -833,6 +841,7 @@ contract StakingHbbft is Initializable, OwnableUpgradeable, ReentrancyGuardUpgra

uint256 gatheredPerStakingAddress = stakeAmountTotal[stakingAddress];
stakeAmountTotal[stakingAddress] = 0;
totalStakeAmount -= gatheredPerStakingAddress;

address[] memory delegators = poolDelegators(stakingAddress);
for (uint256 j = 0; j < delegators.length; ++j) {
Expand Down Expand Up @@ -1348,6 +1357,7 @@ contract StakingHbbft is Initializable, OwnableUpgradeable, ReentrancyGuardUpgra

_stakeAmountByEpoch[_poolStakingAddress][_staker][stakingEpoch] += _amount;
stakeAmountTotal[_poolStakingAddress] += _amount;
totalStakeAmount += _amount;

if (selfStake) {
// `staker` places a stake for himself and becomes a candidate
Expand Down Expand Up @@ -1406,6 +1416,7 @@ contract StakingHbbft is Initializable, OwnableUpgradeable, ReentrancyGuardUpgra
? amountByEpoch - _amount
: 0;
stakeAmountTotal[_poolStakingAddress] -= _amount;
totalStakeAmount -= _amount;

if (newStakeAmount == 0) {
_withdrawCheckPool(_poolStakingAddress, _staker);
Expand Down
2 changes: 2 additions & 0 deletions contracts/interfaces/IStakingHbbft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ interface IStakingHbbft {

function stakeAmountTotal(address) external view returns (uint256);

function totalStakeAmount() external view returns (uint256);

function stakingWithdrawDisallowPeriod() external view returns (uint256);

function stakingEpoch() external view returns (uint256);
Expand Down
2 changes: 0 additions & 2 deletions test/StakingHbbft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2283,8 +2283,6 @@ describe('StakingHbbft', () => {

it('should set delegator min stake', async () => {
const { stakingHbbft } = await helpers.loadFixture(deployContractsFixture);

console.log("mim stake: ", await stakingHbbft.delegatorMinStake());
const minStakeValue = ethers.parseEther('150')
await stakingHbbft.setDelegatorMinStake(minStakeValue);
expect(await stakingHbbft.delegatorMinStake()).to.be.equal(minStakeValue);
Expand Down

0 comments on commit 6753487

Please sign in to comment.