0

I am new to Solidity, and as an exercise, I am trying to create an ERC-20 token.

However, I am encountering an error, and I can't seem to figure out what the issue is.

The error I am getting is

ParserError: Expected '{' but got 'function' --> scripts/erc20.sol:33:5: | 33 | function symbol() public view returns (string) | ^^^^^^^^

The error is resulting from the part of the code that is calling the ERC-20 interface to implement its functions (code pasted below for reference).

Can anybody advise on how I can resolve this? Any advice would be greatly appreciated!

   //ERC Token Standard #20 Interface
 
contract ERC20Interface {
    function name() public view returns (string)
    function symbol() public view returns (string)
    function decimals() public view returns (uint8)
    function totalSupply() public view returns (uint256)
    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 you want to see the full erc20.sol file, here it is:

pragma solidity ^0.8.4;
 
//Safe Math Interface
 
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
 
contract ERC20Interface {
    function name() public view returns (string)
    function symbol() public view returns (string)
    function decimals() public view returns (uint8)
    function totalSupply() public view returns (uint256)
    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)

}
 
 
//Contract function to receive approval and execute function in one call
 
contract ApproveAndCallFallBack {
    function receiveApproval(address from, uint256 tokens, address token, bytes data) public;
}
 
//Actual token contract
 
contract LizCoin is ERC20Interface, SafeMath {
    string public symbol;
    string public  name;
    uint8 public decimals;
    uint public _totalSupply;
 
    mapping(address => uint) balances;
    mapping(address => mapping(address => uint)) allowed;
 
    constructor() public {
        symbol = "LIZ";
        name = "LizCoin";
        decimals = 2;
        _totalSupply = 100000000;
        balances[0xD4aA67f778Bba2805bC122F18F3d61Df59C7A542] = _totalSupply;
        emit Transfer(address(0), 0xD4aA67f778Bba2805bC122F18F3d61Df59C7A542, _totalSupply);
    }
 
    function totalSupply() public constant returns (uint) {
        return _totalSupply  - balances[address(0)];
    }
 
    function balanceOf(address tokenOwner) public constant returns (uint balance) {
        return balances[tokenOwner];
    }
 
    function transfer(address to, uint tokens) public returns (bool success) {
        balances[msg.sender] = safeSub(balances[msg.sender], tokens);
        balances[to] = safeAdd(balances[to], tokens);
        emit Transfer(msg.sender, to, tokens);
        return true;
    }
 
    function approve(address spender, uint tokens) public returns (bool success) {
        allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        return true;
    }
 
    function transferFrom(address from, address to, uint tokens) public returns (bool success) {
        balances[from] = safeSub(balances[from], tokens);
        allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
        balances[to] = safeAdd(balances[to], tokens);
        emit Transfer(from, to, tokens);
        return true;
    }
 
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
        return allowed[tokenOwner][spender];
    }
 
    function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
        allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data);
        return true;
    }
 
    function () public payable {
        revert();
    }
}
2
  • Could you share the erc20.sol file?
    – trizin
    Jun 26 at 21:57
  • Sure thing, I will update the original post
    – whizwang
    Jun 26 at 23:26
0
contract ERC20Interface {
    function name() public view returns (string)
    ...

Changing the "contract" to "interface" and adding semicolons should solve the issue.

interface ERC20Interface {
    function name() external view returns (string memory);
    function symbol() public view returns (string memory);
    function decimals() public view returns (uint8);
    function totalSupply() public view returns (uint256);
    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);

}
5
  • Thanks for looking into it! I tried changing "contract" to "interface" as you suggested, but I'm still getting the same error unfortunately on line 33 :(
    – whizwang
    Jun 27 at 1:35
  • @whizwang I've updated my answer.
    – trizin
    Jun 27 at 10:39
  • Thank you @trizin! That resolved the above issue. However, a new error came up with the totalSupply function.
    – whizwang
    Jun 27 at 22:11
  • Would you know how to resolve the new error? The new error I'm getting is ParserError: Expected '{' but got 'constant' --> scripts/erc20.sol:74:35: | 74 | function totalSupply() public constant returns (uint) { | ^^^^^^^^.
    – whizwang
    Jun 27 at 22:12
  • Your contract seems to have even more issues. Perhaps it's meant to be used with an older version of solidity. Please see here for an up to date version: north-47.com/knowledge-base/…
    – trizin
    Jun 27 at 22:24

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.