Skip to content

Commit

Permalink
Introduce script params
Browse files Browse the repository at this point in the history
  • Loading branch information
DrZoltanFazekas committed Oct 14, 2024
1 parent 9b603ec commit 02db1dc
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 271 deletions.
46 changes: 14 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ forge install OpenZeppelin/openzeppelin-contracts --no-commit
```

## Contract Deployment
The delegation contract is used by delegators to stake and unstake ZIL with the respective validator. It acts as the validator node's control address and interacts with the `Deposit` system contract. `DelegationV1` is the initial implementation of the delegation contract is upgradeable: `DelegationV2` deploys a `NonRebasingLST` contract when it is initialized and `DelegationV3` adds the newest features.
The delegation contract is used by delegators to stake and unstake ZIL with the respective validator. It acts as the validator node's control address and interacts with the `Deposit` system contract. `Delegation` is the initial implementation of the delegation contract that creates a `NonRebasingLST` contract when it is initialized. `DelegationV2` implements staking, unstaking and other features.

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.

To deploy `DelegationV1` run
To deploy `Delegation` run
```bash
forge script script/deploy_Delegation.s.sol --rpc-url https://api.zq2-devnet.zilliqa.com --broadcast --legacy
forge script script/deploy_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy
```
You will see an output like this:
```
Expand All @@ -30,7 +30,7 @@ You will need the proxy address from the above output in all commands below.

To upgrade the contract to `DelegationV2`, run
```bash
forge script script/upgrade_Delegation.s.sol --rpc-url https://api.zq2-devnet.zilliqa.com --broadcast --legacy --sig "run(address payable)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2
forge script script/upgrade_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2
```

The output will look like this:
Expand All @@ -42,34 +42,16 @@ The output will look like this:
Upgraded to version: 2
```

To upgrade the contract to `DelegationV3`, replace line 33 in `upgrade_Delegation.s.sol` with
```solidity
new DelegationV3()
```
and run once again
```bash
forge script script/upgrade_Delegation.s.sol --rpc-url https://api.zq2-devnet.zilliqa.com --broadcast --legacy --sig "run(address payable)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2
```

The output will look like this:
```
Signer is 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77
Upgrading from version: 2
Owner is 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77
New implementation deployed: 0x90A65311b6C7246FFD1F212C123cfE351a6d65A9
Upgraded to version: 3
```

## 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:
```bash
forge script script/commission_Delegation.s.sol --rpc-url https://api.zq2-devnet.zilliqa.com --broadcast --legacy --sig "run(address payable)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2
forge script script/commission_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, uint16, address)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 1000 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77
```

The output will contain the following information:
```
Running version: 3
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
Expand All @@ -78,7 +60,7 @@ The output will contain the following information:
## Validator Activation
Now you are ready to use the contract to activate your node as a validator with a deposit of e.g. 10 million ZIL. Run
```bash
cast send --legacy --value 10000000ether --rpc-url https://api.zq2-devnet.zilliqa.com --private-key $PRIVATE_KEY \
cast send --legacy --value 10000000ether --rpc-url http://localhost:4201 --private-key $PRIVATE_KEY \
0x7a0b7e6d24ede78260c9ddbd98e828b0e11a8ea2 "deposit(bytes,bytes,bytes)" \
0x92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c \
0x002408011220d5ed74b09dcbe84d3b32a56c01ab721cf82809848b6604535212a219d35c412f \
Expand All @@ -93,13 +75,13 @@ Note that the reward address registered for your validator node will be the addr
## Staking and Unstaking
If the above transaction was successful and the node became a validator, it can accept delegations. In order to stake e.g. 200 ZIL, run
```bash
forge script script/stake_Delegation.s.sol --rpc-url https://api.zq2-devnet.zilliqa.com --broadcast --legacy --sig "run(address payable)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 --private-key 0x...
forge script script/stake_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, uint256)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 200000000000000000000 --private-key 0x...
```
with the private key of the delegator account. Make sure the account's balance can cover the transaction fees plus the 200 ZIL to be delegated.

The output will look like this:
```
Running version: 3
Running version: 2
Current stake: 10000000000000000000000000
Current rewards: 110314207650273223687
LST address: 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83
Expand All @@ -110,25 +92,25 @@ The output will look like this:

Note that the staker LST balance in the output will be different from the actual LST balance which you can query by running
```bash
cast call 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83 "balanceOf(address)(uint256)" 0xd819fFcE7A58b1E835c25617Db7b46a00888B013 --rpc-url https://api.zq2-devnet.zilliqa.com
cast call 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83 "balanceOf(address)(uint256)" 0xd819fFcE7A58b1E835c25617Db7b46a00888B013 --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g'
```
This is due to the fact that the above output was generated based on the local script execution before the transaction got submitted to the network.

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, run
Last but not least, to unstake e.g. 100 LST, run
```bash
forge script script/unstake_Delegation.s.sol --rpc-url https://api.zq2-devnet.zilliqa.com --broadcast --legacy --sig "run(address payable)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 --private-key 0x...
forge script script/unstake_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, uint256)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 100000000000000000000 --private-key 0x...
```
with the private key of an account that holds some LST.

