forked from ApeSwapFinance/apeswap-iao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIAO.sol
341 lines (302 loc) · 14.1 KB
/
IAO.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// SPDX-License-Identifier: MIT
pragma solidity 0.8.6;
/*
______ ______
/ \ / \
| ▓▓▓▓▓▓\ ______ ______ | ▓▓▓▓▓▓\__ __ __ ______ ______
| ▓▓__| ▓▓/ \ / \| ▓▓___\▓▓ \ | \ | \| \ / \
| ▓▓ ▓▓ ▓▓▓▓▓▓\ ▓▓▓▓▓▓\\▓▓ \| ▓▓ | ▓▓ | ▓▓ \▓▓▓▓▓▓\ ▓▓▓▓▓▓\
| ▓▓▓▓▓▓▓▓ ▓▓ | ▓▓ ▓▓ ▓▓_\▓▓▓▓▓▓\ ▓▓ | ▓▓ | ▓▓/ ▓▓ ▓▓ | ▓▓
| ▓▓ | ▓▓ ▓▓__/ ▓▓ ▓▓▓▓▓▓▓▓ \__| ▓▓ ▓▓_/ ▓▓_/ ▓▓ ▓▓▓▓▓▓▓ ▓▓__/ ▓▓
| ▓▓ | ▓▓ ▓▓ ▓▓\▓▓ \\▓▓ ▓▓\▓▓ ▓▓ ▓▓\▓▓ ▓▓ ▓▓ ▓▓
\▓▓ \▓▓ ▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓ \▓▓▓▓▓▓ \▓▓▓▓▓\▓▓▓▓ \▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓
| ▓▓ | ▓▓
| ▓▓ | ▓▓
\▓▓ \▓▓
* App: https://apeswap.finance
* Medium: https://ape-swap.medium.com
* Twitter: https://twitter.com/ape_swap
* Discord: https://discord.com/invite/apeswap
* Telegram: https://t.me/ape_swap
* Announcements: https://t.me/ape_swap_news
* GitHub: https://github.com/ApeSwapFinance
*/
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import '@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol';
/// @title Harvest Period based Initial Ape Offering
/// @notice safeTransferStakeInternal uses a fixed gas limit for native transfers which should be evaluated when deploying to new networks.
contract IAO is ReentrancyGuardUpgradeable {
using SafeERC20 for IERC20;
uint256 constant public HARVEST_PERIODS = 4;
uint256[HARVEST_PERIODS] public harvestReleaseBlocks;
// Info of each user.
struct UserInfo {
uint256 amount; // How many tokens the user has provided.
bool[HARVEST_PERIODS] claimed; // default false
bool refunded;
}
// admin address
address public adminAddress;
// The raising token
IERC20 public stakeToken;
// Flag if stake token is native EVM token
bool public isNativeTokenStaking;
// The offering token
IERC20 public offeringToken;
// The block number when IAO starts
uint256 public startBlock;
// The block number when IAO ends
uint256 public endBlock;
// total amount of raising tokens need to be raised
uint256 public raisingAmount;
// total amount of offeringToken that will offer
uint256 public offeringAmount;
// total amount of raising tokens that have already raised
uint256 public totalAmount;
// total amount of tokens to give back to users
uint256 public totalDebt;
// address => amount
mapping(address => UserInfo) public userInfo;
// participators
address[] public addressList;
event Deposit(address indexed user, uint256 amount);
event Harvest(
address indexed user,
uint256 offeringAmount,
uint256 excessAmount
);
event UpdateOfferingAmount(uint256 previousOfferingAmount, uint256 newOfferingAmount);
event UpdateRaisingAmount(uint256 previousRaisingAmount, uint256 newRaisingAmount);
event AdminFinalWithdraw(uint256 stakeTokenAmount, uint256 offerAmount);
event EmergencySweepWithdraw(address indexed receiver, address indexed token, uint256 balance);
function initialize(
IERC20 _stakeToken,
IERC20 _offeringToken,
uint256 _startBlock,
uint256 _endBlockOffset,
uint256 _vestingBlockOffset, // Block offset between vesting distributions
uint256 _offeringAmount,
uint256 _raisingAmount,
address _adminAddress
) external initializer {
stakeToken = _stakeToken;
/// @dev address(0) turns this contract into a native token staking pool
if(address(stakeToken) == address(0)) {
isNativeTokenStaking = true;
}
offeringToken = _offeringToken;
startBlock = _startBlock;
endBlock = _startBlock + _endBlockOffset;
// Setup vesting release blocks
for (uint256 i = 0; i < HARVEST_PERIODS; i++) {
harvestReleaseBlocks[i] = endBlock + (_vestingBlockOffset * i);
}
offeringAmount = _offeringAmount;
raisingAmount = _raisingAmount;
totalAmount = 0;
adminAddress = _adminAddress;
__ReentrancyGuard_init();
}
modifier onlyAdmin() {
require(msg.sender == adminAddress, "caller is not admin");
_;
}
modifier onlyActiveIAO() {
require(
block.number >= startBlock && block.number < endBlock,
"not iao time"
);
_;
}
function setOfferingAmount(uint256 _offerAmount) external onlyAdmin {
require(block.number < startBlock, "cannot update during active iao");
emit UpdateOfferingAmount(offeringAmount, _offerAmount);
offeringAmount = _offerAmount;
}
function setRaisingAmount(uint256 _raisingAmount) external onlyAdmin {
require(block.number < startBlock, "cannot update during active iao");
emit UpdateRaisingAmount(raisingAmount, _raisingAmount);
raisingAmount = _raisingAmount;
}
/// @notice Deposits native EVM tokens into the IAO contract as per the value sent
/// in the transaction.
function depositNative() external payable onlyActiveIAO nonReentrant {
require(isNativeTokenStaking, 'stake token is not native EVM token');
require(msg.value > 0, 'value not > 0');
depositInternal(msg.value);
}
/// @dev Deposit ERC20 tokens with support for reflect tokens
function deposit(uint256 _amount) external onlyActiveIAO nonReentrant {
require(!isNativeTokenStaking, "stake token is native token, deposit through 'depositNative'");
require(_amount > 0, "_amount not > 0");
uint256 pre = getTotalStakeTokenBalance();
stakeToken.safeTransferFrom(
msg.sender,
address(this),
_amount
);
uint256 finalDepositAmount = getTotalStakeTokenBalance() - pre;
require(finalDepositAmount > 0, 'final deposit amount is zero');
depositInternal(finalDepositAmount);
}
/// @notice To support ERC20 and native token deposits this function does not transfer
/// any tokens in, but only updates the state. Make sure to transfer in the funds
/// in a parent function
function depositInternal(uint256 _amount) internal {
if (userInfo[msg.sender].amount == 0) {
addressList.push(msg.sender);
}
userInfo[msg.sender].amount += _amount;
totalAmount += _amount;
totalDebt += _amount;
emit Deposit(msg.sender, _amount);
}
function harvest(uint256 harvestPeriod) external nonReentrant {
require(harvestPeriod < HARVEST_PERIODS, "harvest period out of range");
require(block.number > harvestReleaseBlocks[harvestPeriod], "not harvest time");
require(userInfo[msg.sender].amount > 0, "have you participated?");
require(!userInfo[msg.sender].claimed[harvestPeriod], "harvest for period already claimed");
// Refunds are only given on the first harvest
uint256 refundingTokenAmount = getRefundingAmount(msg.sender);
if (refundingTokenAmount > 0) {
userInfo[msg.sender].refunded = true;
safeTransferStakeInternal(msg.sender, refundingTokenAmount);
}
userInfo[msg.sender].claimed[harvestPeriod] = true;
// Subtract user debt after refund on initial harvest
if(harvestPeriod == 0) {
totalDebt -= userInfo[msg.sender].amount;
}
uint256 offeringTokenAmountPerPeriod = getOfferingAmountPerPeriod(msg.sender);
offeringToken.safeTransfer(msg.sender, offeringTokenAmountPerPeriod);
emit Harvest(msg.sender, offeringTokenAmountPerPeriod, refundingTokenAmount);
}
function hasHarvested(address _user, uint256 harvestPeriod) external view returns (bool) {
return userInfo[_user].claimed[harvestPeriod];
}
/// @notice Calculate a users allocation based on the total amount deposited. This is done
/// by first scaling the deposited amount and dividing by the total amount.
/// @param _user Address of the user allocation to look up
/// @notice This function has been deprecated, but leaving in the contract for backwards compatibility.
function getUserAllocation(address _user) external view returns (uint256) {
// avoid division by zero
if(totalAmount == 0) {
return 0;
}
// allocation:
// 1e12 = 100%
// 1e10 = 1%
// 1e8 = 0.01%
return (userInfo[_user].amount * 1e12 / totalAmount);
}
function getTotalStakeTokenBalance() public view returns (uint256) {
if(isNativeTokenStaking) {
return address(this).balance;
} else {
// Return ERC20 balance
return stakeToken.balanceOf(address(this));
}
}
/// @notice Calculate a user's offering amount to be received by multiplying the offering amount by
/// the user allocation percentage.
/// @param _user Address of the user allocation to look up
function getOfferingAmount(address _user) public view returns (uint256) {
if (totalAmount > raisingAmount) {
return (userInfo[_user].amount * offeringAmount) / totalAmount;
} else {
// Return an offering amount equal to a proportion of the raising amount
return (userInfo[_user].amount * offeringAmount) / raisingAmount;
}
}
// get the amount of IAO token you will get per harvest period
function getOfferingAmountPerPeriod(address _user) public view returns (uint256) {
return getOfferingAmount(_user) / HARVEST_PERIODS;
}
/// @notice Calculate a user's refunding amount to be received by multiplying the raising amount by
/// the user allocation percentage.
/// @param _user Address of the user allocation to look up
function getRefundingAmount(address _user) public view returns (uint256) {
// Users are able to obtain their refund on the first harvest only
if (totalAmount <= raisingAmount || userInfo[_user].refunded == true) {
return 0;
}
uint256 userAmount = userInfo[_user].amount;
uint256 payAmount = (userAmount * raisingAmount) / totalAmount;
return userAmount - payAmount;
}
/// @notice Get the amount of tokens a user is eligible to receive based on current state.
/// @param _user address of user to obtain token status
/// @notice offeringTokensVested should be named offeringTokensVesting. Leaving for backward compatibility
function userTokenStatus(address _user)
public
view
returns (
uint256 stakeTokenHarvest,
uint256 offeringTokenHarvest,
uint256 offeringTokensVested
)
{
uint256 currentBlock = block.number;
if(currentBlock < endBlock) {
return (0,0,0);
}
stakeTokenHarvest = getRefundingAmount(_user);
uint256 userOfferingPerPeriod = getOfferingAmountPerPeriod(_user);
for (uint256 i = 0; i < HARVEST_PERIODS; i++) {
if(currentBlock >= harvestReleaseBlocks[i] && !userInfo[_user].claimed[i]) {
// If offering tokens are available for harvest AND user has not claimed yet
offeringTokenHarvest += userOfferingPerPeriod;
} else if (currentBlock < harvestReleaseBlocks[i]) {
// If harvest period is in the future
offeringTokensVested += userOfferingPerPeriod;
}
}
return (stakeTokenHarvest, offeringTokenHarvest, offeringTokensVested);
}
function getAddressListLength() external view returns (uint256) {
return addressList.length;
}
function finalWithdraw(uint256 _stakeTokenAmount, uint256 _offerAmount)
external
onlyAdmin
{
safeTransferStakeInternal(msg.sender, _stakeTokenAmount);
if(_offerAmount > 0) {
require(
_offerAmount <= offeringToken.balanceOf(address(this)),
"not enough offering token"
);
offeringToken.safeTransfer(msg.sender, _offerAmount);
}
emit AdminFinalWithdraw(_stakeTokenAmount, _offerAmount);
}
/// @notice Internal function to handle stake token transfers. Depending on the stake
/// token type, this can transfer ERC-20 tokens or native EVM tokens.
/// @param _to address to send stake token to
/// @param _amount value of reward token to transfer
function safeTransferStakeInternal(address _to, uint256 _amount) internal {
if(_amount == 0) { return; }
require(
_amount <= getTotalStakeTokenBalance(),
"not enough stake token"
);
if (isNativeTokenStaking) {
// Transfer native token to address
(bool success, ) = _to.call{gas: 23000, value: _amount}("");
require(success, "TransferHelper: NATIVE_TRANSFER_FAILED");
} else {
// Transfer ERC20 to address
stakeToken.safeTransfer(_to, _amount);
}
}
/// @notice Sweep accidental ERC20 transfers to this contract. Can only be called by admin.
/// @param token The address of the ERC20 token to sweep
function sweepToken(IERC20 token) external onlyAdmin {
require(address(token) != address(stakeToken), "can not sweep stake token");
require(address(token) != address(offeringToken), "can not sweep offering token");
uint256 balance = token.balanceOf(address(this));
token.safeTransfer(msg.sender, balance);
emit EmergencySweepWithdraw(msg.sender, address(token), balance);
}
}