This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 211
/
StrategyVaultUSDC.sol
148 lines (123 loc) · 5.37 KB
/
StrategyVaultUSDC.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.17;
import "@openzeppelinV2/contracts/token/ERC20/IERC20.sol";
import "@openzeppelinV2/contracts/math/SafeMath.sol";
import "@openzeppelinV2/contracts/utils/Address.sol";
import "@openzeppelinV2/contracts/token/ERC20/SafeERC20.sol";
import "../../interfaces/aave/Aave.sol";
import "../../interfaces/aave/LendingPoolAddressesProvider.sol";
import "../../interfaces/yearn/IController.sol";
import "../../interfaces/yearn/IVault.sol";
contract StrategyVaultUSDC {
using SafeERC20 for IERC20;
using Address for address;
using SafeMath for uint256;
address public constant want = address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
address public constant vault = address(0x597aD1e0c13Bfe8025993D9e79C69E1c0233522e);
address public constant aave = address(0x24a42fD28C976A61Df5D00D0599C34c4f90748c8);
address public governance;
address public controller;
constructor(address _controller) public {
governance = msg.sender;
controller = _controller;
}
function deposit() external {
uint256 _balance = IERC20(want).balanceOf(address(this));
if (_balance > 0) {
IERC20(want).safeApprove(address(vault), 0);
IERC20(want).safeApprove(address(vault), _balance);
IVault(vault).deposit(_balance);
}
}
function getAave() public view returns (address) {
return LendingPoolAddressesProvider(aave).getLendingPool();
}
function getName() external pure returns (string memory) {
return "StrategyVaultUSDC";
}
function debt() external view returns (uint256) {
(, uint256 currentBorrowBalance, , , , , , , , ) = Aave(getAave()).getUserReserveData(
want,
IController(controller).vaults(address(this))
);
return currentBorrowBalance;
}
function have() public view returns (uint256) {
uint256 _have = balanceOf();
return _have;
}
function skimmable() public view returns (uint256) {
(, uint256 currentBorrowBalance, , , , , , , , ) = Aave(getAave()).getUserReserveData(
want,
IController(controller).vaults(address(this))
);
uint256 _have = have();
if (_have > currentBorrowBalance) {
return _have.sub(currentBorrowBalance);
} else {
return 0;
}
}
function skim() external {
uint256 _balance = IERC20(want).balanceOf(address(this));
uint256 _amount = skimmable();
if (_balance < _amount) {
_amount = _withdrawSome(_amount.sub(_balance));
_amount = _amount.add(_balance);
}
IERC20(want).safeTransfer(controller, _amount);
}
// Controller only function for creating additional rewards from dust
function withdraw(IERC20 _asset) external returns (uint256 balance) {
require(msg.sender == controller, "!controller");
require(address(_asset) != address(want), "!want");
require(address(_asset) != address(vault), "!vault");
balance = _asset.balanceOf(address(this));
_asset.safeTransfer(controller, balance);
}
// Withdraw partial funds, normally used with a vault withdrawal
function withdraw(uint256 _amount) external {
require(msg.sender == controller, "!controller");
uint256 _balance = IERC20(want).balanceOf(address(this));
if (_balance < _amount) {
_amount = _withdrawSome(_amount.sub(_balance));
_amount = _amount.add(_balance);
}
address _vault = IController(controller).vaults(address(this));
require(_vault != address(0), "!vault"); // additional protection so we don't burn the funds
IERC20(want).safeTransfer(_vault, _amount);
}
// Withdraw all funds, normally used when migrating strategies
function withdrawAll() external returns (uint256 balance) {
require(msg.sender == controller, "!controller");
_withdrawAll();
balance = IERC20(want).balanceOf(address(this));
address _vault = IController(controller).vaults(address(this));
require(_vault != address(0), "!vault"); // additional protection so we don't burn the funds
IERC20(want).safeTransfer(_vault, balance);
}
function _withdrawAll() internal {
IVault(vault).withdraw(IERC20(vault).balanceOf(address(this)));
}
function _withdrawSome(uint256 _amount) internal returns (uint256) {
uint256 _redeem = IERC20(vault).balanceOf(address(this)).mul(_amount).div(balanceSavingsInToken());
uint256 _before = IERC20(want).balanceOf(address(this));
IVault(vault).withdraw(_redeem);
uint256 _after = IERC20(want).balanceOf(address(this));
return _after.sub(_before);
}
function balanceOf() public view returns (uint256) {
return IERC20(want).balanceOf(address(this)).add(balanceSavingsInToken());
}
function balanceSavingsInToken() public view returns (uint256) {
return IERC20(vault).balanceOf(address(this)).mul(IVault(vault).getPricePerFullShare()).div(1e18);
}
function setGovernance(address _governance) external {
require(msg.sender == governance, "!governance");
governance = _governance;
}
function setController(address _controller) external {
require(msg.sender == governance, "!governance");
controller = _controller;
}
}