This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SoftVault.sol
124 lines (107 loc) · 4 KB
/
SoftVault.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import "../utils/BlueBerryConst.sol";
import "../utils/BlueBerryErrors.sol";
import "../interfaces/IProtocolConfig.sol";
import "../interfaces/ISoftVault.sol";
import "../interfaces/compound/ICErc20.sol";
contract SoftVault is
OwnableUpgradeable,
ERC20Upgradeable,
ReentrancyGuardUpgradeable,
ISoftVault
{
using SafeERC20Upgradeable for IERC20Upgradeable;
/// @dev address of cToken for underlying token
ICErc20 public cToken;
/// @dev address of underlying token
IERC20Upgradeable public uToken;
IProtocolConfig public config;
event Deposited(
address indexed account,
uint256 amount,
uint256 shareAmount
);
event Withdrawn(
address indexed account,
uint256 amount,
uint256 shareAmount
);
function initialize(
IProtocolConfig _config,
ICErc20 _cToken,
string memory _name,
string memory _symbol
) external initializer {
__ERC20_init(_name, _symbol);
__Ownable_init();
if (address(_cToken) == address(0) || address(_config) == address(0))
revert ZERO_ADDRESS();
IERC20Upgradeable _uToken = IERC20Upgradeable(_cToken.underlying());
config = _config;
cToken = _cToken;
uToken = _uToken;
_uToken.safeApprove(address(_cToken), type(uint256).max);
}
function decimals() public view override returns (uint8) {
return cToken.decimals();
}
/**
* @notice Deposit underlying assets on Compound and issue share token
* @param amount Underlying token amount to deposit
* @return shareAmount cToken amount
*/
function deposit(uint256 amount)
external
override
nonReentrant
returns (uint256 shareAmount)
{
if (amount == 0) revert ZERO_AMOUNT();
uint256 uBalanceBefore = uToken.balanceOf(address(this));
uToken.safeTransferFrom(msg.sender, address(this), amount);
uint256 uBalanceAfter = uToken.balanceOf(address(this));
uint256 cBalanceBefore = cToken.balanceOf(address(this));
if (cToken.mint(uBalanceAfter - uBalanceBefore) != 0)
revert LEND_FAILED(amount);
uint256 cBalanceAfter = cToken.balanceOf(address(this));
shareAmount = cBalanceAfter - cBalanceBefore;
_mint(msg.sender, shareAmount);
emit Deposited(msg.sender, amount, shareAmount);
}
/**
* @notice Withdraw underlying assets from Compound
* @param shareAmount Amount of cTokens to redeem
* @return withdrawAmount Amount of underlying assets withdrawn
*/
function withdraw(uint256 shareAmount)
external
override
nonReentrant
returns (uint256 withdrawAmount)
{
if (shareAmount == 0) revert ZERO_AMOUNT();
_burn(msg.sender, shareAmount);
uint256 uBalanceBefore = uToken.balanceOf(address(this));
if (cToken.redeem(shareAmount) != 0) revert REDEEM_FAILED(shareAmount);
uint256 uBalanceAfter = uToken.balanceOf(address(this));
withdrawAmount = uBalanceAfter - uBalanceBefore;
// Cut withdraw fee if it is in withdrawVaultFee Window (2 months)
if (
block.timestamp <
config.withdrawVaultFeeWindowStartTime() +
config.withdrawVaultFeeWindow()
) {
uint256 fee = (withdrawAmount * config.withdrawVaultFee()) /
DENOMINATOR;
uToken.safeTransfer(config.treasury(), fee);
withdrawAmount -= fee;
}
uToken.safeTransfer(msg.sender, withdrawAmount);
emit Withdrawn(msg.sender, withdrawAmount, shareAmount);
}
}