How to do it…

  1. Create a new token contract that extends the default ERC20 contract created from the first three recipes. You can also include the following functions as part of the ERC20 contract itself, but it is recommended to modularize the code for better reusability:
pragma solidity ^0.4.23;

import "./ERC20.sol";

contract Mintable is ERC20 {
...
}
  1. Create a mint function that will accept the target address and token amount as inputs. It should return a boolean value just like the transfer function:
function mint(address _to, uint amount) public 
returns (bool) { }
  1. Use this function to create new tokens and allocate them to a target user account. It is also important to add the newly created token count to the total supply function:
totalSupply_ = totalSupply_.add(_amount); 
balances[_to] = balances[_to].add(_amount);
  1. Emit an event for each mint transaction. It can help to keep track of newly created tokens. Also, emit the transfer event to indicate a new token transfer:
// Event declaration
event Mint(address indexed to, uint256 amount);

// Events emitted
emit Mint(_to, _amount);
emit Transfer(address(0), _to, _amount);
  1. Unlimited minting is not a good idea and it should be restricted. Use a status flag to impose this restriction. Allow the owner to change the status to finished, once all the required tokens are minted:
//Status flag
bool public mintingFinished = false;

// Status change event
event MintFinished();

// Function to change the status
function finishMinting() onlyOwner public returns (bool) {
mintingFinished = true;
emit MintFinished();
return true;
}
  1. Create a modifier using the status flag so that restrictions can be imposed on other functions:
modifier canMint() { 
require(!mintingFinished);
_;
}
  1. The final contract will look like this:
pragma solidity ^0.4.23;

import "./ERC20.sol";

contract Mintable is ERC20 {
    event Mint(address indexed to, uint256 amount);
    event MintFinished();

    bool public mintingFinished = false;
    address owner;

modifier canMint() { require(!mintingFinished); _; } modifier onlyOwner() { require(msg.sender == owner); _; }

constructor() {
owner = msg.sender;
}

/** * @dev Function to mint tokens * @param _to Address that will receive the minted tokens. * @param _amount Amount of tokens to mint. * @return Boolean that indicates if the operation was successful. */ function mint(address _to, uint256 _amount) onlyOwner canMint public returns (bool) { totalSupply_ = totalSupply_.add(_amount); balances[_to] = balances[_to].add(_amount); emit Mint(_to, _amount); emit Transfer(address(0), _to, _amount); return true; } /** * @dev Function to stop minting new tokens. * @return True if the operation was successful. */ function finishMinting() onlyOwner canMint public
returns (bool) { mintingFinished = true; emit MintFinished(); return true; } }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.208.97