Skip to content

Commit

Permalink
Clean tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brickpop committed Apr 19, 2024
1 parent 999cc52 commit 555a0ec
Show file tree
Hide file tree
Showing 2 changed files with 429 additions and 17 deletions.
272 changes: 268 additions & 4 deletions test/OptimisticTokenVotingPlugin.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import {DaoUnauthorized} from "@aragon/osx/core/utils/auth.sol";
import {ERC20VotesMock} from "./mocks/ERC20VotesMock.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {IERC165Upgradeable} from "@openzeppelin/contracts-upgradeable/utils/introspection/IERC165Upgradeable.sol";
import {IVotesUpgradeable} from "@openzeppelin/contracts-upgradeable/governance/utils/IVotesUpgradeable.sol";

contract OptimisticTokenVotingPluginTest is Test {
address immutable daoBase = address(new DAO());
address immutable pluginBase = address(new OptimisticTokenVotingPlugin());
address votingTokenBase = address(new ERC20VotesMock());
address immutable votingTokenBase = address(new ERC20VotesMock());

DAO public dao;
OptimisticTokenVotingPlugin public plugin;
Expand Down Expand Up @@ -54,8 +53,6 @@ contract OptimisticTokenVotingPluginTest is Test {

function setUp() public {
vm.startPrank(alice);
vm.deal(alice, 1000 ether);
vm.deal(bob, 1000 ether);

// Deploy a DAO with Alice as root
dao = DAO(
Expand Down Expand Up @@ -148,6 +145,22 @@ contract OptimisticTokenVotingPluginTest is Test {
)
)
);
assertEq(
plugin.totalVotingPower(block.number - 1),
10 ether,
"Incorrect token supply"
);
assertEq(
plugin.minVetoRatio(),
uint32(RATIO_BASE / 10),
"Incorrect minVetoRatio"
);
assertEq(plugin.minDuration(), 10 days, "Incorrect minDuration");
assertEq(
plugin.minProposerVotingPower(),
0,
"Incorrect minProposerVotingPower"
);

// Different minVetoRatio
settings.minVetoRatio = uint32(RATIO_BASE / 5);
Expand All @@ -162,6 +175,11 @@ contract OptimisticTokenVotingPluginTest is Test {
)
)
);
assertEq(
plugin.minVetoRatio(),
uint32(RATIO_BASE / 5),
"Incorrect minVetoRatio"
);

// Different minDuration
settings.minDuration = 25 days;
Expand All @@ -176,6 +194,7 @@ contract OptimisticTokenVotingPluginTest is Test {
)
)
);
assertEq(plugin.minDuration(), 25 days, "Incorrect minDuration");

// A token with 10 eth supply
votingToken = ERC20VotesMock(
Expand All @@ -198,6 +217,11 @@ contract OptimisticTokenVotingPluginTest is Test {
)
)
);
assertEq(
plugin.totalVotingPower(block.number - 1),
10 ether,
"Incorrect token supply"
);

// Different minProposerVotingPower
settings.minProposerVotingPower = 1 ether;
Expand All @@ -212,6 +236,11 @@ contract OptimisticTokenVotingPluginTest is Test {
)
)
);
assertEq(
plugin.minProposerVotingPower(),
1 ether,
"Incorrect minProposerVotingPower"
);
}

function test_InitializeEmitsEvent() public {
Expand Down Expand Up @@ -286,6 +315,13 @@ contract OptimisticTokenVotingPluginTest is Test {
supported = plugin.supportsInterface(bytes4(0xffffffff));
assertEq(supported, false, "Should not support any other interface");

// Some fuzzing values are expected to be true
if (_randomInterfaceId == bytes4(0x52d1902d)) {
supported = plugin.supportsInterface(_randomInterfaceId);
assertEq(supported, true, "proxiableUUID should be supported");
return;
}

supported = plugin.supportsInterface(_randomInterfaceId);
assertEq(supported, false, "Should not support any other interface");
}
Expand Down Expand Up @@ -340,7 +376,68 @@ contract OptimisticTokenVotingPluginTest is Test {
);
}

function test_TotalVotingPowerReturnsTheRightSupply() public {
assertEq(
plugin.totalVotingPower(block.number - 1),
votingToken.getPastTotalSupply(block.number - 1),
"Incorrect total voting power"
);
assertEq(
plugin.totalVotingPower(block.number - 1),
10 ether,
"Incorrect total voting power"
);

// New token
votingToken = ERC20VotesMock(
createProxyAndCall(
address(votingTokenBase),
abi.encodeWithSelector(ERC20VotesMock.initialize.selector)
)
);
votingToken.mint(alice, 15 ether);
vm.roll(block.number + 1);

// Deploy a new plugin instance
OptimisticTokenVotingPlugin.OptimisticGovernanceSettings
memory settings = OptimisticTokenVotingPlugin
.OptimisticGovernanceSettings({
minVetoRatio: uint32(RATIO_BASE / 10),
minDuration: 10 days,
minProposerVotingPower: 0
});

plugin = OptimisticTokenVotingPlugin(
createProxyAndCall(
address(pluginBase),
abi.encodeWithSelector(
OptimisticTokenVotingPlugin.initialize.selector,
dao,
settings,
votingToken
)
)
);

assertEq(
plugin.totalVotingPower(block.number - 1),
votingToken.getPastTotalSupply(block.number - 1),
"Incorrect total voting power"
);
assertEq(
plugin.totalVotingPower(block.number - 1),
15 ether,
"Incorrect total voting power"
);
}

function test_MinVetoRatioReturnsTheRightValue() public {
assertEq(
plugin.minVetoRatio(),
uint32(RATIO_BASE / 10),
"Incorrect minVetoRatio"
);

// New plugin instance
OptimisticTokenVotingPlugin.OptimisticGovernanceSettings
memory settings = OptimisticTokenVotingPlugin
Expand All @@ -361,9 +458,17 @@ contract OptimisticTokenVotingPluginTest is Test {
)
)
);

