Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add end to end tests #1

Merged
merged 4 commits into from Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions brownie-config.yaml
@@ -0,0 +1,8 @@
networks:
default: mainnet-fork

compiler:
solc:
version: 0.6.9
remappings:
- "@openzeppelin=OpenZeppelin/openzeppelin-contracts@3.2.0"
8 changes: 7 additions & 1 deletion contracts/Interfaces/Yearn/IController.sol
Expand Up @@ -12,4 +12,10 @@ interface IController {
function rewards() external view returns (address);

function vaults(address) external view returns (address);
}

function approveStrategy(address, address) external;

function setStrategy(address, address) external;

function strategies(address) external view returns (address);
}
37 changes: 37 additions & 0 deletions contracts/Interfaces/Yearn/IVault.sol
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.6.9;

interface IVault {
function token() external view returns (address);

function underlying() external view returns (address);

function name() external view returns (string memory);

function symbol() external view returns (string memory);

function decimals() external view returns (uint8);

function controller() external view returns (address);

function governance() external view returns (address);

function getPricePerFullShare() external view returns (uint256);

function deposit(uint256) external;

function depositAll() external;

function withdraw(uint256) external;

function withdrawAll() external;

function balanceOf(address) external view returns (uint256);

function totalSupply() external view returns (uint256);

function earn() external;

function setMin(uint256) external;
}
69 changes: 69 additions & 0 deletions tests/test_strategy_comp_dai.py
@@ -0,0 +1,69 @@
from itertools import count
from brownie import Wei, reverts
import brownie

def test_strategy(accounts, interface, chain, web3, history, YearnCompDaiStrategy):
user = accounts[0]
whale = accounts.at("0xBE0eB53F46cd790Cd13851d5EFf43D12404d33E8", force=True)
ychad = accounts.at(web3.ens.resolve('ychad.eth'), force=True)

solo = interface.ISoloMargin('0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e')
comptroller = interface.ComptrollerI('0x3d9819210a31b4961b30ef54be2aed79b9c9cd3b')
dai = interface.ERC20('0x6b175474e89094c44da98b954eedeac495271d0f')
cdai = interface.CErc20I('0x5d3a536e4d6dbd6114cc1ead35777bab948e3643')
comp = interface.ERC20('0xc00e94cb662c3520282e6f5717214004a7f26888')
controller = interface.IController('0x9E65Ad11b299CA0Abefc2799dDB6314Ef2d91080')
vault = interface.IVault(controller.vaults(dai))
strategy = YearnCompDaiStrategy.deploy(controller, {'from': user})
assert strategy.want() == vault.token() == dai
strategy.setCollateralTarget(3900)
with brownie.reverts("Target too close to 4x leverage"):
strategy.setCollateralTarget(4000)

print('migrate strategy')
controller.approveStrategy(dai, strategy, {'from': ychad})
controller.setStrategy(dai, strategy, {'from': ychad})
vault.setMin(10000, {'from': ychad})
assert controller.strategies(dai) == strategy
print('dai in vault:', dai.balanceOf(vault).to('ether'))

print('deposit funds into new strategy')
vault.earn({'from': user})
print('balance of strategy:', strategy.balanceOf().to('ether'))

amount = dai.balanceOf(whale)
dai.approve(vault, amount, {'from': whale})
print('deposit amount:', amount.to('ether'))
vault.deposit(amount, {'from': whale})
for i in count(1):
print(f'\ndeposit {i}')
vault.deposit(0, {'from': whale})
vault.earn({'from': user})
print('balance of strategy:', strategy.balanceOf().to('ether'))
deposits, borrows = strategy.getCurrentPosition()
print('deposits:', Wei(deposits).to('ether'))
print('borrows:', Wei(borrows).to('ether'))
collat = borrows / deposits
leverage = 1 / (1 - collat)
print(f'collat: {collat:.5%}')
print(f'leverage: {leverage:.5f}x')
print('liquidity:', strategy.getLiquidity().to('ether'))
if leverage >= strategy.leverageTarget() / 1001 or i > 10:
print('target leverage reached')
break

print('\nharvest')
before = strategy.balanceOf()
blocks_per_year = 2_300_000
sample = 100
chain.mine(sample)
strategy.harvest()
after = strategy.balanceOf()
assert after >= before
print('balance increase:', (after - before).to('ether'))
print(f'implied apr: {(after / before - 1) * (blocks_per_year / sample):.8%}')

vault.withdrawAll({'from': whale})
after = dai.balanceOf(whale)
print(f'\nuser balance increase:', (after - amount).to('ether'))
assert after >= amount