Help me write an erc20 token and a crowdsale contract

help me write an erc20 contract with a fixed issue of 1,000,000 tokens, half (500,000) of which will belong to the creator of the contract, and the rest can be bought at a price of 0.0012 ETH for one token, always until they are all sold . Something like a stable coin. Ethers must be sent to the contract itself, and the creator can transfer them to another ethereum account at any time.

1 Like

Hi @andrei,

I recommend reading the documentation on ERC20 and Crowdsales

The following are simple examples of a token and a crowdsale. (based on examples: https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/examples)

Deploy both contracts (setting the required rate, wallet address and token address in the crowdsale) and then transfer the required tokens to the crowdsale.

For experimenting in a test environment you could set the rate to 1.
To calculate required rates, please see the documentation: https://docs.openzeppelin.com/contracts/2.x/crowdsales#crowdsale-rate

I haven’t tested the code below other than manually deploying in truffle console.
Any such contracts need thorough and appropriate testing and auditing.

For testing, I recommend following: Test smart contracts like a rockstar

Before an audit I recommend following this checklist: https://blog.openzeppelin.com/follow-this-quality-checklist-before-an-audit-8cc6a0e44845/

OpenZeppelin can perform audits: https://openzeppelin.com/security-audits/

SimpleToken

pragma solidity ^0.5.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";

/**
 * @title SimpleToken
 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` functions.
 */
contract SimpleToken is Context, ERC20, ERC20Detailed {

    /**
     * @dev Constructor that gives _msgSender() all of existing tokens.
     */
    constructor () public ERC20Detailed("SimpleToken", "SIM", 18) {
        _mint(_msgSender(), 1000000 * (10 ** uint256(decimals())));
    }
}

SimpleCrowdsale

pragma solidity ^0.5.0;

import "@openzeppelin/contracts/crowdsale/Crowdsale.sol";


/**
 * @title SimpleCrowdsale
 * @dev This is an example of a fully fledged crowdsale.
 */
contract SimpleCrowdsale is Crowdsale {
    constructor (
        uint256 rate,
        address payable wallet,
        IERC20 token
    )
        public
        Crowdsale(rate, wallet, token)
    {
    }
}