The output will look like this:
```
Running version: 3
Running version: 2
Current stake: 10000000000000000000000000
Current rewards: 331912568306010928520
LST address: 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83
Owner balance: 10000000000000000000000000
Staker balance: 199993784619390291653
Staker balance: 0
Staker balance: 99993784619390291653
```
10 changes: 5 additions & 5 deletions script/commission_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ pragma solidity ^0.8.26;

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

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

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

DelegationV3 delegation = DelegationV3(
DelegationV2 delegation = DelegationV2(
proxy
);

Expand All @@ -33,8 +33,8 @@ contract Stake is Script {

vm.startBroadcast(deployerPrivateKey);

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

vm.stopBroadcast();

Expand Down
4 changes: 0 additions & 4 deletions script/deploy_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ contract Deploy is Script {
proxy
);

delegation.stake();
delegation.unstake();
delegation.claim();

console.log("Deployed version: %s",
delegation.version()
);
Expand Down
4 changes: 2 additions & 2 deletions script/deposit_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.26;

import {Script} from "forge-std/Script.sol";
import {NonRebasingLST} from "src/NonRebasingLST.sol";
import {DelegationV3} from "src/DelegationV3.sol";
import {DelegationV2} from "src/DelegationV2.sol";
import "forge-std/console.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

Expand All @@ -15,7 +15,7 @@ contract Deposit is Script {

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

DelegationV3 delegation = DelegationV3(
DelegationV2 delegation = DelegationV2(
proxy
);
/*
Expand Down
8 changes: 4 additions & 4 deletions script/stake_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pragma solidity ^0.8.26;

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

contract Stake is Script {
function run(address payable proxy) external {
function run(address payable proxy, uint256 amount) external {

uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
address owner = vm.addr(deployerPrivateKey);
Expand All @@ -17,7 +17,7 @@ contract Stake is Script {
address staker = msg.sender;
//address payable proxy = payable(0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2);

DelegationV3 delegation = DelegationV3(
DelegationV2 delegation = DelegationV2(
proxy
);

Expand Down Expand Up @@ -47,7 +47,7 @@ contract Stake is Script {
vm.broadcast();

delegation.stake{
value: 200 ether
value: amount
}();

console.log("Staker balance: %s",
Expand Down
11 changes: 5 additions & 6 deletions script/unstake_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pragma solidity ^0.8.26;

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

contract Unstake is Script {
function run(address payable proxy) external {
function run(address payable proxy, uint256 amount) external {

uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
address owner = vm.addr(deployerPrivateKey);
Expand All @@ -17,7 +17,7 @@ contract Unstake is Script {
address staker = msg.sender;
//address payable proxy = payable(0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2);

DelegationV3 delegation = DelegationV3(
DelegationV2 delegation = DelegationV2(
proxy
);

Expand All @@ -39,16 +39,15 @@ contract Unstake is Script {
lst.balanceOf(owner)
);

uint256 stakerBalance = lst.balanceOf(staker);
console.log("Staker balance: %s",
stakerBalance
lst.balanceOf(staker)
);

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

delegation.unstake(
stakerBalance
amount
);

console.log("Staker balance: %s",
Expand Down
1 change: 0 additions & 1 deletion script/upgrade_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.26;
import {Script} from "forge-std/Script.sol";
import {Delegation} from "src/Delegation.sol";
import {DelegationV2} from "src/DelegationV2.sol";
import {DelegationV3} from "src/DelegationV3.sol";
import "forge-std/console.sol";

contract Upgrade is Script {
Expand Down
25 changes: 7 additions & 18 deletions src/Delegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import "@openzeppelin/contracts-upgradeable/utils/PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
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
struct Storage {
bytes blsPubKey;
bytes peerId;
address lst;
}

// keccak256(abi.encode(uint256(keccak256("zilliqa.storage.Delegation")) - 1)) & ~bytes32(uint256(0xff))
Expand Down Expand Up @@ -40,29 +40,18 @@ contract Delegation is Initializable, PausableUpgradeable, Ownable2StepUpgradeab
__Ownable_init(initialOwner);
__Ownable2Step_init();
__UUPSUpgradeable_init();
Storage storage $ = _getStorage();
$.lst = address(new NonRebasingLST(address(this)));
}

function _authorizeUpgrade(address newImplementation) internal onlyOwner override {}

// this is to receive rewards
receive() payable external {
}

// called by the node's account that deployed this contract and is its owner
function deposit(
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public payable onlyOwner {
function getLST() public view returns(address) {
Storage storage $ = _getStorage();
$.blsPubKey = blsPubKey;
$.peerId = peerId;
}

function stake() public payable {}

function unstake() public {}

function claim() public{}
return $.lst;
}

}
Loading

0 comments on commit 02db1dc

Please sign in to comment.