Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.75 KB | None | 0 0
  1. pragma solidity 0.4.19;
  2.  
  3. library SafeMath {
  4. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  5. uint256 c = a * b;
  6. assert(a == 0 || c / a == b);
  7. return c;
  8. }
  9.  
  10. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  11. // assert(b > 0); // Solidity automatically throws when dividing by 0
  12. uint256 c = a / b;
  13. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  14. return c;
  15. }
  16.  
  17. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  18. assert(b <= a);
  19. return a - b;
  20. }
  21.  
  22. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  23. uint256 c = a + b;
  24. assert(c >= a);
  25. return c;
  26. }
  27. }
  28.  
  29. contract Ownable {
  30. address public owner;
  31.  
  32.  
  33. event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
  34.  
  35.  
  36. /**
  37. * @dev The Ownable constructor sets the original `owner` of the contract to the sender
  38. * account.
  39. */
  40. function Ownable() public {
  41. owner = msg.sender;
  42. }
  43.  
  44.  
  45. /**
  46. * @dev Throws if called by any account other than the owner.
  47. */
  48. modifier onlyOwner() {
  49. require(msg.sender == owner);
  50. _;
  51. }
  52.  
  53.  
  54. /**
  55. * @dev Allows the current owner to transfer control of the contract to a newOwner.
  56. * @param newOwner The address to transfer ownership to.
  57. */
  58. function transferOwnership(address newOwner) onlyOwner public {
  59. require(newOwner != address(0));
  60. OwnershipTransferred(owner, newOwner);
  61. owner = newOwner;
  62. }
  63.  
  64. }
  65.  
  66. contract Haltable is Ownable {
  67.  
  68. // public variables
  69. bool public halted;
  70.  
  71. modifier stopInEmergency() {
  72. require(!halted);
  73.  
  74. _;
  75. }
  76.  
  77. modifier stopInEmergencyNonOwners() {
  78. if(halted && msg.sender != owner)
  79. revert();
  80.  
  81. _;
  82. }
  83.  
  84. function halt() external onlyOwner {
  85. halted = true;
  86. }
  87.  
  88. function unhalt() external onlyOwner {
  89. halted = false;
  90. }
  91.  
  92. }
  93.  
  94. contract ERC20Basic {
  95. uint256 public totalSupply;
  96. function balanceOf(address who) public constant returns (uint256);
  97. function transfer(address to, uint256 value) public returns (bool);
  98. event Transfer(address indexed from, address indexed to, uint256 value);
  99. }
  100.  
  101. contract BasicToken is ERC20Basic {
  102. using SafeMath for uint256;
  103.  
  104. mapping(address => uint256) balances;
  105.  
  106. /**
  107. * @dev transfer token for a specified address
  108. * @param _to The address to transfer to.
  109. * @param _value The amount to be transferred.
  110. */
  111. function transfer(address _to, uint256 _value) public returns (bool) {
  112. require(_to != address(0));
  113.  
  114. // SafeMath.sub will throw if there is not enough balance.
  115. balances[msg.sender] = balances[msg.sender].sub(_value);
  116. balances[_to] = balances[_to].add(_value);
  117. Transfer(msg.sender, _to, _value);
  118. return true;
  119. }
  120.  
  121. /**
  122. * @dev Gets the balance of the specified address.
  123. * @param _owner The address to query the the balance of.
  124. * @return An uint256 representing the amount owned by the passed address.
  125. */
  126. function balanceOf(address _owner) public constant returns (uint256 balance) {
  127. return balances[_owner];
  128. }
  129.  
  130. }
  131.  
  132. contract FriendzToken is BasicToken, Ownable {
  133.  
  134. // public variables
  135. mapping(address => uint256) public release_dates;
  136. mapping(address => uint256) public purchase_dates;
  137. mapping(address => uint256) public blocked_amounts;
  138. mapping (address => mapping (address => uint256)) public allowed;
  139. bool public free_transfer = false;
  140.  
  141. // private variables
  142. address private co_owner;
  143.  
  144. // ERC20 variables
  145. string public name;
  146. string public symbol;
  147. uint256 public decimals;
  148.  
  149. event Approval(address indexed owner, address indexed spender, uint256 value);
  150.  
  151. function FriendzToken(string _name, string _symbol, uint256 _decimals, uint256 _supply) public {
  152. // safety checks
  153. require(_decimals > 0);
  154. require(_supply > 0);
  155.  
  156. // set owner
  157. owner = msg.sender;
  158.  
  159. // assign variables
  160. name = _name;
  161. symbol = _symbol;
  162. decimals = _decimals;
  163. totalSupply = _supply * 10 ** decimals;
  164.  
  165. // assign the total supply to the owner
  166. balances[owner] = totalSupply;
  167. }
  168.  
  169. // modifiers
  170.  
  171. // checks if the address can transfer tokens
  172. modifier canTransfer(address _sender, uint256 _value) {
  173. uint256 after_math = balances[_sender].sub(_value);
  174. require(
  175. (free_transfer) ||
  176. (
  177. _sender != address(0) &&
  178. after_math >= getMinimumAmount(_sender)
  179. )
  180. );
  181.  
  182. _;
  183. }
  184.  
  185. // check if we're in a free-transfter state
  186. modifier isFreeTransfer() {
  187. require(free_transfer);
  188.  
  189. _;
  190. }
  191.  
  192. // check if we're in non free-transfter state
  193. modifier isBlockingTransfer() {
  194. require(!free_transfer);
  195.  
  196. _;
  197. }
  198.  
  199. // functions
  200.  
  201. // set co-owner
  202. function setCoOwner(address _addr) onlyOwner public {
  203. co_owner = _addr;
  204. }
  205.  
  206. // set token informations
  207. function setInformations(string _name, string _symbol) onlyOwner public {
  208. require(bytes(_name).length != 0);
  209. require(bytes(_symbol).length != 0);
  210.  
  211. name = _name;
  212. symbol = _symbol;
  213. }
  214.  
  215. // calculate the amount of tokens an address can use
  216. function getMinimumAmount(address _addr) constant public returns (uint256) {
  217. // if the address ha no limitations just return 0
  218. if(blocked_amounts[_addr] == 0x0)
  219. return 0x0;
  220.  
  221. uint256 alpha = uint256(now).sub(purchase_dates[_addr]); // absolute purchase date
  222. uint256 beta = release_dates[_addr].sub(purchase_dates[_addr]); // absolute token release date
  223. uint256 tokens = blocked_amounts[_addr].sub(alpha.mul(blocked_amounts[_addr]).div(beta)); // T - (α * T) / β
  224.  
  225. return tokens;
  226. }
  227.  
  228. // set blocking state to an address
  229. function setBlockingState(address _addr, uint256 _end, uint256 _value) isBlockingTransfer public {
  230. // only the onwer and the co-owner can call this function
  231. require(
  232. msg.sender == owner ||
  233. msg.sender == co_owner
  234. );
  235. require(_addr != address(0));
  236.  
  237. if(release_dates[_addr] != 0x0){
  238. // if it's not the first time this function is beign called for this address
  239. // update its information instead of setting them (add value to previous value)
  240. release_dates[_addr] = _end;
  241. purchase_dates[_addr] = now;
  242. blocked_amounts[_addr] = blocked_amounts[_addr].add(_value);
  243. }else{
  244. release_dates[_addr] = _end;
  245. purchase_dates[_addr] = now;
  246. blocked_amounts[_addr] = _value;
  247. }
  248. }
  249.  
  250. // all addresses can transfer tokens now
  251. function freeToken() public onlyOwner {
  252. free_transfer = true;
  253. }
  254.  
  255. // override function using canTransfer on the sender address
  256. function transfer(address _to, uint _value) canTransfer(msg.sender, _value) public returns (bool success) {
  257. return super.transfer(_to, _value);
  258. }
  259.  
  260. // transfer tokens from one address to another
  261. function transferFrom(address _from, address _to, uint _value) canTransfer(_from, _value) public returns (bool success) {
  262. require(_from != address(0));
  263. require(_to != address(0));
  264.  
  265. // SafeMath.sub will throw if there is not enough balance.
  266. balances[_from] = balances[_from].sub(_value);
  267. balances[_to] = balances[_to].add(_value);
  268. allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value); // this will throw if we don't have enough allowance
  269.  
  270. // this event comes from BasicToken.sol
  271. Transfer(_from, _to, _value);
  272.  
  273. return true;
  274. }
  275.  
  276. // erc20 functions
  277. function approve(address _spender, uint256 _value) public returns (bool) {
  278. require(_value == 0 || allowed[msg.sender][_spender] == 0);
  279.  
  280. allowed[msg.sender][_spender] = _value;
  281. Approval(msg.sender, _spender, _value);
  282.  
  283. return true;
  284. }
  285.  
  286. function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {
  287. return allowed[_owner][_spender];
  288. }
  289.  
  290. /**
  291. * approve should be called when allowed[_spender] == 0. To increment
  292. * allowed value is better to use this function to avoid 2 calls (and wait until
  293. * the first transaction is mined)
  294. * From MonolithDAO Token.sol
  295. */
  296. function increaseApproval (address _spender, uint256 _addedValue) public returns (bool success) {
  297. allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);
  298. Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  299. return true;
  300. }
  301.  
  302. function decreaseApproval (address _spender, uint256 _subtractedValue) public returns (bool success) {
  303. uint256 oldValue = allowed[msg.sender][_spender];
  304. if (_subtractedValue > oldValue) {
  305. allowed[msg.sender][_spender] = 0;
  306. } else {
  307. allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
  308. }
  309. Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
  310. return true;
  311. }
  312.  
  313. }
  314.  
  315. contract Sale is Haltable {
  316.  
  317. // usings
  318. using SafeMath for uint256;
  319.  
  320. // public variables
  321. FriendzToken public token;
  322. address public wallet;
  323. uint256 public rate;
  324. string public name;
  325. mapping (address => uint256) invested_amount_wei;
  326. mapping (address => uint256) invested_amount_tokens;
  327.  
  328. uint256 public raised_wei = 0;
  329. uint256 public raised_tokens = 0;
  330. uint256 public investor_count = 0;
  331.  
  332. // events
  333. event Invested(address indexed investor, uint256 wei_amount, uint256 token_amout);
  334.  
  335. function Sale(string _name, address _token, address _wallet, uint256 _rate) public {
  336. // safety checks
  337. require(_token != address(0));
  338. require(_wallet != address(0));
  339. require(_rate > 0);
  340.  
  341. // set owner
  342. owner = msg.sender;
  343.  
  344. // assign variables
  345. token = FriendzToken(_token);
  346. wallet = _wallet;
  347. rate = _rate;
  348. name = _name;
  349. }
  350.  
  351. // assign tokens to the investor, abstract
  352. function assignTokens(address _to, uint256 _amount) internal;
  353. }
  354.  
  355. contract PreSale is Sale {
  356.  
  357. // public variables
  358. address public holder;
  359. uint256 public start_date;
  360. uint256 public end_date;
  361. uint256 public discount;
  362. uint256 public available_tokens;
  363.  
  364. // constants
  365. uint256 constant MINIMUM_INVESTMENT = 10 * 1 ether;
  366.  
  367. // events
  368. event Invested(address indexed investor, uint256 wei_amount, uint256 token_amout);
  369.  
  370. function PreSale(address _holder, string _name, address _token, address _wallet, uint256 _rate, uint256 _discount, uint256 _available_tokens, uint256 _start_date, uint256 _end_date)
  371. Sale(_name, _token, _wallet, _rate) public // extends 'Sale' contract
  372. {
  373. // safety checks
  374. require(_holder != address(0));
  375. require(_available_tokens > 0);
  376. require(_start_date > 0);
  377. require(_end_date > 0);
  378. require(_start_date < _end_date);
  379.  
  380. // assign variables
  381. holder = _holder;
  382. discount = _discount;
  383. available_tokens = _available_tokens;
  384. start_date = _start_date;
  385. end_date = _end_date;
  386. }
  387.  
  388. // checks if the presale is finished or not
  389. modifier canInvest() {
  390. require(now >= start_date);
  391. require(now < end_date);
  392.  
  393. _;
  394. }
  395.  
  396. // chcks if the presale is finished
  397. modifier isFinished() {
  398. require(now >= end_date);
  399.  
  400. _;
  401. }
  402.  
  403. // fallback function, the only one that can receive payments
  404. function () payable external {
  405. investInternal(msg.sender);
  406. }
  407.  
  408. // internal function for handling payments
  409. function investInternal(address _to) canInvest stopInEmergency internal returns(uint256 tokens) {
  410. // safety checks
  411. require(_to != address(0));
  412. require(msg.value >= MINIMUM_INVESTMENT);
  413.  
  414. uint256 value_wei = msg.value;
  415. uint256 new_rate = rate.mul(discount.add(100)).div(100);
  416. uint256 value_tokens = value_wei.mul(new_rate);
  417.  
  418. require(value_tokens > 0);
  419.  
  420. // increase investor count only if it's their the first time
  421. if(invested_amount_wei[_to] == 0){
  422. investor_count = investor_count.add(1);
  423. }
  424.  
  425. // update investor
  426. invested_amount_wei[_to] = invested_amount_wei[_to].add(value_wei);
  427. invested_amount_tokens[_to] = invested_amount_tokens[_to].add(value_tokens);
  428.  
  429. // update amounts
  430. raised_wei = raised_wei.add(value_wei);
  431. raised_tokens = raised_tokens.add(value_tokens);
  432.  
  433. // this will throw an error and revert in case 'value_tokens' is greater than 'available_tokens'
  434. available_tokens = available_tokens.sub(value_tokens);
  435.  
  436. // send ether to the wallet
  437. wallet.transfer(value_wei);
  438.  
  439. // assign tokens to investor
  440. assignTokens(_to, value_tokens);
  441.  
  442. Invested(_to, value_wei, value_tokens);
  443.  
  444. return value_tokens;
  445. }
  446.  
  447. // internal function for assigning tokens to an address
  448. function assignTokens(address _to, uint256 _value) internal {
  449. if(!token.transferFrom(holder, _to, _value))
  450. revert();
  451. }
  452.  
  453. // once the presale is finished we kill the contract and remove it from the blockchain.
  454. // all remaining ethers in the contract (shouldn't be any) will be send to '_address'
  455. function kill(address _address) onlyOwner isFinished public {
  456. selfdestruct(_address);
  457. }
  458. }
  459.  
  460. contract Reservation is Haltable {
  461.  
  462. // usings
  463. using SafeMath for uint256;
  464.  
  465. // public variables
  466. uint256 public maximum;
  467. uint256 public rate;
  468. uint256 public start;
  469. uint256 public end;
  470. address[] public investors;
  471. mapping (address => uint256) public balances;
  472. mapping (address => bool) public invested;
  473. mapping (address => bool) public whitelist;
  474.  
  475. // private variables
  476. Crowdsale private ico; // we don't want to show the final ico address
  477.  
  478. // constants
  479. uint256 constant MINIMUM_INVESTMENT = 0.5 * 1 ether;
  480. uint256 constant KYC_REQUIRED = 20 * 1 ether;
  481.  
  482. // events
  483. event Invested(address indexed sender, uint256 amount, uint256 token_amount);
  484. event Whitelisted(address indexed investor);
  485.  
  486. function Reservation(uint256 _maximum, uint256 _rate, uint256 _start, uint256 _end) public {
  487. // safety checks
  488. require(_maximum > 0);
  489. require(_rate > 0);
  490. require(_start > 0);
  491. require(_end > 0);
  492. require(_start < _end);
  493.  
  494. // set owner
  495. owner = msg.sender;
  496.  
  497. // set variables
  498. maximum = _maximum;
  499. rate = _rate;
  500. start = _start;
  501. end = _end;
  502. }
  503.  
  504. // checks if the reservation is finished
  505. modifier isFinished() {
  506. require(now >= end);
  507.  
  508. _;
  509. }
  510.  
  511. // invest etheres
  512. function () payable external {
  513. require(this.balance < maximum);
  514. require(now >= start);
  515. require(now < end);
  516.  
  517. // to invest more than 20 ethers you need to be whitelisted
  518. require(
  519. msg.value >= MINIMUM_INVESTMENT &&
  520. (
  521. msg.value < KYC_REQUIRED ||
  522. whitelist[msg.sender] == true
  523. )
  524. );
  525.  
  526. uint256 tokens = uint256(msg.value).mul(rate).mul(uint256(7).div(5));
  527. ico.reserve(tokens); // this reverts the transaction if we can't reserva our tokens
  528.  
  529. if(!invested[msg.sender]){
  530. investors.push(msg.sender);
  531. invested[msg.sender] = true;
  532. }
  533.  
  534. balances[msg.sender] = balances[msg.sender].add(tokens);
  535.  
  536. Invested(msg.sender, msg.value, tokens);
  537. }
  538.  
  539. // send the tokens using the ico contract
  540. function sendTokens() onlyOwner isFinished public {
  541. require(address(ico) != address(0));
  542.  
  543. for(uint i = 0; i < investors.length; i++){
  544. if(balances[investors[i]] > 0){
  545. uint256 amount = balances[investors[i]];
  546. delete balances[investors[i]];
  547.  
  548. ico.invest.value(amount)(investors[i]);
  549. }
  550. }
  551. }
  552.  
  553. // withdraw ethers
  554. function withdraw() public {
  555. uint256 amount = balances[msg.sender];
  556. require(amount > 0);
  557.  
  558. balances[msg.sender] = 0;
  559. msg.sender.transfer(amount);
  560. }
  561.  
  562. // whitelist an address
  563. function whitelistAddress(address _address) onlyOwner public {
  564. require(_address != address(0));
  565.  
  566. whitelist[_address] = true;
  567.  
  568. Whitelisted(_address);
  569. }
  570.  
  571. function setIco(address _ico) onlyOwner public {
  572. ico = Crowdsale(_ico);
  573. }
  574.  
  575. // once the reservation sale is finished we kill the contract and remove it from the blockchain.
  576. // all remaining ethers in the contract (shouldn't be any) will be send to '_address'
  577. function kill(address _address) onlyOwner isFinished public {
  578. selfdestruct(_address);
  579. }
  580.  
  581. }
  582.  
  583. contract TimeManager is Ownable {
  584.  
  585. // usings
  586. using SafeMath for uint256;
  587.  
  588. // defines
  589. struct TimeSlice {
  590. uint256 start; // start time
  591. uint256 end; // end time
  592. uint256 discount; // discount percentage
  593. bool active; // is active
  594. bool blocking; // in this period of time all tokens emitted are blocked
  595. }
  596.  
  597. // public variables
  598. uint256 public constant MAX_SLICES = 10;
  599. TimeSlice[10] public time_slices; // max 10 slices
  600.  
  601. // private variables
  602. uint256 private slice_count;
  603. bool private finished = false;
  604.  
  605. function TimeManager(uint256[] _slices) public {
  606. require(
  607. _slices.length % 4 == 0 && // gotta have 4 values per slice
  608. _slices.length < MAX_SLICES.sub(1).mul(4) // check we don't exceed max_slices
  609. );
  610.  
  611. slice_count = _slices.length.div(4);
  612.  
  613. require(slice_count > 1); // we need more than one slice...
  614.  
  615. for(uint i = 0; i < slice_count; i++){
  616. time_slices[i].start = _slices[i * 4 + 0];
  617. time_slices[i].end = _slices[i * 4 + 1];
  618. time_slices[i].discount = _slices[i * 4 + 2];
  619. time_slices[i].blocking = _slices[i * 4 + 3] == 1;
  620. time_slices[i].active = true;
  621. }
  622.  
  623. // create the last slice
  624. time_slices[slice_count].start = time_slices[slice_count - 1].end;
  625. time_slices[slice_count].end = 1609372800; // 2020-12-31 00:00:00
  626. time_slices[slice_count].active = false; // we can't buy tokens here
  627.  
  628. // our first slice must be a dummy one
  629. require(
  630. time_slices[0].start == 0 &&
  631. time_slices[0].end == 0
  632. );
  633.  
  634. // we set the end of our dummy slice to the start of our first 'real' slice
  635. time_slices[0].end = time_slices[1].start;
  636. time_slices[0].active = false; // we can't buy tokens here
  637. }
  638.  
  639. function getCurrentSlice() private constant returns(TimeSlice) {
  640. for(uint i = 0; i < time_slices.length; i++){
  641. if(time_slices[i].start <= now && time_slices[i].end > now){
  642. return time_slices[i];
  643. }
  644. }
  645. }
  646.  
  647. function setSlice(uint256 _index, uint256 _start, uint256 _end, uint256 _discount, bool _active, bool _blocked) onlyOwner public {
  648. require(_index < slice_count);
  649. require(_start > 0);
  650. require(_end > 0);
  651. require(_start < _end);
  652.  
  653. time_slices[_index].start = _start;
  654. time_slices[_index].end = _end;
  655. time_slices[_index].discount = _discount;
  656. time_slices[_index].active = _active;
  657. time_slices[_index].blocking = _blocked;
  658. }
  659.  
  660. function calculateDiscount(uint256 _wei, uint256 _rate) public constant returns(uint256) {
  661. TimeSlice memory slice = getCurrentSlice();
  662.  
  663. if(slice.active == false)
  664. return 0; // return 0 so that in Crowdsale.sol we'll fire an exception because we don't support 0-valued transactions
  665.  
  666. return _wei.mul(_rate.mul(slice.discount.add(100)).div(100));
  667. }
  668.  
  669. function isBlocked() public constant returns(bool) {
  670. TimeSlice memory slice = getCurrentSlice();
  671. return slice.blocking;
  672. }
  673.  
  674. // this contract can't be payed
  675. function () payable public {
  676. revert();
  677. }
  678.  
  679. // kill this contract once we've finished with him
  680. function kill() onlyOwner public {
  681. selfdestruct(this);
  682. }
  683.  
  684. }
  685.  
  686. contract Crowdsale is Sale {
  687.  
  688. // uninitialized public variables
  689. TimeManager public time_manager;
  690. address public holder;
  691. bool public finished = false;
  692. uint256 public available;
  693. uint256 public reserve_available;
  694. uint256 public minimum;
  695.  
  696. // constants
  697. uint256 constant MINIMUM_INVESTMENT = 0.5 * 1 ether;
  698. uint256 constant BLOCKING_PERIOD = 1 years;
  699.  
  700. function Crowdsale(address _holder, string _name, address _token, address _time_manager, address _wallet, uint256 _rate, uint256 _available, uint256 _minimum)
  701. Sale(_name, _token, _wallet, _rate) public
  702. {
  703. // safety checks
  704. require(_time_manager != address(0));
  705. require(_holder != address(0));
  706. require(_available > 0);
  707. require(_minimum > 0);
  708.  
  709. time_manager = TimeManager(_time_manager);
  710. holder = _holder;
  711. available = _available;
  712. reserve_available = _available;
  713. minimum = _minimum;
  714. }
  715.  
  716. // checks if we have closed the crowdsale
  717. modifier notFinished() {
  718. require(!finished);
  719.  
  720. _;
  721. }
  722.  
  723. modifier isFinished() {
  724. require(finished);
  725.  
  726. _;
  727. }
  728.  
  729. // fallback function
  730. function () payable public {
  731. invest(msg.sender);
  732. }
  733.  
  734. // end crawdsale
  735. function finish() onlyOwner public {
  736. finished = true;
  737. }
  738.  
  739. // internal function for accepting payments and transfering tokens
  740. function invest(address _to) notFinished stopInEmergency payable public returns(uint256 tokens) {
  741. // minimum value
  742. require(msg.value >= MINIMUM_INVESTMENT);
  743.  
  744. uint256 value_wei = msg.value;
  745. uint256 value_tokens = time_manager.calculateDiscount(value_wei, rate);
  746.  
  747. // 'calculateDiscount' will return 0 if we can't buy tokens
  748. require(value_tokens > 0);
  749.  
  750. // increase investor count only if it's their the first time
  751. if(invested_amount_wei[_to] == 0) {
  752. investor_count = investor_count.add(1);
  753. }
  754.  
  755. // update investor
  756. invested_amount_wei[_to] = invested_amount_wei[_to].add(value_wei);
  757. invested_amount_tokens[_to] = invested_amount_tokens[_to].add(value_tokens);
  758.  
  759. // update amounts
  760. raised_wei = raised_wei.add(value_wei);
  761. raised_tokens = raised_tokens.add(value_tokens);
  762.  
  763. // it will throw if 'available' is less than 'value_tokens', this is our HARD CAP
  764. available = available.sub(value_tokens);
  765.  
  766. // send ether to the wallet
  767. wallet.transfer(value_wei);
  768.  
  769. // assign tokens to investor
  770. assignTokens(_to, value_tokens);
  771.  
  772. Invested(_to, value_wei, value_tokens);
  773.  
  774. return value_tokens;
  775. }
  776.  
  777. // internal function for assigning tokens to an address
  778. function assignTokens(address _to, uint256 _value) internal {
  779. if(!token.transferFrom(holder, _to, _value))
  780. revert();
  781.  
  782. if(time_manager.isBlocked()){
  783. uint256 end = now + BLOCKING_PERIOD;
  784. token.setBlockingState(_to, end, _value);
  785. }
  786. }
  787.  
  788. // a reservation contract calls this function to try to reserve some tokens
  789. function reserve(uint256 _value) public {
  790. // this is going to throw if we try to go under 0
  791. reserve_available = reserve_available.sub(_value);
  792. }
  793.  
  794. // once the crowdsale is finished we kill the contract and remove it from the blockchain.
  795. // all remaining ethers in the contract (shouldn't be any) will be send to '_address'
  796. function kill(address _address) onlyOwner isFinished public {
  797. selfdestruct(_address);
  798. }
  799.  
  800. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement