pragma solidity ^0.4.24;




interface IERC20 {
  function totalSupply() external view returns (uint256);

  function balanceOf(address who) external view returns (uint256);

  function allowance(address owner, address spender)
    external view returns (uint256);

  function transfer(address to, uint256 value) external returns (bool);

  function approve(address spender, uint256 value)
    external returns (bool);

  function transferFrom(address from, address to, uint256 value)
    external returns (bool);

  event Transfer(
    address indexed from,
    address indexed to,
    uint256 value
  );

  event Approval(
    address indexed owner,
    address indexed spender,
    uint256 value
  );
}




library SafeMath {

  
  function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    
    
    
    if (a == 0) {
      return 0;
    }

    uint256 c = a * b;
    require(c / a == b);

    return c;
  }

  
  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    require(b > 0); 
    uint256 c = a / b;
    

    return c;
  }

  
  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    require(b <= a);
    uint256 c = a - b;

    return c;
  }

  
  function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    require(c >= a);

    return c;
  }

  
  function mod(uint256 a, uint256 b) internal pure returns (uint256) {
    require(b != 0);
    return a % b;
  }
}




contract ERC20 is IERC20 {
  using SafeMath for uint256;

  mapping (address => uint256) private _balances;

  mapping (address => mapping (address => uint256)) private _allowed;

  uint256 private _totalSupply;

  
  function totalSupply() public view returns (uint256) {
    return _totalSupply;
  }

  
  function balanceOf(address owner) public view returns (uint256) {
    return _balances[owner];
  }

  
  function allowance(
    address owner,
    address spender
   )
    public
    view
    returns (uint256)
  {
    return _allowed[owner][spender];
  }

  
  function transfer(address to, uint256 value) public returns (bool) {
    require(value <= _balances[msg.sender]);
    require(to != address(0));

    _balances[msg.sender] = _balances[msg.sender].sub(value);
    _balances[to] = _balances[to].add(value);
    emit Transfer(msg.sender, to, value);
    return true;
  }

  
  function approve(address spender, uint256 value) public returns (bool) {
    require(spender != address(0));

    _allowed[msg.sender][spender] = value;
    emit Approval(msg.sender, spender, value);
    return true;
  }

  
  function transferFrom(
    address from,
    address to,
    uint256 value
  )
    public
    returns (bool)
  {
    require(value <= _balances[from]);
    require(value <= _allowed[from][msg.sender]);
    require(to != address(0));

    _balances[from] = _balances[from].sub(value);
    _balances[to] = _balances[to].add(value);
    _allowed[from][msg.sender] = _allowed[from][msg.sender].sub(value);
    emit Transfer(from, to, value);
    return true;
  }

  
  function increaseAllowance(
    address spender,
    uint256 addedValue
  )
    public
    returns (bool)
  {
    require(spender != address(0));

    _allowed[msg.sender][spender] = (
      _allowed[msg.sender][spender].add(addedValue));
    emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
    return true;
  }

  
  function decreaseAllowance(
    address spender,
    uint256 subtractedValue
  )
    public
    returns (bool)
  {
    require(spender != address(0));

    _allowed[msg.sender][spender] = (
      _allowed[msg.sender][spender].sub(subtractedValue));
    emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
    return true;
  }

  
  function _mint(address account, uint256 amount) internal {
    require(account != 0);
    _totalSupply = _totalSupply.add(amount);
    _balances[account] = _balances[account].add(amount);
    emit Transfer(address(0), account, amount);
  }

  
  function _burn(address account, uint256 amount) internal {
    require(account != 0);
    require(amount <= _balances[account]);

    _totalSupply = _totalSupply.sub(amount);
    _balances[account] = _balances[account].sub(amount);
    emit Transfer(account, address(0), amount);
  }

  
  function _burnFrom(address account, uint256 amount) internal {
    require(amount <= _allowed[account][msg.sender]);

    
    
    _allowed[account][msg.sender] = _allowed[account][msg.sender].sub(
      amount);
    _burn(account, amount);
  }
}




