-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathKaliDAOloglikeCurve.sol
173 lines (123 loc) · 5.09 KB
/
KaliDAOloglikeCurve.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
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.4;
import '../../libraries/SafeTransferLib.sol';
import '../../interfaces/IERC20minimal.sol';
import '../../interfaces/IKaliAccessManager.sol';
import '../../utils/ReentrancyGuard.sol';
/// @notice Crowdsale contract that receives ETH or tokens to mint registered DAO tokens, including merkle access lists.
/// @dev This is meant to simulate a logarithmic curve with significantly less computation.
contract KaliDAOloglikeCurve is ReentrancyGuard {
using SafeTransferLib for address;
event ExtensionSet(
uint256 indexed listId,
uint256 startingPrice,
uint96 purchaseLimit,
uint96 blockSize,
uint96 blockPriceIncrement,
uint8 velocity,
uint32 saleEnds
);
event ExtensionCalled(address indexed dao, address indexed member, uint256 indexed amountOut);
error SaleEnded();
error NotListed();
error NotPrice();
error PurchaseLimit();
error InvalidVelocity();
IKaliAccessManager public immutable accessManager;
mapping(address => Crowdsale) public crowdsales;
struct Crowdsale {
uint256 listId;
uint256 startingPrice;
uint96 purchaseLimit;
uint96 amountPurchased;
uint96 blockSize;
uint96 blockPriceIncrement;
uint8 velocity;
uint32 saleEnds;
}
constructor(IKaliAccessManager accessManager_) {
accessManager = accessManager_;
}
function setExtension(bytes calldata extensionData) public nonReentrant virtual {
(
uint256 listId,
uint256 startingPrice,
uint96 purchaseLimit,
uint96 blockSize,
uint96 blockPriceIncrement,
uint8 velocity,
uint32 saleEnds
)
= abi.decode(extensionData, (uint256, uint256, uint96, uint96, uint96, uint8, uint32));
if (velocity >= 100 || velocity == 0) revert InvalidVelocity();
crowdsales[msg.sender] = Crowdsale({
listId: listId,
startingPrice: startingPrice,
purchaseLimit: purchaseLimit,
amountPurchased: 0,
blockSize: blockSize,
blockPriceIncrement: blockPriceIncrement,
velocity: velocity,
saleEnds: saleEnds
});
emit ExtensionSet(listId, startingPrice, purchaseLimit, blockSize, blockPriceIncrement, velocity, saleEnds);
}
function callExtension(
address account,
uint256 amount,
bytes calldata
) public payable nonReentrant virtual returns (bool mint, uint256 amountOut) {
Crowdsale storage sale = crowdsales[msg.sender];
if (block.timestamp > sale.saleEnds) revert SaleEnded();
if (sale.listId != 0)
if (accessManager.balanceOf(msg.sender, sale.listId) == 0) revert NotListed();
uint256 estPrice = estimatePrice(sale, amount);
if (msg.value != estPrice) revert NotPrice();
amountOut = amount;
if (sale.amountPurchased + amountOut > sale.purchaseLimit) revert PurchaseLimit();
// send ETH to DAO
msg.sender._safeTransferETH(msg.value);
sale.amountPurchased += uint96(amountOut);
mint = true;
emit ExtensionCalled(msg.sender, account, amountOut);
}
function estimatePrice(Crowdsale memory sale, uint256 amount) public view returns (uint256) {
uint256 totalSupply = IERC20minimal(msg.sender).totalSupply();
uint256 used = totalSupply % sale.blockSize;
uint256 remainingInBlock = sale.blockSize - used;
uint256 currentPrice = sale.startingPrice;
uint256 currentBlock = totalSupply / sale.blockSize;
uint256 increment = sale.blockPriceIncrement;
for (uint256 i = 0; i < currentBlock; i++) {
increment = changeIncrement(sale, increment);
currentPrice += increment;
}
uint256 estTotal;
if (amount <= remainingInBlock) {
estTotal = amount * currentPrice;
} else {
estTotal += remainingInBlock * currentPrice;
currentBlock = totalSupply / sale.blockSize;
increment = sale.blockPriceIncrement;
for (uint256 i = 0; i < currentBlock; i++) {
increment = changeIncrement(sale, increment);
}
currentPrice += increment;
uint256 remainingAmount = amount - remainingInBlock;
uint256 remainder = remainingAmount % sale.blockSize;
uint256 blocksRemaining = remainingAmount / sale.blockSize;
for (uint256 i = 0; i < blocksRemaining; i++) {
estTotal += currentPrice * sale.blockSize;
increment = changeIncrement(sale, increment);
currentPrice += increment;
}
if (remainder != 0) {
estTotal += remainder * currentPrice;
}
}
return estTotal;
}
function changeIncrement(Crowdsale memory sale, uint256 increment) public pure returns (uint256) {
return (increment * sale.velocity) / 100;
}
}