Skip to content

Commit

Permalink
Fix issue 9 and other minor improvements (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrZoltanFazekas authored Nov 20, 2024
1 parent a3984cf commit e925c76
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 360 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The delegation contract is used by delegators to stake and unstake ZIL with the

`LiquidDelegation` is the initial implementation of the liquid staking variant of the delegation contract that creates a `NonRebasingLST` contract when it is initialized. `LiquidDelegationV2` implements all other features. `NonLiquidDelegation` is the initial implementation of the non-liquid staking variant of the delegation contract that allows delegators to withdraw rewards.

The delegation contract shall be deployed and upgraded by the account with the private key that was used to run the validator node and was used to generate its BLS keypair and peer id. Make sure the `PRIVATE_KEY` environment variable is set accordingly.
Before running the deployment script, set the `PRIVATE_KEY` environment variable to the private key of the contract owner. Note that only the contract owner will be able to upgrade the contract, change the commission and activate the node as a validator.

To deploy `LiquidDelegation` run
```bash
Expand Down Expand Up @@ -60,7 +60,7 @@ The output will look like this:

Now or at a later time you can set the commission on the rewards the validator earns to e.g. 10% as follows:
```bash
forge script script/commission_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, uint16, bool)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 1000 false
forge script script/commission_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, string, bool)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 1000 false
```

The output will contain the following information:
Expand All @@ -77,7 +77,7 @@ cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "DENOMINATOR()(uint256)" --

Once the validator is activated and starts earning rewards, commissions are transferred automatically to the validator node's account. Commissions of a non-liquid staking validator are deducted when delegators withdraw rewards. In case of the liquid staking variant, commissions are deducted each time delegators stake, unstake or claim what they unstaked, or when the node requests the outstanding commissions that haven't been transferred yet. To collect them, run
```bash
forge script script/commission_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, string, bool)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 10001 true
forge script script/commission_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, string, bool)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 same true
```
using `same` for the second argument to leave the commission percentage unchanged and `true` for the third argument. Replacing the second argument with `same` and the third argument with `false` only displays the current commission rate.

Expand Down
2 changes: 2 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ src = "src"
out = "out"
libs = ["lib"]
evm_version = 'shanghai'
via_ir = true
gas_limit = 10000000000

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
16 changes: 11 additions & 5 deletions src/BaseDelegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import "src/Delegation.sol";

library WithdrawalQueue {

//TODO: value set for testing purposes, make it equal to 2 * 7 * 24 * 60 * 60
// if governance changes the unbonding period, update the value and upgrade the contract
uint256 public constant UNBONDING_PERIOD = 30;
address public constant DEPOSIT_CONTRACT = 0x000000000000000000005a494C4445504F534954;

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

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

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

Expand Down Expand Up @@ -216,7 +222,7 @@ abstract contract BaseDelegation is Delegation, PausableUpgradeable, Ownable2Ste
if (!_isActivated())
return address(this).balance;
(bool success, bytes memory data) = DEPOSIT_CONTRACT.staticcall(
abi.encodeWithSignature("getStake(bytes)", $.blsPubKey)
abi.encodeWithSignature("getFutureStake(bytes)", $.blsPubKey)
);
require(success, "could not retrieve staked amount");
return abi.decode(data, (uint256));
Expand Down
Loading

0 comments on commit e925c76

Please sign in to comment.