From f0b4d20fa92fb63ec4f6e3a1d003f53d278e0da9 Mon Sep 17 00:00:00 2001 From: DrZoltanFazekas Date: Wed, 4 Dec 2024 13:31:01 +0100 Subject: [PATCH] Remove duplicate code from the test contracts --- src/BaseDelegation.sol | 12 ++ src/LiquidDelegation.sol | 16 ++ src/LiquidDelegationV2.sol | 4 +- src/NonLiquidDelegation.sol | 16 ++ src/NonLiquidDelegationV2.sol | 4 +- test/BaseDelegation.t.sol | 174 ++++++++++++++++++++++ test/LiquidDelegation.t.sol | 240 +++++++----------------------- test/NonLiquidDelegation.t.sol | 259 +++++++-------------------------- 8 files changed, 331 insertions(+), 394 deletions(-) create mode 100644 test/BaseDelegation.t.sol diff --git a/src/BaseDelegation.sol b/src/BaseDelegation.sol index 8e56c79..c9d0fb3 100644 --- a/src/BaseDelegation.sol +++ b/src/BaseDelegation.sol @@ -119,6 +119,18 @@ abstract contract BaseDelegation is Delegation, PausableUpgradeable, Ownable2Ste require(success, "deposit failed"); } + function deposit( + bytes calldata blsPubKey, + bytes calldata peerId, + bytes calldata signature + ) public virtual payable; + + function deposit2( + bytes calldata blsPubKey, + bytes calldata peerId, + bytes calldata signature + ) public virtual; + function _increaseDeposit(uint256 amount) internal virtual { // topup the deposit only if already activated as a validator if (_isActivated()) { diff --git a/src/LiquidDelegation.sol b/src/LiquidDelegation.sol index b2ba7ce..3d7e068 100644 --- a/src/LiquidDelegation.sol +++ b/src/LiquidDelegation.sol @@ -44,6 +44,22 @@ contract LiquidDelegation is BaseDelegation, ILiquidDelegation { return $.lst; } + function deposit( + bytes calldata blsPubKey, + bytes calldata peerId, + bytes calldata signature + ) public override payable { + revert("not implemented"); + } + + function deposit2( + bytes calldata blsPubKey, + bytes calldata peerId, + bytes calldata signature + ) public override { + revert("not implemented"); + } + function stake() external override payable { revert("not implemented"); } diff --git a/src/LiquidDelegationV2.sol b/src/LiquidDelegationV2.sol index 8d2debb..61bd05a 100644 --- a/src/LiquidDelegationV2.sol +++ b/src/LiquidDelegationV2.sol @@ -51,7 +51,7 @@ contract LiquidDelegationV2 is BaseDelegation, ILiquidDelegation { bytes calldata blsPubKey, bytes calldata peerId, bytes calldata signature - ) public onlyOwner { + ) public override onlyOwner { _deposit( blsPubKey, peerId, @@ -67,7 +67,7 @@ contract LiquidDelegationV2 is BaseDelegation, ILiquidDelegation { bytes calldata blsPubKey, bytes calldata peerId, bytes calldata signature - ) public payable onlyOwner { + ) public override payable onlyOwner { _deposit( blsPubKey, peerId, diff --git a/src/NonLiquidDelegation.sol b/src/NonLiquidDelegation.sol index 9723257..efbd65b 100644 --- a/src/NonLiquidDelegation.sol +++ b/src/NonLiquidDelegation.sol @@ -39,6 +39,22 @@ contract NonLiquidDelegation is BaseDelegation, INonLiquidDelegation { __BaseDelegation_init(initialOwner); } + function deposit( + bytes calldata blsPubKey, + bytes calldata peerId, + bytes calldata signature + ) public override payable { + revert("not implemented"); + } + + function deposit2( + bytes calldata blsPubKey, + bytes calldata peerId, + bytes calldata signature + ) public override { + revert("not implemented"); + } + function stake() external payable override { revert("not implemented"); } diff --git a/src/NonLiquidDelegationV2.sol b/src/NonLiquidDelegationV2.sol index 3febd65..f2f01a1 100644 --- a/src/NonLiquidDelegationV2.sol +++ b/src/NonLiquidDelegationV2.sol @@ -120,7 +120,7 @@ contract NonLiquidDelegationV2 is BaseDelegation, INonLiquidDelegation { bytes calldata blsPubKey, bytes calldata peerId, bytes calldata signature - ) public onlyOwner { + ) public override onlyOwner { _deposit( blsPubKey, peerId, @@ -136,7 +136,7 @@ contract NonLiquidDelegationV2 is BaseDelegation, INonLiquidDelegation { bytes calldata blsPubKey, bytes calldata peerId, bytes calldata signature - ) public payable onlyOwner { + ) public payable override onlyOwner { _deposit( blsPubKey, peerId, diff --git a/test/BaseDelegation.t.sol b/test/BaseDelegation.t.sol new file mode 100644 index 0000000..da7fc54 --- /dev/null +++ b/test/BaseDelegation.t.sol @@ -0,0 +1,174 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 +pragma solidity ^0.8.26; + +import {BaseDelegation} from "src/BaseDelegation.sol"; +import {Delegation} from "src/Delegation.sol"; +import {Deposit, InitialStaker} from "@zilliqa/zq2/deposit.sol"; +import {Console} from "src/Console.sol"; +import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; +import {Test, Vm} from "forge-std/Test.sol"; +import "forge-std/console.sol"; + +contract PopVerifyPrecompile { + function popVerify(bytes memory, bytes memory) public pure returns(bool) { + return true; + } +} + +abstract contract BaseDelegationTest is Test { + address payable proxy; + address oldImplementation; + bytes initializerCall; + address payable newImplementation; + bytes reinitializerCall; + address owner; + address[4] stakers = [ + 0xd819fFcE7A58b1E835c25617Db7b46a00888B013, + 0x092E5E57955437876dA9Df998C96e2BE19341670, + 0xeA78aAE5Be606D2D152F00760662ac321aB8F017, + 0x6603A37980DF7ef6D44E994B3183A15D0322B7bF + ]; + + constructor() { + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); + owner = vm.addr(deployerPrivateKey); + //console.log("Signer is %s", owner); + } + + function storeDelegation() internal virtual; + + function setUp() public { + vm.chainId(33469); + vm.deal(owner, 100_000 ether); + vm.startPrank(owner); + + proxy = payable( + new ERC1967Proxy(oldImplementation, initializerCall) + ); + /* + console.log( + "Proxy deployed: %s \r\n Implementation deployed: %s", + proxy, + oldImplementation + ); + //*/ + + BaseDelegation oldDelegation = BaseDelegation( + proxy + ); + /* + console.log("Deployed version: %s", + oldDelegation.version() + ); + + console.log("Owner is %s", + oldDelegation.owner() + ); + //*/ + + /* + console.log("New implementation deployed: %s", + newImplementation + ); + //*/ + + oldDelegation.upgradeToAndCall( + newImplementation, + reinitializerCall + ); + + storeDelegation(); + BaseDelegation delegation = BaseDelegation( + proxy + ); + + /* + console.log("Upgraded to version: %s", + delegation.version() + ); + //*/ + /* + Console.log("Old commission rate: %s.%s%s%%", + delegation.getCommissionNumerator(), + 2 + ); + //*/ + uint256 commissionNumerator = 1_000; + delegation.setCommissionNumerator(commissionNumerator); + /* + Console.log("New commission rate: %s.%s%s%%", + delegation.getCommissionNumerator(), + 2 + ); + //*/ + + InitialStaker[] memory initialStakers = new InitialStaker[](0); + //vm.deployCodeTo("Deposit.sol", delegation.DEPOSIT_CONTRACT()); + vm.etch( + delegation.DEPOSIT_CONTRACT(), //0x000000000000000000005a494C4445504F534954, + address(new Deposit(10_000_000 ether, 256, 10, initialStakers)).code + ); + vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(11)), bytes32(uint256(block.number / 10))); + vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(12)), bytes32(uint256(10_000_000 ether))); + vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(13)), bytes32(uint256(256))); + vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(14)), bytes32(uint256(10))); + /* + console.log("Deposit.minimimStake() =", Deposit(delegation.DEPOSIT_CONTRACT()).minimumStake()); + console.log("Deposit.maximumStakers() =", Deposit(delegation.DEPOSIT_CONTRACT()).maximumStakers()); + console.log("Deposit.blocksPerEpoch() =", Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch()); + //*/ + + vm.etch(address(0x5a494c80), address(new PopVerifyPrecompile()).code); + + vm.stopPrank(); + } + + function deposit( + BaseDelegation delegation, + uint256 depositAmount, + bool initialDeposit + ) internal { + if (initialDeposit) { + vm.deal(owner, owner.balance + depositAmount); + vm.startPrank(owner); + + delegation.deposit{ + value: depositAmount + }( + bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c"), + bytes(hex"002408011220d5ed74b09dcbe84d3b32a56c01ab721cf82809848b6604535212a219d35c412f"), + bytes(hex"b14832a866a49ddf8a3104f8ee379d29c136f29aeb8fccec9d7fb17180b99e8ed29bee2ada5ce390cb704bc6fd7f5ce814f914498376c4b8bc14841a57ae22279769ec8614e2673ba7f36edc5a4bf5733aa9d70af626279ee2b2cde939b4bd8a") + ); + } else { + vm.deal(stakers[0], stakers[0].balance + depositAmount); + vm.startPrank(stakers[0]); + + vm.expectEmit( + true, + false, + false, + false, + address(delegation) + ); + emit Delegation.Staked( + stakers[0], + depositAmount, + "" + ); + + delegation.stake{ + value: depositAmount + }(); + + vm.startPrank(owner); + + delegation.deposit2( + bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c"), + bytes(hex"002408011220d5ed74b09dcbe84d3b32a56c01ab721cf82809848b6604535212a219d35c412f"), + bytes(hex"b14832a866a49ddf8a3104f8ee379d29c136f29aeb8fccec9d7fb17180b99e8ed29bee2ada5ce390cb704bc6fd7f5ce814f914498376c4b8bc14841a57ae22279769ec8614e2673ba7f36edc5a4bf5733aa9d70af626279ee2b2cde939b4bd8a") + ); + } + // wait 2 epochs for the change to the deposit to take affect + vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2); + } +} \ No newline at end of file diff --git a/test/LiquidDelegation.t.sol b/test/LiquidDelegation.t.sol index c376ad7..51d01f8 100644 --- a/test/LiquidDelegation.t.sol +++ b/test/LiquidDelegation.t.sol @@ -1,133 +1,43 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 pragma solidity ^0.8.26; +import {BaseDelegationTest, PopVerifyPrecompile} from "test/BaseDelegation.t.sol"; import {LiquidDelegation} from "src/LiquidDelegation.sol"; import {LiquidDelegationV2} from "src/LiquidDelegationV2.sol"; import {NonRebasingLST} from "src/NonRebasingLST.sol"; -import {WithdrawalQueue} from "src/BaseDelegation.sol"; +import {BaseDelegation, WithdrawalQueue} from "src/BaseDelegation.sol"; import {Delegation} from "src/Delegation.sol"; -import {Deposit, InitialStaker} from "@zilliqa/zq2/deposit.sol"; +import {Deposit} from "@zilliqa/zq2/deposit.sol"; import {Console} from "src/Console.sol"; -import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; -import {Test, Vm} from "forge-std/Test.sol"; +import {Vm} from "forge-std/Test.sol"; import "forge-std/console.sol"; -contract PopVerifyPrecompile { - function popVerify(bytes memory, bytes memory) public pure returns(bool) { - return true; - } -} - -contract LiquidDelegationTest is Test { - address payable proxy; +contract LiquidDelegationTest is BaseDelegationTest { LiquidDelegationV2 delegation; NonRebasingLST lst; - address owner; - address staker = 0xd819fFcE7A58b1E835c25617Db7b46a00888B013; - - function setUp() public { - vm.chainId(33469); - uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); - owner = vm.addr(deployerPrivateKey); - //console.log("Signer is %s", owner); - vm.deal(owner, 100_000 ether); - vm.startPrank(owner); - - address oldImplementation = address( - new LiquidDelegation() - ); - bytes memory initializerCall = abi.encodeWithSelector( + constructor() BaseDelegationTest() { + oldImplementation = address(new LiquidDelegation()); + newImplementation = payable(new LiquidDelegationV2()); + initializerCall = abi.encodeWithSelector( LiquidDelegation.initialize.selector, owner ); - - proxy = payable( - new ERC1967Proxy(oldImplementation, initializerCall) - ); - /* - console.log( - "Proxy deployed: %s \r\n Implementation deployed: %s", - proxy, - oldImplementation - ); - //*/ - LiquidDelegation oldDelegation = LiquidDelegation( - proxy - ); - /* - console.log("Deployed version: %s", - oldDelegation.version() - ); - - console.log("Owner is %s", - oldDelegation.owner() - ); - //*/ - address payable newImplementation = payable( - new LiquidDelegationV2() - ); - /* - console.log("New implementation deployed: %s", - newImplementation - ); - //*/ - bytes memory reinitializerCall = abi.encodeWithSelector( + reinitializerCall = abi.encodeWithSelector( LiquidDelegationV2.reinitialize.selector ); + } - oldDelegation.upgradeToAndCall( - newImplementation, - reinitializerCall - ); - + function storeDelegation() internal override { delegation = LiquidDelegationV2( - proxy - ); - /* - console.log("Upgraded to version: %s", - delegation.version() + proxy ); - //*/ lst = NonRebasingLST(delegation.getLST()); /* console.log("LST address: %s", address(lst) ); - - Console.log("Old commission rate: %s.%s%s%%", - delegation.getCommissionNumerator(), - 2 - ); //*/ - uint256 commissionNumerator = 1_000; - delegation.setCommissionNumerator(commissionNumerator); - /* - Console.log("New commission rate: %s.%s%s%%", - delegation.getCommissionNumerator(), - 2 - ); - //*/ - - InitialStaker[] memory initialStakers = new InitialStaker[](0); - //vm.deployCodeTo("Deposit.sol", delegation.DEPOSIT_CONTRACT()); - vm.etch( - delegation.DEPOSIT_CONTRACT(), //0x000000000000000000005a494C4445504F534954, - address(new Deposit(10_000_000 ether, 256, 10, initialStakers)).code - ); - vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(11)), bytes32(uint256(block.number / 10))); - vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(12)), bytes32(uint256(10_000_000 ether))); - vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(13)), bytes32(uint256(256))); - vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(14)), bytes32(uint256(10))); - /* - console.log("Deposit.minimimStake() =", Deposit(delegation.DEPOSIT_CONTRACT()).minimumStake()); - console.log("Deposit.maximumStakers() =", Deposit(delegation.DEPOSIT_CONTRACT()).maximumStakers()); - console.log("Deposit.blocksPerEpoch() =", Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch()); - //*/ - - vm.etch(address(0x5a494c80), address(new PopVerifyPrecompile()).code); - - vm.stopPrank(); } function run( @@ -144,53 +54,12 @@ contract LiquidDelegationTest is Test { delegation = LiquidDelegationV2(proxy); lst = NonRebasingLST(delegation.getLST()); - if (initialDeposit) { - vm.deal(owner, owner.balance + depositAmount); - vm.startPrank(owner); - - delegation.deposit{ - value: depositAmount - }( - bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c"), - bytes(hex"002408011220d5ed74b09dcbe84d3b32a56c01ab721cf82809848b6604535212a219d35c412f"), - bytes(hex"b14832a866a49ddf8a3104f8ee379d29c136f29aeb8fccec9d7fb17180b99e8ed29bee2ada5ce390cb704bc6fd7f5ce814f914498376c4b8bc14841a57ae22279769ec8614e2673ba7f36edc5a4bf5733aa9d70af626279ee2b2cde939b4bd8a") - ); - } else { - vm.deal(staker, staker.balance + depositAmount); - vm.startPrank(staker); - - vm.expectEmit( - true, - false, - false, - true, - address(delegation) - ); - emit Delegation.Staked( - staker, - depositAmount, - abi.encode(depositAmount) - ); - - delegation.stake{ - value: depositAmount - }(); - - vm.startPrank(owner); - - delegation.deposit2( - bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c"), - bytes(hex"002408011220d5ed74b09dcbe84d3b32a56c01ab721cf82809848b6604535212a219d35c412f"), - bytes(hex"b14832a866a49ddf8a3104f8ee379d29c136f29aeb8fccec9d7fb17180b99e8ed29bee2ada5ce390cb704bc6fd7f5ce814f914498376c4b8bc14841a57ae22279769ec8614e2673ba7f36edc5a4bf5733aa9d70af626279ee2b2cde939b4bd8a") - ); - } - // wait 2 epochs for the change to the deposit to take affect - vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2); + deposit(BaseDelegation(delegation), depositAmount, initialDeposit); vm.store(address(delegation), 0xfa57cbed4b267d0bc9f2cbdae86b4d1d23ca818308f873af9c968a23afadfd01, bytes32(taxedRewardsBeforeStaking)); vm.deal(address(delegation), rewardsBeforeStaking); - vm.deal(staker, 100_000 ether); - vm.startPrank(staker); + vm.deal(stakers[0], 100_000 ether); + vm.startPrank(stakers[0]); Console.log("Deposit before staking: %s.%s%s ZIL", delegation.getStake() @@ -205,11 +74,11 @@ contract LiquidDelegationTest is Test { ); Console.log("Staker balance before staking: %s.%s%s ZIL", - staker.balance + stakers[0].balance ); Console.log("Staker balance before staking: %s.%s%s LST", - lst.balanceOf(staker) + lst.balanceOf(stakers[0]) ); Console.log("Total supply before staking: %s.%s%s LST", @@ -217,7 +86,6 @@ contract LiquidDelegationTest is Test { ); uint256[2] memory ownerZIL = [uint256(0), 0]; - uint256 ownerZILAfter; uint256 loggedAmount; uint256 loggedShares; uint256 totalShares; @@ -239,7 +107,7 @@ contract LiquidDelegationTest is Test { address(delegation) ); emit Delegation.Staked( - staker, + stakers[0], delegatedAmount, abi.encode(lst.totalSupply() * delegatedAmount / (delegation.getStake() + delegation.getRewards())) ); @@ -286,11 +154,11 @@ contract LiquidDelegationTest is Test { ); Console.log("Staker balance after staking: %s.%s%s ZIL", - staker.balance + stakers[0].balance ); Console.log("Staker balance after staking: %s.%s%s LST", - lst.balanceOf(staker) + lst.balanceOf(stakers[0]) ); Console.log("Total supply after staking: %s.%s%s LST", @@ -323,15 +191,15 @@ contract LiquidDelegationTest is Test { address(delegation) ); emit Delegation.Unstaked( - staker, - (delegation.getStake() + delegation.getRewards()) * lst.balanceOf(staker) / lst.totalSupply(), - abi.encode(lst.balanceOf(staker)) + stakers[0], + (delegation.getStake() + delegation.getRewards()) * lst.balanceOf(stakers[0]) / lst.totalSupply(), + abi.encode(lst.balanceOf(stakers[0])) ); - uint256[2] memory stakerLST = [lst.balanceOf(staker), 0]; + uint256[2] memory stakerLST = [lst.balanceOf(stakers[0]), 0]; ownerZIL[0] = delegation.owner().balance; - uint256 shares = initialDeposit ? lst.balanceOf(staker) : lst.balanceOf(staker) - depositAmount; + uint256 shares = initialDeposit ? lst.balanceOf(stakers[0]) : lst.balanceOf(stakers[0]) - depositAmount; assertEq(totalShares, shares, "staked shares balance mismatch"); delegation.unstake( @@ -341,7 +209,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()).blocksPerEpoch() * 2); - stakerLST[1] = lst.balanceOf(staker); + stakerLST[1] = lst.balanceOf(stakers[0]); ownerZIL[1] = delegation.owner().balance; assertEq((rewardsBeforeUnstaking - taxedRewardsAfterStaking) * delegation.getCommissionNumerator() / delegation.DENOMINATOR(), ownerZIL[1] - ownerZIL[0], "commission mismatch after unstaking"); @@ -377,13 +245,13 @@ contract LiquidDelegationTest is Test { taxedRewardsAfterUnstaking ); - uint256 stakerBalanceAfterUnstaking = staker.balance; + uint256 stakerBalanceAfterUnstaking = stakers[0].balance; Console.log("Staker balance after unstaking: %s.%s%s ZIL", stakerBalanceAfterUnstaking ); Console.log("Staker balance after unstaking: %s.%s%s LST", - lst.balanceOf(staker) + lst.balanceOf(stakers[0]) ); Console.log("Total supply after unstaking: %s.%s%s LST", @@ -407,17 +275,17 @@ contract LiquidDelegationTest is Test { address(delegation) ); emit Delegation.Claimed( - staker, + stakers[0], unstakedAmount, "" ); - uint256[2] memory stakerZIL = [staker.balance, 0]; + uint256[2] memory stakerZIL = [stakers[0].balance, 0]; ownerZIL[0] = delegation.owner().balance; delegation.claim(); - stakerZIL[1] = staker.balance; + stakerZIL[1] = stakers[0].balance; ownerZIL[1] = delegation.owner().balance; entries = vm.getRecordedLogs(); @@ -448,12 +316,12 @@ contract LiquidDelegationTest is Test { ); Console.log("Staker balance after claiming: %s.%s%s ZIL", - staker.balance + stakers[0].balance ); - assertEq(staker.balance, stakerBalanceAfterUnstaking + unstakedAmount, "final staker balance mismatch"); + assertEq(stakers[0].balance, stakerBalanceAfterUnstaking + unstakedAmount, "final staker balance mismatch"); Console.log("Staker balance after claiming: %s.%s%s LST", - lst.balanceOf(staker) + lst.balanceOf(stakers[0]) ); Console.log("Total supply after claiming: %s.%s%s LST", @@ -463,7 +331,7 @@ contract LiquidDelegationTest is Test { } function test_1a_LargeStake_Late_NoRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 10_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 10_000 ether; @@ -488,7 +356,7 @@ contract LiquidDelegationTest is Test { //TODO: remove the test once https://github.com/Zilliqa/zq2/issues/1761 is fixed function test_DepositContract() public { vm.deal(owner, 10_000_000 ether + 1_000_000 ether + 0 ether); - vm.deal(staker, 0); + vm.deal(stakers[0], 0); vm.startPrank(owner); Deposit(delegation.DEPOSIT_CONTRACT()).deposit{ value: 10_000_000 ether @@ -496,7 +364,7 @@ contract LiquidDelegationTest is Test { bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c"), bytes(hex"002408011220d5ed74b09dcbe84d3b32a56c01ab721cf82809848b6604535212a219d35c412f"), bytes(hex"b14832a866a49ddf8a3104f8ee379d29c136f29aeb8fccec9d7fb17180b99e8ed29bee2ada5ce390cb704bc6fd7f5ce814f914498376c4b8bc14841a57ae22279769ec8614e2673ba7f36edc5a4bf5733aa9d70af626279ee2b2cde939b4bd8a"), - address(staker) + address(stakers[0]) ); console.log("validator deposited"); console.log("validator stake: %s", Deposit(delegation.DEPOSIT_CONTRACT()).getStake( @@ -542,7 +410,7 @@ contract LiquidDelegationTest is Test { } function test_1b_LargeStake_Early_NoRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 10_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 10_000 ether; @@ -565,7 +433,7 @@ contract LiquidDelegationTest is Test { } function test_2a_LargeStake_Late_SmallValidator_OwnDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 10_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 10_000 ether; @@ -588,7 +456,7 @@ contract LiquidDelegationTest is Test { } function test_3a_SmallStake_Late_SmallValidator_OwnDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 10_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 100 ether; @@ -611,7 +479,7 @@ contract LiquidDelegationTest is Test { } function test_4a_LargeStake_Late_LargeValidator_OwnDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 100_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 10_000 ether; @@ -634,7 +502,7 @@ contract LiquidDelegationTest is Test { } function test_5a_SmallStake_Late_LargeValidator_OwnDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 100_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 100 ether; @@ -657,7 +525,7 @@ contract LiquidDelegationTest is Test { } function test_2b_LargeStake_Late_SmallValidator_DelegatedDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 10_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 10_000 ether; @@ -680,7 +548,7 @@ contract LiquidDelegationTest is Test { } function test_3b_SmallStake_Late_SmallValidator_DelegatedDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 10_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 100 ether; @@ -703,7 +571,7 @@ contract LiquidDelegationTest is Test { } function test_4b_LargeStake_Late_LargeValidator_DelegatedDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 100_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 10_000 ether; @@ -726,7 +594,7 @@ contract LiquidDelegationTest is Test { } function test_5b_SmallStake_Late_LargeValidator_DelegatedDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 100_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 100 ether; @@ -749,7 +617,7 @@ contract LiquidDelegationTest is Test { } function test_2c_LargeStake_Early_SmallValidator_OwnDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 10_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 10_000 ether; @@ -772,7 +640,7 @@ contract LiquidDelegationTest is Test { } function test_3c_SmallStake_Early_SmallValidator_OwnDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 10_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 100 ether; @@ -795,7 +663,7 @@ contract LiquidDelegationTest is Test { } function test_4c_LargeStake_Early_LargeValidator_OwnDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 100_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 10_000 ether; @@ -818,7 +686,7 @@ contract LiquidDelegationTest is Test { } function test_5c_SmallStake_Early_LargeValidator_OwnDeposit_OneYearOfRewards_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 100_000_000 ether; uint256 totalDeposit = 5_200_000_000 ether; uint256 delegatedAmount = 100 ether; @@ -841,7 +709,7 @@ contract LiquidDelegationTest is Test { } function test_6a_ManyVsOneStake_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 10_000_000 ether; uint256 totalDeposit = 110_000_000 ether; uint256 delegatedAmount = 10_000 ether; @@ -866,7 +734,7 @@ contract LiquidDelegationTest is Test { } function test_6b_OneVsManyStakes_UnstakeAll() public { - staker = 0x092E5E57955437876dA9Df998C96e2BE19341670; + stakers[0] = 0x092E5E57955437876dA9Df998C96e2BE19341670; uint256 depositAmount = 10_000_000 ether; uint256 totalDeposit = 110_000_000 ether; uint256 delegatedAmount = 90_000 ether; @@ -911,7 +779,7 @@ contract LiquidDelegationTest is Test { */ //TODO: update the values based on the devnet and fix the failing test (typo intentional) function est_0_ReproduceRealNetwork() public { - staker = 0xd819fFcE7A58b1E835c25617Db7b46a00888B013; + stakers[0] = 0xd819fFcE7A58b1E835c25617Db7b46a00888B013; uint256 delegatedAmount = 10_000 ether; // Insert the following values output by the STATE script below uint256 rewardsBeforeStaking = 197818620596390326580; diff --git a/test/NonLiquidDelegation.t.sol b/test/NonLiquidDelegation.t.sol index 6a5282f..7acb8c8 100644 --- a/test/NonLiquidDelegation.t.sol +++ b/test/NonLiquidDelegation.t.sol @@ -1,190 +1,41 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 pragma solidity ^0.8.26; +import {BaseDelegationTest, PopVerifyPrecompile} from "test/BaseDelegation.t.sol"; import {NonLiquidDelegation} from "src/NonLiquidDelegation.sol"; import {NonLiquidDelegationV2} from "src/NonLiquidDelegationV2.sol"; -import {WithdrawalQueue} from "src/BaseDelegation.sol"; +import {BaseDelegation, WithdrawalQueue} from "src/BaseDelegation.sol"; import {Delegation} from "src/Delegation.sol"; -import {Deposit, InitialStaker} from "@zilliqa/zq2/deposit.sol"; +import {Deposit} from "@zilliqa/zq2/deposit.sol"; import {Console} from "src/Console.sol"; import {Strings} from "@openzeppelin/contracts/utils/Strings.sol"; -import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; -import {Test, Vm} from "forge-std/Test.sol"; +import {Vm} from "forge-std/Test.sol"; import "forge-std/console.sol"; -contract PopVerifyPrecompile { - function popVerify(bytes memory, bytes memory) public pure returns(bool) { - return true; - } -} - -contract NonLiquidDelegationTest is Test { - address payable proxy; - address owner; +contract NonLiquidDelegationTest is BaseDelegationTest { NonLiquidDelegationV2 delegation; - address[4] staker = [ - 0xd819fFcE7A58b1E835c25617Db7b46a00888B013, - 0x092E5E57955437876dA9Df998C96e2BE19341670, - 0xeA78aAE5Be606D2D152F00760662ac321aB8F017, - 0x6603A37980DF7ef6D44E994B3183A15D0322B7bF - ]; - - function setUp() public { - vm.chainId(33469); - uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); - owner = vm.addr(deployerPrivateKey); - //console.log("Signer is %s", owner); - vm.deal(owner, 100_000 ether); - vm.startPrank(owner); - - address oldImplementation = address( - new NonLiquidDelegation() - ); - bytes memory initializerCall = abi.encodeWithSelector( + constructor() BaseDelegationTest() { + oldImplementation = address(new NonLiquidDelegation()); + newImplementation = payable(new NonLiquidDelegationV2()); + initializerCall = abi.encodeWithSelector( NonLiquidDelegation.initialize.selector, owner ); - - proxy = payable( - new ERC1967Proxy(oldImplementation, initializerCall) - ); - /* - console.log( - "Proxy deployed: %s \r\n Implementation deployed: %s", - proxy, - oldImplementation - ); - //*/ - - NonLiquidDelegation oldDelegation = NonLiquidDelegation( - proxy - ); - /* - console.log("Deployed version: %s", - oldDelegation.version() - ); - - console.log("Owner is %s", - oldDelegation.owner() - ); - //*/ - - address payable newImplementation = payable( - new NonLiquidDelegationV2() - ); - - /* - console.log("New implementation deployed: %s", - newImplementation - ); - //*/ - - bytes memory reinitializerCall = abi.encodeWithSelector( + reinitializerCall = abi.encodeWithSelector( NonLiquidDelegationV2.reinitialize.selector ); + } - oldDelegation.upgradeToAndCall( - newImplementation, - reinitializerCall - ); - + function storeDelegation() internal override { delegation = NonLiquidDelegationV2( proxy ); - - /* - console.log("Upgraded to version: %s", - delegation.version() - ); - //*/ - /* - Console.log("Old commission rate: %s.%s%s%%", - delegation.getCommissionNumerator(), - 2 - ); - //*/ - uint256 commissionNumerator = 1_000; - delegation.setCommissionNumerator(commissionNumerator); - /* - Console.log("New commission rate: %s.%s%s%%", - delegation.getCommissionNumerator(), - 2 - ); - //*/ - - InitialStaker[] memory initialStakers = new InitialStaker[](0); - //vm.deployCodeTo("Deposit.sol", delegation.DEPOSIT_CONTRACT()); - vm.etch( - delegation.DEPOSIT_CONTRACT(), //0x000000000000000000005a494C4445504F534954, - address(new Deposit(10_000_000 ether, 256, 10, initialStakers)).code - ); - vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(11)), bytes32(uint256(block.number / 10))); - vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(12)), bytes32(uint256(10_000_000 ether))); - vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(13)), bytes32(uint256(256))); - vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(14)), bytes32(uint256(10))); - /* - console.log("Deposit.minimimStake() =", Deposit(delegation.DEPOSIT_CONTRACT()).minimumStake()); - console.log("Deposit.maximumStakers() =", Deposit(delegation.DEPOSIT_CONTRACT()).maximumStakers()); - console.log("Deposit.blocksPerEpoch() =", Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch()); - //*/ - - vm.etch(address(0x5a494c80), address(new PopVerifyPrecompile()).code); - - vm.stopPrank(); - } - - function deposit( - uint256 depositAmount, - bool initialDeposit - ) internal { - if (initialDeposit) { - vm.deal(owner, owner.balance + depositAmount); - vm.startPrank(owner); - - delegation.deposit{ - value: depositAmount - }( - bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c"), - bytes(hex"002408011220d5ed74b09dcbe84d3b32a56c01ab721cf82809848b6604535212a219d35c412f"), - bytes(hex"b14832a866a49ddf8a3104f8ee379d29c136f29aeb8fccec9d7fb17180b99e8ed29bee2ada5ce390cb704bc6fd7f5ce814f914498376c4b8bc14841a57ae22279769ec8614e2673ba7f36edc5a4bf5733aa9d70af626279ee2b2cde939b4bd8a") - ); - } else { - vm.deal(staker[0], staker[0].balance + depositAmount); - vm.startPrank(staker[0]); - - vm.expectEmit( - true, - false, - false, - true, - address(delegation) - ); - emit Delegation.Staked( - staker[0], - depositAmount, - "" - ); - - delegation.stake{ - value: depositAmount - }(); - - vm.startPrank(owner); - - delegation.deposit2( - bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c"), - bytes(hex"002408011220d5ed74b09dcbe84d3b32a56c01ab721cf82809848b6604535212a219d35c412f"), - bytes(hex"b14832a866a49ddf8a3104f8ee379d29c136f29aeb8fccec9d7fb17180b99e8ed29bee2ada5ce390cb704bc6fd7f5ce814f914498376c4b8bc14841a57ae22279769ec8614e2673ba7f36edc5a4bf5733aa9d70af626279ee2b2cde939b4bd8a") - ); - } - // wait 2 epochs for the change to the deposit to take affect - vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2); } function findStaker(address a) internal view returns(uint256) { - for (uint256 i = 0; i < staker.length; i++) - if (staker[i] == a) + for (uint256 i = 0; i < stakers.length; i++) + if (stakers[i] == a) return i; revert("staker not found"); } @@ -192,7 +43,7 @@ contract NonLiquidDelegationTest is Test { function snapshot(string memory s, uint256 i, uint256 x) internal view { console.log("-----------------------------------------------"); console.log(s, i, x); - uint256[] memory shares = new uint256[](staker.length); + uint256[] memory shares = new uint256[](stakers.length); NonLiquidDelegationV2.Staking[] memory stakings = delegation.getStakingHistory(); for (i = 0; i < stakings.length; i++) //i = stakings.length - 1; @@ -201,7 +52,7 @@ contract NonLiquidDelegationTest is Test { shares[stakerIndex] = stakings[i].amount; s = string.concat("index: ", Strings.toString(i)); s = string.concat(s, "\tstaker "); - assertEq(stakings[i].staker, staker[stakerIndex], "found staker mismatch"); + assertEq(stakings[i].staker, stakers[stakerIndex], "found staker mismatch"); s = string.concat(s, Strings.toString(stakerIndex + 1)); s = string.concat(s, ": "); s = string.concat(s, Strings.toHexString(stakings[i].staker)); @@ -264,12 +115,12 @@ contract NonLiquidDelegationTest is Test { if (initialDeposit) // otherwise snapshot() doesn't find the staker and reverts - staker[0] = owner; - deposit(depositAmount, initialDeposit); + stakers[0] = owner; + deposit(BaseDelegation(delegation), depositAmount, initialDeposit); - for (uint256 i = 0; i < staker.length; i++) { - vm.deal(staker[i], 10 * depositAmount); - console.log("staker %s: %s", i+1, staker[i]); + for (uint256 i = 0; i < stakers.length; i++) { + vm.deal(stakers[i], 10 * depositAmount); + console.log("staker %s: %s", i+1, stakers[i]); } delegation = NonLiquidDelegationV2(proxy); @@ -280,7 +131,7 @@ contract NonLiquidDelegationTest is Test { for (uint256 i = 0; i < stakerIndicesBeforeWithdrawals.length; i++) { vm.deal(address(delegation), address(delegation).balance + rewardsAccruedAfterEach); int256 x = relativeAmountsBeforeWithdrawals[i] * int256(depositAmount) / 10; - vm.startPrank(staker[stakerIndicesBeforeWithdrawals[i]-1]); + vm.startPrank(stakers[stakerIndicesBeforeWithdrawals[i]-1]); if (x >= 0) { delegation.stake{value: uint256(x)}(); //snapshot("staker %s staked %s", stakerIndices[i], uint256(x)); @@ -297,14 +148,14 @@ contract NonLiquidDelegationTest is Test { //vm.deal(address(delegation), address(delegation).balance + rewardsAccruedAfterEach); for (uint256 i = 1; i <= 2; i++) { - vm.startPrank(staker[i-1]); + vm.startPrank(stakers[i-1]); if (steps == 123_456_789) snapshot("staker %s withdrawing all, remaining rewards:", i, 0); else snapshot("staker %s withdrawing 1+%s times", i, steps); Console.log("rewards accrued until last staking: %s.%s%s", delegation.getTotalRewards()); Console.log("delegation contract balance: %s.%s%s", address(delegation).balance); - //Console.log("staker balance: %s.%s%s", staker[i-1].balance); + //Console.log("staker balance: %s.%s%s", stakers[i-1].balance); Console.log("staker rewards: %s.%s%s", delegation.rewards()); if (steps == 123_456_789) Console.log("staker withdrew: %s.%s%s", delegation.withdrawAllRewards()); @@ -312,14 +163,14 @@ contract NonLiquidDelegationTest is Test { Console.log("staker withdrew: %s.%s%s", delegation.withdrawRewards(delegation.rewards(steps), steps)); Console.log("rewards accrued until last staking: %s.%s%s", delegation.getTotalRewards()); Console.log("delegation contract balance: %s.%s%s", address(delegation).balance); - //Console.log("staker balance: %s.%s%s", staker[i-1].balance); + //Console.log("staker balance: %s.%s%s", stakers[i-1].balance); vm.stopPrank(); } for (uint256 i = 0; i < stakerIndicesAfterWithdrawals.length; i++) { vm.deal(address(delegation), address(delegation).balance + rewardsAccruedAfterEach); int256 x = relativeAmountsAfterWithdrawals[i] * int256(depositAmount) / 10; - vm.startPrank(staker[stakerIndicesAfterWithdrawals[i]-1]); + vm.startPrank(stakers[stakerIndicesAfterWithdrawals[i]-1]); if (x >= 0) { delegation.stake{value: uint256(x)}(); //snapshot("staker %s staked %s", stakerIndices[i], uint256(x)); @@ -335,15 +186,15 @@ contract NonLiquidDelegationTest is Test { //further rewards accrued since the last staking vm.deal(address(delegation), address(delegation).balance + rewardsAccruedAfterEach); - for (uint256 i = 1; i <= staker.length; i++) { - vm.startPrank(staker[i-1]); + for (uint256 i = 1; i <= stakers.length; i++) { + vm.startPrank(stakers[i-1]); if (steps == 123_456_789) snapshot("staker %s withdrawing all, remaining rewards:", i, 0); else snapshot("staker %s withdrawing 1+%s times", i, steps); Console.log("rewards accrued until last staking: %s.%s%s", delegation.getTotalRewards()); Console.log("delegation contract balance: %s.%s%s", address(delegation).balance); - //Console.log("staker balance: %s.%s%s", staker[i-1].balance); + //Console.log("staker balance: %s.%s%s", stakers[i-1].balance); Console.log("staker rewards: %s.%s%s", delegation.rewards()); if (steps == 123_456_789) Console.log("staker withdrew: %s.%s%s", delegation.withdrawAllRewards()); @@ -352,22 +203,22 @@ contract NonLiquidDelegationTest is Test { Console.log("staker withdrew: %s.%s%s", delegation.withdrawRewards(delegation.rewards(steps), steps)); Console.log("rewards accrued until last staking: %s.%s%s", delegation.getTotalRewards()); Console.log("delegation contract balance: %s.%s%s", address(delegation).balance); - //Console.log("staker balance: %s.%s%s", staker[i-1].balance); + //Console.log("staker balance: %s.%s%s", stakers[i-1].balance); vm.stopPrank(); } // if we try to withdraw again immediately (in the same block), // the amount withdrawn must equal zero //* - for (uint256 i = 1; i <= staker.length; i++) { - vm.startPrank(staker[i-1]); + for (uint256 i = 1; i <= stakers.length; i++) { + vm.startPrank(stakers[i-1]); if (steps == 123_456_789) snapshot("staker %s withdrawing all, remaining rewards:", i, 0); else snapshot("staker %s withdrawing 1+%s times", i, steps); Console.log("rewards accrued until last staking: %s.%s%s", delegation.getTotalRewards()); Console.log("delegation contract balance: %s.%s%s", address(delegation).balance); - //Console.log("staker balance: %s.%s%s", staker[i-1].balance); + //Console.log("staker balance: %s.%s%s", stakers[i-1].balance); Console.log("staker rewards: %s.%s%s", delegation.rewards()); if (steps == 123_456_789) Console.log("staker withdrew: %s.%s%s", delegation.withdrawAllRewards()); @@ -376,7 +227,7 @@ contract NonLiquidDelegationTest is Test { Console.log("staker withdrew: %s.%s%s", delegation.withdrawRewards(delegation.rewards(steps), steps)); Console.log("rewards accrued until last staking: %s.%s%s", delegation.getTotalRewards()); Console.log("delegation contract balance: %s.%s%s", address(delegation).balance); - //Console.log("staker balance: %s.%s%s", staker[i-1].balance); + //Console.log("staker balance: %s.%s%s", stakers[i-1].balance); vm.stopPrank(); } //*/ @@ -529,14 +380,14 @@ contract NonLiquidDelegationTest is Test { uint256 x; uint64 steps = 11_000; - deposit(10_000_000 ether, true); + 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()).blocksPerEpoch() * 2); for (i = 0; i < 4; i++) { - vm.deal(staker[i], 100_000 ether); - console.log("staker %s: %s", i+1, staker[i]); + vm.deal(stakers[i], 100_000 ether); + console.log("staker %s: %s", i+1, stakers[i]); } delegation = NonLiquidDelegationV2(proxy); @@ -546,7 +397,7 @@ contract NonLiquidDelegationTest is Test { x = 50; for (uint256 j = 0; j < steps / 8; j++) { for (i = 1; i <= 4; i++) { - vm.startPrank(staker[i-1]); + vm.startPrank(stakers[i-1]); vm.recordLogs(); vm.expectEmit( true, @@ -556,7 +407,7 @@ contract NonLiquidDelegationTest is Test { address(delegation) ); emit Delegation.Staked( - staker[i-1], + stakers[i-1], x * 1 ether, "" ); @@ -568,7 +419,7 @@ contract NonLiquidDelegationTest is Test { vm.deal(address(delegation), address(delegation).balance + 10_000 ether); } for (i = 1; i <= 4; i++) { - vm.startPrank(staker[i-1]); + vm.startPrank(stakers[i-1]); vm.recordLogs(); vm.expectEmit( true, @@ -578,7 +429,7 @@ contract NonLiquidDelegationTest is Test { address(delegation) ); emit Delegation.Unstaked( - staker[i-1], + stakers[i-1], x * 1 ether, "" ); @@ -592,9 +443,9 @@ contract NonLiquidDelegationTest is Test { } i = 1; - vm.startPrank(staker[i-1]); + vm.startPrank(stakers[i-1]); //snapshot("staker %s withdrawing 1+%s times", i, steps); - //Console.log("staker balance: %s.%s%s", staker[i-1].balance); + //Console.log("staker balance: %s.%s%s", stakers[i-1].balance); //uint256 rewards = delegation.rewards(steps); uint256 rewards = delegation.rewards(); Console.log("staker rewards: %s.%s%s", rewards); @@ -607,7 +458,7 @@ contract NonLiquidDelegationTest is Test { rewards += delegation.withdrawRewards(1000000, 2000); //*/ Console.log("staker withdrew: %s.%s%s", rewards); - //Console.log("staker balance: %s.%s%s", staker[i-1].balance); + //Console.log("staker balance: %s.%s%s", stakers[i-1].balance); vm.stopPrank(); vm.roll(block.number + WithdrawalQueue.unbondingPeriod()); @@ -616,7 +467,7 @@ contract NonLiquidDelegationTest is Test { i = 1; - vm.startPrank(staker[i-1]); + vm.startPrank(stakers[i-1]); vm.recordLogs(); vm.expectEmit( true, @@ -626,7 +477,7 @@ contract NonLiquidDelegationTest is Test { address(delegation) ); emit Delegation.Claimed( - staker[i-1], + stakers[i-1], steps / 8 * x * 1 ether, "" ); @@ -639,14 +490,14 @@ contract NonLiquidDelegationTest is Test { uint256 x; // otherwise snapshot() doesn't find the staker and reverts - staker[0] = owner; - deposit(10_000_000 ether, true); + stakers[0] = owner; + 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()).blocksPerEpoch() * 2); for (i = 0; i < 4; i++) { - vm.deal(staker[i], 100_000 ether); + vm.deal(stakers[i], 100_000 ether); } delegation = NonLiquidDelegationV2(proxy); @@ -655,7 +506,7 @@ contract NonLiquidDelegationTest is Test { vm.deal(address(delegation), 50_000 ether); x = 50; i = 2; - vm.startPrank(staker[i-1]); + vm.startPrank(stakers[i-1]); vm.recordLogs(); vm.expectEmit( true, @@ -665,7 +516,7 @@ contract NonLiquidDelegationTest is Test { address(delegation) ); emit Delegation.Staked( - staker[i-1], + stakers[i-1], x * 1 ether, "" ); @@ -676,12 +527,12 @@ contract NonLiquidDelegationTest is Test { vm.stopPrank(); vm.deal(address(delegation), address(delegation).balance + 10_000 ether); - vm.startPrank(staker[i-1]); + vm.startPrank(stakers[i-1]); snapshot("staker %s withdrawing all, remaining rewards:", i, 0); console.log("-----------------------------------------------"); Console.log("contract balance: %s.%s%s", address(delegation).balance); - Console.log("staker balance: %s.%s%s", staker[i-1].balance); + Console.log("staker balance: %s.%s%s", stakers[i-1].balance); uint256 rewards = delegation.rewards(); Console.log("staker rewards: %s.%s%s", rewards); @@ -705,7 +556,7 @@ contract NonLiquidDelegationTest is Test { address(delegation) ); emit NonLiquidDelegationV2.RewardPaid( - staker[i-1], + stakers[i-1], rewards ); rewards = delegation.withdrawAllRewards(); @@ -722,7 +573,7 @@ contract NonLiquidDelegationTest is Test { console.log("allWithdrawnRewards = %s withdrawnAfterLastStaking = %s", allWithdrawnRewards, withdrawnAfterLastStaking); Console.log("contract balance: %s.%s%s", address(delegation).balance); - Console.log("staker balance: %s.%s%s", staker[i-1].balance); + Console.log("staker balance: %s.%s%s", stakers[i-1].balance); Console.log("staker rewards: %s.%s%s", delegation.rewards()); Console.log("staker should have received: %s.%s%s", rewards); vm.stopPrank();