Skip to content

Commit

Permalink
chore: split gov into multiple addrs
Browse files Browse the repository at this point in the history
  • Loading branch information
anticlimactic committed Mar 23, 2022
1 parent a281e35 commit 6beb9b5
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 39 deletions.
10 changes: 10 additions & 0 deletions src/interfaces/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ interface IVault is IERC20 {
*/
function debtOutstanding() external view returns (uint256);

function debtOutstanding(address _strategy) external view returns (uint256);

/**
* View how much the Vault expect this Strategy to return at the current
* block, based on its present performance (since its last report). Can be
Expand Down Expand Up @@ -136,9 +138,17 @@ interface IVault is IERC20 {

function setEmergencyShutdown(bool active) external;

function setManagementFee(uint256 fee) external;

function updateStrategyDebtRatio(address strategy, uint256 debtRatio)
external;

function withdraw(
uint256 maxShare,
address recipient,
uint256 maxLoss
) external;

/**
* View the governance address of the Vault to assert privileged functions
* can only be called by governance. The Strategy serves the Vault, so it
Expand Down
6 changes: 5 additions & 1 deletion src/test/StrategyMigration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ contract StrategyMigrationTest is StrategyFixture {
// Use another copy of the strategy to simmulate the migration
// Show that nothing is lost.
function testMigration(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

// Deposit to the vault and harvest
vm_std_cheats.prank(user);
want.approve(address(vault), _amount);
vm_std_cheats.prank(user);
vault.deposit(_amount);
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();
assertEq(strategy.estimatedTotalAssets(), _amount);

// Migrate to a new strategy
vm_std_cheats.prank(strategist);
address newStrategyAddr = deployStrategy(address(vault));
vm_std_cheats.prank(gov);
vault.migrateStrategy(address(strategy), newStrategyAddr);
assertEq(Strategy(newStrategyAddr).estimatedTotalAssets(), _amount);
}
Expand Down
50 changes: 40 additions & 10 deletions src/test/StrategyOperation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ contract StrategyOperationsTest is StrategyFixture {

/// Test Operations
function testStrategyOperation(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

uint256 balanceBefore = want.balanceOf(address(user));
vm_std_cheats.prank(user);
Expand All @@ -38,10 +40,11 @@ contract StrategyOperationsTest is StrategyFixture {

// Note: need to check if this is equivalent to chain.sleep in brownie
skip(60 * 3); // skip 3 minutes
// harvest
vm_std_cheats.prank(strategist);
strategy.harvest();
assertEq(strategy.estimatedTotalAssets(), _amount);
// tend
vm_std_cheats.prank(strategist);
strategy.tend();

vm_std_cheats.prank(user);
Expand All @@ -51,26 +54,33 @@ contract StrategyOperationsTest is StrategyFixture {
}

function testEmergencyExit(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

// Deposit to the vault
vm_std_cheats.prank(user);
want.approve(address(vault), _amount);
vm_std_cheats.prank(user);
vault.deposit(_amount);
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();
assertEq(strategy.estimatedTotalAssets(), _amount);

// set emergency and exit
vm_std_cheats.prank(gov);
strategy.setEmergencyExit();
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();
assertLt(strategy.estimatedTotalAssets(), _amount);
}

function testProfitableHarvest(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

// Deposit to the vault
vm_std_cheats.prank(user);
Expand All @@ -81,13 +91,15 @@ contract StrategyOperationsTest is StrategyFixture {

// Harvest 1: Send funds through the strategy
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();
assertEq(strategy.estimatedTotalAssets(), _amount);

// TODO: Add some code before harvest #2 to simulate earning yield

// Harvest 2: Realize profit
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();
skip(3600 * 6);

Expand All @@ -98,34 +110,44 @@ contract StrategyOperationsTest is StrategyFixture {
}

function testChangeDebt(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

// Deposit to the vault and harvest
vm_std_cheats.prank(user);
want.approve(address(vault), _amount);
vm_std_cheats.prank(user);
vault.deposit(_amount);
vm_std_cheats.prank(gov);
vault.updateStrategyDebtRatio(address(strategy), 5_000);
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();
uint256 half = uint256(_amount / 2);
assertEq(strategy.estimatedTotalAssets(), half);

vm_std_cheats.prank(gov);
vault.updateStrategyDebtRatio(address(strategy), 10_000);
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();
assertEq(strategy.estimatedTotalAssets(), _amount);

// In order to pass these tests, you will need to implement prepareReturn.
// TODO: uncomment the following lines.
// vm_std_cheats.prank(gov);
// vault.updateStrategyDebtRatio(address(strategy), 5_000);
// skip(1);
// vm_std_cheats.prank(strategist);
// strategy.harvest();
// assertEq(strategy.estimatedTotalAssets(), half);
}

function testSweep(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

vm_std_cheats.prank(user);
// solhint-disable-next-line
Expand All @@ -138,37 +160,45 @@ contract StrategyOperationsTest is StrategyFixture {
assertEq(address(want), address(strategy.want()));
assertGt(want.balanceOf(address(strategy)), 0);

vm_std_cheats.prank(gov);
vm_std_cheats.expectRevert("!want");
strategy.sweep(address(want));

// Vault share token doesn't work
vm_std_cheats.prank(gov);
vm_std_cheats.expectRevert("!shares");
strategy.sweep(address(vault));

// TODO: If you add protected tokens to the strategy.
// Protected token doesn't work
// vm_std_cheats.prank(gov);
// vm_std_cheats.expectRevert("!protected");
// strategy.sweep(strategy.protectedToken());

uint256 beforeBalance = weth.balanceOf(address(this));
uint256 beforeBalance = weth.balanceOf(gov);
vm_std_cheats.prank(user);
weth.transfer(address(strategy), WETH_AMT);
assertNeq(address(weth), address(strategy.want()));
assertEq(weth.balanceOf(address(user)), 0);
assertEq(weth.balanceOf(user), 0);
vm_std_cheats.prank(gov);
strategy.sweep(address(weth));
assertEq(weth.balanceOf(address(this)), WETH_AMT + beforeBalance);
assertEq(weth.balanceOf(gov), WETH_AMT + beforeBalance);
}

function testTriggers(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

// Deposit to the vault and harvest
vm_std_cheats.prank(user);
want.approve(address(vault), _amount);
vm_std_cheats.prank(user);
vault.deposit(_amount);
vm_std_cheats.prank(gov);
vault.updateStrategyDebtRatio(address(strategy), 5_000);
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();

strategy.harvestTrigger(0);
Expand Down
14 changes: 12 additions & 2 deletions src/test/StrategyRevoke.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,48 @@ contract StrategyRevokeTest is StrategyFixture {
}

function testRevokeStrategyFromVault(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

// Deposit to the vault and harvest
vm_std_cheats.prank(user);
want.approve(address(vault), _amount);
vm_std_cheats.prank(user);
vault.deposit(_amount);
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();
assertEq(strategy.estimatedTotalAssets(), _amount);

// In order to pass these tests, you will need to implement prepareReturn.
// TODO: uncomment the following lines.
// vm_std_cheats.prank(gov);
// vault.revokeStrategy(address(strategy));
// skip(1);
// vm_std_cheats.prank(strategist);
// strategy.harvest();
// assertEq(want.balanceOf(address(vault)), _amount);
}

function testRevokeStrategyFromStrategy(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

vm_std_cheats.prank(user);
want.approve(address(vault), _amount);
vm_std_cheats.prank(user);
vault.deposit(_amount);
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();
assertEq(strategy.estimatedTotalAssets(), _amount);

vm_std_cheats.prank(gov);
strategy.setEmergencyExit();
skip(1);
vm_std_cheats.prank(strategist);
strategy.harvest();
assertEq(want.balanceOf(address(vault)), _amount);
}
Expand Down
17 changes: 11 additions & 6 deletions src/test/StrategyShutdown.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ contract StrategyShutdownTest is StrategyFixture {
}

function testVaultShutdownCanWithdraw(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

// Deposit to the vault
vm_std_cheats.prank(user);
Expand All @@ -26,11 +28,12 @@ contract StrategyShutdownTest is StrategyFixture {

// Harvest 1: Send funds through the strategy
skip(3600 * 7);
vm_std_cheats.roll(block.number + 1);
vm_std_cheats.prank(strategist);
strategy.harvest();
assertEq(strategy.estimatedTotalAssets(), _amount);

// Set Emergency
vm_std_cheats.prank(gov);
vault.setEmergencyShutdown(true);

// Withdraw (does it work, do you get what you expect)
Expand All @@ -41,7 +44,9 @@ contract StrategyShutdownTest is StrategyFixture {
}

function testBasicShutdown(uint256 _amount) public {
vm_std_cheats.assume(_amount > 0.1 ether && _amount < 10e18);
vm_std_cheats.assume(
_amount > 0.1 ether && _amount < 100_000_000 ether
);

// Deposit to the vault
vm_std_cheats.prank(user);
Expand All @@ -52,23 +57,23 @@ contract StrategyShutdownTest is StrategyFixture {

// Harvest 1: Send funds through the strategy
skip(1 days);
vm_std_cheats.roll(block.number + 100);
vm_std_cheats.prank(strategist);
strategy.harvest();
assertEq(strategy.estimatedTotalAssets(), _amount);

// Earn interest
skip(1 days);
vm_std_cheats.roll(block.number + 1);

// Harvest 2: Realize profit
vm_std_cheats.prank(strategist);
strategy.harvest();
skip(6 hours);
vm_std_cheats.roll(block.number + 1);

// Set emergency
vm_std_cheats.prank(strategist);
strategy.setEmergencyExit();

vm_std_cheats.prank(strategist);
strategy.harvest(); // Remove funds from strategy

assertEq(want.balanceOf(address(strategy)), 0);
Expand Down
37 changes: 35 additions & 2 deletions src/test/utils/ExtendedDSTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,35 @@ contract ExtendedDSTest is DSTest {
}
}

// @dev checks whether @a is within certain percentage of @b
// @a actual value
// @b expected value
// solhint-disable-next-line
function assertRelApproxEq(
uint256 a,
uint256 b,
uint256 maxPercentDelta
) internal virtual {
uint256 delta = a > b ? a - b : b - a;
uint256 maxRelDelta = b / maxPercentDelta;

if (delta > maxRelDelta) {
emit log("Error: a ~= b not satisfied [uint]");
emit log_named_uint(" Expected", b);
emit log_named_uint(" Actual", a);
emit log_named_uint(" Max Delta", maxRelDelta);
emit log_named_uint(" Delta", delta);
fail();
}
}

// Can be removed once https://github.com/dapphub/ds-test/pull/25 is merged and we update submodules, but useful for now
function assertApproxEq(uint a, uint b, uint margin_of_error) internal {
// solhint-disable-next-line
function assertApproxEq(
uint256 a,
uint256 b,
uint256 margin_of_error
) internal {
if (a > b) {
if (a - b > margin_of_error) {
emit log("Error a not equal to b");
Expand All @@ -33,7 +60,13 @@ contract ExtendedDSTest is DSTest {
}
}

function assertApproxEq(uint a, uint b, uint margin_of_error, string memory err) internal {
// solhint-disable-next-line
function assertApproxEq(
uint256 a,
uint256 b,
uint256 margin_of_error,
string memory err
) internal {
if (a > b) {
if (a - b > margin_of_error) {
emit log_named_string("Error", err);
Expand Down
Loading

0 comments on commit 6beb9b5

Please sign in to comment.