@gooddollar/goodprotocol 1.0.8 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/artifacts/contracts/DAOStackInterfaces.sol/Avatar.dbg.json +1 -1
  2. package/artifacts/contracts/DAOStackInterfaces.sol/Controller.dbg.json +1 -1
  3. package/artifacts/contracts/DAOStackInterfaces.sol/GlobalConstraintInterface.dbg.json +1 -1
  4. package/artifacts/contracts/DAOStackInterfaces.sol/IntVoteInterface.dbg.json +1 -1
  5. package/artifacts/contracts/DAOStackInterfaces.sol/ReputationInterface.dbg.json +1 -1
  6. package/artifacts/contracts/DAOStackInterfaces.sol/SchemeRegistrar.dbg.json +1 -1
  7. package/artifacts/contracts/Interfaces.sol/AggregatorV3Interface.dbg.json +1 -1
  8. package/artifacts/contracts/Interfaces.sol/ERC20.dbg.json +1 -1
  9. package/artifacts/contracts/Interfaces.sol/IAaveIncentivesController.dbg.json +1 -1
  10. package/artifacts/contracts/Interfaces.sol/IAdminWallet.dbg.json +1 -1
  11. package/artifacts/contracts/Interfaces.sol/IDonationStaking.dbg.json +1 -1
  12. package/artifacts/contracts/Interfaces.sol/IERC2917.dbg.json +1 -1
  13. package/artifacts/contracts/Interfaces.sol/IFirstClaimPool.dbg.json +1 -1
  14. package/artifacts/contracts/Interfaces.sol/IGoodDollar.dbg.json +1 -1
  15. package/artifacts/contracts/Interfaces.sol/IGoodStaking.dbg.json +1 -1
  16. package/artifacts/contracts/Interfaces.sol/IHasRouter.dbg.json +1 -1
  17. package/artifacts/contracts/Interfaces.sol/IIdentity.dbg.json +1 -1
  18. package/artifacts/contracts/Interfaces.sol/ILendingPool.dbg.json +1 -1
  19. package/artifacts/contracts/Interfaces.sol/INameService.dbg.json +1 -1
  20. package/artifacts/contracts/Interfaces.sol/IUBIScheme.dbg.json +1 -1
  21. package/artifacts/contracts/Interfaces.sol/ProxyAdmin.dbg.json +1 -1
  22. package/artifacts/contracts/Interfaces.sol/Reserve.dbg.json +1 -1
  23. package/artifacts/contracts/Interfaces.sol/Staking.dbg.json +1 -1
  24. package/artifacts/contracts/Interfaces.sol/Uniswap.dbg.json +1 -1
  25. package/artifacts/contracts/Interfaces.sol/UniswapFactory.dbg.json +1 -1
  26. package/artifacts/contracts/Interfaces.sol/UniswapPair.dbg.json +1 -1
  27. package/artifacts/contracts/Interfaces.sol/cERC20.dbg.json +1 -1
  28. package/artifacts/contracts/governance/ClaimersDistribution.sol/ClaimersDistribution.dbg.json +1 -1
  29. package/artifacts/contracts/governance/CompoundVotingMachine.sol/CompoundVotingMachine.dbg.json +1 -1
  30. package/artifacts/contracts/governance/GReputation.sol/GReputation.dbg.json +1 -1
  31. package/artifacts/contracts/governance/GovarnanceStaking.sol/GovernanceStaking.dbg.json +1 -1
  32. package/artifacts/contracts/governance/MultiBaseGovernanceShareField.sol/MultiBaseGovernanceShareField.dbg.json +1 -1
  33. package/artifacts/contracts/governance/Reputation.sol/Reputation.dbg.json +1 -1
  34. package/artifacts/contracts/governance/StakersDistribution.sol/StakersDistribution.dbg.json +1 -1
  35. package/artifacts/contracts/governance/StakersDistribution.sol/StakersDistribution.json +2 -2
  36. package/artifacts/contracts/mocks/AaveMock.sol/AaveMock.dbg.json +1 -1
  37. package/artifacts/contracts/mocks/DAIMock.sol/DAIMock.dbg.json +1 -1
  38. package/artifacts/contracts/mocks/DecimalsMock.sol/DecimalsMock.dbg.json +1 -1
  39. package/artifacts/contracts/mocks/GoodCompoundStakingTest.sol/GoodCompoundStakingTest.dbg.json +1 -1
  40. package/artifacts/contracts/mocks/GoodCompoundStakingTest.sol/GoodCompoundStakingTest.json +6 -6
  41. package/artifacts/contracts/mocks/GoodFundManagerTest.sol/GoodFundManagerTest.dbg.json +1 -1
  42. package/artifacts/contracts/mocks/IncentiveControllerMock.sol/IncentiveControllerMock.dbg.json +1 -1
  43. package/artifacts/contracts/mocks/LendingPoolMock.sol/LendingPoolMock.dbg.json +1 -1
  44. package/artifacts/contracts/mocks/OverMintTester.sol/OverMintTester.dbg.json +1 -1
  45. package/artifacts/contracts/mocks/OverMintTesterRegularStake.sol/OverMintTesterRegularStake.dbg.json +1 -1
  46. package/artifacts/contracts/mocks/OverMintTesterRegularStake.sol/OverMintTesterRegularStake.json +2 -2
  47. package/artifacts/contracts/mocks/SixteenDecimalsTokenMock.sol/SixteenDecimalsTokenMock.dbg.json +1 -1
  48. package/artifacts/contracts/mocks/SwapHelperTest.sol/SwapHelperTest.dbg.json +1 -1
  49. package/artifacts/contracts/mocks/TwentyDecimalsTokenMock.sol/TwentyDecimalsTokenMock.dbg.json +1 -1
  50. package/artifacts/contracts/mocks/UpgradableMocks.sol/UpgradableMock.dbg.json +1 -1
  51. package/artifacts/contracts/mocks/UpgradableMocks.sol/UpgradableMock2.dbg.json +1 -1
  52. package/artifacts/contracts/mocks/UsdcMock.sol/USDCMock.dbg.json +1 -1
  53. package/artifacts/contracts/mocks/cBATMock.sol/cBATMock.dbg.json +1 -1
  54. package/artifacts/contracts/mocks/cDAILowWorthMock.sol/cDAILowWorthMock.dbg.json +1 -1
  55. package/artifacts/contracts/mocks/cDAIMock.sol/cDAIMock.dbg.json +1 -1
  56. package/artifacts/contracts/mocks/cDAINonMintableMock.sol/cDAINonMintableMock.dbg.json +1 -1
  57. package/artifacts/contracts/mocks/cDecimalsMock.sol/cDecimalsMock.dbg.json +1 -1
  58. package/artifacts/contracts/mocks/cSDTMock.sol/cSDTMock.dbg.json +1 -1
  59. package/artifacts/contracts/mocks/cUSDCMock.sol/cUSDCMock.dbg.json +1 -1
  60. package/artifacts/contracts/reserve/ExchangeHelper.sol/ExchangeHelper.dbg.json +1 -1
  61. package/artifacts/contracts/reserve/GoodMarketMaker.sol/GoodMarketMaker.dbg.json +1 -1
  62. package/artifacts/contracts/reserve/GoodReserveCDai.sol/ContributionCalc.dbg.json +1 -1
  63. package/artifacts/contracts/reserve/GoodReserveCDai.sol/GoodReserveCDai.dbg.json +1 -1
  64. package/artifacts/contracts/staking/BaseShareField.sol/BaseShareField.dbg.json +1 -1
  65. package/artifacts/contracts/staking/BaseShareFieldV2.sol/BaseShareFieldV2.dbg.json +4 -0
  66. package/artifacts/contracts/staking/BaseShareFieldV2.sol/BaseShareFieldV2.json +200 -0
  67. package/artifacts/contracts/staking/DonationsStaking.sol/DonationsStaking.dbg.json +1 -1
  68. package/artifacts/contracts/staking/DonationsStaking.sol/DonationsStaking.json +2 -2
  69. package/artifacts/contracts/staking/GoodFundManager.sol/GoodFundManager.dbg.json +1 -1
  70. package/artifacts/contracts/staking/SimpleStaking.sol/SimpleStaking.dbg.json +1 -1
  71. package/artifacts/contracts/staking/SimpleStakingV2.sol/SimpleStakingV2.dbg.json +4 -0
  72. package/artifacts/contracts/staking/SimpleStakingV2.sol/SimpleStakingV2.json +958 -0
  73. package/artifacts/contracts/staking/UniswapV2SwapHelper.sol/UniswapV2SwapHelper.dbg.json +1 -1
  74. package/artifacts/contracts/staking/aave/AaveStakingFactory.sol/AaveStakingFactory.dbg.json +1 -1
  75. package/artifacts/contracts/staking/aave/AaveStakingFactory.sol/AaveStakingFactory.json +65 -12
  76. package/artifacts/contracts/staking/aave/GoodAaveStaking.sol/GoodAaveStaking.dbg.json +1 -1
  77. package/artifacts/contracts/staking/aave/GoodAaveStaking.sol/GoodAaveStaking.json +4 -4
  78. package/artifacts/contracts/staking/aave/GoodAaveStakingV2.sol/GoodAaveStakingV2.dbg.json +4 -0
  79. package/artifacts/contracts/staking/aave/GoodAaveStakingV2.sol/GoodAaveStakingV2.json +1149 -0
  80. package/artifacts/contracts/staking/compound/CompoundStakingFactory.sol/CompoundStakingFactory.dbg.json +1 -1
  81. package/artifacts/contracts/staking/compound/CompoundStakingFactory.sol/CompoundStakingFactory.json +57 -14
  82. package/artifacts/contracts/staking/compound/GoodCompoundStaking.sol/GoodCompoundStaking.dbg.json +1 -1
  83. package/artifacts/contracts/staking/compound/GoodCompoundStaking.sol/GoodCompoundStaking.json +6 -6
  84. package/artifacts/contracts/staking/compound/GoodCompoundStakingV2.sol/GoodCompoundStakingV2.dbg.json +4 -0
  85. package/artifacts/contracts/staking/compound/GoodCompoundStakingV2.sol/GoodCompoundStakingV2.json +1092 -0
  86. package/artifacts/contracts/ubi/UBIScheme.sol/UBIScheme.dbg.json +1 -1
  87. package/artifacts/contracts/unaudited-foundation/FuseFaucet.sol/FuseFaucet.dbg.json +1 -1
  88. package/artifacts/contracts/unaudited-foundation/InvitesV1.sol/InvitesV1.dbg.json +1 -1
  89. package/artifacts/contracts/utils/BancorFormula.sol/BancorFormula.dbg.json +1 -1
  90. package/artifacts/contracts/utils/DAOContract.sol/DAOContract.dbg.json +1 -1
  91. package/artifacts/contracts/utils/DAOUpgradeableContract.sol/DAOUpgradeableContract.dbg.json +1 -1
  92. package/artifacts/contracts/utils/DSMath.sol/DSMath.dbg.json +1 -1
  93. package/artifacts/contracts/utils/DataTypes.sol/DataTypes.dbg.json +1 -1
  94. package/artifacts/contracts/utils/NameService.sol/NameService.dbg.json +1 -1
  95. package/artifacts/contracts/utils/ProtocolUpgrade.sol/OldMarketMaker.dbg.json +1 -1
  96. package/artifacts/contracts/utils/ProtocolUpgrade.sol/ProtocolUpgrade.dbg.json +1 -1
  97. package/artifacts/contracts/utils/ProtocolUpgradeFuse.sol/ProtocolUpgradeFuse.dbg.json +1 -1
  98. package/artifacts/contracts/utils/ReputationTestHelper.sol/ReputationTestHelper.dbg.json +1 -1
  99. package/contracts/staking/BaseShareFieldV2.sol +343 -0
  100. package/contracts/staking/SimpleStaking.sol +0 -7
  101. package/contracts/staking/SimpleStakingV2.sol +488 -0
  102. package/contracts/staking/aave/AaveStakingFactory.sol +45 -19
  103. package/contracts/staking/aave/GoodAaveStaking.sol +1 -1
  104. package/contracts/staking/aave/GoodAaveStakingV2.sol +263 -0
  105. package/contracts/staking/compound/CompoundStakingFactory.sol +41 -18
  106. package/contracts/staking/compound/GoodCompoundStaking.sol +1 -1
  107. package/contracts/staking/compound/GoodCompoundStakingV2.sol +315 -0
  108. package/package.json +1 -1
  109. package/releases/deployment.json +55 -47
  110. package/scripts/deployFullDAO.ts +1 -1
  111. package/scripts/upgradeToV2/upgradeToV2.ts +7 -3
  112. package/test/governance/StakersDistribution.test.ts +43 -14
  113. package/test/helpers.ts +6 -1
  114. package/test/staking/CompoundStakingFactory.test.ts +24 -21
  115. package/test/staking/DifferentStakingTokens.test.ts +3 -1
  116. package/test/staking/DonationsStaking.test.ts +3 -1
  117. package/test/staking/GoodAaveStakingFactory.test.ts +26 -23
  118. package/test/staking/SimpleDAIStaking.test.ts +3 -11
  119. package/test/staking/StakingRewards.test.ts +20 -14
  120. package/test/staking/SwapHelper.test.ts +4 -1
  121. package/test/staking/UsdcAaveStaking.test.ts +22 -3
  122. package/test/ubi/UBIScheme.e2e.test.ts +1 -1
  123. package/test/utils/ProtocolUpgrade.test.ts +1 -1
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "_format": "hh-sol-dbg-1",
3
- "buildInfo": "../../../build-info/82d74f20f1a2ee68fd4fbdf578e2beb6.json"
3
+ "buildInfo": "../../../build-info/78fe23216a2e71b0745d62e12899dde9.json"
4
4
  }
