VaultHealer V2

Created Diff never expires
1 removal
172 lines
1 addition
172 lines
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT
pragma solidity 0.8.6;
pragma solidity 0.8.6;


import "@openzeppelin/contracts/token/ERC20/Utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/Utils/SafeERC20.sol";
import "@openzeppelin/contracts/Utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts/Utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts/Security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/Security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/Utils/Math/SafeMath.sol";
import "@openzeppelin/contracts/Utils/Math/SafeMath.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";


import "./libs/IStrategy.sol";
import "./libs/IStrategy.sol";


contract VaultApe is ReentrancyGuard, Ownable {
contract VaultHealer is ReentrancyGuard, Ownable {
using SafeMath for uint256;
using SafeMath for uint256;
using SafeERC20 for IERC20;
using SafeERC20 for IERC20;


// Info of each user.
// Info of each user.
struct UserInfo {
struct UserInfo {
uint256 shares; // How many LP tokens the user has provided.
uint256 shares; // How many LP tokens the user has provided.
}
}


struct PoolInfo {
struct PoolInfo {
IERC20 want; // Address of the want token.
IERC20 want; // Address of the want token.
address strat; // Strategy address that will auto compound want tokens
address strat; // Strategy address that will auto compound want tokens
}
}


PoolInfo[] public poolInfo; // Info of each pool.
PoolInfo[] public poolInfo; // Info of each pool.
mapping(uint256 => mapping(address => UserInfo)) public userInfo; // Info of each user that stakes LP tokens.
mapping(uint256 => mapping(address => UserInfo)) public userInfo; // Info of each user that stakes LP tokens.
mapping(address => bool) private strats;
mapping(address => bool) private strats;


event AddPool(address indexed strat);
event AddPool(address indexed strat);
event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
event EmergencyWithdraw(address indexed user, uint256 indexed pid, uint256 amount);
event EmergencyWithdraw(address indexed user, uint256 indexed pid, uint256 amount);


function poolLength() external view returns (uint256) {
function poolLength() external view returns (uint256) {
return poolInfo.length;
return poolInfo.length;
}
}


/**
/**
* @dev Add a new want to the pool. Can only be called by the owner.
* @dev Add a new want to the pool. Can only be called by the owner.
*/
*/
function addPool(address _strat) external onlyOwner nonReentrant {
function addPool(address _strat) external onlyOwner nonReentrant {
require(!strats[_strat], "Existing strategy");
require(!strats[_strat], "Existing strategy");
poolInfo.push(
poolInfo.push(
PoolInfo({
PoolInfo({
want: IERC20(IStrategy(_strat).wantAddress()),
want: IERC20(IStrategy(_strat).wantAddress()),
strat: _strat
strat: _strat
})
})
);
);
strats[_strat] = true;
strats[_strat] = true;
resetSingleAllowance(poolInfo.length.sub(1));
resetSingleAllowance(poolInfo.length.sub(1));
emit AddPool(_strat);
emit AddPool(_strat);
}
}


// View function to see staked Want tokens on frontend.
// View function to see staked Want tokens on frontend.
function stakedWantTokens(uint256 _pid, address _user) external view returns (uint256) {
function stakedWantTokens(uint256 _pid, address _user) external view returns (uint256) {
PoolInfo storage pool = poolInfo[_pid];
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_user];
UserInfo storage user = userInfo[_pid][_user];


uint256 sharesTotal = IStrategy(pool.strat).sharesTotal();
uint256 sharesTotal = IStrategy(pool.strat).sharesTotal();
uint256 wantLockedTotal = IStrategy(poolInfo[_pid].strat).wantLockedTotal();
uint256 wantLockedTotal = IStrategy(poolInfo[_pid].strat).wantLockedTotal();
if (sharesTotal == 0) {
if (sharesTotal == 0) {
return 0;
return 0;
}
}
return user.shares.mul(wantLockedTotal).div(sharesTotal);
return user.shares.mul(wantLockedTotal).div(sharesTotal);
}
}


// Want tokens moved from user -> this -> Strat (compounding)
// Want tokens moved from user -> this -> Strat (compounding)
function deposit(uint256 _pid, uint256 _wantAmt) external nonReentrant {
function deposit(uint256 _pid, uint256 _wantAmt) external nonReentrant {
_deposit(_pid, _wantAmt, msg.sender);
_deposit(_pid, _wantAmt, msg.sender);
}
}


// For depositing for other users
// For depositing for other users
function deposit(uint256 _pid, uint256 _wantAmt, address _to) external nonReentrant {
function deposit(uint256 _pid, uint256 _wantAmt, address _to) external nonReentrant {
_deposit(_pid, _wantAmt, _to);
_deposit(_pid, _wantAmt, _to);
}
}


function _deposit(uint256 _pid, uint256 _wantAmt, address _to) internal {
function _deposit(uint256 _pid, uint256 _wantAmt, address _to) internal {
PoolInfo storage pool = poolInfo[_pid];
PoolInfo storage pool = poolInfo[_pid];
require(pool.strat != address(0), "That strategy does not exist");
require(pool.strat != address(0), "That strategy does not exist");
UserInfo storage user = userInfo[_pid][_to];
UserInfo storage user = userInfo[_pid][_to];


if (_wantAmt > 0) {
if (_wantAmt > 0) {
// Call must happen before transfer
// Call must happen before transfer
uint256 wantBefore = IERC20(pool.want).balanceOf(address(this));
uint256 wantBefore = IERC20(pool.want).balanceOf(address(this));
pool.want.safeTransferFrom(msg.sender, address(this), _wantAmt);
pool.want.safeTransferFrom(msg.sender, address(this), _wantAmt);
uint256 finalDeposit = IERC20(pool.want).balanceOf(address(this)).sub(wantBefore);
uint256 finalDeposit = IERC20(pool.want).balanceOf(address(this)).sub(wantBefore);


// Proper deposit amount for tokens with fees
// Proper deposit amount for tokens with fees
uint256 sharesAdded = IStrategy(poolInfo[_pid].strat).deposit(_to, finalDeposit);
uint256 sharesAdded = IStrategy(poolInfo[_pid].strat).deposit(_to, finalDeposit);
user.shares = user.shares.add(sharesAdded);
user.shares = user.shares.add(sharesAdded);
}
}
emit Deposit(_to, _pid, _wantAmt);
emit Deposit(_to, _pid, _wantAmt);
}
}


// Withdraw LP tokens from MasterChef.
// Withdraw LP tokens from MasterChef.
function withdraw(uint256 _pid, uint256 _wantAmt) external nonReentrant {
function withdraw(uint256 _pid, uint256 _wantAmt) external nonReentrant {
_withdraw(_pid, _wantAmt, msg.sender);
_withdraw(_pid, _wantAmt, msg.sender);
}
}


// For withdrawing to other address
// For withdrawing to other address
function withdraw(uint256 _pid, uint256 _wantAmt, address _to) external nonReentrant {
function withdraw(uint256 _pid, uint256 _wantAmt, address _to) external nonReentrant {
_withdraw(_pid, _wantAmt, _to);
_withdraw(_pid, _wantAmt, _to);
}
}


function _withdraw(uint256 _pid, uint256 _wantAmt, address _to) internal {
function _withdraw(uint256 _pid, uint256 _wantAmt, address _to) internal {
PoolInfo storage pool = poolInfo[_pid];
PoolInfo storage pool = poolInfo[_pid];
require(pool.strat != address(0), "That strategy does not exist");
require(pool.strat != address(0), "That strategy does not exist");
UserInfo storage user = userInfo[_pid][msg.sender];
UserInfo storage user = userInfo[_pid][msg.sender];


uint256 wantLockedTotal = IStrategy(poolInfo[_pid].strat).wantLockedTotal();
uint256 wantLockedTotal = IStrategy(poolInfo[_pid].strat).wantLockedTotal();
uint256 sharesTotal = IStrategy(poolInfo[_pid].strat).sharesTotal();
uint256 sharesTotal = IStrategy(poolInfo[_pid].strat).sharesTotal();


require(user.shares > 0, "user.shares is 0");
require(user.shares > 0, "user.shares is 0");
require(sharesTotal > 0, "sharesTotal is 0");
require(sharesTotal > 0, "sharesTotal is 0");


// Withdraw want tokens
// Withdraw want tokens
uint256 amount = user.shares.mul(wantLockedTotal).div(sharesTotal);
uint256 amount = user.shares.mul(wantLockedTotal).div(sharesTotal);
if (_wantAmt > amount) {
if (_wantAmt > amount) {
_wantAmt = amount;
_wantAmt = amount;
}
}
if (_wantAmt > 0) {
if (_wantAmt > 0) {
uint256 sharesRemoved = IStrategy(poolInfo[_pid].strat).withdraw(msg.sender, _wantAmt);
uint256 sharesRemoved = IStrategy(poolInfo[_pid].strat).withdraw(msg.sender, _wantAmt);


if (sharesRemoved > user.shares) {
if (sharesRemoved > user.shares) {
user.shares = 0;
user.shares = 0;
} else {
} else {
user.shares = user.shares.sub(sharesRemoved);
user.shares = user.shares.sub(sharesRemoved);
}
}


uint256 wantBal = IERC20(pool.want).balanceOf(address(this));
uint256 wantBal = IERC20(pool.want).balanceOf(address(this));
if (wantBal < _wantAmt) {
if (wantBal < _wantAmt) {
_wantAmt = wantBal;
_wantAmt = wantBal;
}
}
pool.want.safeTransfer(_to, _wantAmt);
pool.want.safeTransfer(_to, _wantAmt);
}
}
emit Withdraw(msg.sender, _pid, _wantAmt);
emit Withdraw(msg.sender, _pid, _wantAmt);
}
}


// Withdraw everything from pool for yourself
// Withdraw everything from pool for yourself
function withdrawAll(uint256 _pid) external {
function withdrawAll(uint256 _pid) external {
_withdraw(_pid, type(uint256).max, msg.sender);
_withdraw(_pid, type(uint256).max, msg.sender);
}
}


function resetAllowances() external onlyOwner {
function resetAllowances() external onlyOwner {
for (uint256 i=0; i<poolInfo.length; i++) {
for (uint256 i=0; i<poolInfo.length; i++) {
PoolInfo storage pool = poolInfo[i];
PoolInfo storage pool = poolInfo[i];
pool.want.safeApprove(pool.strat, uint256(0));
pool.want.safeApprove(pool.strat, uint256(0));
pool.want.safeIncreaseAllowance(pool.strat, type(uint256).max);
pool.want.safeIncreaseAllowance(pool.strat, type(uint256).max);
}
}
}
}


function earnAll() external {
function earnAll() external {
for (uint256 i=0; i<poolInfo.length; i++) {
for (uint256 i=0; i<poolInfo.length; i++) {
if (!IStrategy(poolInfo[i].strat).paused())
if (!IStrategy(poolInfo[i].strat).paused())
IStrategy(poolInfo[i].strat).earn(_msgSender());
IStrategy(poolInfo[i].strat).earn(_msgSender());
}
}
}
}


function earnSome(uint256[] memory pids) external {
function earnSome(uint256[] memory pids) external {
for (uint256 i=0; i<pids.length; i++) {
for (uint256 i=0; i<pids.length; i++) {
if (poolInfo.length >= pids[i] && !IStrategy(poolInfo[pids[i]].strat).paused())
if (poolInfo.length >= pids[i] && !IStrategy(poolInfo[pids[i]].strat).paused())
IStrategy(poolInfo[pids[i]].strat).earn(_msgSender());
IStrategy(poolInfo[pids[i]].strat).earn(_msgSender());
}
}
}
}


function resetSingleAllowance(uint256 _pid) public onlyOwner {
function resetSingleAllowance(uint256 _pid) public onlyOwner {
PoolInfo storage pool = poolInfo[_pid];
PoolInfo storage pool = poolInfo[_pid];
pool.want.safeApprove(pool.strat, uint256(0));
pool.want.safeApprove(pool.strat, uint256(0));
pool.want.safeIncreaseAllowance(pool.strat, type(uint256).max);
pool.want.safeIncreaseAllowance(pool.strat, type(uint256).max);
}
}
}
}