library SafeERC20 {
  function safeTransfer(
    IERC20 token,
    address to,
    uint256 value
  )
    internal
  {
    require(token.transfer(to, value));
  }

  function safeTransferFrom(
    IERC20 token,
    address from,
    address to,
    uint256 value
  )
    internal
  {
    require(token.transferFrom(from, to, value));
  }

  function safeApprove(
    IERC20 token,
    address spender,
    uint256 value
  )
    internal
  {
    require(token.approve(spender, value));
  }
}




contract Crowdsale {
  using SafeMath for uint256;
  using SafeERC20 for IERC20;

  
  IERC20 private _token;

  
  address private _wallet;

  
  
  
  
  uint256 private _rate;

  
  uint256 private _weiRaised;

  
  event TokensPurchased(
    address indexed purchaser,
    address indexed beneficiary,
    uint256 value,
    uint256 amount
  );

  
  constructor(uint256 rate, address wallet, IERC20 token) public {
    require(rate > 0);
    require(wallet != address(0));
    require(token != address(0));

    _rate = rate;
    _wallet = wallet;
    _token = token;
  }

  
  
  

  
  function () external payable {
    buyTokens(msg.sender);
  }

  
  function token() public view returns(IERC20) {
    return _token;
  }

  
  function wallet() public view returns(address) {
    return _wallet;
  }

  
  function rate() public view returns(uint256) {
    return _rate;
  }

  
  function weiRaised() public view returns (uint256) {
    return _weiRaised;
  }

  
  function buyTokens(address beneficiary) public payable {

    uint256 weiAmount = msg.value;
    _preValidatePurchase(beneficiary, weiAmount);

    
    uint256 tokens = _getTokenAmount(weiAmount);

    
    _weiRaised = _weiRaised.add(weiAmount);

    _processPurchase(beneficiary, tokens);
    emit TokensPurchased(
      msg.sender,
      beneficiary,
      weiAmount,
      tokens
    );

    _updatePurchasingState(beneficiary, weiAmount);

    _forwardFunds();
    _postValidatePurchase(beneficiary, weiAmount);
  }

  
  
  

  
  function _preValidatePurchase(
    address beneficiary,
    uint256 weiAmount
  )
    internal
  {
    require(beneficiary != address(0));
    require(weiAmount != 0);
  }

  
  function _postValidatePurchase(
    address beneficiary,
    uint256 weiAmount
  )
    internal
  {
    
  }

  
  function _deliverTokens(
    address beneficiary,
    uint256 tokenAmount
  )
    internal
  {
    _token.safeTransfer(beneficiary, tokenAmount);
  }

  
  function _processPurchase(
    address beneficiary,
    uint256 tokenAmount
  )
    internal
  {
    _deliverTokens(beneficiary, tokenAmount);
  }

  
  function _updatePurchasingState(
    address beneficiary,
    uint256 weiAmount
  )
    internal
  {
    
  }

  
  function _getTokenAmount(uint256 weiAmount)
    internal view returns (uint256)
  {
    return weiAmount.mul(_rate);
  }

  
  function _forwardFunds() internal {
    _wallet.transfer(msg.value);
  }
}




contract TieredPriceCrowdsale is Crowdsale {
    uint256 private _baseRate;
    uint256 private _tier2Start;
    uint256 private _tier3Start;
    uint256 private _tier4Start;

    constructor( 
      uint256 baseRate,
      uint256 openingTimeTier2,
      uint256 openingTimeTier3, 
      uint256 openingTimeTier4
    ) 
    public 
    {
        require(baseRate > 0);
        require(openingTimeTier2 > block.timestamp);
        require(openingTimeTier3 >= openingTimeTier2);
        require(openingTimeTier4 >= openingTimeTier3);

        _baseRate = baseRate;
        _tier4Start = openingTimeTier4;
        _tier3Start = openingTimeTier3;
        _tier2Start = openingTimeTier2;
    }

    function _getbonusRate()
      internal view returns (uint256)
    {
        
        if(_tier2Start > block.timestamp){
            return(_baseRate * 6 / 5);
        }
        else if(_tier3Start > block.timestamp){
            return(_baseRate * 11 / 10);
        }
        else if(_tier4Start > block.timestamp){
            return(_baseRate * 21 / 20);
        }
        else {
            return(_baseRate);
        }
    }

    
    function bonusRate() public view returns(uint256) {
        return _getbonusRate();
    }

     
    function tierStartTime(
        uint256 tier       
    ) external view returns(uint256) 
    {
        if(tier == 2){
            return _tier2Start;
        }
        else if(tier == 3){
            return _tier3Start;
        }
        else if(tier == 4){
            return _tier4Start;
        }
        return 0;
    }

    
    function _getTokenAmount(
        uint256 weiAmount
    )
      internal view returns (uint256)
    {
        return weiAmount.mul(_getbonusRate());
    }
}




