-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* forge install: forge-std * forge install: prb-math * OZ M-03 Unit tests
- Loading branch information
Showing
11 changed files
with
822 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ brownie pm install OpenZeppelin/[email protected] | |
yarn install | ||
``` | ||
|
||
## Running contract tests | ||
## Running contract tests (brownie) | ||
|
||
```bash | ||
cd contracts | ||
|
@@ -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` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.