contract CrowdsaleToken is MintedCrowdsale, PausableCrowdsale, FinalizableCrowdsale {

    /** 
    *
    * @dev Creates a new crowdsale contracts to sell a `token`
    * representing a pyshical asset that
    *
    * - Will be payed at the defined `rate` to `wallet`.
    * - Will open at block `openingTime` and close at block `closingTime`.
    * - Mintable, so that tokens are directly minted during the crowdsale.
    *
    */

   constructor(uint256 rate, address payable wallet, IERC20 token,
               uint256 openingTime, uint256 closingTime
              ) public Crowdsale(rate, wallet, token)
                        TimedCrowdsale(openingTime, closingTime)
                        FinalizableCrowdsale() { }

   /**
     * @dev Overrides finalization of FinalizableCrowdsale. It stores the remaining tokens to
            `wallet` and renounces minter role. `Finalize()` function should be called once the crowdsale
            is finalized to recover the tokens that weren't sold. Even if it is not called, the crowdsale
            enters state of finalization and adquistions are no longer possible.
   */

   function _finalization() internal {

      uint256 remaining = ERC20Capped(address(token())).cap() - ERC20(address(token())).totalSupply();
      if (remaining > 0) ERC20Capped(address(super.token())).mint(super.wallet(), remaining);

      MinterRole(address(token())).renounceMinter();

      super._finalization();

   }

}
/**
 *
 * @dev A contract that is thought to create a Crowdsale for a token
 * representing a pyshical asset. This crowdsale is thought to be:
 *
 * - Pausable, so that the owner can pause it in case of emergency.
 * - Minted, so that tokens are minted directly.
 * - Mintable, so that tokens are directly minted during the crowdsale.
 * - Finalizable, so that it has a limited duration and has a finalization
 *   procedure explained in _finalization().
 *
 */
function _finalization() internal {

   uint256 remaining = ERC20Capped(address(token())).cap() - ERC20(address(token())).totalSupply();
   if (remaining > 0) ERC20Capped(address(super.token())).mint(super.wallet(), remaining);

   MinterRole(address(token())).renounceMinter();

   super._finalization();

}

/**
 * @dev Overrides finalization of FinalizableCrowdsale. It stores the remaining tokens to
 `wallet` and renounces minter role. `Finalize()` function should be called once the crowdsale
 is finalized to recover the tokens that weren't sold. Even if it is not called, the crowdsale
 enters state of finalization and adquistions are no longer possible.
 */