library Roles {
  struct Role {
    mapping (address => bool) bearer;
  }

  
  function add(Role storage role, address account) internal {
    require(account != address(0));
    role.bearer[account] = true;
  }

  
  function remove(Role storage role, address account) internal {
    require(account != address(0));
    role.bearer[account] = false;
  }

  
  function has(Role storage role, address account)
    internal
    view
    returns (bool)
  {
    require(account != address(0));
    return role.bearer[account];
  }
}



contract CapperRole {
  using Roles for Roles.Role;

  event CapperAdded(address indexed account);
  event CapperRemoved(address indexed account);

  Roles.Role private cappers;

  constructor() public {
    cappers.add(msg.sender);
  }

  modifier onlyCapper() {
    require(isCapper(msg.sender));
    _;
  }

  function isCapper(address account) public view returns (bool) {
    return cappers.has(account);
  }

  function addCapper(address account) public onlyCapper {
    cappers.add(account);
    emit CapperAdded(account);
  }

  function renounceCapper() public {
    cappers.remove(msg.sender);
  }

  function _removeCapper(address account) internal {
    cappers.remove(account);
    emit CapperRemoved(account);
  }
}




contract WhitelistedCrowdsale is Crowdsale, CapperRole {
    using SafeMath for uint256;

    uint256 private _invCap;   

    mapping(address => uint256) private _contributions;
    mapping(address => uint256) private _whitelist;

    
    event WhitelistUpdated(
        address indexed _account,
        uint8 _phase
    );

    constructor(uint256 invCap) public
    {
        require(invCap > 0);
        _invCap = invCap;
    }

    
    function isWhitelisted(address _account) public view returns (bool) {
        return _whitelist[_account] != 0;
    }

    
    function updateWhitelist(address _account, uint8 _phase) external onlyCapper returns (bool) {
        require(_account != address(0));
        _updateWhitelist(_account, _phase);
        return true;
    }

    
    function updateWhitelistAddresses(address[] _accounts, uint8 _phase) external onlyCapper {
        for (uint256 i = 0; i < _accounts.length; i++) {
            require(_accounts[i] != address(0));
            _updateWhitelist(_accounts[i], _phase);
        }
    }

    
    function _updateWhitelist(
        address _account, 
        uint8 _phase
    ) 
    internal 
    {
        if(_phase == 1){
            _whitelist[_account] = _invCap;
        } else {
            _whitelist[_account] = 0;
        }
        emit WhitelistUpdated(
            _account, 
            _phase
        );
    }

    
    
    
    
    
    

    
    
    
    
    
    

    
    
    
    
    
    

    
    
    
    
    
    

    
    function getContribution(address _account)
    public view returns (uint256)
    {
        return _contributions[_account];
    }

    
    function _preValidatePurchase(
        address _account,
        uint256 weiAmount
    )
    internal
    {
        super._preValidatePurchase(_account, weiAmount);
        require(
            _contributions[_account].add(weiAmount) <= _whitelist[_account]);
    }

    
    function _updatePurchasingState(
        address _account,
        uint256 weiAmount
    )
    internal
    {
        super._updatePurchasingState(_account, weiAmount);
        _contributions[_account] = _contributions[_account].add(weiAmount);
    }

}




contract CappedCrowdsale is Crowdsale {
  using SafeMath for uint256;

  uint256 private _cap;

  
  constructor(uint256 cap) public {
    require(cap > 0);
    _cap = cap;
  }

  
  function cap() public view returns(uint256) {
    return _cap;
  }

  
  function capReached() public view returns (bool) {
    return weiRaised() >= _cap;
  }

  
  function _preValidatePurchase(
    address beneficiary,
    uint256 weiAmount
  )
    internal
  {
    super._preValidatePurchase(beneficiary, weiAmount);
    require(weiRaised().add(weiAmount) <= _cap);
  }

}




