Twitter: Jack Lee|Conflux DAO
Community: JackLee.io |Conflux Forum
All code and tutorials are open-sourced on GitHub: https://github.com/jackleeio/TokenCraft
The CappedCrowdsale
contract allows project teams to raise funds by selling ERC20 tokens with a cap on the total amount of tokens sold. This contract ensures that the token sale does not exceed the specified cap.
CappedCrowdsale Contract: CappedCrowdsale.sol
forge test --match-contract CappedCrowdsaleTest -vvv
Tests should cover the following scenarios:
- Initial token allocation
- Validation of crowdsale start and end times
- Token purchase behavior
- Withdrawal of unsold tokens
- Enforcement of the cap on total tokens sold
The CappedCrowdsale
contract inherits from Ownable
. The main method calls are:
buyTokens()
: Allows users to purchase tokens.withdrawTokens()
: Allows the owner to withdraw unsold tokens.hasCrowdsaleStarted()
: Checks if the crowdsale has started.hasCrowdsaleEnded()
: Checks if the crowdsale has ended.
The CappedCrowdsaleDeploy.s.sol
script is used to deploy the token contract and the crowdsale contract. It sets the following parameters:
- Rate: 1 ETH = 100 tokens
- Crowdsale allocation: Set as needed
- Crowdsale start time: 1 day after deployment
- Crowdsale duration: 30 days
- Crowdsale cap: Set to 1,000,000 tokens
To deploy the CappedCrowdsale
, you can use the following script:
forge script script/Crowdsale/CappedCrowdsaleDeploy.s.sol:CappedCrowdsaleDeploy --rpc-url $RPC_URL --broadcast --verify
Note: Before deploying, make sure your account has enough native tokens (such as ETH, CFX) to pay for gas fees.