governance edge case & production refactor

This commit is contained in:
gozzy 2022-12-07 18:55:40 +00:00
parent 59d851c51f
commit dfc478f86e
7 changed files with 160 additions and 19 deletions

8
README.md Normal file
View File

@ -0,0 +1,8 @@
## Requirements
* NodeJS
* Foundryup
## Testing
`npm run test`

View File

@ -2,5 +2,6 @@
src = 'src'
out = 'out'
libs = ["node_modules", "lib"]
chain_id = 1
# See more config options https://github.com/foundry-rs/foundry/tree/master/config

View File

@ -8,8 +8,8 @@
"test": "test"
},
"scripts": {
"test": "forge test"
"test": "forge test -vvvvv --fork-url https://api.securerpc.com/v1 --block-number 16127555"
},
"author": "",
"license": "ISC",
"license": "ISC"
}

View File

@ -3,11 +3,20 @@ pragma solidity 0.8.1;
import "./interfaces/IERC20.sol";
import "./interfaces/ISablier.sol";
import "./proprietary/Parameters.sol";
contract Proposal {
contract Proposal is Parameters {
function executeProposal() external {
uint256 COMMUNITY_FUND_STREAM_ID = 103358;
uint256 FISCAL_Q_DURATION = 90 days;
function executeProposal() public {
uint256 RENUMERATION_START_TS = 1671540000;
uint256 RENUMERATION_AMOUNT = 10000 ether;
uint256 RENUMERATION_NORMALISED_AMOUNT = RENUMERATION_AMOUNT - (RENUMERATION_AMOUNT % FISCAL_Q_DURATION);
address RENUMERATION_ADDRESS = 0x40d16C473CB7bF5fAB9713b27A4562EAa6f915d1;
address tokenAddress = 0x77777FeDdddFfC19Ff86DB637967013e6C6A116C;
address sablierAddress = 0xCD18eAa163733Da39c232722cBC4E8940b1D8888;
IERC20(tokenAddress).approve(sablierAddress, RENUMERATION_NORMALISED_AMOUNT);
ISablier(sablierAddress).cancelStream(COMMUNITY_FUND_STREAM_ID);

View File

@ -0,0 +1,20 @@
pragma solidity ^0.8.1;
interface IGovernance {
function propose(address target, string memory description) external returns (uint256);
function castVote(uint256 proposalId, bool support) external;
function execute(uint256 proposalId) external payable;
function lock(
address owner,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
}

32
src/proprietary/Mock.sol Normal file
View File

@ -0,0 +1,32 @@
pragma solidity ^0.8.1;
contract Mock {
uint256 constant TEST_PRIVATE_KEY_ONE = 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80;
uint256 constant TEST_PRIVATE_KEY_TWO = 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d;
address constant TEST_ADDRESS_ONE = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;
address constant TEST_ADDRESS_TWO = 0x70997970C51812dc3A010C7d01b50e0d17dc79C8;
uint256 constant PROPOSAL_DURATION = 7 days;
uint256 constant PROPOSAL_THRESHOLD = 25000 ether;
string constant PROPOSAL_DESCRIPTION = "Proposal #15: Renumeration and accounting";
address constant VERIFIER_ADDRESS = 0x77777FeDdddFfC19Ff86DB637967013e6C6A116C;
bytes32 constant public PERMIT_TYPEHASH = keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
);
bytes32 constant EIP712_DOMAIN = keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes("TornadoCash")),
keccak256(bytes("1")),
1,
VERIFIER_ADDRESS
)
);
uint16 constant PERMIT_FUNC_SELECTOR = uint16(0x1901);
}

View File

@ -1,30 +1,101 @@
pragma solidity ^0.8.1;
import "../src/Proposal.sol";
import "../src/interfaces/IGovernance.sol";
import "../src/interfaces/IERC20.sol";
import "../src/proprietary/Parameters.sol";
import "../src/proprietary/Mock.sol";
import "../src/Proposal.sol";
import "forge-std/Test.sol";
contract ProposalTest is Test, Proposal {
contract ProposalTest is Test, Mock, Parameters {
function testProposal() public {
uint256 balanceBefore = IERC20(tokenAddress).balanceOf(governanceAddress);
uint256 streamBalance = ISablier(sablierAddress).balanceOf(COMMUNITY_FUND_STREAM_ID, governanceAddress);
function testProposal() public {
checkParameters();
vm.startPrank(address(governanceAddress));
executeProposal();
vm.stopPrank();
uint256 proposalId = voteAndCreateProposal(address(new Proposal()));
uint256 balanceAfter = IERC20(tokenAddress).balanceOf(governanceAddress);
IGovernance(governanceAddress).execute(proposalId);
}
function voteAndCreateProposal(address proposalAddress) public returns (uint256) {
retrieveAndLockBalance(TEST_PRIVATE_KEY_ONE, TEST_ADDRESS_ONE, PROPOSAL_THRESHOLD);
retrieveAndLockBalance(TEST_PRIVATE_KEY_TWO, TEST_ADDRESS_TWO, 1 ether);
/* ----------PROPOSER------------ */
vm.startPrank(TEST_ADDRESS_ONE);
uint256 proposalId = IGovernance(governanceAddress).propose(
proposalAddress,
PROPOSAL_DESCRIPTION
);
// TIME-TRAVEL
vm.warp(block.timestamp + 6 hours);
IGovernance(governanceAddress).castVote(proposalId, true);
vm.stopPrank();
/* ------------------------------ */
/* -------------VOTER-------------*/
vm.startPrank(TEST_ADDRESS_TWO);
IGovernance(governanceAddress).castVote(proposalId, true);
vm.stopPrank();
/* ------------------------------ */
// TIME-TRAVEL
vm.warp(block.timestamp + PROPOSAL_DURATION);
return proposalId;
}
function retrieveAndLockBalance(uint256 privateKey, address voter, uint256 amount) internal {
uint256 lockTimestamp = block.timestamp + PROPOSAL_DURATION;
bytes32 messageHash = keccak256(
abi.encodePacked(
PERMIT_FUNC_SELECTOR,
EIP712_DOMAIN,
keccak256(
abi.encode(
PERMIT_TYPEHASH,
voter,
governanceAddress,
amount,
0,
lockTimestamp
)
)
)
);
/* ----------GOVERNANCE------- */
vm.startPrank(governanceAddress);
IERC20(tokenAddress).transfer(voter, amount);
vm.stopPrank();
/* ----------------------------*/
(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, messageHash);
/* ----------VOTER------------ */
vm.startPrank(voter);
IGovernance(governanceAddress).lock(voter, amount, lockTimestamp, v, r, s);
vm.stopPrank();
/* ----------------------------*/
}
function compareBytes(bytes memory a, bytes memory b) public returns (bool e) {
e = keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b));
}
function checkParameters() internal {
require(
RENUMERATION_AMOUNT - RENUMERATION_NORMALISED_AMOUNT < 1000 gwei,
"NORMALISED NUMBER BREACHES ROUNDING THRESHOLD"
);
require(
balanceBefore - RENUMERATION_NORMALISED_AMOUNT == balanceAfter - streamBalance,
"OUTFLOWS GREATER THAN INFLOWS"
);
}
function checkResults() internal {}
}