Skip to content

Commit

Permalink
Implement claiming after unbonding period
Browse files Browse the repository at this point in the history
  • Loading branch information
DrZoltanFazekas committed Oct 16, 2024
1 parent 02db1dc commit 680aa2f
Show file tree
Hide file tree
Showing 12 changed files with 1,004 additions and 189 deletions.
48 changes: 33 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,22 @@ The output will look like this:

## Contract Configuration

Now or at a later time you can set the commission on the rewards the validator earns to e.g. 10% and the wallet address the commission will be sent to e.g. the validator node's address:
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, address)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 1000 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77
forge script script/commission_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, uint16)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 1000
```

The output will contain the following information:
```
Running version: 2
LST address: 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83
Current commission rate and commission address is: 0.0% 0x0000000000000000000000000000000000000000
New commission rate and commission address is: 10.0% 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77
Old commission rate: 0.0%
New commission rate: 10.0%
```

Note that the commission rate is specified as an integer to be devided by the `DENOMINATOR` which can be retrieved from the delegation contract:
```bash
cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "DENOMINATOR()(uint256)" --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g'
```

## Validator Activation
Expand Down Expand Up @@ -82,12 +87,12 @@ with the private key of the delegator account. Make sure the account's balance c
The output will look like this:
```
Running version: 2
Current stake: 10000000000000000000000000
Current rewards: 110314207650273223687
Current stake: 10000000000000000000000000 ZIL
Current rewards: 110314207650273223687 ZIL
LST address: 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83
Owner balance: 10000000000000000000000000
Staker balance: 0
Staker balance: 199993793908430833324
Owner balance: 10000000000000000000000000 LST
Staker balance before: 99899145245801454561224 ZIL 0 LST
Staker balance after: 99699145245801454561224 ZIL 199993793908430833324 LST
```

Note that the staker LST balance in the output will be different from the actual LST balance which you can query by running
Expand All @@ -98,7 +103,7 @@ This is due to the fact that the above output was generated based on the local s

You can copy the LST address from the above output and add it to your wallet to transfer your liquid staking tokens to another account if you want to.

Last but not least, to unstake e.g. 100 LST, run
To unstake e.g. 100 LST, run
```bash
forge script script/unstake_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, uint256)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 100000000000000000000 --private-key 0x...
```
Expand All @@ -107,10 +112,23 @@ with the private key of an account that holds some LST.
The output will look like this:
```
Running version: 2
Current stake: 10000000000000000000000000
Current rewards: 331912568306010928520
Current stake: 10000000000000000000000000 ZIL
Current rewards: 331912568306010928520 ZIL
LST address: 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83
Owner balance: 10000000000000000000000000
Staker balance: 199993784619390291653
Staker balance: 99993784619390291653
Owner balance: 10000000000000000000000000 LST
Staker balance before: 99698814298179759361224 ZIL 199993784619390291653 LST
Staker balance after: 99698814298179759361224 ZIL 99993784619390291653 LST
```

Last but not least, to claim the amount that is available after the unbonding period, run
```bash
forge script script/claim_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 --private-key 0x...
```
with the private key of an account that unstaked some LST.

The output will look like this:
```
Running version: 2
Staker balance before: 99698086421983460161224 ZIL
Staker balance after: 99798095485861371162343 ZIL
```
33 changes: 33 additions & 0 deletions script/claim_Delegation.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.26;

import {Script} from "forge-std/Script.sol";
import {DelegationV2} from "src/DelegationV2.sol";
import "forge-std/console.sol";

contract Claim is Script {
function run(address payable proxy) external {

address staker = msg.sender;

DelegationV2 delegation = DelegationV2(
proxy
);

console.log("Running version: %s",
delegation.version()
);

console.log("Staker balance before: %s ZIL",
staker.balance
);

vm.broadcast();

delegation.claim();

console.log("Staker balance after: %s ZIL",
staker.balance
);
}
}
20 changes: 8 additions & 12 deletions script/commission_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {DelegationV2} from "src/DelegationV2.sol";
import "forge-std/console.sol";

contract Stake is Script {
function run(address payable proxy, uint16 commissionNumerator, address commissionAddress) external {
function run(address payable proxy, uint16 commissionNumerator) external {

uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
address owner = vm.addr(deployerPrivateKey);

DelegationV2 delegation = DelegationV2(
proxy
Expand All @@ -25,23 +24,20 @@ contract Stake is Script {
address(lst)
);

console.log("Current commission rate and commission address: %s.%s%% %s",
console.log("Old commission rate: %s.%s%%",
uint256(delegation.getCommissionNumerator()) * 100 / uint256(delegation.DENOMINATOR()),
uint256(delegation.getCommissionNumerator()) % (uint256(delegation.DENOMINATOR()) / 100),
delegation.getCommissionAddress()
//TODO: check if the decimals are printed correctly e.g. 12.01% vs 12.1%
uint256(delegation.getCommissionNumerator()) % (uint256(delegation.DENOMINATOR()) / 100)
);

vm.startBroadcast(deployerPrivateKey);
vm.broadcast(deployerPrivateKey);

delegation.setCommissionNumerator(commissionNumerator);
delegation.setCommissionAddress(commissionAddress);

vm.stopBroadcast();

console.log("New commission rate and commission address: %s.%s%% %s",
console.log("New commission rate: %s.%s%%",
uint256(delegation.getCommissionNumerator()) * 100 / uint256(delegation.DENOMINATOR()),
uint256(delegation.getCommissionNumerator()) % (uint256(delegation.DENOMINATOR()) / 100),
delegation.getCommissionAddress()
//TODO: check if the decimals are printed correctly e.g. 12.01% vs 12.1%
uint256(delegation.getCommissionNumerator()) % (uint256(delegation.DENOMINATOR()) / 100)
);
}
}
2 changes: 0 additions & 2 deletions script/deploy_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ contract Deploy is Script {
vm.startBroadcast(deployerPrivateKey);

address implementation = address(
//new Delegation{salt: "zilliqa"}()
new Delegation()
);

Expand All @@ -25,7 +24,6 @@ contract Deploy is Script {
);

address payable proxy = payable(
//new ERC1967Proxy{salt: "zilliqa"}(implementation, initializerCall)
new ERC1967Proxy(implementation, initializerCall)
);

Expand Down
78 changes: 0 additions & 78 deletions script/deposit_Delegation.s.sol

This file was deleted.

15 changes: 6 additions & 9 deletions script/stake_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ contract Stake is Script {

uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
address owner = vm.addr(deployerPrivateKey);
//console.log("Owner is %s", owner);

//address staker = 0xd819fFcE7A58b1E835c25617Db7b46a00888B013;
address staker = msg.sender;
//address payable proxy = payable(0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2);

DelegationV2 delegation = DelegationV2(
proxy
Expand All @@ -25,7 +21,7 @@ contract Stake is Script {
delegation.version()
);

console.log("Current stake: %s \r\n Current rewards: %s",
console.log("Current stake: %s ZIL \r\n Current rewards: %s ZIL",
delegation.getStake(),
delegation.getRewards()
);
Expand All @@ -35,22 +31,23 @@ contract Stake is Script {
address(lst)
);

console.log("Owner balance: %s",
console.log("Owner balance: %s LST",
lst.balanceOf(owner)
);

console.log("Staker balance: %s",
console.log("Staker balance before: %s ZIL %s LST",
staker.balance,
lst.balanceOf(staker)
);

//vm.broadcast(staker);
vm.broadcast();

delegation.stake{
value: amount
}();

console.log("Staker balance: %s",
console.log("Staker balance after: %s ZIL %s LST",
staker.balance,
lst.balanceOf(staker)
);
}
Expand Down
19 changes: 10 additions & 9 deletions script/unstake_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ contract Unstake is Script {

uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
address owner = vm.addr(deployerPrivateKey);
//console.log("Owner is %s", owner);

//address staker = 0xd819fFcE7A58b1E835c25617Db7b46a00888B013;
address staker = msg.sender;
//address payable proxy = payable(0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2);

DelegationV2 delegation = DelegationV2(
proxy
Expand All @@ -25,7 +21,7 @@ contract Unstake is Script {
delegation.version()
);

console.log("Current stake: %s \r\n Current rewards: %s",
console.log("Current stake: %s ZIL \r\n Current rewards: %s ZIL",
delegation.getStake(),
delegation.getRewards()
);
Expand All @@ -35,22 +31,27 @@ contract Unstake is Script {
address(lst)
);

console.log("Owner balance: %s",
console.log("Owner LST balance: %s LST",
lst.balanceOf(owner)
);

console.log("Staker balance: %s",
console.log("Staker balance before: %s ZIL %s LST",
staker.balance,
lst.balanceOf(staker)
);

//vm.broadcast(staker);
if (amount == 0) {
amount = lst.balanceOf(staker);
}

vm.broadcast();

delegation.unstake(
amount
);

console.log("Staker balance: %s",
console.log("Staker balance after: %s ZIL %s LST",
staker.balance,
lst.balanceOf(staker)
);
}
Expand Down
2 changes: 0 additions & 2 deletions script/upgrade_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ contract Upgrade is Script {
address owner = vm.addr(deployerPrivateKey);
console.log("Signer is %s", owner);

//address payable proxy = payable(0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2);

Delegation oldDelegation = Delegation(
proxy
);
Expand Down
1 change: 0 additions & 1 deletion src/Delegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "src/NonRebasingLST.sol";

// the contract is supposed to be deployed with the node's signer account
contract Delegation is Initializable, PausableUpgradeable, Ownable2StepUpgradeable, UUPSUpgradeable {

/// @custom:storage-location erc7201:zilliqa.storage.Delegation
Expand Down
Loading

0 comments on commit 680aa2f

Please sign in to comment.