From 0d3af249ee5b2c4f8b4874bbb7ee48ad625e8051 Mon Sep 17 00:00:00 2001 From: DrZoltanFazekas Date: Wed, 4 Jun 2025 08:40:28 +0200 Subject: [PATCH] Allow unstaking everything even if less than 100 ZIL --- src/BaseDelegation.sol | 2 +- src/NonLiquidDelegation.sol | 11 +++++++---- test/NonLiquidDelegation.t.sol | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/BaseDelegation.sol b/src/BaseDelegation.sol index cdca13e..4ece2a0 100644 --- a/src/BaseDelegation.sol +++ b/src/BaseDelegation.sol @@ -244,7 +244,7 @@ abstract contract BaseDelegation is IDelegation, PausableUpgradeable, Ownable2St // ************************************************************************ /// @dev The current version of all upgradeable contracts in the repository. - uint64 internal immutable VERSION = encodeVersion(1, 1, 0); + uint64 internal immutable VERSION = encodeVersion(1, 1, 1); /** * @dev Return the contracts' version. diff --git a/src/NonLiquidDelegation.sol b/src/NonLiquidDelegation.sol index 88799a8..3d714d3 100644 --- a/src/NonLiquidDelegation.sol +++ b/src/NonLiquidDelegation.sol @@ -365,16 +365,19 @@ contract NonLiquidDelegation is IDelegation, BaseDelegation { * caller's stake if the `value` to be unstaked is greater than the current stake. */ function _appendToHistory(int256 value, address staker) internal { - if (value >= 0) - require(uint256(value) >= MIN_DELEGATION, AmountTooLow(uint256(value))); - else - require(uint256(-value) >= MIN_DELEGATION, AmountTooLow(uint256(-value))); NonLiquidDelegationStorage storage $ = _getNonLiquidDelegationStorage(); int256 amount = value; if ($.stakingIndices[staker].length > 0) amount += int256($.stakings[ $.stakingIndices[staker][$.stakingIndices[staker].length - 1] ].amount); + if (value >= 0) + require(uint256(value) >= MIN_DELEGATION, AmountTooLow(uint256(value))); + else + require( + uint256(-value) >= MIN_DELEGATION || amount == 0, + AmountTooLow(uint256(-value)) + ); if (value < 0) require( amount >= 0, diff --git a/test/NonLiquidDelegation.t.sol b/test/NonLiquidDelegation.t.sol index 8ba7fe8..b5698c4 100644 --- a/test/NonLiquidDelegation.t.sol +++ b/test/NonLiquidDelegation.t.sol @@ -2304,8 +2304,8 @@ contract NonLiquidDelegationTest is BaseDelegationTest { continue; Console.log("block %s avg rewards %s", block.number, rewards / blocks); uint256 amount = - operation % 2 == 0 && stakedZil[user] > 2 * delegation.MIN_DELEGATION() ? - vm.randomUint(delegation.MIN_DELEGATION(), stakedZil[user] / 2): + operation % 2 == 0 && stakedZil[user] >= delegation.MIN_DELEGATION() ? + vm.randomUint(delegation.MIN_DELEGATION(), stakedZil[user]): stakedZil[user]; uint256 pendingBefore = delegation.totalPendingWithdrawals(); uint256 totalStakeValue = delegation.getDelegatedTotal();