contract TimedCrowdsale is Crowdsale {
  using SafeMath for uint256;

  uint256 private _openingTime;
  uint256 private _closingTime;

  
  modifier onlyWhileOpen {
    require(isOpen());
    _;
  }

  
  constructor(uint256 openingTime, uint256 closingTime) public {
    
    require(openingTime >= block.timestamp);
    require(closingTime >= openingTime);

    _openingTime = openingTime;
    _closingTime = closingTime;
  }

  
  function openingTime() public view returns(uint256) {
    return _openingTime;
  }

  
  function closingTime() public view returns(uint256) {
    return _closingTime;
  }

  
  function isOpen() public view returns (bool) {
    
    return block.timestamp >= _openingTime && block.timestamp <= _closingTime;
  }

  
  function hasClosed() public view returns (bool) {
    
    return block.timestamp > _closingTime;
  }

  
  function _preValidatePurchase(
    address beneficiary,
    uint256 weiAmount
  )
    internal
    onlyWhileOpen
  {
    super._preValidatePurchase(beneficiary, weiAmount);
  }

}




contract FinalizableCrowdsale is TimedCrowdsale {
  using SafeMath for uint256;

  bool private _finalized = false;

  event CrowdsaleFinalized();

  
  function finalized() public view returns (bool) {
    return _finalized;
  }

  
  function finalize() public {
    require(!_finalized);
    require(hasClosed());

    _finalization();
    emit CrowdsaleFinalized();

    _finalized = true;
  }

  
  function _finalization() internal {
  }

}



contract MinterRole {
  using Roles for Roles.Role;

  event MinterAdded(address indexed account);
  event MinterRemoved(address indexed account);

  Roles.Role private minters;

  constructor() public {
    minters.add(msg.sender);
  }

  modifier onlyMinter() {
    require(isMinter(msg.sender));
    _;
  }

  function isMinter(address account) public view returns (bool) {
    return minters.has(account);
  }

  function addMinter(address account) public onlyMinter {
    minters.add(account);
    emit MinterAdded(account);
  }

  function renounceMinter() public {
    minters.remove(msg.sender);
  }

  function _removeMinter(address account) internal {
    minters.remove(account);
    emit MinterRemoved(account);
  }
}




contract ERC20Mintable is ERC20, MinterRole {
  event MintingFinished();

  bool private _mintingFinished = false;

  modifier onlyBeforeMintingFinished() {
    require(!_mintingFinished);
    _;
  }

  
  function mintingFinished() public view returns(bool) {
    return _mintingFinished;
  }

  
  function mint(
    address to,
    uint256 amount
  )
    public
    onlyMinter
    onlyBeforeMintingFinished
    returns (bool)
  {
    _mint(to, amount);
    return true;
  }

  
  function finishMinting()
    public
    onlyMinter
    onlyBeforeMintingFinished
    returns (bool)
  {
    _mintingFinished = true;
    emit MintingFinished();
    return true;
  }
}




contract MintedCrowdsale is Crowdsale {

  
  function _deliverTokens(
    address beneficiary,
    uint256 tokenAmount
  )
    internal
  {
    
    require(
      ERC20Mintable(address(token())).mint(beneficiary, tokenAmount));
  }
}



contract PlazaCrowdsale is CappedCrowdsale, FinalizableCrowdsale, MintedCrowdsale, WhitelistedCrowdsale, TieredPriceCrowdsale {
    constructor(
        uint256 openingTime,
        uint256 closingTime,
        uint256 rate,
        address wallet,
        uint256 cap,
        ERC20Mintable token,
        uint256 openingTimeTier4, 
        uint256 openingTimeTier3, 
        uint256 openingTimeTier2,
        uint256 invCap
    )
    public
    Crowdsale(rate, wallet, token)
    CappedCrowdsale(cap)
    WhitelistedCrowdsale(invCap)
    TimedCrowdsale(openingTime, closingTime)
    TieredPriceCrowdsale(rate, openingTimeTier2, openingTimeTier3, openingTimeTier4)
    {}

}