assertEq(
plugin.minVetoRatio(),
uint32(RATIO_BASE / 5),
"Incorrect minVetoRatio"
);
}

function test_MinDurationReturnsTheRightValue() public {
assertEq(plugin.minDuration(), 10 days, "Incorrect minDuration");

// New plugin instance
OptimisticTokenVotingPlugin.OptimisticGovernanceSettings
memory settings = OptimisticTokenVotingPlugin
Expand All @@ -384,6 +489,103 @@ contract OptimisticTokenVotingPluginTest is Test {
)
)
);

assertEq(plugin.minDuration(), 25 days, "Incorrect minDuration");
}

function test_MinProposerVotingPowerReturnsTheRightValue() public {
assertEq(
plugin.minProposerVotingPower(),
0,
"Incorrect minProposerVotingPower"
);

// New token
votingToken = ERC20VotesMock(
createProxyAndCall(
address(votingTokenBase),
abi.encodeWithSelector(ERC20VotesMock.initialize.selector)
)
);
votingToken.mint(alice, 10 ether);
vm.roll(block.number + 1);

// Deploy a new plugin instance
OptimisticTokenVotingPlugin.OptimisticGovernanceSettings
memory settings = OptimisticTokenVotingPlugin
.OptimisticGovernanceSettings({
minVetoRatio: uint32(RATIO_BASE / 10),
minDuration: 10 days,
minProposerVotingPower: 1 ether
});

plugin = OptimisticTokenVotingPlugin(
createProxyAndCall(
address(pluginBase),
abi.encodeWithSelector(
OptimisticTokenVotingPlugin.initialize.selector,
dao,
settings,
votingToken
)
)
);

assertEq(
plugin.minProposerVotingPower(),
1 ether,
"Incorrect minProposerVotingPower"
);
}

function test_TokenHoldersAreMembers() public {
assertEq(plugin.isMember(alice), true, "Alice should not be a member");
assertEq(plugin.isMember(bob), false, "Bob should not be a member");
assertEq(
plugin.isMember(randomWallet),
false,
"Random wallet should not be a member"
);

// New token
votingToken = ERC20VotesMock(
createProxyAndCall(
address(votingTokenBase),
abi.encodeWithSelector(ERC20VotesMock.initialize.selector)
)
);
votingToken.mint(alice, 10 ether);
votingToken.mint(bob, 5 ether);
vm.roll(block.number + 1);

// Deploy a new plugin instance
OptimisticTokenVotingPlugin.OptimisticGovernanceSettings
memory settings = OptimisticTokenVotingPlugin
.OptimisticGovernanceSettings({
minVetoRatio: uint32(RATIO_BASE / 10),
minDuration: 10 days,
minProposerVotingPower: 1 ether
});

plugin = OptimisticTokenVotingPlugin(
createProxyAndCall(
address(pluginBase),
abi.encodeWithSelector(
OptimisticTokenVotingPlugin.initialize.selector,
dao,
settings,
votingToken
)
)
);

assertEq(plugin.isMember(alice), true, "Alice should be a member");
assertEq(plugin.isMember(bob), true, "Bob should be a member");
assertEq(
plugin.isMember(randomWallet),
false,
"Random wallet should not be a member"
);
}

// Create proposal
Expand Down Expand Up @@ -675,6 +877,68 @@ contract OptimisticTokenVotingPluginTest is Test {
plugin.createProposal("", actions, 0, 0, 0);
}

function test_GetProposalReturnsTheRightValues() public {
vm.warp(500);
uint32 startDate = 600;
uint32 endDate = startDate + 15 days;

IDAO.Action[] memory actions = new IDAO.Action[](1);
actions[0].to = address(plugin);
actions[0].value = 1 wei;
actions[0].data = abi.encodeWithSelector(
OptimisticTokenVotingPlugin.totalVotingPower.selector,
0
);
uint256 failSafeBitmap = 1;

uint256 proposalId = plugin.createProposal(
"ipfs://",
actions,
failSafeBitmap,
startDate,
endDate
);

(bool open0, , , , , ) = plugin.getProposal(proposalId);
assertEq(open0, false, "The proposal should not be open");

// Move on
vm.warp(startDate);

(
bool open,
bool executed,
OptimisticTokenVotingPlugin.ProposalParameters memory parameters,
uint256 vetoTally,
IDAO.Action[] memory actualActions,
uint256 actualFailSafeBitmap
) = plugin.getProposal(proposalId);

assertEq(open, true, "The proposal should be open");
assertEq(executed, false, "The proposal should not be executed");
assertEq(parameters.startDate, startDate, "Incorrect startDate");
assertEq(parameters.endDate, endDate, "Incorrect endDate");
assertEq(parameters.snapshotBlock, 1, "Incorrect snapshotBlock");
assertEq(
parameters.minVetoVotingPower,
plugin.totalVotingPower(block.number - 1) / 10,
"Incorrect minVetoVotingPower"
);
assertEq(vetoTally, 0, "The tally should be zero");
assertEq(actualActions.length, 1, "Actions should have one item");
assertEq(
actualFailSafeBitmap,
failSafeBitmap,
"Incorrect failsafe bitmap"
);

// Move on
vm.warp(endDate);

(bool open1, , , , , ) = plugin.getProposal(proposalId);
assertEq(open1, false, "The proposal should not be open anymore");
}

// Can Veto
function test_CanVetoReturnsFalseWhenAProposalDoesntExist() public {
vm.roll(10);
Expand Down
Loading

0 comments on commit 555a0ec

Please sign in to comment.