0

Should I include all public and view functions under contract ERC20Interface, why or why not ?

  • can you elaborate your question – Sanjay S B Sep 4 at 6:32
3

I think the other answer didn't get the question completely right. Here is what I think of it. You should put all public variables/getter in the interface. Furthermore add all the functions (and events) to the interface as well. This is my basic template for a perfect ERC20 token:

pragma solidity ^0.4.21;

contract ERC20TokenInterface {

    uint256 public totalSupply;

    function balanceOf(address _owner) public view returns (uint256 balance);

    function transfer(address _to, uint256 _value) public returns (bool success);

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);

    function approve(address _spender, uint256 _value) public returns (bool success);

    function allowance(address _owner, address _spender) public view returns (uint256 remaining);

    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

If I understand your question correctly you know how to use it right? Either way you just inherit this interface from your main contract:

pragma solidity ^0.4.21;

import "./ERC20TokenInterface.sol";


contract ERC20Token is ERC20TokenInterface {
    // your Content here
}

The interface contract complies with the EIP20 standard.

  • OK, now the question is... why all of them ? – Damir Olejar Sep 3 at 18:57
  • 1
    I do this for added security. Using interfaces is a basic principle of Object-Orient-Programming (OOP). It prevents making mistakes when changing the parameters/names of functions. When developing a new contract the interface makes sure that you implement all the functions. – Niklas Feurstein Sep 3 at 18:59
  • Sometimes in OOP it is not necessary to declare everything... For example, some model classes don't need everything declared to connect to DB (lets say)... so that is why I asked. Thank you for your answers !!! – Damir Olejar Sep 3 at 20:13
  • 1
    Everything in an interface is declared as an abstract. Therefore, you HAVE TO implement it. You are probably referencing virtual which makes a implementation optional if you already have a base implementation. – Niklas Feurstein Sep 3 at 20:30
1

I assume ERC20Interface declares public and view functions as per the ERC20 standard https://eips.ethereum.org/EIPS/eip-20. If you want your contract to be an ERC20 token then you need to implement the functions as per the standard.

If you are creating ERC20 tokens you may want to look at the OpenZeppelin Contracts implementation to see if this meets your needs. See the documentation for details: https://docs.openzeppelin.com/contracts/2.x/tokens#ERC20

If you have questions on using OpenZeppelin you can ask in the Community Forum: https://forum.openzeppelin.com/

Disclosure: I am the Community Manager at OpenZeppelin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.