-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenSale.sol
160 lines (135 loc) · 6.28 KB
/
TokenSale.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
pragma solidity ^0.4.10;
import './ENKToken.sol';
import './Pausable.sol';
import './BTC.sol';
import './Utils.sol';
import './SafeMath.sol';
import './PricingStrategy.sol';
import './Ownable.sol';
import './Sales.sol';
contract TokenSale is Ownable,Pausable, Utils,Sales{
ENKToken token;
uint256 public initialSupplyPreSale;
uint256 public initialSupplyPublicSale;
PricingStrategy pricingstrategy;
uint256 public tokenCreationMax = 15*(10**5) * (10**18);
///token creation max for the pre ico state
///token creation max for the public sale
///tokens for bonus
uint256 public bonus = 25 * (10**6) * (10**18);
///team
uint256 public team = 35 * (10**6) * (10**18);
///tokens for reserve
uint256 public affiliate = 40 * (10**6) * (10**18);
///tokens for the mentors
uint256 public advisors = 30 * (10**6) * (10**18);
///tokkens for the bounty
uint256 public bounty = 20*(10**6) * (10**18);
///address for the teeam,investores,etc
address public addressbonus = "";
address public addressteam = "";
address public addressaffiliate = "";
address public addressVestedAdvisors = "";
address public addressbounty = "";
///array of addresses for the ethereum relateed back funding contract
uint256 public numberOfBackers;
/* Max investment count when we are still allowed to change the multisig address */
///the txorigin is the web3.eth.coinbase account
//record Transactions that have claimed ether to prevent the replay attacks
//to-do
mapping(uint256 => bool) transactionsClaimed;
uint256 public valueToBeSent;
uint public investorCount;
////thr owner address
address public ownerAddr = "";
///the event log to log out the address of the multisig wallet
event logaddr(address addr);
//the constructor function
function TokenSale(address tokenAddress,address strategy){
//require(bytes(_name).length > 0 && bytes(_symbol).length > 0); // validate input
token = ENKToken(tokenAddress);
valueToBeSent = token.valueToBeSent();
pricingstrategy = PricingStrategy(strategy);
}
/**
Payable function to send the ether funds
**/
function() external payable stopInEmergency{
///This is the main payable function
///get the current state of the ico
bool isValid = token.isValid();
if(!isValid) throw;
ICOSaleState currentState = getStateFunding();
if(currentState==ICOSaleState.Failed) throw;
if (msg.value == 0) throw;
var (discount,usd,tokens) = pricingstrategy.totalDiscount(currentState,msg.value,"ethereum",initialSupplyPublicSale);
uint256 totalTokens = SafeMath.add(tokens,SafeMath.div(SafeMath.mul(tokens,discount),100));
if(currentState==ICOSaleState.PreSale){
require(SafeMath.add(initialSupplyPreSale,totalTokens)<token.tokenCreationMaxPreSale());
initialSupplyPreSale = SafeMath.add(initialSupplyPreSale,totalTokens);
}else{
require(SafeMath.add(initialSupplyPublicSale,totalTokens)<token.tokenCreationMaxPublicSale());
initialSupplyPublicSale = SafeMath.add(initialSupplyPublicSale,totalTokens);
}
tokenCreationMax = SafeMath.sub(tokenCreationMax,totalTokens);
token.addToBalances(msg.sender,totalTokens);
token.increaseEthRaised(msg.value);
numberOfBackers++;
token.increaseUSDRaised(usd);
if(!ownerAddr.send(this.balance))throw;
}
//Token distribution for the case of the ICO
///function to run when the transaction has been veified
function processTransaction(bytes txn, uint256 txHash,address addr,bytes20 btcaddr) onlyOwner returns (uint)
{
bool valueSent;
bool isValid = token.isValid();
if(!isValid) throw;
ICOSaleState currentState = getStateFunding();
if(!transactionsClaimed[txHash]){
var (a,b) = BTC.checkValueSent(txn,btcaddr,valueToBeSent);
if(a){
valueSent = true;
transactionsClaimed[txHash] = true;
///since we are creating tokens we need to increase the total supply
allottTokensBTC(addr,b,currentState);
return 1;
}
}
}
///function to allot tokens to address
function allottTokensBTC(address addr,uint256 value,ICOSaleState state) internal{
ICOSaleState currentState = getStateFunding();
if(currentState==ICOSaleState.Failed) throw;
var (discount,usd,tokens) = pricingstrategy.totalDiscount(state,value,"bitcoin",initialSupplyPublicSale);
uint256 totalTokens = SafeMath.add(tokens,SafeMath.div(SafeMath.mul(tokens,discount),100));
if(currentState==ICOSaleState.PreSale){
require(SafeMath.add(initialSupplyPreSale,totalTokens)<token.tokenCreationMaxPreSale());
initialSupplyPreSale = SafeMath.add(initialSupplyPreSale,totalTokens);
}else{
require(SafeMath.add(initialSupplyPublicSale,totalTokens)<token.tokenCreationMaxPublicSale());
initialSupplyPublicSale = SafeMath.add(initialSupplyPublicSale,totalTokens);
}
tokenCreationMax = SafeMath.sub(tokenCreationMax,totalTokens);
token.addToBalances(addr,totalTokens);
numberOfBackers++;
token.increaseBTCRaised(value);
token.increaseUSDRaised(usd);
}
function finalizeTokenSale() public onlyOwner{
ICOSaleState currentState = getStateFunding();
if(currentState!=ICOSaleState.Success) throw;
token.addToVesting(addressbonus,bonus);
token.addToBalances(addressteam,team);
token.addToBalances(addressaffiliate,affiliate);
token.addToVesting(addressVestedAdvisors,advisors);
token.addToVesting(addressbounty,bounty);
token.finalizeICO();
}
function getStateFunding() returns (ICOSaleState){
if(now>token.fundingStartBlock() && now<=token.presaleEndBlock()) return ICOSaleState.PreSale;
if(now>token.presaleEndBlock() && now<=token.fundingEndBlock()) return ICOSaleState.PublicSale;
if(now>token.fundingEndBlock() && token.usdraised()<token.minCapUSD()) return ICOSaleState.Failed;
if(now>token.fundingEndBlock() && token.usdraised()>=token.minCapUSD()) return ICOSaleState.Success;
}
}