Skip to content

Commit

Permalink
Instant unstaking before pool activation (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrZoltanFazekas authored Feb 13, 2025
1 parent b33233d commit f0e4b9e
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 102 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ You will see an output like this:
Proxy deployed: 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2
Implementation deployed: 0x7C623e01c5ce2e313C223ef2aEc1Ae5C6d12D9DD
Owner is 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77
Upgraded to version: 0.3.5
Upgraded to version: 0.3.6
```

You will need the proxy address from the above output in all commands below. If you know the address of a proxy contract but don't know which variant of staking it supports, run
Expand All @@ -66,10 +66,10 @@ forge script script/Upgrade.s.sol --broadcast --legacy --sig "run(address payabl
The output will look like this:
```
Signer is 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77
Upgrading from version: 0.3.4
Upgrading from version: 0.3.5
Owner is 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77
New implementation deployed: 0x64Fa96a67910956141cc481a43f242C045c10165
Upgraded to version: 0.3.5
Upgraded to version: 0.3.6
```

If you want to check the current version your contract was upgraded to, run
Expand All @@ -89,7 +89,7 @@ forge script script/Configure.s.sol --broadcast --legacy --sig "commissionRate(a

The output will contain the following information:
```
Running version: 0.3.5
Running version: 0.3.6
Commission rate: 0.0%
New commission rate: 10.0%
```
Expand All @@ -116,7 +116,7 @@ forge script script/Configure.s.sol --broadcast --legacy --sig "commissionReceiv

The output will contain the following information:
```
Running version: 0.3.5
Running version: 0.3.6
Commission receiver: 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77
New commission receiver: 0xeA78aAE5Be606D2D152F00760662ac321aB8F017
```
Expand Down Expand Up @@ -207,7 +207,7 @@ with the private key of delegator account. It's important to make sure the accou

The output will look like this for liquid staking:
```
Running version: 0.3.5
Running version: 0.3.6
Current stake: 10000000000000000000000000 wei
Current rewards: 110314207650273223687 wei
LST address: 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83
Expand All @@ -216,7 +216,7 @@ The output will look like this for liquid staking:
```
and like this for the non-liquid variant:
```
Running version: 0.3.5
Running version: 0.3.6
Current stake: 10000000000000000000000000 wei
Current rewards: 110314207650273223687 wei
Staker balance before: 99899145245801454561224 wei
Expand All @@ -243,7 +243,7 @@ using the private key of an account that holds some LST in case of the liquid va

The output will look like this for liquid staking:
```
Running version: 0.3.5
Running version: 0.3.6
Current stake: 10000000000000000000000000 wei
Current rewards: 331912568306010928520 wei
LST address: 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83
Expand All @@ -252,7 +252,7 @@ The output will look like this for liquid staking:
```
and like this for the non-liquid variant:
```
Running version: 0.3.5
Running version: 0.3.6
Current stake: 10000000000000000000000000 wei
Current rewards: 331912568306010928520 wei
Staker balance before: 99698814298179759361224 wei
Expand All @@ -267,7 +267,7 @@ with the private key of the account that unstaked in the previous step.

The output will look like this:
```
Running version: 0.3.5
Running version: 0.3.6
Staker balance before: 99698086421983460161224 wei
Staker balance after: 99798095485861371162343 wei
```
Expand Down Expand Up @@ -387,6 +387,7 @@ function stake() external payable;
function unstake(uint256) external returns(uint256 unstakedZil);
function claim() external;
function unbondingPeriod() external virtual view returns(uint256 numberOfBlocks);
function getClaimable() external virtual view returns(uint256 total);
function getPendingClaims() external virtual view returns(uint256[2][] memory blockNumbersAndAmounts);
function getMinDelegation() external view returns(uint256 amount);
Expand Down
16 changes: 13 additions & 3 deletions src/BaseDelegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract contract BaseDelegation is IDelegation, PausableUpgradeable, Ownable2St
}

uint256 public constant MIN_DELEGATION = 10 ether;
address public constant DEPOSIT_CONTRACT = WithdrawalQueue.DEPOSIT_CONTRACT;
address public constant DEPOSIT_CONTRACT = address(0x5A494C4445504F53495450524F5859);
uint256 public constant DENOMINATOR = 10_000;

event ValidatorJoined(bytes indexed blsPubKey);
Expand All @@ -57,7 +57,7 @@ abstract contract BaseDelegation is IDelegation, PausableUpgradeable, Ownable2St
// contract file names remain the same across all versions
// so that the upgrade script does not need to be modified
// to import the new version each time there is one
uint64 internal immutable VERSION = encodeVersion(0, 3, 5);
uint64 internal immutable VERSION = encodeVersion(0, 3, 6);

function version() public view returns(uint64) {
return _getInitializedVersion();
Expand Down Expand Up @@ -501,10 +501,20 @@ abstract contract BaseDelegation is IDelegation, PausableUpgradeable, Ownable2St

function _enqueueWithdrawal(uint256 amount) internal virtual {
BaseDelegationStorage storage $ = _getBaseDelegationStorage();
$.withdrawals[_msgSender()].enqueue(amount);
$.withdrawals[_msgSender()].enqueue(amount, unbondingPeriod());
$.totalWithdrawals += amount;
}

function unbondingPeriod() public view returns(uint256) {
if (!_isActivated())
return 0;
(bool success, bytes memory data) = DEPOSIT_CONTRACT.staticcall(
abi.encodeWithSignature("withdrawalPeriod()")
);
require(success, "unbonding period unknown");
return abi.decode(data, (uint256));
}

function getTotalWithdrawals() public virtual view returns(uint256) {
BaseDelegationStorage storage $ = _getBaseDelegationStorage();
return $.totalWithdrawals;
Expand Down
14 changes: 2 additions & 12 deletions src/WithdrawalQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ pragma solidity ^0.8.26;

library WithdrawalQueue {

address public constant DEPOSIT_CONTRACT = address(0x5A494C4445504F53495450524F5859);

struct Item {
uint256 blockNumber;
uint256 amount;
Expand All @@ -16,16 +14,8 @@ library WithdrawalQueue {
mapping(uint256 => Item) items;
}

function unbondingPeriod() internal view returns(uint256) {
(bool success, bytes memory data) = DEPOSIT_CONTRACT.staticcall(
abi.encodeWithSignature("withdrawalPeriod()")
);
require(success, "unbonding period unknown");
return abi.decode(data, (uint256));
}

function enqueue(Fifo storage fifo, uint256 amount) internal {
fifo.items[fifo.last] = Item(block.number + unbondingPeriod(), amount);
function enqueue(Fifo storage fifo, uint256 amount, uint256 period) internal {
fifo.items[fifo.last] = Item(block.number + period, amount);
fifo.last++;
}

Expand Down
2 changes: 1 addition & 1 deletion test/BaseDelegation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ abstract contract BaseDelegationTest is Test {
}
assertEq(delegation.getClaimable() + totalPending, totalUnstaked, "claims must match unstaked amount");

vm.roll(block.number + WithdrawalQueue.unbondingPeriod());
vm.roll(block.number + delegation.unbondingPeriod());

console.log("--------------------------------------------------------------------");
console.log("block number: %s", block.number);
Expand Down
Loading

0 comments on commit f0e4b9e

Please sign in to comment.