@@ -70,6 +70,16 @@ contract DividendContract is ReentrancyGuard {
70
70
return cycles[currentCycleIndex];
71
71
}
72
72
73
+ function getTotalStaked (uint256 cycleIndex ) public view returns (uint256 ) {
74
+ if (cycleIndex == currentCycleIndex) {
75
+ return totalStaked;
76
+ } else if (cycleIndex < currentCycleIndex) {
77
+ return cycles[cycleIndex].totalStaked;
78
+ } else {
79
+ return 0 ;
80
+ }
81
+ }
82
+
73
83
// deposit token to the current cycle
74
84
function _depositToken (address token , uint256 amount ) internal {
75
85
require (amount > 0 , "Cannot deposit 0 " );
@@ -118,10 +128,12 @@ contract DividendContract is ReentrancyGuard {
118
128
}
119
129
120
130
// print the stake records
131
+ /*
121
132
console.log("will print stake records for user %s", user);
122
133
for (uint i = 0; i < stakeRecords.length; i++) {
123
134
console.log("StakeRecords: cycleIndex %d, stake mount %d", stakeRecords[i].cycleIndex, stakeRecords[i].amount);
124
135
}
136
+ */
125
137
126
138
for (uint i = stakeRecords.length - 1 ; ; i-- ) {
127
139
if (stakeRecords[i].cycleIndex <= cycleIndex) {
@@ -165,20 +177,21 @@ contract DividendContract is ReentrancyGuard {
165
177
// withdraw staking tokens from current first and then next cycles
166
178
// withdraw amount must be less than or equal to the staked amount
167
179
function unstake (uint256 amount ) external nonReentrant {
180
+ require (amount > 0 , "Cannot unstake 0 " );
168
181
StakeRecord[] storage stakeRecords = UserStakeRecords[msg .sender ];
169
182
require (stakeRecords.length > 0 , "No stake record found " );
170
183
171
184
console.log ("user unstake <=== amount %d, cycle %d, user %s " , amount, currentCycleIndex, msg .sender );
172
185
173
186
// get the last stake record of the user
174
187
StakeRecord storage lastStakeRecord = stakeRecords[stakeRecords.length - 1 ];
175
- require (lastStakeRecord.amount >= amount, "Insufficient staked amount " );
188
+ require (lastStakeRecord.amount >= amount, "Insufficient stake amount " );
176
189
177
190
// 如果存在当前周期的质押操作,那么这个质押操作是可以直接撤销的不影响周期数据(当前质押要在下个周期进入cycleInfo中)
178
191
// 如果不是当前周期的质押操作,或者当前周期的质押数量不足,那么这个质押操作是需要从上个周期关联的cycleInfo数据中减去的
179
- console.log ("unstaking amount %d " , amount);
180
- console.log ("currentCycleIndex %d, lastStakeRecord.cycleIndex %d, amount %d " , currentCycleIndex, lastStakeRecord.cycleIndex, lastStakeRecord.amount);
181
- console.log ("lastStakeRecord.cycleIndex %d " , lastStakeRecord.cycleIndex);
192
+ // console.log("unstaking amount %d", amount);
193
+ // console.log("currentCycleIndex %d, lastStakeRecord.cycleIndex %d, amount %d", currentCycleIndex, lastStakeRecord.cycleIndex, lastStakeRecord.amount);
194
+ // console.log("lastStakeRecord.cycleIndex %d", lastStakeRecord.cycleIndex);
182
195
if (lastStakeRecord.cycleIndex == currentCycleIndex) {
183
196
184
197
uint256 newAmount = 0 ;
@@ -197,9 +210,9 @@ contract DividendContract is ReentrancyGuard {
197
210
uint256 diff = amount - newAmount;
198
211
199
212
StakeRecord storage prevStakeRecord = stakeRecords[stakeRecords.length - 2 ];
200
- console.log ("prevStakeRecord.amount %d " , prevStakeRecord.amount);
201
- console.log ("prevStakeRecord.cycleIndex %d " , prevStakeRecord.cycleIndex);
202
- console.log ("prevStakeRecord.totalStaked %d " , cycles[prevStakeRecord.cycleIndex].totalStaked);
213
+ // console.log("prevStakeRecord.amount %d", prevStakeRecord.amount);
214
+ // console.log("prevStakeRecord.cycleIndex %d", prevStakeRecord.cycleIndex);
215
+ // console.log("prevStakeRecord.totalStaked %d", cycles[prevStakeRecord.cycleIndex].totalStaked);
203
216
204
217
// the last record is unstaked all and is empty, delete it
205
218
stakeRecords.pop ();
@@ -218,6 +231,7 @@ contract DividendContract is ReentrancyGuard {
218
231
219
232
totalStaked -= amount;
220
233
234
+ console.log ("will unstake transfer %s ===> %d " , msg .sender , amount);
221
235
require (IERC20 (stakingToken).transfer (msg .sender , amount), "Unstake failed " );
222
236
223
237
emit Unstake (msg .sender , amount);
@@ -244,15 +258,16 @@ contract DividendContract is ReentrancyGuard {
244
258
}
245
259
246
260
// check if the user has settled the rewards for the cycle
247
- function isDividendWithdrawed (address user , uint256 cycleIndex , address token ) public view returns (bool ) {
248
- bytes32 key = keccak256 (abi.encodePacked (user , cycleIndex, token));
261
+ function isDividendWithdrawed (uint256 cycleIndex , address token ) public view returns (bool ) {
262
+ bytes32 key = keccak256 (abi.encodePacked (msg . sender , cycleIndex, token));
249
263
return withdrawDividendState[key];
250
264
}
251
265
252
266
// claim rewards for the cycle
253
267
function withdrawDividends (uint256 [] calldata cycleIndexs , address [] calldata tokens ) external nonReentrant {
254
268
require (cycleIndexs.length > 0 , "No cycle index " );
255
269
require (tokens.length > 0 , "No token " );
270
+ // require(UserStakeRecords[msg.sender].length > 0, "No stake record");
256
271
257
272
// display the params
258
273
console.log ("will withdraw dividends user %s " , msg .sender );
@@ -275,7 +290,8 @@ contract DividendContract is ReentrancyGuard {
275
290
// withdraw every token
276
291
for (uint j = 0 ; j < tokens.length ; j++ ) {
277
292
address token = tokens[j];
278
- require (! isDividendWithdrawed (msg .sender , cycleIndex, token), "Already claimed " );
293
+ bytes32 key = keccak256 (abi.encodePacked (msg .sender , cycleIndex, token));
294
+ require (! withdrawDividendState[key], "Already claimed " );
279
295
280
296
CycleInfo storage cycle = cycles[cycleIndex];
281
297
@@ -287,6 +303,9 @@ contract DividendContract is ReentrancyGuard {
287
303
// 所以需要进入下一个周期才会生效,所以这里使用前一个周期的数据
288
304
uint256 userStaked = _getStakeAmount (msg .sender , cycleIndex - 1 );
289
305
console.log ("userStaked %d, cycle %d " , userStaked, cycleIndex);
306
+ if (userStaked == 0 ) {
307
+ continue ;
308
+ }
290
309
291
310
// find the token reward of the cycle
292
311
uint256 rewardAmount = 0 ;
@@ -305,14 +324,14 @@ contract DividendContract is ReentrancyGuard {
305
324
}
306
325
307
326
// set the withdraw state of the user and the cycle and the token
308
- bytes32 key = keccak256 (abi.encodePacked (msg .sender , cycleIndex, token));
309
327
withdrawDividendState[key] = true ;
310
328
}
311
329
}
312
330
313
331
// do the transfer
314
332
for (uint i = 0 ; i < realRewardLength; i++ ) {
315
333
RewardInfo memory reward = rewards[i];
334
+ console.log ("will withdraw transfer %s %s ===> %d " , reward.token, msg .sender , reward.amount);
316
335
if (reward.token == address (0 )) {
317
336
payable (msg .sender ).transfer (reward.amount);
318
337
} else {
0 commit comments