OpenZeppelin Part 4 Crowdsales

Crowdsales

What is a Crowdsale?

In Blockchain, crowdsales are fundraisers to assist in the development of a project. Backers use the tokens sold during the crowdsale to participate in the project once it's launched. The tokens are usable only within this project.

OpenZeppelin & Crowdsales

OpenZeppelin created four categories of contracts to assist in the creation of a crowdsale contract based on the most important properties of a crowdsale.

Price & Rate Configuration

Before creating a crowdsale it's important to understand the rate. Currency math is always done in the smallest denomination. To read the amount, the currency is converted to the correct decimal place. The smallest currency is Wei.

1 Eth = 10^18 Wei

In terms of tokens, the smallest denomination is TKNbits, a.k.a. bits.

1 TKN = 10^(decimals) TKNbits

Keep these conversions in mind when working with math in contracts, because it's possible to distribute more or less tokens/ether than you thought. Remember that calculations are always in Wei and TKNbits.

In the price category we have one contract, IncreasingPriceCrowdsale.sol This allows you over a set period of time to have the price of your tokens increase.

pragma solidity ^0.5.2;

import "../crowdsale/price/IncreasingPriceCrowdsale.sol";
import "../math/SafeMath.sol";

contract IncreasingPriceCrowdsaleImpl is IncreasingPriceCrowdsale {
    constructor (
        uint256 openingTime,
        uint256 closingTime,
        address payable wallet,
        IERC20 token,
        uint256 initialRate,
        uint256 finalRate
    )
        public
        Crowdsale(initialRate, wallet, token)
        TimedCrowdsale(openingTime, closingTime)
        IncreasingPriceCrowdsale(initialRate, finalRate)
    {
        // solhint-disable-previous-line no-empty-blocks
    }
}

Read more in the documentation.

Emission

Emission refers to the process of how the token reaches the buyer. The default method is to immediately transfer the token to the buyer. Other methods are available which can help to control other aspects of the crowdsale such as price point and the number of tokens sold.

pragma solidity ^ 0.5.2;

import "openzeppelin-solidity/contracts/crowdsale/emission/emission-you-choose.sol";

contract myCrowdsale is emission-you-choose {
    //the rest of your code
}

Validation

Validation contains contracts that add more customization to your crowdsale. They limit access to token purchases.

pragma solidity ^ 0.5 .2;

import "openzeppelin-solidity/contracts/crowdsale/validation/validation-you-choose.sol";

contract myCrowdsale is validation-you-choose {
    //the rest of your code
}

Distribution

The most important part of the crowdsale is when the tokens are released to the buyer.

pragma solidity ^ 0.5 .2;

import "openzeppelin-solidity/contracts/crowdsale/distribution/distribution-you-choose.sol";

contract myCrowdsale is distribution-you-choose {
    //the rest of your code
}

Conclusion

Crowdsales don't have to be complex to write and using OpenZeppelin can help incorporate features that give you, the developer, more control.

Note: You can use more than one crowdsale feature. They each have to have an import statement as well as separated by a comma.

pragma solidity ^ 0.5 .2;

import "openzeppelin-solidity/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol";
import "openzeppelin-solidity/contracts/crowdsale/validation/TimedCrowdsale.sol";

contract myCrowdsale is PostDeliveryCrowdsale, TimedCrowdsale {
    //the rest of your code
}

Next Steps

For examples of how to use OpenZeppelin Crowdsale contracts use the following link to access open source code: