This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
KaliDAOcrowdsale.sol
97 lines (66 loc) · 3.3 KB
/
KaliDAOcrowdsale.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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.4;
import '../../libraries/SafeTransferLib.sol';
import '../../interfaces/IKaliDAOextension.sol';
import '../../interfaces/IKaliWhitelistManager.sol';
import '../../utils/ReentrancyGuard.sol';
/// @notice Crowdsale contract that receives ETH or tokens to mint registered DAO tokens, including merkle whitelisting.
contract KaliDAOcrowdsale is ReentrancyGuard {
using SafeTransferLib for address;
event ExtensionSet(address dao, uint256 listId, address purchaseToken, uint8 purchaseMultiplier, uint96 purchaseLimit, uint32 saleEnds);
event ExtensionCalled(address indexed dao, address indexed member, uint256 indexed amountOut);
error NullMultiplier();
error SaleEnded();
error NotWhitelisted();
error PurchaseLimit();
IKaliWhitelistManager public immutable whitelistManager;
mapping(address => Crowdsale) public crowdsales;
struct Crowdsale {
uint256 listId;
address purchaseToken;
uint8 purchaseMultiplier;
uint96 purchaseLimit;
uint96 amountPurchased;
uint32 saleEnds;
}
constructor(IKaliWhitelistManager whitelistManager_) {
whitelistManager = whitelistManager_;
}
function setExtension(bytes calldata extensionData) public nonReentrant virtual {
(uint256 listId, address purchaseToken, uint8 purchaseMultiplier, uint96 purchaseLimit, uint32 saleEnds)
= abi.decode(extensionData, (uint256, address, uint8, uint96, uint32));
if (purchaseMultiplier == 0) revert NullMultiplier();
crowdsales[msg.sender] = Crowdsale({
listId: listId,
purchaseToken: purchaseToken,
purchaseMultiplier: purchaseMultiplier,
purchaseLimit: purchaseLimit,
amountPurchased: 0,
saleEnds: saleEnds
});
emit ExtensionSet(msg.sender, listId, purchaseToken, purchaseMultiplier, purchaseLimit, saleEnds);
}
function callExtension(address dao, uint256 amount) public payable nonReentrant virtual returns (uint256 amountOut) {
Crowdsale storage sale = crowdsales[dao];
bytes memory extensionData = abi.encode(true);
if (block.timestamp > sale.saleEnds) revert SaleEnded();
if (sale.listId != 0)
if (!whitelistManager.whitelistedAccounts(sale.listId, msg.sender)) revert NotWhitelisted();
if (sale.purchaseToken == address(0)) {
amountOut = msg.value * sale.purchaseMultiplier;
if (sale.amountPurchased + amountOut > sale.purchaseLimit) revert PurchaseLimit();
// send ETH to DAO
dao._safeTransferETH(msg.value);
sale.amountPurchased += uint96(amountOut);
IKaliDAOextension(dao).callExtension(msg.sender, amountOut, extensionData);
} else {
// send tokens to DAO
sale.purchaseToken._safeTransferFrom(msg.sender, dao, amount);
amountOut = amount * sale.purchaseMultiplier;
if (sale.amountPurchased + amountOut > sale.purchaseLimit) revert PurchaseLimit();
sale.amountPurchased += uint96(amountOut);
IKaliDAOextension(dao).callExtension(msg.sender, amountOut, extensionData);
}
emit ExtensionCalled(msg.sender, dao, amountOut);
}
}