@@ -105,8 +105,17 @@ contract DividendContract is ReentrancyGuard {
105105 _depositToken (token, amount);
106106 }
107107
108- function getStakedAmount (address user , uint256 cycleIndex ) public view returns (uint256 ) {
108+ function getStakeAmount (uint256 cycleIndex ) public view returns (uint256 ) {
109+ require (cycleIndex <= currentCycleIndex, "Invalid cycle index " );
110+
111+ return _getStakeAmount (msg .sender , cycleIndex);
112+ }
113+
114+ function _getStakeAmount (address user , uint256 cycleIndex ) internal view returns (uint256 ) {
109115 StakeRecord[] memory stakeRecords = UserStakeRecords[user];
116+ if (stakeRecords.length == 0 ) {
117+ return 0 ;
118+ }
110119
111120 // print the stake records
112121 console.log ("will print stake records for user %s " , user);
@@ -115,9 +124,7 @@ contract DividendContract is ReentrancyGuard {
115124 }
116125
117126 for (uint i = stakeRecords.length - 1 ; ; i-- ) {
118-
119- // stakeRecords里面的对应周期的质押数据,都是对应周期发起的操作导致的状态,所以需要进入下一个周期才会生效,所以这里使用 < 而不是 <=
120- if (stakeRecords[i].cycleIndex < cycleIndex) {
127+ if (stakeRecords[i].cycleIndex <= cycleIndex) {
121128 return stakeRecords[i].amount;
122129 }
123130
@@ -247,12 +254,23 @@ contract DividendContract is ReentrancyGuard {
247254 require (cycleIndexs.length > 0 , "No cycle index " );
248255 require (tokens.length > 0 , "No token " );
249256
257+ // display the params
258+ console.log ("will withdraw dividends user %s " , msg .sender );
259+ for (uint i = 0 ; i < cycleIndexs.length ; i++ ) {
260+ console.log ("cycleIndexs %d " , cycleIndexs[i]);
261+ }
262+
250263 RewardInfo[] memory rewards = new RewardInfo [](cycleIndexs.length * tokens.length );
251264 uint256 realRewardLength = 0 ;
252265
253266 for (uint i = 0 ; i < cycleIndexs.length ; i++ ) {
254267 uint256 cycleIndex = cycleIndexs[i];
255- require (cycleIndex < currentCycleIndex, "Cannot claim current cycle " );
268+ require (cycleIndex < currentCycleIndex, "Cannot claim current or future cycle " );
269+
270+ // cycle 0 has no full cycle stake tokens
271+ if (cycleIndex == 0 ) {
272+ continue ;
273+ }
256274
257275 // withdraw every token
258276 for (uint j = 0 ; j < tokens.length ; j++ ) {
@@ -265,7 +283,9 @@ contract DividendContract is ReentrancyGuard {
265283 continue ;
266284 }
267285
268- uint256 userStaked = getStakedAmount (msg .sender , cycleIndex);
286+ // stakeRecords里面的对应周期的质押数据,都是对应周期发起的操作导致的状态,
287+ // 所以需要进入下一个周期才会生效,所以这里使用前一个周期的数据
288+ uint256 userStaked = _getStakeAmount (msg .sender , cycleIndex - 1 );
269289 console.log ("userStaked %d, cycle %d " , userStaked, cycleIndex);
270290
271291 // find the token reward of the cycle
0 commit comments