@@ -0,0 +1,343 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity >=0.8.0;
3
+ import "../Interfaces.sol";
4
+
5
+ contract BaseShareFieldV2 {
6
+ // rewards claimed by users
7
+ uint128 mintedRewards;
8
+ // total staked for shares calculation
9
+ uint128 totalProductivity;
10
+ // total staked that earns rewards (some stakers can donate their rewards)
11
+ uint128 totalEffectiveStakes;
12
+ // rewards accumulated for distribution
13
+ uint128 accumulatedRewards;
14
+ // block of last rewards accumulation
15
+ uint128 lastRewardBlock;
16
+ // number of blocks before reaching the max rewards multiplier (starting at 0.5 reaching 1 after maxMultiplierThreshold)
17
+ uint64 maxMultiplierThreshold;
18
+ // Staking contracts accepts Tokens with max 18 decimals so this variable holds decimal difference between 18 and Token's decimal in order to make calculations
19
+ uint8 tokenDecimalDifference;
20
+
21
+ // accumulated rewards per share in 27 decimals precision
22
+ uint256 accAmountPerShare;
23
+
24
+ //status of user rewards. everything is in 18 decimals
25
+ struct UserInfo {
26
+ uint128 amount; // How many tokens the user has provided.
27
+ uint128 effectiveStakes; // stakes not including stakes that donate their rewards
28
+ uint128 rewardDebt; // Reward debt.
29
+ uint128 rewardEarn; // Reward earn and not minted
30
+ uint128 rewardMinted; //Rewards minted to user so far
31
+ uint64 lastRewardTime; // Last time that user got rewards
32
+ uint64 multiplierResetTime; // Reset time of multiplier
33
+ }
34
+ mapping(address => UserInfo) public users;
35
+
36
+ function getStats()
37
+ external
38
+ view
39
+ returns (
40
+ uint256 _accAmountPerShare,
41
+ uint128 _mintedRewards,
42
+ uint128 _totalProductivity,
43
+ uint128 _totalEffectiveStakes,
44
+ uint128 _accumulatedRewards,
45
+ uint128 _lastRewardBlock,
46
+ uint64 _maxMultiplierThreshold,
47
+ uint8 _tokenDecimalDifference
48
+ )
49
+ {
50
+ return (
51
+ accAmountPerShare,
52
+ mintedRewards,
53
+ totalProductivity,
54
+ totalEffectiveStakes,
55
+ accumulatedRewards,
56
+ lastRewardBlock,
57
+ maxMultiplierThreshold,
58
+ tokenDecimalDifference
59
+ );
60
+ }
61
+
62
+ /**
63
+ * @dev Helper function to check if caller is fund manager
64
+ */
65
+ function _canMintRewards() internal view virtual {}
66
+
67
+ /**
68
+ * @dev Update reward variables of the given pool to be up-to-date.
69
+ * Calculates passed blocks and adding to the reward pool
70
+ * @param rewardsPerBlock how much rewards does this contract earns per block
71
+ * @param blockStart block from which contract starts earning rewards
72
+ * @param blockEnd block from which contract stops earning rewards
73
+ */
74
+ function _update(
75
+ uint256 rewardsPerBlock,
76
+ uint256 blockStart,
77
+ uint256 blockEnd
78
+ ) internal virtual {
79
+ if (totalEffectiveStakes == 0) {
80
+ lastRewardBlock = uint128(block.number);
81
+ return;
82
+ }
83
+ if (block.number >= blockStart && lastRewardBlock < blockStart) {
84
+ lastRewardBlock = uint128(blockStart);
85
+ }
86
+
87
+ uint256 _lastRewardBlock = lastRewardBlock < blockStart &&
88
+ block.number >= blockStart
89
+ ? blockStart
90
+ : lastRewardBlock;
91
+ uint256 curRewardBlock = block.number > blockEnd ? blockEnd : block.number;
92
+
93
+ if (curRewardBlock < blockStart || _lastRewardBlock >= blockEnd) return;
94
+
95
+ uint256 multiplier = curRewardBlock - _lastRewardBlock; // Blocks passed since last reward block
96
+ uint256 reward = multiplier * (rewardsPerBlock * 1e16); // rewardsPerBlock is in G$ which is only 2 decimals, we turn it into 18 decimals by multiplying 1e16
97
+
98
+ accAmountPerShare =
99
+ accAmountPerShare +
100
+ (reward * 1e27) /
101
+ (totalEffectiveStakes * (10**tokenDecimalDifference));
102
+ // Increase totalEffectiveStakes decimals if it is less than 18 decimals then accAmountPerShare in 27 decimals
103
+
104
+ lastRewardBlock = uint128(curRewardBlock);
105
+ }
106
+
107
+ /**
108
+ * @dev Audit user's rewards and calculate their earned rewards
109
+ * For the first month rewards calculated with 0.5x
110
+ * multiplier therefore they just gets half of the rewards which they earned in the first month
111
+ * after first month they get full amount of rewards for the part that they earned after one month
112
+ * @param user the user to audit
113
+ * @param updatedAmount the new stake of the user after deposit/withdraw
114
+ * @param donationPer percentage user is donating from his rewards. (currently just 0 or 100 in SimpleStaking)
115
+ */
116
+ function _audit(
117
+ address user,
118
+ uint256 updatedAmount,
119
+ uint256 donationPer
120
+ ) internal virtual {
121
+ UserInfo storage userInfo = users[user];
122
+ uint256 _amount = userInfo.amount;
123
+ uint256 userEffectiveStake = userInfo.effectiveStakes;
124
+ if (userEffectiveStake > 0) {
125
+ (
126
+ uint256 blocksToPay,
127
+ uint256 firstMonthBlocksToPay,
128
+ uint256 fullBlocksToPay
129
+ ) = _auditCalcs(userInfo);
130
+
131
+ if (blocksToPay != 0) {
132
+ uint256 pending = (userEffectiveStake *
133
+ (10**tokenDecimalDifference) *
134
+ accAmountPerShare) /
135
+ 1e27 -
136
+ userInfo.rewardDebt;
137
+ // Turn userInfo.amount to 18 decimals by multiplying tokenDecimalDifference if it's not and multiply with accAmountPerShare which is 27 decimals then divide it 1e27 bring it down to 18 decimals
138
+ uint256 rewardPerBlock = (pending * 1e9) / blocksToPay; // bring pending to 1e27
139
+ pending =
140
+ ((((firstMonthBlocksToPay * 1e2 * 5) / 10) + fullBlocksToPay * 1e2) * // multiply first month by 0.5x (5/10) since rewards in first month with multiplier 0.5 and multiply it with 1e2 to get it 2decimals so we could get more precision
141
+ rewardPerBlock) / // Multiply fullBlocksToPay with 1e2 to bring it to 2 decimals // rewardPerBlock is in 27decimals
142
+ 1e11; // Pending in 18 decimals so we divide 1e11 to bring it down to 18 decimals
143
+ userInfo.rewardEarn = uint128(userInfo.rewardEarn + pending); // Add user's earned rewards to user's account so it can be minted later
144
+ accumulatedRewards = uint128(accumulatedRewards + pending);
145
+ }
146
+ } else {
147
+ userInfo.multiplierResetTime = uint64(block.number); // Should set user's multiplierResetTime when they stake for the first time
148
+ }
149
+
150
+ //if withdrawing rewards/stake we reset multiplier, only in case of increasinig productivity we dont reset multiplier
151
+ if (updatedAmount <= _amount) {
152
+ userInfo.multiplierResetTime = uint64(block.number);
153
+ if (_amount > 0) {
154
+ //calculate relative part of user effective stakes
155
+ uint256 withdrawFromEffectiveStake = ((_amount - updatedAmount) *
156
+ userInfo.effectiveStakes) / _amount;
157
+ userInfo.effectiveStakes -= uint128(withdrawFromEffectiveStake);
158
+ totalEffectiveStakes -= uint128(withdrawFromEffectiveStake);
159
+ }
160
+ } else if (donationPer == 0) {
161
+ userInfo.effectiveStakes += uint128(updatedAmount - _amount);
162
+ totalEffectiveStakes += uint128(updatedAmount - _amount);
163
+ }
164
+ userInfo.lastRewardTime = uint64(block.number);
165
+ userInfo.amount = uint128(updatedAmount);
166
+ userInfo.rewardDebt = uint128(
167
+ (userInfo.effectiveStakes *
168
+ (10**tokenDecimalDifference) *
169
+ accAmountPerShare) / 1e27
170
+ ); // Divide to 1e27 to keep rewardDebt in 18 decimals since accAmountPerShare is 27 decimals
171
+ }
172
+
173
+ /**
174
+ * @dev Helper function to make calculations in audit and getUserPendingReward methods
175
+ */
176
+ function _auditCalcs(UserInfo memory _userInfo)
177
+ internal
178
+ view
179
+ returns (
180
+ uint256,
181
+ uint256,
182
+ uint256
183
+ )
184
+ {
185
+ uint256 blocksPaid = _userInfo.lastRewardTime -
186
+ _userInfo.multiplierResetTime; // lastRewardTime is always >= multiplierResetTime
187
+ uint256 blocksPassedFirstMonth = maxMultiplierThreshold <
188
+ block.number - _userInfo.multiplierResetTime
189
+ ? maxMultiplierThreshold
190
+ : block.number - _userInfo.multiplierResetTime;
191
+ // blocks which is after first month
192
+ uint256 blocksToPay = block.number - _userInfo.lastRewardTime; // blocks passed since last payment
193
+ uint256 firstMonthBlocksToPay = blocksPaid >= maxMultiplierThreshold
194
+ ? 0
195
+ : blocksPassedFirstMonth - blocksPaid; // block which is in the first month so pays with 0.5x multiplier
196
+ uint256 fullBlocksToPay = blocksToPay - firstMonthBlocksToPay; // blocks to pay in full amount which means with 1x multiplier
197
+ return (blocksToPay, firstMonthBlocksToPay, fullBlocksToPay);
198
+ }
199
+
200
+ /**
201
+ * @dev This function increase user's productivity and updates the global productivity.
202
+ * This function increase user's productivity and updates the global productivity.
203
+ * the users' actual share percentage will calculated by:
204
+ * Formula: user_productivity / global_productivity
205
+ * @param user the user to update
206
+ * @param value the increase in user stake
207
+ * @param rewardsPerBlock how much rewards does this contract earns per block
208
+ * @param blockStart block from which contract starts earning rewards
209
+ * @param blockEnd block from which contract stops earning rewards
210
+ * @param donationPer percentage user is donating from his rewards. (currently just 0 or 100 in SimpleStaking)
211
+ */
212
+ function _increaseProductivity(
213
+ address user,
214
+ uint256 value,
215
+ uint256 rewardsPerBlock,
216
+ uint256 blockStart,
217
+ uint256 blockEnd,
218
+ uint256 donationPer
219
+ ) internal virtual returns (bool) {
220
+ _update(rewardsPerBlock, blockStart, blockEnd);
221
+ _audit(user, users[user].amount + value, donationPer);
222
+
223
+ totalProductivity = uint128(totalProductivity + value);
224
+ return true;
225
+ }
226
+
227
+ /**
228
+ * @dev This function will decreases user's productivity by value, and updates the global productivity
229
+ * it will record which block this is happenning and accumulates the area of (productivity * time)
230
+ * @param user the user to update
231
+ * @param value the increase in user stake
232
+ * @param rewardsPerBlock how much rewards does this contract earns per block
233
+ * @param blockStart block from which contract starts earning rewards
234
+ * @param blockEnd block from which contract stops earning rewards
235
+ */
236
+
237
+ function _decreaseProductivity(
238
+ address user,
239
+ uint256 value,
240
+ uint256 rewardsPerBlock,
241
+ uint256 blockStart,
242
+ uint256 blockEnd
243
+ ) internal virtual returns (bool) {
244
+ _update(rewardsPerBlock, blockStart, blockEnd);
245
+ _audit(user, users[user].amount - value, 1); // donationPer variable should be something different than zero so called with 1
246
+ totalProductivity = uint128(totalProductivity - value);
247
+
248
+ return true;
249
+ }
250
+
251
+ /**
252
+ * @dev Query user's pending reward with updated variables
253
+ * @param user the user to update
254
+ * @param rewardsPerBlock how much rewards does this contract earns per block
255
+ * @param blockStart block from which contract starts earning rewards
256
+ * @param blockEnd block from which contract stops earning rewards
257
+ * @return returns amount of user's earned but not minted rewards
258
+ */
259
+ function getUserPendingReward(
260
+ address user,
261
+ uint256 rewardsPerBlock,
262
+ uint256 blockStart,
263
+ uint256 blockEnd
264
+ ) public view returns (uint256) {
265
+ UserInfo memory userInfo = users[user];
266
+ uint256 _accAmountPerShare = accAmountPerShare;
267
+
268
+ uint256 pending = 0;
269
+ if (
270
+ totalEffectiveStakes != 0 &&
271
+ block.number >= blockStart &&
272
+ blockEnd >= block.number
273
+ ) {
274
+ uint256 multiplier = block.number - lastRewardBlock;
275
+ uint256 reward = multiplier * (rewardsPerBlock * 1e16); // turn it to 18 decimals since rewardsPerBlock in 2 decimals
276
+ (
277
+ uint256 blocksToPay,
278
+ uint256 firstMonthBlocksToPay,
279
+ uint256 fullBlocksToPay
280
+ ) = _auditCalcs(userInfo);
281
+
282
+ _accAmountPerShare =
283
+ _accAmountPerShare +
284
+ (reward * 1e27) /
285
+ (totalEffectiveStakes * 10**tokenDecimalDifference); // Increase totalEffectiveStakes decimals if it is less than 18 decimals then accAmountPerShare in 27 decimals
286
+ UserInfo memory tempUserInfo = userInfo; // to prevent stack too deep error any other recommendation?
287
+ if (blocksToPay != 0) {
288
+ pending =
289
+ (tempUserInfo.effectiveStakes *
290
+ (10**tokenDecimalDifference) *
291
+ _accAmountPerShare) /
292
+ 1e27 -
293
+ tempUserInfo.rewardDebt; // Turn userInfo.amount to 18 decimals by multiplying tokenDecimalDifference if it's not and multiply with accAmountPerShare which is 27 decimals then divide it 1e27 bring it down to 18 decimals
294
+ uint256 rewardPerBlock = (pending * 1e27) / (blocksToPay * 1e18); // bring both variable to 18 decimals and multiply pending by 1e27 so when we divide them to each other result would be in 1e27
295
+ pending =
296
+ ((((firstMonthBlocksToPay * 1e2 * 5) / 10) + fullBlocksToPay * 1e2) * // multiply first month by 0.5x (5/10) since rewards in first month with multiplier 0.5 and multiply it with 1e2 to get it 2decimals so we could get more precision
297
+ rewardPerBlock) / // Multiply fullBlocksToPay with 1e2 to bring it to 2decimals // rewardPerBlock is in 27decimals
298
+ 1e11; // Pending in 18 decimals so we divide 1e11 to bring it down to 18 decimals
299
+ }
300
+ }
301
+ return userInfo.rewardEarn + pending; // rewardEarn is in 18 decimals
302
+ }
303
+
304
+ /**
305
+ * @dev When the fundmanager calls this function it will updates the user records
306
+ * get the user rewards which they earned but not minted and mark it as minted
307
+ * @param user the user to update
308
+ * @param rewardsPerBlock how much rewards does this contract earns per block
309
+ * @param blockStart block from which contract starts earning rewards
310
+ * @param blockEnd block from which contract stops earning rewards
311
+ * @return returns amount to mint as reward to the user
312
+ */
313
+
314
+ function rewardsMinted(
315
+ address user,
316
+ uint256 rewardsPerBlock,
317
+ uint256 blockStart,
318
+ uint256 blockEnd
319
+ ) public returns (uint256) {
320
+ _canMintRewards();
321
+ _update(rewardsPerBlock, blockStart, blockEnd);
322
+ _audit(user, users[user].amount, 1); // donationPer variable should be something different than zero so called with 1
323
+ uint128 amount = users[user].rewardEarn;
324
+ users[user].rewardEarn = 0;
325
+ users[user].rewardMinted += amount;
326
+ mintedRewards = mintedRewards + amount;
327
+ amount = amount / 1e16; // change decimal of mint amount to GD decimals
328
+ return amount;
329
+ }
330
+
331
+ /**
332
+ * @return Returns how many productivity a user has and global has.
333
+ */
334
+
335
+ function getProductivity(address user)
336
+ public
337
+ view
338
+ virtual
339
+ returns (uint256, uint256)
340
+ {
341
+ return (users[user].amount, totalProductivity);
342
+ }
343
+ }
@@ -174,11 +174,6 @@ abstract contract SimpleStaking is
174
174
  uint256
175
175
  );
176
176
 
177
- /**
178
- * @dev Approve infinite tokens to defi protocols in order to save gas
179
- */
180
- function _approveTokens() internal virtual;
181
-
182
177
  /**
183
178
  * @dev Allows a staker to deposit Tokens. Notice that `approve` is
184
179
  * needed to be executed before the execution of this method.
@@ -368,8 +363,6 @@ abstract contract SimpleStaking is
368
363
  );
369
364
 
370
365
  if (address(sd) != address(0)) {
371
- address[] memory contracts;
372
- contracts[0] = (address(this));
373
366
  sd.userWithdraw(_from, _value);
374
367
  sd.userStaked(_to, _value);
375
368
  }