Skip to content

Commit

Permalink
feat: Custom errors and rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
86667 committed Dec 5, 2024
1 parent b154ed5 commit 07dcfae
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 141 deletions.
4 changes: 2 additions & 2 deletions .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"rules": {
"no-inline-assembly": "off",
"avoid-low-level-calls": "off",
"gas-custom-errors": "off",
"reason-string": ["warn",{"maxLength":70}],
"func-visibility": ["warn", { "ignoreConstructors": true }]
"func-visibility": ["warn", { "ignoreConstructors": true }],
"gas-custom-errors": "off"
}
}
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ The Zilliqa 2.0 deposit contract must be compiled for the tests included in this
@zilliqa/zq2/=/home/user/zq2/zilliqa/src/contracts/
```

For linting:
## Development

### Linting
To run linter:
```
npm install -g solhint
solhint 'src/**/*.sol'
```

### Tests
To run Solidity tests:
```sh
PRIVATE_KEY="0x$(openssl rand -hex 32)" forge test
```

## 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 contract.

Expand Down
2 changes: 1 addition & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/
@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/
@zilliqa/zq2/=/home/zoltan/Github/zq2/zilliqa/src/contracts/
@zilliqa/zq2/=/home/tw/zq2/zilliqa/src/contracts/
3 changes: 2 additions & 1 deletion src/BaseDelegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ abstract contract BaseDelegation is Delegation, PausableUpgradeable, Ownable2Ste
return _getInitializedVersion();
}

function __baseDelegationInit(address initialOwner) internal onlyInitializing {
// solhint-disable-next-line func-name-mixedcase
function __baseDelegation_init(address initialOwner) internal onlyInitializing {
__Pausable_init_unchained();
__Ownable2Step_init_unchained();
__Ownable_init_unchained(initialOwner);
Expand Down
3 changes: 2 additions & 1 deletion src/LiquidDelegation.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
/* solhint-disable no-unused-vars */
pragma solidity ^0.8.26;

import {BaseDelegation} from "src/BaseDelegation.sol";
Expand Down Expand Up @@ -34,7 +35,7 @@ contract LiquidDelegation is BaseDelegation, ILiquidDelegation {
}

function initialize(address initialOwner) public initializer {
__baseDelegationInit(initialOwner);
__baseDelegation_init(initialOwner);
LiquidDelegationStorage storage $ = _getLiquidDelegationStorage();
$.lst = address(new NonRebasingLST(address(this)));
}
Expand Down
3 changes: 2 additions & 1 deletion src/NonLiquidDelegation.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
/* solhint-disable no-unused-vars */
pragma solidity ^0.8.26;

import {BaseDelegation} from "src/BaseDelegation.sol";
Expand Down Expand Up @@ -36,7 +37,7 @@ contract NonLiquidDelegation is BaseDelegation, INonLiquidDelegation {
}

function initialize(address initialOwner) public initializer {
__baseDelegationInit(initialOwner);
__baseDelegation_init(initialOwner);
}

function deposit(
Expand Down
106 changes: 0 additions & 106 deletions src/utils/Deque.sol

This file was deleted.

9 changes: 6 additions & 3 deletions src/utils/WithdrawalQueue.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.26;

/// Interal WithdrawalQueue error
error WithdrawalQueueError(string msg);

library WithdrawalQueue {

address public constant DEPOSIT_CONTRACT = 0x000000000000000000005a494C4445504F534954;
address public constant DEPOSIT_CONTRACT = address(0x5A494C4445504F53495450524F5859);

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

function unbondingPeriod() internal view returns(uint256) {
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 {
function enqueue(Fifo storage fifo, uint256 amount) internal {
fifo.items[fifo.last] = Item(block.number + unbondingPeriod(), amount);
fifo.last++;
}
Expand Down
23 changes: 5 additions & 18 deletions test/LiquidDelegation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,12 @@ import {BaseDelegation, WithdrawalQueue} from "src/BaseDelegation.sol";
import {Delegation} from "src/Delegation.sol";
import {Deposit} from "@zilliqa/zq2/deposit_v2.sol";
import {Console} from "src/Console.sol";
<<<<<<< HEAD
import {Vm} from "forge-std/Test.sol";
import "forge-std/console.sol";

contract LiquidDelegationTest is BaseDelegationTest {
LiquidDelegationV2 delegation;
NonRebasingLST lst;
=======
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {Test, Vm} from "forge-std/Test.sol";
import {console} from "forge-std/console.sol";

contract LiquidDelegationTest is Test {
address payable private proxy;
LiquidDelegationV2 private delegation;
NonRebasingLST private lst;
address private owner;
address private staker = 0xd819fFcE7A58b1E835c25617Db7b46a00888B013;
>>>>>>> 0cacad0 (feat: apply basic linting rules to codebase)

constructor() BaseDelegationTest() {
oldImplementation = address(new LiquidDelegation());
Expand Down Expand Up @@ -133,7 +120,7 @@ contract LiquidDelegationTest is Test {
}();

// wait 2 epochs for the change to the deposit to take affect
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);

ownerZIL[1] = delegation.owner().balance;

Expand Down Expand Up @@ -221,7 +208,7 @@ contract LiquidDelegationTest is Test {
);

// wait 2 epochs for the change to the deposit to take affect
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);

stakerLST[1] = lst.balanceOf(stakers[0]);
ownerZIL[1] = delegation.owner().balance;
Expand Down Expand Up @@ -384,7 +371,7 @@ contract LiquidDelegationTest is Test {
console.log("validator stake: %s", Deposit(delegation.DEPOSIT_CONTRACT()).getStake(
bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c")
));
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);
console.log("validator stake: %s", Deposit(delegation.DEPOSIT_CONTRACT()).getStake(
bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c")
));
Expand All @@ -395,7 +382,7 @@ contract LiquidDelegationTest is Test {
console.log("validator stake: %s", Deposit(delegation.DEPOSIT_CONTRACT()).getStake(
bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c")
));
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);
console.log("validator stake: %s", Deposit(delegation.DEPOSIT_CONTRACT()).getStake(
bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c")
));
Expand All @@ -406,7 +393,7 @@ contract LiquidDelegationTest is Test {
console.log("validator stake: %s", Deposit(delegation.DEPOSIT_CONTRACT()).getStake(
bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c")
));
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);
console.log("validator stake: %s", Deposit(delegation.DEPOSIT_CONTRACT()).getStake(
bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c")
));
Expand Down
14 changes: 7 additions & 7 deletions test/NonLiquidDelegation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ contract NonLiquidDelegationTest is BaseDelegationTest {
}
vm.stopPrank();
// wait 2 epochs for the change to the deposit to take affect
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);
}

//no rewards if we withdraw in the same block as the last staking
Expand Down Expand Up @@ -181,7 +181,7 @@ contract NonLiquidDelegationTest is BaseDelegationTest {
}
vm.stopPrank();
// wait 2 epochs for the change to the deposit to take affect
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);
}

//further rewards accrued since the last staking
Expand Down Expand Up @@ -384,7 +384,7 @@ contract NonLiquidDelegationTest is BaseDelegationTest {
deposit(BaseDelegation(delegation), 10_000_000 ether, true);

// wait 2 epochs for the change to the deposit to take affect
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);

for (i = 0; i < 4; i++) {
vm.deal(stakers[i], 100_000 ether);
Expand Down Expand Up @@ -414,7 +414,7 @@ contract NonLiquidDelegationTest is BaseDelegationTest {
);
delegation.stake{value: x * 1 ether}();
// wait 2 epochs for the change to the deposit to take affect
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);
//snapshot("staker %s staked %s", i, x);
vm.stopPrank();
vm.deal(address(delegation), address(delegation).balance + 10_000 ether);
Expand All @@ -436,7 +436,7 @@ contract NonLiquidDelegationTest is BaseDelegationTest {
);
delegation.unstake(x * 1 ether);
// wait 2 epochs for the change to the deposit to take affect
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);
//snapshot("staker %s unstaked %s", i, x);
vm.stopPrank();
vm.deal(address(delegation), address(delegation).balance + 10_000 ether);
Expand Down Expand Up @@ -495,7 +495,7 @@ contract NonLiquidDelegationTest is BaseDelegationTest {
deposit(BaseDelegation(delegation), 10_000_000 ether, true);

// wait 2 epochs for the change to the deposit to take affect
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);

for (i = 0; i < 4; i++) {
vm.deal(stakers[i], 100_000 ether);
Expand Down Expand Up @@ -524,7 +524,7 @@ contract NonLiquidDelegationTest is BaseDelegationTest {
delegation.stake{value: x * 1 ether}();

// wait 2 epochs for the change to the deposit to take affect
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).BLOCKS_PER_EPOCH() * 2);
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);
vm.stopPrank();

vm.deal(address(delegation), address(delegation).balance + 10_000 ether);
Expand Down

0 comments on commit 07dcfae

Please sign in to comment.