Skip to content

Commit 2e71da9

Browse files
committed
feat: emit event LeftoverPNK when PNK becomes available for withdrawal during unlock
1 parent d50b8a4 commit 2e71da9

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

contracts/src/arbitration/SortitionModuleBase.sol

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
9393
/// @param _unlock Whether the stake is locked or unlocked.
9494
event StakeLocked(address indexed _address, uint256 _relativeAmount, bool _unlock);
9595

96+
/// @dev Emitted when leftover PNK is available.
97+
/// @param _account The account of the juror.
98+
/// @param _amount The amount of PNK available.
99+
event LeftoverPNK(address indexed _account, uint256 _amount);
100+
96101
/// @dev Emitted when leftover PNK is withdrawn.
97102
/// @param _account The account of the juror withdrawing PNK.
98103
/// @param _amount The amount of PNK withdrawn.
@@ -370,8 +375,14 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
370375
}
371376

372377
function unlockStake(address _account, uint256 _relativeAmount) external override onlyByCore {
373-
jurors[_account].lockedPnk -= _relativeAmount;
378+
Juror storage juror = jurors[_account];
379+
juror.lockedPnk -= _relativeAmount;
374380
emit StakeLocked(_account, _relativeAmount, true);
381+
382+
uint256 amount = getJurorLeftoverPNK(_account);
383+
if (amount > 0) {
384+
emit LeftoverPNK(_account, amount);
385+
}
375386
}
376387

377388
function penalizeStake(
@@ -414,17 +425,13 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
414425
/// Also note that if the juror has some leftover PNK while not fully unstaked he'll have to manually unstake from all courts to trigger this function.
415426
/// @param _account The juror whose PNK to withdraw.
416427
function withdrawLeftoverPNK(address _account) external override {
417-
Juror storage juror = jurors[_account];
418428
// Can withdraw the leftover PNK if fully unstaked, has no tokens locked and has positive balance.
419429
// This withdrawal can't be triggered by calling setStake() in KlerosCore because current stake is technically 0, thus it is done via separate function.
420-
if (juror.stakedPnk > 0 && juror.courtIDs.length == 0 && juror.lockedPnk == 0) {
421-
uint256 amount = juror.stakedPnk;
422-
juror.stakedPnk = 0;
423-
core.transferBySortitionModule(_account, amount);
424-
emit LeftoverPNKWithdrawn(_account, amount);
425-
} else {
426-
revert("Not eligible for withdrawal.");
427-
}
430+
uint256 amount = getJurorLeftoverPNK(_account);
431+
require(amount > 0, "Not eligible for withdrawal.");
432+
jurors[_account].stakedPnk = 0;
433+
core.transferBySortitionModule(_account, amount);
434+
emit LeftoverPNKWithdrawn(_account, amount);
428435
}
429436

430437
// ************************************* //
@@ -524,6 +531,15 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
524531
return jurors[_juror].stakedPnk > 0;
525532
}
526533

534+
function getJurorLeftoverPNK(address _juror) public view override returns (uint256) {
535+
Juror storage juror = jurors[_juror];
536+
if (juror.courtIDs.length == 0 && juror.lockedPnk == 0) {
537+
return juror.stakedPnk;
538+
} else {
539+
return 0;
540+
}
541+
}
542+
527543
// ************************************* //
528544
// * Internal * //
529545
// ************************************* //

contracts/src/arbitration/interfaces/ISortitionModule.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ interface ISortitionModule {
5252

5353
function isJurorStaked(address _juror) external view returns (bool);
5454

55+
function getJurorLeftoverPNK(address _juror) external view returns (uint256);
56+
5557
function createDisputeHook(uint256 _disputeID, uint256 _roundID) external;
5658

5759
function postDrawHook(uint256 _disputeID, uint256 _roundID) external;

contracts/src/arbitration/university/SortitionModuleUniversity.sol

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ contract SortitionModuleUniversity is ISortitionModuleUniversity, UUPSProxiable,
6060
/// @param _unlock Whether the stake is locked or unlocked.
6161
event StakeLocked(address indexed _address, uint256 _relativeAmount, bool _unlock);
6262

63+
/// @dev Emitted when leftover PNK is available.
64+
/// @param _account The account of the juror.
65+
/// @param _amount The amount of PNK available.
66+
event LeftoverPNK(address indexed _account, uint256 _amount);
67+
6368
/// @dev Emitted when leftover PNK is withdrawn.
6469
/// @param _account The account of the juror withdrawing PNK.
6570
/// @param _amount The amount of PNK withdrawn.
@@ -280,17 +285,13 @@ contract SortitionModuleUniversity is ISortitionModuleUniversity, UUPSProxiable,
280285
/// Also note that if the juror has some leftover PNK while not fully unstaked he'll have to manually unstake from all courts to trigger this function.
281286
/// @param _account The juror whose PNK to withdraw.
282287
function withdrawLeftoverPNK(address _account) external override {
283-
Juror storage juror = jurors[_account];
284288
// Can withdraw the leftover PNK if fully unstaked, has no tokens locked and has positive balance.
285289
// This withdrawal can't be triggered by calling setStake() in KlerosCore because current stake is technically 0, thus it is done via separate function.
286-
if (juror.stakedPnk > 0 && juror.courtIDs.length == 0 && juror.lockedPnk == 0) {
287-
uint256 amount = juror.stakedPnk;
288-
juror.stakedPnk = 0;
289-
core.transferBySortitionModule(_account, amount);
290-
emit LeftoverPNKWithdrawn(_account, amount);
291-
} else {
292-
revert("Not eligible for withdrawal.");
293-
}
290+
uint256 amount = getJurorLeftoverPNK(_account);
291+
require(amount > 0, "Not eligible for withdrawal.");
292+
jurors[_account].stakedPnk = 0;
293+
core.transferBySortitionModule(_account, amount);
294+
emit LeftoverPNKWithdrawn(_account, amount);
294295
}
295296

296297
// ************************************* //
@@ -344,6 +345,14 @@ contract SortitionModuleUniversity is ISortitionModuleUniversity, UUPSProxiable,
344345
return jurors[_juror].stakedPnk > 0;
345346
}
346347

348+
function getJurorLeftoverPNK(address _juror) public view override returns (uint256) {
349+
Juror storage juror = jurors[_juror];
350+
if (juror.courtIDs.length == 0 && juror.lockedPnk == 0) {
351+
return juror.stakedPnk;
352+
}
353+
return 0;
354+
}
355+
347356
// ************************************* //
348357
// * Internal * //
349358
// ************************************* //

0 commit comments

Comments
 (0)