* Install open zeppelin contracts * Init foundry in governance * Add wrapped ZRX token * Add governance contracts testing to CI * Set optimizer runs to default * Upgrade to patched version of openzeppelin/contracts * Test stakingakng / unwrapping ZRX * Init npm package * Lint fix, removing lib from gitignore * Add openzeppelin contracts git submodule for foundry * Add vanilla governor contract * Fix reference paths to imported packages * Temporarily switch to using a mocked version of ZRX * Ignore foundry's lib in link checker * Fix a conflict in gitignore between forge lib adn built lib * Upload governance code coverage report to coveralls * Flesh out test scenarios for wrapping/unwrapping * Add basic ERC20 name and symbol tests * Wire in basic timelock controller and governor test setup * Test basic governor properties * Add basic voting power delegation tests * Add proposal execution happy path test * Split ERC20Votes logic between wrapped token and ZeroExVotes contracts * Exclude BaseTest from coverage in coveralls * Add protocol specific governor with produciton governance settings * Add a dedicated instance for the treasury governor This is currently using the default 1 token 1 vote mechanism but will be migrated * Add test for updating governance settings for voting delay, voting period and proposal threshold * Create seperate timelock contract instance for treasury and protocol * Test updating the timlock min delay * Set timelock delay to 2 days for protocol and 1 sec for treasury * Remove timelock from treasury governor * Refactor _checkpointsLookup to return entire Checkpoint instad of just number of votes * Update the totalSupply checkpoints updating logic * Quadratic voting power transfers and delegations * Fix workflow yaml * Initialise ZeroExVotes behind a ERC1967Proxy Test it cannot be reinitialised * Remove obsoleted console.logs from test * Storage pack Checkpoint enum * Remove keeping track of total balances for voting * Switch to using the foundry artifact in test * Fix rebase issue * Add timelock control over the treasury governor * Add test for wrapped token transfer * Emit separate events for changing linear and quadratic voting power * Add the ability to cancel a proposal * Limit the governors' cancel function to security council only * Eject security council after a proposal is cancelled * Add ability for governance to set the security council * Merge the governors test suites into one reusable set of tests * Add an empty test function to base test contract to remove it from coverage reports. Fudge but no other way to ignore it in report * Security council can rollback protocol upgrades * Upgrade to solidity 0.8.19 * Move IZeroExGovernor to src * Abstract Security council interface into its own * Emit events when assigning and ejecting the security council * Use a cast to bytes4 instead of LibBytes Co-authored-by: duncancmt <1207590+duncancmt@users.noreply.github.com> * Writing total supply checkpoints and setup of quorum percentage of quadratic total supply for treasure governor * Add test for transferring tokens when delegating * Rename IZeroExSecurityCouncil to ISecurityCouncil * Add security council restrictions to governors * Remove obsolete overflow check * Improve test coverage * Upgrade open-zeppelin contracts to 4.8.2 * Test delegation by signature * Test non security council requests to rollback protocol changes cannot be executed * Better revert messages * Test correct interfaces are supported * Remove obsoleted funciton * Further test delegation by signature scenario * Split the delegation functionality tests * Add test for initialisation of voting contract * Add test for reading checkpoints * Update code comments * Fix compilation warnings * Run smt checker * Add checkpoint tests * Rename parameter in moveEntireVotingPower to match the one in movePartialVotingPower * Switch moveEntireVotingPower to a more generic moveVotingPower implementation as in the open-zeppelin contracts * Install foundry earlier in CI * Switch movePartialVotingPower to the generic moveVotingPower implementation * Write totalSupplyCheckpoints via the generic _writeCheckpoint * Add threshold for quadratic voting power * Remove autoinserted code by OZ * Add openzeppelin/contracts-upgradable * Add initializable base to Voting contract * Fix terminogy error in natspec * Fix code comment * Remove obsoleted overrides and add a missing modifier to moveVotingPower * Remove amount check Co-authored-by: duncancmt <1207590+duncancmt@users.noreply.github.com> * Fix a calculation error and clean tests * Update thresholds for treasury governor * Fix testShouldNotBeAbleToDelegateWithSignatureAfterExpiry * Update from @duncancmt without "memory-safe" the IR optimizer produces significantly worse code and it disables the stack limit evader Co-authored-by: duncancmt <1207590+duncancmt@users.noreply.github.com> * Add onlyProxy to initializer * Fix quadratic voting weight base * Rename voting parameter for clarity * Make addresses immutable (#680) * Make addresses immutable * Fix linting issues --------- Co-authored-by: elenadimitrova <elena@arenabg.com> * Prevent griefing by a malicious ZeroExVotes upgrade (#681) * Gas optimization * Minimal change to prevent malicious ZeroExVotes from griefing * Add demonstration of griefing upgrade * Fix rebase issues with tests * Fix prettier issues * Add checks to test --------- Co-authored-by: elenadimitrova <elena@arenabg.com> * Rename SecurityCouncil contract * Add timestamp to delegator balance updates * Make quadraticThreshold `immutable` for gas efficiency * Remove the logic for ejecting security council * Switch balance timestamp to be a block number * Test votes migration for adding a new vote weight mechanism (#674) * Add Emacs files to .gitignore * Make some functions unproected to demonstrate a migration * Add example (broken) migration * Add migration test for voting logic * Try to simplify tests * Fix compilation errors * Fix underflow test with new logic * Flesh out migration test for voting * Replace cube root library * Fix stack too deep in coverage --------- Co-authored-by: elenadimitrova <elena@arenabg.com> * Change test case to testFail * Update contracts/governance/test/ZeroExVotesMigration.sol Co-authored-by: duncancmt <1207590+duncancmt@users.noreply.github.com> --------- Co-authored-by: duncancmt <1207590+duncancmt@users.noreply.github.com> Co-authored-by: Duncan Townsend <git@duncancmt.com>
315 lines
11 KiB
Solidity
315 lines
11 KiB
Solidity
// SPDX-License-Identifier: Apache-2.0
|
|
/*
|
|
|
|
Copyright 2023 ZeroEx Intl.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
|
|
*/
|
|
pragma solidity ^0.8.19;
|
|
|
|
import "./BaseTest.t.sol";
|
|
import "../src/ZRXWrappedToken.sol";
|
|
import "@openzeppelin/token/ERC20/ERC20.sol";
|
|
|
|
contract ZRXWrappedTokenTest is BaseTest {
|
|
IERC20 private token;
|
|
ZRXWrappedToken private wToken;
|
|
ZeroExVotes private votes;
|
|
|
|
function setUp() public {
|
|
(token, wToken, votes, , , , ) = setupGovernance();
|
|
vm.startPrank(account1);
|
|
token.transfer(account2, 100e18);
|
|
token.transfer(account3, 200e18);
|
|
vm.stopPrank();
|
|
}
|
|
|
|
function testShouldReturnCorrectSymbol() public {
|
|
string memory wZRXSymbol = wToken.symbol();
|
|
assertEq(wZRXSymbol, "wZRX");
|
|
}
|
|
|
|
function testShouldReturnCorrectName() public {
|
|
string memory wZRXName = wToken.name();
|
|
assertEq(wZRXName, "Wrapped ZRX");
|
|
}
|
|
|
|
function testShouldReturnCorrectNumberOfDecimals() public {
|
|
uint8 wZRXDecimals = wToken.decimals();
|
|
assertEq(wZRXDecimals, 18);
|
|
}
|
|
|
|
function testShouldBeAbleToWrapZRX() public {
|
|
vm.startPrank(account2);
|
|
|
|
// Approve the wrapped token and deposit 1e18 ZRX
|
|
token.approve(address(wToken), 1e18);
|
|
wToken.depositFor(account2, 1e18);
|
|
|
|
// Check the token balances even out
|
|
uint256 wTokenBalance = wToken.balanceOf(account2);
|
|
assertEq(wTokenBalance, 1e18);
|
|
uint256 tokenBalance = token.balanceOf(account2);
|
|
assertEq(tokenBalance, 100e18 - wTokenBalance);
|
|
}
|
|
|
|
function testShouldBeAbleToUnwrapToZRX() public {
|
|
vm.startPrank(account2);
|
|
|
|
// Approve the wrapped token and deposit 1e18 ZRX
|
|
token.approve(address(wToken), 1e18);
|
|
wToken.depositFor(account2, 1e18);
|
|
|
|
// Withdraw 1e6 wZRX back to ZRX to own account
|
|
wToken.withdrawTo(account2, 1e6);
|
|
|
|
// Check token balances even out
|
|
uint256 wTokenBalance = wToken.balanceOf(account2);
|
|
assertEq(wTokenBalance, 1e18 - 1e6);
|
|
uint256 tokenBalance = token.balanceOf(account2);
|
|
assertEq(tokenBalance, 100e18 - wTokenBalance);
|
|
}
|
|
|
|
function testShouldBeAbleToUnwrapToZRXToAnotherAccount() public {
|
|
vm.startPrank(account2);
|
|
|
|
// Approve the wrapped token and deposit 1e18 ZRX
|
|
token.approve(address(wToken), 1e18);
|
|
wToken.depositFor(account2, 1e18);
|
|
|
|
// Withdraw 1e7 wZRX back to ZRX to account4 (which owns no tokens to start with)
|
|
wToken.withdrawTo(account4, 1e7);
|
|
|
|
// Check token balances even out
|
|
uint256 wTokenBalance2 = wToken.balanceOf(account2);
|
|
assertEq(wTokenBalance2, 1e18 - 1e7);
|
|
|
|
uint256 tokenBalance4 = token.balanceOf(account4);
|
|
assertEq(tokenBalance4, 1e7);
|
|
|
|
uint256 tokenBalance2 = token.balanceOf(account2);
|
|
assertEq(tokenBalance2, 100e18 - wTokenBalance2 - tokenBalance4);
|
|
}
|
|
|
|
function testWrappedZRXTotalsAreCorrect() public {
|
|
// Wrap 1e18 and check total supply is correct
|
|
vm.startPrank(account2);
|
|
token.approve(address(wToken), 1e18);
|
|
wToken.depositFor(account2, 1e18);
|
|
vm.stopPrank();
|
|
uint256 wTokenBalance = wToken.totalSupply();
|
|
assertEq(wTokenBalance, 1e18);
|
|
|
|
// Wrap 2e18 more and check total supply is correct
|
|
vm.startPrank(account3);
|
|
token.approve(address(wToken), 2e18);
|
|
wToken.depositFor(account3, 2e18);
|
|
vm.stopPrank();
|
|
wTokenBalance = wToken.totalSupply();
|
|
assertEq(wTokenBalance, 1e18 + 2e18);
|
|
|
|
// Unwrap 1e7 and check total supply is correct
|
|
vm.startPrank(account2);
|
|
wToken.withdrawTo(account2, 1e7);
|
|
vm.stopPrank();
|
|
wTokenBalance = wToken.totalSupply();
|
|
assertEq(wTokenBalance, 3e18 - 1e7);
|
|
|
|
// Unwrap 8e17 and check total supply is correct
|
|
vm.startPrank(account2);
|
|
wToken.withdrawTo(account2, 8e17);
|
|
vm.stopPrank();
|
|
wTokenBalance = wToken.totalSupply();
|
|
assertEq(wTokenBalance, 3e18 - 1e7 - 8e17);
|
|
|
|
// We are not keeping record of total balances so check they are zero
|
|
assertEq(votes.getPastTotalSupply(0), 0);
|
|
assertEq(votes.getPastQuadraticTotalSupply(0), 0);
|
|
}
|
|
|
|
function testWhenMintingFirstTimeForAccountTotalSupplyCheckpointsAreCorrect() public {
|
|
vm.startPrank(account2);
|
|
|
|
// Approve the wrapped token and deposit 1e18 ZRX
|
|
token.approve(address(wToken), 1e18);
|
|
vm.roll(2);
|
|
wToken.depositFor(account2, 1e18);
|
|
vm.roll(3);
|
|
|
|
// Check the totals are correct
|
|
uint256 totalSupplyVotes = votes.getPastTotalSupply(2);
|
|
uint256 totalSupplyQuadraticVotes = votes.getPastQuadraticTotalSupply(2);
|
|
assertEq(totalSupplyVotes, 1e18);
|
|
assertEq(totalSupplyQuadraticVotes, 1e18);
|
|
}
|
|
|
|
function testWhenMintingForAccountWithExistingBalanceTotalSupplyCheckpointsAreCorrect() public {
|
|
vm.startPrank(account2);
|
|
|
|
// Approve the wrapped token and deposit 1e18 ZRX
|
|
token.approve(address(wToken), 5e18);
|
|
wToken.depositFor(account2, 1e18);
|
|
|
|
vm.roll(2);
|
|
// Depost 3e18 more for the same account
|
|
wToken.depositFor(account2, 3e18);
|
|
vm.roll(3);
|
|
|
|
// Check the totals are correct
|
|
uint256 totalSupplyVotes = votes.getPastTotalSupply(2);
|
|
uint256 totalSupplyQuadraticVotes = votes.getPastQuadraticTotalSupply(2);
|
|
assertEq(totalSupplyVotes, 4e18);
|
|
assertEq(totalSupplyQuadraticVotes, 4e18);
|
|
}
|
|
|
|
function testWhenMintingForMultipleAccountsTotalSupplyCheckpointsAreCorrect() public {
|
|
// Deposit 1e18 ZRX by account2
|
|
vm.startPrank(account2);
|
|
token.approve(address(wToken), 5e18);
|
|
wToken.depositFor(account2, 1e18);
|
|
vm.stopPrank();
|
|
|
|
// Deposit 2e18 ZRX by account3
|
|
vm.startPrank(account3);
|
|
token.approve(address(wToken), 2e18);
|
|
wToken.depositFor(account3, 2e18);
|
|
vm.stopPrank();
|
|
|
|
// Deposit 4e18 ZRX by account2
|
|
vm.startPrank(account2);
|
|
vm.roll(2);
|
|
wToken.depositFor(account2, 4e18);
|
|
vm.stopPrank();
|
|
vm.roll(3);
|
|
|
|
// Check the totals are correct
|
|
uint256 totalSupplyVotes = votes.getPastTotalSupply(2);
|
|
uint256 totalSupplyQuadraticVotes = votes.getPastQuadraticTotalSupply(2);
|
|
assertEq(totalSupplyVotes, 7e18);
|
|
assertEq(totalSupplyQuadraticVotes, 5e18 + 2e18);
|
|
}
|
|
|
|
function testWhenBurningForMultipleAccountsTotalSupplyCheckpointsAreCorrect() public {
|
|
// Deposit 5e18 ZRX by account2
|
|
vm.startPrank(account2);
|
|
token.approve(address(wToken), 5e18);
|
|
wToken.depositFor(account2, 5e18);
|
|
vm.stopPrank();
|
|
|
|
// Deposit 2e18 ZRX by account3
|
|
vm.startPrank(account3);
|
|
token.approve(address(wToken), 2e18);
|
|
wToken.depositFor(account3, 2e18);
|
|
vm.stopPrank();
|
|
|
|
// Burn 4e18 ZRX by account2
|
|
vm.startPrank(account2);
|
|
vm.roll(2);
|
|
wToken.withdrawTo(account2, 4e18);
|
|
vm.stopPrank();
|
|
vm.roll(3);
|
|
|
|
// Check the totals are correct
|
|
uint256 totalSupplyVotes = votes.getPastTotalSupply(2);
|
|
uint256 totalSupplyQuadraticVotes = votes.getPastQuadraticTotalSupply(2);
|
|
assertEq(totalSupplyVotes, 3e18);
|
|
assertEq(totalSupplyQuadraticVotes, 1e18 + 2e18);
|
|
}
|
|
|
|
function testShouldBeAbleToTransferCorrectly() public {
|
|
assertEq(wToken.balanceOf(account4), 0);
|
|
|
|
vm.startPrank(account2);
|
|
token.approve(address(wToken), 1e18);
|
|
wToken.depositFor(account2, 1e18);
|
|
wToken.transfer(account4, 1e17);
|
|
vm.stopPrank();
|
|
|
|
assertEq(wToken.balanceOf(account4), 1e17);
|
|
}
|
|
|
|
function testShouldTransferVotingPowerWhenTransferringTokens() public {
|
|
// Account 2 wraps ZRX and delegates voting power to itself
|
|
vm.startPrank(account2);
|
|
token.approve(address(wToken), 10e18);
|
|
wToken.depositFor(account2, 10e18);
|
|
wToken.delegate(account2);
|
|
|
|
wToken.transfer(account3, 3e18);
|
|
|
|
assertEq(wToken.balanceOf(account2), 7e18);
|
|
assertEq(wToken.balanceOf(account3), 3e18);
|
|
|
|
assertEq(votes.getVotes(account2), 7e18);
|
|
assertEq(votes.getQuadraticVotes(account2), 7e18);
|
|
|
|
// Since account3 is not delegating to anyone, they should have no voting power
|
|
assertEq(votes.getVotes(account3), 0);
|
|
assertEq(votes.getQuadraticVotes(account3), 0);
|
|
}
|
|
|
|
function testShouldUpdateVotingPowerWhenDepositing() public {
|
|
// Account 2 wraps ZRX and delegates voting power to itself
|
|
vm.startPrank(account2);
|
|
token.approve(address(wToken), 10e18);
|
|
wToken.depositFor(account2, 7e18);
|
|
wToken.delegate(account2);
|
|
|
|
assertEq(votes.getVotes(account2), 7e18);
|
|
assertEq(votes.getQuadraticVotes(account2), 7e18);
|
|
|
|
wToken.depositFor(account2, 2e18);
|
|
assertEq(votes.getVotes(account2), 9e18);
|
|
assertEq(votes.getQuadraticVotes(account2), 9e18);
|
|
}
|
|
|
|
function testShouldUpdateVotingPowerWhenWithdrawing() public {
|
|
// Account 2 wraps ZRX and delegates voting power to itself
|
|
vm.startPrank(account2);
|
|
token.approve(address(wToken), 10e18);
|
|
wToken.depositFor(account2, 10e18);
|
|
wToken.delegate(account2);
|
|
|
|
assertEq(votes.getVotes(account2), 10e18);
|
|
assertEq(votes.getQuadraticVotes(account2), 10e18);
|
|
|
|
wToken.withdrawTo(account2, 2e18);
|
|
assertEq(votes.getVotes(account2), 8e18);
|
|
assertEq(votes.getQuadraticVotes(account2), 8e18);
|
|
}
|
|
|
|
function testShouldSetDelegateBalanceLastUpdatedOnTransfer() public {
|
|
ZRXWrappedToken.DelegateInfo memory account2DelegateInfo = wToken.delegateInfo(account2);
|
|
assertEq(account2DelegateInfo.delegate, address(0));
|
|
assertEq(account2DelegateInfo.balanceLastUpdated, 0);
|
|
|
|
// Account 2 wraps ZRX and delegates voting power to account3
|
|
vm.startPrank(account2);
|
|
token.approve(address(wToken), 10e18);
|
|
wToken.depositFor(account2, 10e18);
|
|
wToken.delegate(account3);
|
|
|
|
account2DelegateInfo = wToken.delegateInfo(account2);
|
|
assertEq(account2DelegateInfo.delegate, account3);
|
|
assertEq(account2DelegateInfo.balanceLastUpdated, 1); // Set to the block.number
|
|
|
|
vm.roll(3);
|
|
wToken.transfer(account3, 3e18);
|
|
|
|
account2DelegateInfo = wToken.delegateInfo(account2);
|
|
assertEq(account2DelegateInfo.delegate, account3);
|
|
assertEq(account2DelegateInfo.balanceLastUpdated, 3);
|
|
}
|
|
}
|