• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NexusMutual / smart-contracts / #894

04 Sep 2024 10:31AM UTC coverage: 87.044% (-0.5%) from 87.593%
#894

push

web-flow
Merge pull request #1231 from NexusMutual/release-candidate

feat: StakingPool fixes + batch NXM withdrawals v2.8.0

1032 of 1294 branches covered (79.75%)

Branch coverage included in aggregate %.

165 of 219 new or added lines in 6 files covered. (75.34%)

1 existing line in 1 file now uncovered.

3046 of 3391 relevant lines covered (89.83%)

169.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/contracts/modules/staking/StakingExtrasLib.sol
1
// SPDX-License-Identifier: GPL-3.0-only
2

3
pragma solidity ^0.8.18;
4

5
import "../../interfaces/IStakingPool.sol";
6
import "../../libraries/Math.sol";
7
import "../../libraries/UncheckedMath.sol";
8
import "../../libraries/SafeUintCast.sol";
9

10
library StakingExtrasLib {
11
  using SafeUintCast for uint;
12
  using UncheckedMath for uint;
13

14
  uint public constant TRANCHE_DURATION = 91 days;
15
  uint public constant MAX_ACTIVE_TRANCHES = 8;
16
  uint public constant POOL_FEE_DENOMINATOR = 100;
17
  uint public constant ONE_NXM = 1 ether;
18

19
  function updateRewardsShares(
20
    // storage refs
21
    mapping(uint => mapping(uint => IStakingPool.Deposit)) storage deposits,
22
    mapping(uint => IStakingPool.Tranche) storage tranches,
23
    // state
24
    uint accNxmPerRewardsShare,
25
    uint rewardsSharesSupply,
26
    uint poolFee,
27
    // input
28
    uint trancheId,
29
    uint[] calldata tokenIds
30
  ) external returns (uint newRewardsSharesSupply) {
31

NEW
32
    IStakingPool.Deposit memory feeDeposit = deposits[0][trancheId];
×
NEW
33
    IStakingPool.Tranche memory tranche = tranches[trancheId];
×
34

NEW
35
    {
×
36
      // update manager's pending rewards
NEW
37
      uint newRewardPerRewardsShare = accNxmPerRewardsShare.uncheckedSub(feeDeposit.lastAccNxmPerRewardShare);
×
NEW
38
      feeDeposit.pendingRewards += (newRewardPerRewardsShare * feeDeposit.rewardsShares / ONE_NXM).toUint96();
×
NEW
39
      feeDeposit.lastAccNxmPerRewardShare = accNxmPerRewardsShare.toUint96();
×
40
    }
41

NEW
42
    uint trancheShares;
×
43

NEW
44
    for (uint i = 0; i < tokenIds.length; i++) {
×
NEW
45
      require(tokenIds[i] != 0, "INVALID_TOKEN_ID");
×
46

47
      // sload and sum up
NEW
48
      IStakingPool.Deposit memory deposit = deposits[tokenIds[i]][trancheId];
×
NEW
49
      trancheShares += deposit.stakeShares;
×
50

51
      // update
NEW
52
      uint newRewardPerRewardsShare = accNxmPerRewardsShare.uncheckedSub(deposit.lastAccNxmPerRewardShare);
×
NEW
53
      deposit.pendingRewards += (newRewardPerRewardsShare * deposit.rewardsShares / ONE_NXM).toUint96();
×
NEW
54
      deposit.lastAccNxmPerRewardShare = accNxmPerRewardsShare.toUint96();
×
55

56
      // reset rewards shares
NEW
57
      deposit.rewardsShares = deposit.stakeShares;
×
58

59
      // sstore
NEW
60
      deposits[tokenIds[i]][trancheId] = deposit;
×
61
    }
62

63
    // make sure all deposits (token ids) for the current tranche were included in the input
NEW
64
    require(trancheShares == tranche.stakeShares, "INVALID_TOTAL_SHARES");
×
65

66
    // update manager's rewards shares
NEW
67
    feeDeposit.rewardsShares = (trancheShares * poolFee / (POOL_FEE_DENOMINATOR - poolFee)).toUint128();
×
68

NEW
69
    {
×
70
      // update tranche rewards shares and supply
NEW
71
      uint previousRewardsShares = tranche.rewardsShares;
×
NEW
72
      uint updatedRewardsShares = trancheShares + feeDeposit.rewardsShares;
×
73

NEW
74
      tranche.rewardsShares = updatedRewardsShares.toUint128();
×
NEW
75
      rewardsSharesSupply = rewardsSharesSupply - previousRewardsShares + updatedRewardsShares;
×
76
    }
77

78
    // sstore
NEW
79
    deposits[0][trancheId] = feeDeposit;
×
NEW
80
    tranches[trancheId] = tranche;
×
81

NEW
82
    return rewardsSharesSupply;
×
83
  }
84
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc