Day 13 of
#30DaysOfSolidity by @the_web3compass
π¨βπ» Today's Focus: Pre-sale Smart Contract on top of an ERC-20 Token
Key Concepts:
- Contract Inheritance: preorderToken inherits from MyToken (ERC-20). This allows it to reuse token functionality and extend it with custom sale logic.
Token Sale Logic:
- buyToken() lets users purchase tokens with ETH based on a set tokenPrice.
- Sale is active only between saleStartTime and
saleEndTime, and honors minPurchase and maxpurchase limits.
- Tokens are transferred from the contractβs balance to buyers.
Transfer Locking:
- Until finalizeSale() is called by the projectOwner and the sale period ends, tokens are non-transferable (except by the contract itself).
- Overrides transfer and transferFrom to enforce this lock.
Finalization:
- Ends the sale, calculates tokens sold, and sends raised ETH to the projectOwner.
Utilities:
- timeRemaining() returns seconds left in the sale.
- tokenAvailable() returns the remaining tokens in the contract.