forked from iankressin/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacocaVault.sol
More file actions
241 lines (194 loc) · 7.35 KB
/
Copy pathPacocaVault.sol
File metadata and controls
241 lines (194 loc) · 7.35 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "./interfaces/IPacocaFarm.sol";
contract PacocaVault is Ownable, ReentrancyGuard {
using SafeERC20 for IERC20;
using SafeMath for uint256;
struct UserInfo {
uint256 shares; // number of shares for a user
uint256 lastDepositedTime; // keeps track of deposited time for potential penalty
uint256 pacocaAtLastUserAction; // keeps track of pacoca deposited at the last user action
uint256 lastUserActionTime; // keeps track of the last user action time
}
IERC20 public immutable token; // Pacoca token
IPacocaFarm public immutable masterchef;
mapping(address => UserInfo) public userInfo;
uint256 public totalShares;
uint256 public lastHarvestedTime;
address public treasury;
uint256 public withdrawFee;
uint256 public withdrawFeePeriod = 72 hours; // 3 days
uint256 public constant MAX_WITHDRAW_FEE = 200; // 2%
event Deposit(address indexed sender, uint256 amount, uint256 shares, uint256 lastDepositedTime);
event Withdraw(address indexed sender, uint256 amount, uint256 shares);
event Harvest(address indexed sender);
event SetTreasury(address treasury);
event SetWithdrawFee(uint256 withdrawFee);
/**
* @notice Constructor
* @param _token: Pacoca token contract
* @param _masterchef: MasterChef contract
* @param _owner: address of the owner
* @param _treasury: address of the treasury (collects fees)
*/
constructor(
IERC20 _token,
IPacocaFarm _masterchef,
address _owner,
address _treasury,
uint256 _withdrawFee
) public {
token = _token;
masterchef = _masterchef;
treasury = _treasury;
withdrawFee = _withdrawFee;
transferOwnership(_owner);
}
/**
* @notice Deposits funds into the Pacoca Vault
* @param _amount: number of tokens to deposit (in PACOCA)
*/
function deposit(uint256 _amount) external nonReentrant {
require(_amount > 0, "PacocaVault: Nothing to deposit");
uint256 pool = underlyingTokenBalance();
token.safeTransferFrom(msg.sender, address(this), _amount);
uint256 currentShares = 0;
if (totalShares != 0) {
currentShares = (_amount.mul(totalShares)).div(pool);
} else {
currentShares = _amount;
}
UserInfo storage user = userInfo[msg.sender];
user.shares = user.shares.add(currentShares);
user.lastDepositedTime = block.timestamp;
totalShares = totalShares.add(currentShares);
user.pacocaAtLastUserAction = user.shares.mul(underlyingTokenBalance()).div(totalShares);
user.lastUserActionTime = block.timestamp;
_earn();
emit Deposit(msg.sender, _amount, currentShares, block.timestamp);
}
/**
* @notice Withdraws all funds for a user
*/
function withdrawAll() external {
withdraw(userInfo[msg.sender].shares);
}
/**
* @notice Reinvests PACOCA tokens into MasterChef
*/
function harvest() external {
masterchef.withdraw(0, 0);
_earn();
emit Harvest(msg.sender);
}
/**
* @notice Sets treasury address
* @dev Only callable by the contract owner.
*/
function setTreasury(address _treasury) external onlyOwner {
require(_treasury != address(0), "PacocaVault: Cannot be zero address");
treasury = _treasury;
emit SetTreasury(treasury);
}
/**
* @notice Sets withdraw fee
* @dev Only callable by the contract owner.
*/
function setWithdrawFee(uint256 _withdrawFee) external onlyOwner {
require(
_withdrawFee <= MAX_WITHDRAW_FEE,
"PacocaVault: withdrawFee cannot be more than MAX_WITHDRAW_FEE"
);
withdrawFee = _withdrawFee;
emit SetWithdrawFee(withdrawFee);
}
/**
* @notice Calculates the total pending rewards that can be restaked
* @return Returns total pending Pacoca rewards
*/
function calculateTotalPendingPacocaRewards() external view returns (uint256) {
uint256 amount = masterchef.pendingPACOCA(0, address(this));
amount = amount.add(available());
return amount;
}
/**
* @notice Calculates the price per share
*/
function getPricePerFullShare() external view returns (uint256) {
return totalShares == 0 ? 1e18 : underlyingTokenBalance().mul(1e18).div(totalShares);
}
/**
* @notice Withdraws from funds from the Pacoca Vault
* @param _shares: Number of shares to withdraw
*/
function withdraw(uint256 _shares) public nonReentrant {
UserInfo storage user = userInfo[msg.sender];
require(
_shares > 0,
"PacocaVault: Nothing to withdraw"
);
require(
_shares <= user.shares,
"PacocaVault: Withdraw amount exceeds balance"
);
uint256 currentAmount = (underlyingTokenBalance().mul(_shares)).div(totalShares);
user.shares = user.shares.sub(_shares);
totalShares = totalShares.sub(_shares);
uint256 bal = available();
if (bal < currentAmount) {
uint256 balWithdraw = currentAmount.sub(bal);
masterchef.withdraw(0, balWithdraw);
uint256 balAfter = available();
uint256 diff = balAfter.sub(bal);
if (diff < balWithdraw) {
currentAmount = bal.add(diff);
}
}
if (
withdrawFee > 0 &&
block.timestamp < user.lastDepositedTime.add(withdrawFeePeriod)
) {
uint256 currentWithdrawFee = currentAmount.mul(withdrawFee).div(10000);
token.safeTransfer(treasury, currentWithdrawFee);
currentAmount = currentAmount.sub(currentWithdrawFee);
}
if (user.shares > 0) {
user.pacocaAtLastUserAction = user.shares.mul(underlyingTokenBalance()).div(totalShares);
} else {
user.pacocaAtLastUserAction = 0;
}
user.lastUserActionTime = block.timestamp;
token.safeTransfer(msg.sender, currentAmount);
emit Withdraw(msg.sender, currentAmount, _shares);
}
/**
* @notice Custom logic for how much the vault allows to be borrowed
* @dev The contract puts 100% of the tokens to work.
*/
function available() public view returns (uint256) {
return token.balanceOf(address(this));
}
/**
* @notice Calculates the total underlying tokens
* @dev It includes tokens held by the contract and held in MasterChef
*/
function underlyingTokenBalance() public view returns (uint256) {
(uint256 amount,) = masterchef.userInfo(0, address(this));
return token.balanceOf(address(this)).add(amount);
}
/**
* @notice Deposits tokens into MasterChef to earn staking rewards
*/
function _earn() internal {
uint256 balance = available();
if (balance > 0) {
if (token.allowance(address(this), address(masterchef)) < balance) {
token.safeApprove(address(masterchef), uint(- 1));
}
masterchef.deposit(0, balance);
}
}
}