pragma solidity ^0.4.25;
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;
  }
}

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 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 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 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 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 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 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 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 MintedCrowdsale is Crowdsale {

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

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 BoltCrowdsaleTwo is Crowdsale, CappedCrowdsale, TimedCrowdsale, CapperRole {
  using SafeMath for uint256;

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

  
  mapping(address => uint256) private _lockedTokens;
  uint256 private _bonusAvailableUntil;
  uint256 private _bonusUnlockTime;

  constructor(
      uint256 rate,               
      address wallet,             
      ERC20 token,                
      uint256 cap,                
      uint256 openingTime,        
      uint256 closingTime,        
      uint256 bonusUnlockTime,    
      uint256 bonusAvailableUntil 
  )
      CapperRole()
      TimedCrowdsale(openingTime, closingTime)
      CappedCrowdsale(cap)
      Crowdsale(rate, wallet, token)
      public
  {
    require(
      bonusUnlockTime > closingTime,
      "Cannot unlock bonus tokens before crowdsale ends"
    );

    require(
      bonusAvailableUntil >= openingTime && bonusAvailableUntil <= closingTime,
      "Cannot unlock bonus tokens before crowdsale ends"
    );

    _bonusUnlockTime = bonusUnlockTime;
    _bonusAvailableUntil = bonusAvailableUntil;
  }

  
  function _isBonusAvailable()
    private
    view
    returns (bool)
  {
    
    return block.timestamp <= _bonusAvailableUntil;
  }

  
  function _getTokenAmount(uint256 weiAmount)
    internal
    view
    returns (uint256)
  {
    uint256 baseTokenAmount = weiAmount.mul(rate());
    uint256 bonusAmount = _isBonusAvailable() ?
      baseTokenAmount.div(5) : 
      0;

    return baseTokenAmount + bonusAmount;
  }

  
  function _deliverTokens(address beneficiary, uint256 tokenAmount)
    internal
  {
    uint256 baseAmount = _isBonusAvailable() ?
      tokenAmount.div(6).mul(5) : 
      0;
    uint256 lockedBonusAmount = tokenAmount.sub(baseAmount);

    require(
      ERC20Mintable(address(token())).mint(beneficiary, baseAmount),
      "Could not mint tokens to beneficiary."
    );

    if (lockedBonusAmount > 0) {
      require(
        ERC20Mintable(address(token())).mint(this, lockedBonusAmount),
        "Could not mint tokens to self for lockup."
      );
      _lockedTokens[beneficiary] = _lockedTokens[beneficiary].add(lockedBonusAmount);
    }
  }

  
  function getLockedTokens(address beneficiary)
    public
    view
    returns (uint256)
  {
    return _lockedTokens[beneficiary];
  }

  
  function deliverBonusTokens(address[] beneficiaries)
    external
  {
    require(
      
      _bonusUnlockTime <= block.timestamp,
      "Tokens are not yet unlocked."
    );

    for (uint8 i = 0; i < beneficiaries.length && i < 255; i ++) {
      _deliverBonusTokens(beneficiaries[i]);
    }
  }

  
  function _deliverBonusTokens(address beneficiary)
    private
  {
    require(
      _lockedTokens[beneficiary] > 0,
      "No tokens to unlock."
    );

    _lockedTokens[beneficiary] = 0;

    require(
      ERC20(address(token())).transfer(beneficiary, _lockedTokens[beneficiary]),
      "Could not transfer tokens."
    );
  }

  
  function setCaps(address[] beneficiaries, uint256[] caps)
    external
    onlyCapper
  {
    for (uint8 i = 0; i < beneficiaries.length && i < 255; i ++) {
      _caps[beneficiaries[i]] = caps[i];
    }
  }

  
  function setCap(address beneficiary, uint256 cap)
    external
    onlyCapper
  {
    _caps[beneficiary] = cap;
  }

  
  function getCap(address beneficiary)
    public
    view
    returns (uint256)
  {
    return _caps[beneficiary];
  }

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

  
  function _preValidatePurchase(
    address beneficiary,
    uint256 weiAmount
  )
    internal
  {
    super._preValidatePurchase(beneficiary, weiAmount);
    require(
      _contributions[beneficiary].add(weiAmount) <= _caps[beneficiary],
      "Contribution cap exceeded."
    );
  }

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