Skip to content

Commit

Permalink
OZ M-03 Unit tests (#100)
Browse files Browse the repository at this point in the history
* forge install: forge-std

* forge install: prb-math

* OZ M-03 Unit tests
  • Loading branch information
DanielVF authored Jun 7, 2022
1 parent b0f4b04 commit a8a9d36
Show file tree
Hide file tree
Showing 11 changed files with 822 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
[submodule "lib/openzeppelin-contracts-upgradeable"]
path = lib/openzeppelin-contracts-upgradeable
url = https://github.com/openzeppelin/openzeppelin-contracts-upgradeable
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/brockelmore/forge-std
[submodule "lib/prb-math"]
path = lib/prb-math
url = https://github.com/paulrberg/prb-math
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ brownie pm install OpenZeppelin/[email protected]
yarn install
```

## Running contract tests
## Running contract tests (brownie)

```bash
cd contracts
Expand All @@ -42,6 +42,15 @@ brownie test --network hardhat

_If this command reverts with an error it may be an incompatability with python 3.10. Try python 3.9 instead ([pyenv](https://github.com/pyenv/pyenv) is a good solution for managing multiple python versions)._

## Running contract tests (forge)

The OGV staking contracts use forge for tests.

```bash
forge install
forge test
```

## Running a local node

Copy `dev.env` to `.env` and fill out the `PROVIDER_URL`
Expand Down
2 changes: 1 addition & 1 deletion contracts/OgvStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ contract OgvStaking is ERC20Votes {
oldAmount,
duration
);
require(newEnd > oldEnd, "New lockup must be longer");
require(newEnd > oldEnd, "Staking: New lockup must be longer");
lockup.end = uint128(newEnd);
lockup.points = newPoints;
lockups[msg.sender][lockupId] = lockup;
Expand Down
12 changes: 12 additions & 0 deletions contracts/tests/MockOGV.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ERC20 } from "OpenZeppelin/[email protected]/contracts/token/ERC20/ERC20.sol";

contract MockOgv is ERC20 {

constructor() ERC20("OGV","OGV") {

}

function mint(address to, uint256 amount) external {
_mint(to, amount);
}
}
6 changes: 5 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[default]
src = 'contracts'
test = 'tests'
remappings = [
"OpenZeppelin/openzeppelin-contracts@02fcc75bb7f35376c22def91b0fb9bc7a50b9458/=./lib/openzeppelin-contracts",
"OpenZeppelin/openzeppelin-contracts-upgradeable@a16f26a063cd018c4c986832c3df332a131f53b9/=./lib/openzeppelin-contracts-upgradeable"
"OpenZeppelin/openzeppelin-contracts-upgradeable@a16f26a063cd018c4c986832c3df332a131f53b9/=./lib/openzeppelin-contracts-upgradeable",
"OpenZeppelin/[email protected]/=./lib/openzeppelin-contracts",
"OpenZeppelin/[email protected]/=./lib/openzeppelin-contracts-upgradeable",
"paulrberg/[email protected]/=./lib/prb-math"
]
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 564510
1 change: 1 addition & 0 deletions lib/prb-math
Submodule prb-math added at 701b1b
107 changes: 107 additions & 0 deletions tests/staking/DelegationTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: Unlicense
pragma solidity 0.8.10;

import "forge-std/Test.sol";
import "../../contracts/OgvStaking.sol";
import "../../contracts/RewardsSource.sol";
import "../../contracts/tests/MockOgv.sol";

//
// Sanity test of OpenZeppelin's voting and deletegation.
//

contract DelegationTest is Test {
MockOgv ogv;
OgvStaking staking;
RewardsSource source;

address oak = address(0x42);
address aspen = address(0x43);
address taz = address(0x44);
address team = address(0x50);

uint256 constant EPOCH = 1 days;

uint256 POINTS = 0;

function setUp() public {
vm.startPrank(team);
ogv = new MockOgv();
source = new RewardsSource(address(ogv));
staking = new OgvStaking(address(ogv), EPOCH, address(source));
source.setRewardsTarget(address(staking));
vm.stopPrank();

ogv.mint(oak, 1000 ether);
ogv.mint(aspen, 1000 ether);
ogv.mint(taz, 100000000 ether);

vm.prank(oak);
ogv.approve(address(staking), 1e70);
vm.prank(aspen);
ogv.approve(address(staking), 1e70);
vm.prank(taz);
ogv.approve(address(staking), 1e70);

vm.prank(oak);
staking.stake(1 ether, 100 days);
vm.prank(aspen);
staking.stake(2 ether, 100 days);

POINTS = staking.balanceOf(oak);
}

function testSelfDelegate() external {
vm.roll(1);
assertEq(staking.getVotes(oak), 0, "zero until delegated");
assertEq(staking.getPastVotes(oak, block.number - 1), 0);
assertEq(staking.delegates(oak), address(0));

vm.roll(2);
vm.prank(oak);
staking.delegate(oak);

assertEq(
staking.getVotes(oak),
1 * POINTS,
"can vote after delegation"
);
assertEq(staking.getPastVotes(oak, block.number - 1), 0);
assertEq(staking.delegates(oak), oak, "self is delegate");

vm.roll(3);
vm.prank(oak);
staking.delegate(address(0));

assertEq(staking.getVotes(oak), 0, "zero after delegation removed");
assertEq(staking.getPastVotes(oak, block.number - 1), 1 * POINTS);
assertEq(staking.delegates(oak), address(0));
}

function testDelegate() external {
vm.roll(1);
assertEq(staking.getVotes(oak), 0, "zero until delegated");
assertEq(staking.getPastVotes(oak, block.number - 1), 0);
assertEq(staking.delegates(oak), address(0));

vm.roll(2);
vm.prank(oak);
staking.delegate(aspen);

assertEq(
staking.getVotes(aspen),
1 * POINTS,
"can vote after delegation"
);
assertEq(staking.getPastVotes(aspen, block.number - 1), 0);
assertEq(staking.delegates(oak), aspen, "delegated");

vm.roll(3);
vm.prank(aspen);
staking.delegate(aspen); // Self delegate

assertEq(staking.getVotes(aspen), 3 * POINTS, "two users points");
assertEq(staking.getPastVotes(aspen, block.number - 1), 1 * POINTS);
assertEq(staking.delegates(aspen), aspen);
}
}
Loading

0 comments on commit a8a9d36

Please sign in to comment.