1

I have been searching the web for a simple/safe ERC-20 Token Bare-Bone Contract example to use as a base for learning. The Bare-Bones as I understand would only include what the ERC-20 Token Standard requires it to:

    function totalSupply() public constant returns (uint);
    function balanceOf(address tokenOwner) public constant returns (uint balance);
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);

    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);

sources: https://theethereum.wiki/w/index.php/ERC20_Token_Standard , https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md

However, even when finding simple templates, you find differences, for instance:

includes the burn(...) and burnFrom(...) functions. At first glance, all that might need to be done is delete these two. However when looking at another example:

there was the inclusion of various other portions of code that at first glance might seem vital for the ERC-20 standard (but I'm unsure), for instance:

// ----------------------------------------------------------------------------
// Safe maths
// ----------------------------------------------------------------------------
contract SafeMath {
    function safeAdd(uint a, uint b) public pure returns (uint c) {
        c = a + b;
        require(c >= a);
    }
    function safeSub(uint a, uint b) public pure returns (uint c) {
        require(b <= a);
        c = a - b;
    }
    function safeMul(uint a, uint b) public pure returns (uint c) {
        c = a * b;
        require(a == 0 || c / a == b);
    }
    function safeDiv(uint a, uint b) public pure returns (uint c) {
        require(b > 0);
        c = a / b;
    }
}

// ----------------------------------------------------------------------------
// ERC Token Standard #20 Interface
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// ----------------------------------------------------------------------------
contract ERC20Interface {
    function totalSupply() public constant returns (uint);
    function balanceOf(address tokenOwner) public constant returns (uint balance);
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);

    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}

QUESTIONS:

Is the SafeMaths section preventing some types of hacks/exploits? And if that were the case, why don't I see something as important as this in other token examples? Also I'm unsure why another contract called ERC20Interface is being made that initiates the functions, is it just for organization purposes?

If someone has a better simple BARE-BONES template, please do sure! Thank you.

1

The ERC20 interface is defined by the contract itself (ERC20Interface) and that is the minimum you need to extend to define an ERC20 token.

SafeMath doesn't by itself do any prevention automatically. It is a set of tools for writing safer mathematical operations. For example using safeAdd instead of + will allow you to prevent numerical overflow.

You can refer to the following tutorial for steps on creating an ERC20 token:

https://hashnode.com/post/how-to-build-your-own-ethereum-based-erc20-token-and-launch-an-ico-in-next-20-minutes-cjbcpwzec01c93awtbij90uzn

This article aims to give you an overview of how smart contracts work in Ethereum by launching a simple demo ICO.

1

Your SafeMaths section is a safety check. It prevents overflows. You can get a good and well tested library from OpenZeppelin

OpenZeppelin has also a very good introduction for smart contracts newbies: https://blog.zeppelin.solutions/the-hitchhikers-guide-to-smart-contracts-in-ethereum-848f08001f05

I’ve decided to compile a short guide to ease the way of future programmers learning Ethereum smart contract development. I’ve divided the guide in two sections: how to get started building smart contracts in Ethereum, and a quick note on smart contract security.

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.