How To Create A Basic Time-Sensitive Crowdsale Token With OpenZeppelin Library

Add an opening time and closing time for a crowdsale token using OpenZeppelin Library

Recap: In the last tutorial series, we created a capped crowdsale using OpenZeppelin library, limiting an investor’s maximum and minimum investment. In this tutorial, we will extend that concept and make our contract time sensitive as well.

Today, we will add opening time and closing time for our crowdsale with open zeppelin library. We will continue building upon our ExampleTokenCrowdsale.sol contract.

Benefits of time-sensitive crowdsale

  • Planning marketing strategies
  • Communication with investors
  • More focused approach because of time limitation
  • Idea validation to pursue the project or not

Time-sensitive ExampleTokenCrowdsale.sol :

                                                               Example token Crowdsale

Open-zeppelin library provides TimeCrowdsale.sol which gives us basic functionality to create time-sensitive crowdsale.

So, let’s take a look:

pragma solidity ^0.4.23;
import "../../math/SafeMath.sol";import "../Crowdsale.sol";
contract TimedCrowdsale is Crowdsale {using SafeMath for uint256;
uint256 public openingTime;uint256 public closingTime;
modifier onlyWhileOpen {require(block.timestamp >= openingTime && block.timestamp <= closingTime);_;}
constructor(uint256 _openingTime, uint256 _closingTime) public {require(_openingTime >= block.timestamp);require(_closingTime >= _openingTime);
openingTime = _openingTime;closingTime = _closingTime;}
function hasClosed() public view returns (bool) {return block.timestamp > closingTime;}
function _preValidatePurchase(address _beneficiary,uint256 _weiAmount)internalonlyWhileOpen{super._preValidatePurchase(_beneficiary, _weiAmount);}}

As you can see TimeCrowdsale constructor takes two parameters _openingTime and _closingTime. We will use this constructor in our ExampleTokenCrowdsale.sol to define opening time and closing time for our crowdsale.

This contract uses block.timerstamp which is the timestamp of current block since epoch. This time is used in comparing the opening time and closing time for the crowdsale.

It also has a _preValidatePurchase method which you can extend in your crowdsale implementation to add some specific time conditions.

Now let’s see how we use this contract in our ExampleTokenCrowdsale.sol constructor.

constructor(uint256 _rate,address _wallet,ERC20 _token,uint256 _cap,uint256 _openingTime,uint256 _closingTime)Crowdsale(_rate, _wallet, _token)CappedCrowdsale(_cap)TimedCrowdsale(_openingTime, _closingTime)public{}

As you can see, we are using TimeCrowdsale in our ExampleTokenCrowdsale constructor and passing _openingTime and _closingTime.

Now we can pass opening time and closing time while deploying our contracts. As mentioned in previous tutorials, open-zeppelin library is well tested and we don’t need to test the functionality given by the library.

But you can write some basic tests and check that everything worked fine. If you need a reference, you can check test cases which I have written while building these tokens.

If you have any doubt regarding above, Let us know in comment sections.

Originally published:

October 24, 2018

Related Articles