forked from miladmostavi/gnosis-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAODutchAuction.sol
212 lines (191 loc) · 5.44 KB
/
DAODutchAuction.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
pragma solidity ^0.4.0;
import "Tokens/AbstractToken.sol";
/// @title DAO Dutch auction contract - Sale of Gnosis tokens.
/// @author Stefan George - <stefan.george@consensys.net>
contract DAODutchAuction {
/*
* Events
*/
event BidSubmission(address indexed investor, uint256 amount);
/*
* External contracts
*/
Token public daoToken;
/*
* Constants
*/
uint constant public WAITING_PERIOD = 7 days;
/*
* Storage
*/
address public tokenWallet;
address public etherWallet;
address public owner;
uint public startBlock;
uint public endTime;
uint public totalRaised;
uint public finalPrice;
// user => amount
mapping (address => uint) public bids;
Stages public stage = Stages.AuctionStarted;
enum Stages {
AuctionStarted,
AuctionEnded
}
/*
* Modifiers
*/
modifier atStage(Stages _stage) {
if (stage != _stage) {
// Contract not in expected state
throw;
}
_;
}
modifier isOwner() {
if (msg.sender != owner) {
// Only owner is allowed to proceed
throw;
}
_;
}
modifier timedTransitions() {
if (stage == Stages.AuctionStarted && calcTokenPrice() <= calcStopPrice()) {
finalizeAuction();
}
_;
}
/*
* Constants
*/
uint constant public FUNDING_GOAL = 1250000 ether;
uint constant public TOTAL_TOKENS = 10000000; // 10M
uint constant public MAX_TOKENS_SOLD = 9000000; // 9M
/*
* Read and write functions
*/
/// @dev Allows to send a bid to the auction.
function bid()
public
payable
timedTransitions
atStage(Stages.AuctionStarted)
{
uint investment = msg.value;
if (totalRaised + investment > FUNDING_GOAL) {
investment = FUNDING_GOAL - totalRaised;
// Send change back
if (!msg.sender.send(msg.value - investment)) {
// Sending failed
throw;
}
}
// Forward funding to ether wallet
if (investment == 0 || !etherWallet.send(investment)) {
// No investment done or sending failed
throw;
}
bids[msg.sender] += investment;
totalRaised += investment;
if (totalRaised == FUNDING_GOAL) {
finalizeAuction();
}
BidSubmission(msg.sender, investment);
}
function finalizeAuction()
private
{
stage = Stages.AuctionEnded;
if (totalRaised == FUNDING_GOAL) {
finalPrice = calcTokenPrice();
}
else {
finalPrice = calcStopPrice();
}
uint soldTokens = totalRaised * 10**18 / finalPrice;
// Auction contract transfers all unsold tokens to founders' multisig-wallet
daoToken.transfer(tokenWallet, TOTAL_TOKENS * 10**18 - soldTokens);
endTime = block.timestamp;
}
/// @dev Claims tokens for bidder after auction.
function claimTokens()
public
timedTransitions
atStage(Stages.AuctionEnded)
{
uint tokenCount = bids[msg.sender] * 10**18 / finalPrice;
bids[msg.sender] = 0;
daoToken.transfer(msg.sender, tokenCount);
}
/// @dev Setup function sets external contracts' addresses.
/// @param _daoToken DAO token address.
/// @param _tokenWallet DAO founders address.
function setup(address _daoToken, address _tokenWallet, address _etherWallet)
external
isOwner
{
if (tokenWallet != 0 || etherWallet != 0 || address(daoToken) != 0) {
// Setup was executed already
throw;
}
tokenWallet = _tokenWallet;
etherWallet = _etherWallet;
daoToken = Token(_daoToken);
}
/// @dev Contract constructor function sets start date.
function DAODutchAuction() {
startBlock = block.number;
owner = msg.sender;
}
/*
* Read functions
*/
/// @dev Calculates stop price.
/// @return stopPrice Returns stop price.
function calcStopPrice()
constant
public
returns (uint stopPrice)
{
return totalRaised / MAX_TOKENS_SOLD;
}
/// @dev Calculates token price.
/// @return tokenPrice Returns token price.
function calcTokenPrice()
constant
public
returns (uint tokenPrice)
{
return 20000 * 1 ether / (block.number - startBlock + 1);
}
/// @dev Returns if one week after auction passed.
/// @return launched Returns if one week after auction passed.
function tokenLaunched()
external
timedTransitions
returns (bool launched)
{
return endTime + WAITING_PERIOD < block.timestamp;
}
/// @dev Returns correct stage, even if a function with timedTransitions modifier has not yet been called successfully.
/// @return _stage Returns current auction stage.
function updateStage()
external
timedTransitions
returns (Stages _stage)
{
return stage;
}
/// @dev Calculates current token price.
/// @return tokenPrice Returns token price.
function calcCurrentTokenPrice()
external
timedTransitions
returns (uint tokenPrice)
{
if (stage == Stages.AuctionEnded) {
return finalPrice;
}
return calcTokenPrice();
}
}