1

Hello i am getting error on line 4 TypeError: Contract "Token" should be marked as abstract. I know there was a lot threads made about this before but none of them seems to give me solution .

pragma solidity ^0.8.0;

contract Token {

    mapping(address => uint256) AccountsBalances; // Token accounts 
    string public constant TokenName = "NewToken";
    string public constant TokenSymbol = "ALPH";
    uint public  MaxTokens = 1000000000;
    uint256 public constant decimals = 18;

    function transfer(address _to,uint256 _amount) virtual payable public  returns (bool);
    function transferFrom(address _from , address _to ,uint _amount) virtual payable public returns (bool);
    function AllowToSpend( address _spender , uint256 _allowanceAmount) virtual public returns (bool);
    function CheckMyAllowance(address _walletAddress) virtual public view returns (uint256);
    function BalanceOfWallet(address _walletAddress) virtual public view returns (uint256);
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event ApprovalToSpend(address indexed _owner, address indexed _spender, uint256 _value);
}


contract StandardToken is Token{

    
    mapping (address => mapping (address => uint256)) Allowed;// Allowance count


    function transfer(address _to, uint256 _amount)override payable public returns (bool){
        if(AccountsBalances[msg.sender] >= _amount && _amount > 0 && msg.sender != _to)
        {
            AccountsBalances[msg.sender] -= _amount;
            AccountsBalances[_to] += _amount;
            Transfer(msg.sender, _to, _amount);
        } else return false;
    }


    function transferFrom(address _TransferFrom , address _TransferTo , uint256 _amount)override payable public returns (bool)
    {
        if(CheckMyAllowance(_TransferFrom) >= _amount && AccountsBalances[_TransferFrom] >= _amount && _amount > 0)
        {
            AccountsBalances[_TransferFrom] -= _amount;
            Allowed[_TransferFrom][msg.sender] -= _amount;
            AccountsBalances[_TransferTo] += _amount;
            Transfer(_TransferFrom,_TransferTo,_amount);
        }else return false;
    }

    function AllowToSpend(address _spender,uint256 _allowanceAmount) override public returns (bool)
    {
        Allowed[msg.sender][_spender] = _allowanceAmount;
        ApprovalToSpend(msg.sender,_spender,_allowanceAmount);
        return true;
    }

    function CheckMyAllowance(address _walletAddress) override public view returns (uint256)
    {
        return Allowed[_walletAddress][msg.sender];
    }


    function BalanceOfWallet(address _walletAddress)override public view returns (uint256)
    {
        require(CheckMyAllowance(_walletAddress) > 0, "You have no allowance on this account and cannot see balance");
        return AccountsBalances[_walletAddress];
    }
}

I was reading other posts about this issue but none of them seems to help me find solution for this .

1
  • Sorry for the wrong edit, I confused your code with the corrected version I sent you and though I made a mistake, I rolled it back, my bad.
    – hroussille
    Commented Feb 20, 2022 at 13:29

2 Answers 2

3

From the documentation :

Contracts need to be marked as abstract when at least one of their functions is not implemented. Contracts may be marked as abstract even though all functions are implemented.

Since Token has no implementation you must mark it as abstract.

You also have some issues on your event related syntax and some potential bugs when returning default boolean values in your transferFrom and transfer functions.

Here is a corrected version of your code :

pragma solidity ^0.8.0;

abstract contract Token {

    mapping(address => uint256) AccountsBalances; // Token accounts 
    string public constant TokenName = "NewToken";
    string public constant TokenSymbol = "ALPH";
    uint public  MaxTokens = 1000000000;
    uint256 public constant decimals = 18;

    function transfer(address _to,uint256 _amount) virtual payable public  returns (bool);
    function transferFrom(address _from , address _to ,uint _amount) virtual payable public returns (bool);
    function AllowToSpend( address _spender , uint256 _allowanceAmount) virtual public returns (bool);
    function CheckMyAllowance(address _walletAddress) virtual public view returns (uint256);
    function BalanceOfWallet(address _walletAddress) virtual public view returns (uint256);
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event ApprovalToSpend(address indexed _owner, address indexed _spender, uint256 _value);
}


contract StandardToken is Token{

    
    mapping (address => mapping (address => uint256)) Allowed;// Allowance count


    function transfer(address _to, uint256 _amount) override payable public returns (bool){
        if(AccountsBalances[msg.sender] >= _amount && _amount > 0 && msg.sender != _to)
        {
            AccountsBalances[msg.sender] -= _amount;
            AccountsBalances[_to] += _amount;
            emit Transfer(msg.sender, _to, _amount);
            return true;
        } else return false;
    }


    function transferFrom(address _TransferFrom , address _TransferTo , uint256 _amount)override payable public returns (bool)
    {
        if(CheckMyAllowance(_TransferFrom) >= _amount && AccountsBalances[_TransferFrom] >= _amount && _amount > 0)
        {
            AccountsBalances[_TransferFrom] -= _amount;
            Allowed[_TransferFrom][msg.sender] -= _amount;
            AccountsBalances[_TransferTo] += _amount;
            emit Transfer(_TransferFrom,_TransferTo,_amount);
            return true;
        }else return false;
    }

    function AllowToSpend(address _spender,uint256 _allowanceAmount) override public returns (bool)
    {
        Allowed[msg.sender][_spender] = _allowanceAmount;
        emit ApprovalToSpend(msg.sender,_spender,_allowanceAmount);
        return true;
    }

    function CheckMyAllowance(address _walletAddress) override public view returns (uint256)
    {
        return Allowed[_walletAddress][msg.sender];
    }


    function BalanceOfWallet(address _walletAddress)override public view returns (uint256)
    {
        require(CheckMyAllowance(_walletAddress) > 0, "You have no allowance on this account and cannot see balance");
        return AccountsBalances[_walletAddress];
    }
}

If you are writing this for learning that's fine (just make sure to match the EIP-20 specifications), otherwise consider using well tested implementations such as the ones from OpenZeppelin. The one that would suit you the best is probably the ERC-20 implementation.

1
  • Okay i see those mistakes i made now ... I was writing this both for learning and possible use case .but i will look into ERC20 thank you
    – yahoo5000
    Commented Feb 20, 2022 at 13:34
1

Since the Token contract has functions that have no implementation, the contract needs to be marked as abstract. An abstract contract is a contract written for development purposes only and is not meant to be deployed but extended by other contracts that override the unimplemented functions.

The Token contract could be marked as abstract as follows:

abstract contract Token {
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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