[Ethereum] Minting Tokens and OnlyOwner – ERC20

erc-20presalesolidity

I have created a contract based on a standard one to mint and I have code as follows –

  modifier canMint() {
    require(!mintingFinished);
    _;
  }

  /**
   * Function to mint tokens
   * @param _to The address that will recieve the minted tokens.
   * @param _amount The amount of tokens to mint.
   * @return A boolean that indicates if the operation was successful.
   */
  function mint(address _to, uint256 _amount) onlyOwner canMint returns (bool) {
    totalSupply = totalSupply.add(_amount);
    balances[_to] = balances[_to].add(_amount);
    Mint(_to, _amount);
    return true;
  }

  /**
   * Function to stop minting new tokens.
   * @return True if the operation was successful.
   */
  function finishMinting() onlyOwner returns (bool) {
    mintingFinished = true;
    MintFinished();
    return true;
  }

I understand intuitively what onlyOwner means but how is this actually enforced in practice to prevent anyone just minting coins?

I intend to deploy via myetherwallet. So I would have whatever credentials and available via that system.

Once I deployed via myetherwallet, then I need to somehow run the contract. Usually in test I use geth but for the full ethereum system I cannot do that.

So how would I actually run the finishMinting etc? Is there a command line option for production Ethereum, or do I need a Web3 interface in React or something like that, and then how would I confirm I am the onlyowner?

Best Answer

If you have taken the contracts from Zeppelin Solidity, then there should be a contract called Ownable. In that contract there is a modifier called onlyOwner and the contract should look something like:

address public owner;

function Ownable() public {
   owner = msg.sender; //ownership is assigned to the address used to deploy contract

}

modifier onlyOwner {
    require(owner == msg.sender); //if msg.sender != owner, then mint function will fail to execute.
    _;
}

You will notice that onlyOwner is in the 'mint' function signature. The modifier is there to check that the msg.sender is the owner before executing any of the function's body.

Related Topic