@@ -105,8 +105,17 @@ contract DividendContract is ReentrancyGuard {
105
105
_depositToken (token, amount);
106
106
}
107
107
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 ) {
109
115
StakeRecord[] memory stakeRecords = UserStakeRecords[user];
116
+ if (stakeRecords.length == 0 ) {
117
+ return 0 ;
118
+ }
110
119
111
120
// print the stake records
112
121
console.log ("will print stake records for user %s " , user);
@@ -115,9 +124,7 @@ contract DividendContract is ReentrancyGuard {
115
124
}
116
125
117
126
for (uint i = stakeRecords.length - 1 ; ; i-- ) {
118
-
119
- // stakeRecords里面的对应周期的质押数据,都是对应周期发起的操作导致的状态,所以需要进入下一个周期才会生效,所以这里使用 < 而不是 <=
120
- if (stakeRecords[i].cycleIndex < cycleIndex) {
127
+ if (stakeRecords[i].cycleIndex <= cycleIndex) {
121
128
return stakeRecords[i].amount;
122
129
}
123
130
@@ -247,12 +254,23 @@ contract DividendContract is ReentrancyGuard {
247
254
require (cycleIndexs.length > 0 , "No cycle index " );
248
255
require (tokens.length > 0 , "No token " );
249
256
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
+
250
263
RewardInfo[] memory rewards = new RewardInfo [](cycleIndexs.length * tokens.length );
251
264
uint256 realRewardLength = 0 ;
252
265
253
266
for (uint i = 0 ; i < cycleIndexs.length ; i++ ) {
254
267
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
+ }
256
274
257
275
// withdraw every token
258
276
for (uint j = 0 ; j < tokens.length ; j++ ) {
@@ -265,7 +283,9 @@ contract DividendContract is ReentrancyGuard {
265
283
continue ;
266
284
}
267
285
268
- uint256 userStaked = getStakedAmount (msg .sender , cycleIndex);
286
+ // stakeRecords里面的对应周期的质押数据,都是对应周期发起的操作导致的状态,
287
+ // 所以需要进入下一个周期才会生效,所以这里使用前一个周期的数据
288
+ uint256 userStaked = _getStakeAmount (msg .sender , cycleIndex - 1 );
269
289
console.log ("userStaked %d, cycle %d " , userStaked, cycleIndex);
270
290
271
291
// find the token reward of the cycle
0 commit comments