To get this out of the way - I am fairly new to Solidity... Can't understand every concept yet.
I have been trying to create a Crowdsale for an ERC20 token in order to practice using OpenZeppelin libraries / standards.
The goal of my project was:
- Create an ERC20 token
- Create a Crowdsale (which will hold all of the token supply, has a 1 to 1 ERC20 token to Wei ratio)
- Make the Crowdsale pause itself for X days/weeks/months after (say) 50% of the ERC20 tokens are bought.
From what I understood, those X days/weeks/months should be calculated by an estimation of time until Z blocks are mined, or something like that. I haven't delved deeper into this subject yet since I'm taking it step by step.
My current issue is - I don't know what the best practice is in order to pause the contract (from within itself, automatically) when the cap of 50% is reached. I don't really know if using CappedCrowdsale is necessary, because I plan on having the Crowdsale open again after X time has passed, and I don't know if that is possible once that cap is reached. Currently I have added the following override of _postValidatePurchase:
function _postValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
super._postValidatePurchase(beneficiary, weiAmount);
if (weiRaised == cap) {
pause;
}
}
But it doesn't seem to work and I don't really know how to make it work.
I've been working on this project using OpenZeppelin's Remix IDE.
My full .sol files are the following -
OgreToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "@openzeppelin/contracts@2.5.1/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts@2.5.1/token/ERC20/ERC20Mintable.sol";
import "@openzeppelin/contracts@2.5.1/token/ERC20/ERC20Detailed.sol";
contract OgreToken is ERC20, ERC20Detailed, ERC20Mintable {
constructor() ERC20Detailed("OgreToken", "OT", 18) public {
}
}
and
OgreCrowdsaleDeployer.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "./OgreToken.sol";
import "@openzeppelin/contracts@2.5.1/crowdsale/Crowdsale.sol";
import "@openzeppelin/contracts@2.5.1/crowdsale/validation/CappedCrowdsale.sol";
import "@openzeppelin/contracts@2.5.1/crowdsale/validation/PausableCrowdsale.sol";
contract OgreCrowdsale is Crowdsale, PausableCrowdsale, CappedCrowdsale {
constructor(
uint256 _rate,
address payable _wallet,
OgreToken _token,
uint256 _cap
)
PausableCrowdsale()
CappedCrowdsale(_cap)
Crowdsale(_rate, _wallet, _token)
public
{
}
function _postValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
super._postValidatePurchase(beneficiary, weiAmount);
if (weiRaised == cap) {
pause;
}
}
}
contract OgreCrowdsaleDeployer {
address public token_address;
address public crowdsale_address;
constructor()
public
{
// Create the token
OgreToken ogreToken = new OgreToken();
token_address = address(ogreToken);
uint total_supply = 10 ** uint256(ogreToken.decimals());
// Create the crowdsale and tell it about the token
OgreCrowdsale ogreCrowdsale = new OgreCrowdsale(
1, // _rate
msg.sender, // _wallet
ogreToken, // _token
total_supply / 2 // _cap - half of total supply
);
crowdsale_address = address(ogreCrowdsale);
ogreToken.mint(crowdsale_address, total_supply);
ogreCrowdsale.addPauser(msg.sender);
ogreCrowdsale.addPauser(crowdsale_address);
ogreCrowdsale.renouncePauser();
}
}
Thank you in advance for any type of help or suggestions!