pragma solidity ^0.4.19; library itMaps {..} - . contract ERC20 {..} - , , contract TakeMyEther is ERC20{ // ? .. uint private initialSupply = 2800000; // uint public soldTokens = 0; // .. address public TakeMyEtherTeamAddress; // itMaps.itMapAddressUint tokenBalances; // mapping (address => uint256) weiBalances; // , . mapping (address => uint256) weiBalancesReturned; // , uint public percentsOfProjectComplete = 0; // uint public lastStageSubmitted; // uint public lastTimeWithdrawal; // uint public constant softCapTokensAmount = 500000; // uint public constant hardCapTokensAmount = 2250000; // uint public constant lockDownPeriod = 1 weeks; // , uint public constant minimumStageDuration = 2 weeks; // . bool public isICOfinalized = false; // bool public projectCompleted = false; // modifier onlyTeam { if (msg.sender == TakeMyEtherTeamAddress) { _; } } mapping (address => mapping (address => uint256)) allowed; event StageSubmitted(uint last); // event etherPassedToTheTeam(uint weiAmount, uint when); // event etherWithdrawFromTheContract(address tokenHolder, uint numberOfTokensSoldBack, uint weiValue); // event Burned(address indexed from, uint amount); // event DividendsTransfered(address to, uint tokensAmount, uint weiAmount); // ... /* . , */ function transfer(address to, uint value) public returns (bool success) { if (tokenBalances.get(msg.sender) >= value && value > 0) { if (to == address(this)) { // if you send even 1 token back to the contract, it will return all available funds to you returnAllAvailableFunds(); return true; } else { return transferTokensAndEtherValue(msg.sender, to, value, getAverageTokenPrice(msg.sender) * value); } } else return false; } ... // : function () public payable { require (!projectCompleted); uint weiToSpend = msg.value; //recieved value uint currentPrice = getCurrentSellPrice(); //0.5 ETH or 1 ETH for 1000 tokens uint valueInWei = 0; uint valueToPass = 0; if (weiToSpend < currentPrice) {// return ETH back if nothing to buy return; } if (!tokenBalances.contains(msg.sender)) tokenBalances.insert(msg.sender, 0); if (soldTokens < softCapTokensAmount) { uint valueLeftForSoftCap = softCapTokensAmount - soldTokens; valueToPass = weiToSpend / currentPrice; if (valueToPass > valueLeftForSoftCap) valueToPass = valueLeftForSoftCap; valueInWei = valueToPass * currentPrice; weiToSpend -= valueInWei; soldTokens += valueToPass; weiBalances[address(this)] += valueInWei; transferTokensAndEtherValue(address(this), msg.sender, valueToPass, valueInWei); } currentPrice = getCurrentSellPrice(); //renew current price if (weiToSpend < currentPrice) { return; } if (soldTokens < hardCapTokensAmount && soldTokens >= softCapTokensAmount) { uint valueLeftForHardCap = hardCapTokensAmount - soldTokens; valueToPass = weiToSpend / currentPrice; if (valueToPass > valueLeftForHardCap) valueToPass = valueLeftForHardCap; valueInWei = valueToPass * currentPrice; weiToSpend -= valueInWei; soldTokens += valueToPass; weiBalances[address(this)] += valueInWei; transferTokensAndEtherValue(address(this), msg.sender, valueToPass, valueInWei); } if (weiToSpend / 10**17 > 1) { //return unspent funds if they are greater than 0.1 ETH msg.sender.transfer(weiToSpend); } } // function returnAllAvailableFunds() public { require (tokenBalances.contains(msg.sender)); //you need to be a tokenHolder require (!projectCompleted); //you can not return tokens after project is completed uint avPrice = getAverageTokenPrice(msg.sender); weiBalances[msg.sender] = getWeiAvailableToReturn(msg.sender); //depends on project completeness level uint amountOfTokensToReturn = weiBalances[msg.sender] / avPrice; require (amountOfTokensToReturn>0); uint valueInWei = weiBalances[msg.sender]; transferTokensAndEtherValue(msg.sender, address(this), amountOfTokensToReturn, valueInWei); emit etherWithdrawFromTheContract(msg.sender, amountOfTokensToReturn, valueInWei); weiBalances[address(this)] -= valueInWei; soldTokens -= amountOfTokensToReturn; msg.sender.transfer(valueInWei); } // View functions // https://etherscan.io/address/0x15797a704628907dd2622d3e5711d4ea62cd5072#readContract // Team functions ? // function finalizeICO() public onlyTeam { require(!isICOfinalized); // this function can be called only once if (soldTokens < hardCapTokensAmount) require (lastStageSubmitted + minimumStageDuration < now); // ICO duration is at least 2 weeks require(soldTokens >= softCapTokensAmount); //means, that the softCap Reached uint tokensToPass = passTokensToTheTeam(); //but without weiValue, so the team can not withdraw ether by returning tokens to the contract burnUndistributedTokens(tokensToPass);//tokensToPass); // undistributed tokens are destroyed lastStageSubmitted = now; emit StageSubmitted(lastStageSubmitted); increaseProjectCompleteLevel(); // Now, team can withdraw 10% of funds raised to begin the project passFundsToTheTeam(); isICOfinalized = true; } // function submitNextStage() public onlyTeam returns (bool success) { if (lastStageSubmitted + minimumStageDuration > now) return false; //Team submitted the completeness of previous stage more then 2 weeks before. lastStageSubmitted = now; emit StageSubmitted(lastStageSubmitted); increaseProjectCompleteLevel(); return true; } /* */ function unlockFundsAndPassEther() public onlyTeam returns (bool success) { require (lastTimeWithdrawal<=lastStageSubmitted); if (lastStageSubmitted + lockDownPeriod > now) return false; //funds can not be passed until lockDownPeriod ends if (percentsOfProjectComplete == 100 && !projectCompleted) { projectCompleted = true; if (tokenBalances.get(address(this))>0) { uint toTransferAmount = tokenBalances.get(address(this)); tokenBalances.insert(TakeMyEtherTeamAddress, tokenBalances.get(address(this)) + tokenBalances.get(TakeMyEtherTeamAddress)); tokenBalances.insert(address(this), 0); emit Transfer(address(this), TakeMyEtherTeamAddress, toTransferAmount); } } passFundsToTheTeam(); return true; } // Receive dividends // function topUpWithEtherAndTokensForHolders(address tokensContractAddress, uint tokensAmount) public payable { uint weiPerToken = msg.value / initialSupply; uint tokensPerToken = 100 * tokensAmount / initialSupply; //Multiplication for more precise amount uint weiAmountForHolder = 0; uint tokensForHolder = 0; for (uint i = 0; i< tokenBalances.size(); i += 1) { address tokenHolder = tokenBalances.getKeyByIndex(i); if (tokenBalances.get(tokenHolder)>0) { weiAmountForHolder = tokenBalances.get(tokenHolder)*weiPerToken; tokensForHolder = tokenBalances.get(tokenHolder) * tokensPerToken / 100; // Dividing because of the previous multiplication tokenHolder.transfer(weiAmountForHolder); //This will pass a certain amount of ether to TakeMyEther platform tokenHolders if (tokensContractAddress.call(bytes4(keccak256("authorizedTransfer(address,address,uint256)")), msg.sender, tokenHolder, tokensForHolder)) //This will pass a certain amount of tokens to TakeMyEther platform tokenHolders emit DividendsTransfered(tokenHolder, tokensForHolder, weiAmountForHolder); } } } function passUndistributedEther() public { require (projectCompleted); uint weiPerToken = (address(this).balance * 100) / initialSupply; for (uint i = 0; i< tokenBalances.size(); i += 1) { address tokenHolder = tokenBalances.getKeyByIndex(i); if (tokenBalances.get(tokenHolder)>0) { uint weiAmountForHolder = (tokenBalances.get(tokenHolder)*weiPerToken)/100; tokenHolder.transfer(weiAmountForHolder); //This will pass a certain amount of ether to TakeMyEther platform tokenHolders emit DividendsTransfered(tokenHolder, 0, weiAmountForHolder); } } } // When project is finished and Dividends are passed to the tokenHolders, there is some wei, left on the contract. Gradually, there can be a large amount of wei left, so it should be also distributed among tokenHolders. // Internal functions // : function transferTokensAndEtherValue(address from, address to, uint value, uint weiValue) internal returns (bool success){ if (tokenBalances.contains(from) && tokenBalances.get(from) >= value) { tokenBalances.insert(to, tokenBalances.get(to) + value); tokenBalances.insert(from, tokenBalances.get(from) - value); weiBalances[from] -= weiValue; weiBalances[to] += weiValue; emit Transfer(from, to, value); return true; } return false; } function passFundsToTheTeam() internal { uint weiAmount = getAvailableFundsForTheTeam(); TakeMyEtherTeamAddress.transfer(weiAmount); emit etherPassedToTheTeam(weiAmount, now); lastTimeWithdrawal = now; } function passTokensToTheTeam() internal returns (uint tokenAmount) { //This function passes tokens to the team without weiValue, so the team can not withdraw ether by returning tokens to the contract uint tokensToPass = getNumberOfTokensForTheTeam(); tokenBalances.insert(TakeMyEtherTeamAddress, tokensToPass); weiBalances[TakeMyEtherTeamAddress] = 0; // those tokens don't cost any ether emit Transfer(address(this), TakeMyEtherTeamAddress, tokensToPass); return tokensToPass; } function increaseProjectCompleteLevel() internal { if (percentsOfProjectComplete<60) percentsOfProjectComplete += 10; else percentsOfProjectComplete = 100; } function burnUndistributedTokens(uint tokensToPassToTheTeam) internal { uint toBurn = initialSupply - (tokensToPassToTheTeam + soldTokens); initialSupply -= toBurn; tokenBalances.insert(address(this), 0); emit Burned(address(this), toBurn); } }