Okhawere Igbuan
Okhawere Igbuan

Reputation: 71

DeclarationError: Undeclared identifier

I am trying to create a token using solidity programming, but I keep getting this undeclared identifier error when I compile on remix browser IDE. I am new to solidity and how can I solve this problem?

I have attached my code here:

pragma solidity >=0.4.16 < 0.6.0;
    /*declare an interfaced named tokenReceipent so that any contract that implements receiveApproval function counts as a tokenReceipent*/
interface tokenRecipient
    {
         function receiveApproval(address _from, uint256 _value, address _token, bytes calldata _extraData) external;
    }
    contract TokenERC20  //create a contract ERC20 and declare public variables of the token
    {
        string public name;
        string public symbol;
        uint8 public decimals = 18;
        uint256 public totalSupply;
        mapping(address => uint256)public balanceOf; // create mapping with all balances
        mapping(address => mapping(address => uint256)) public allowance;
        event Transfer(address indexed from, address indexed to, uint256 value); //create an event on blockchain that will notify clients
        event Approval(address indexed _owner, address indexed _spender, uint256 _value);
        event Burn(address indexed from,uint256 value);  //create an event that notifies clients about the amount burn
        constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol) // create a construct that initialized tokens to the creator of the contract
        public
        {
            totalSupply = initialSupply*10** uint256(decimals); //update total supply with des 1 1 out
            balanceOf[msg.sender]=totalSupply;//give creator all initial tokens 
            name=tokenName; //set the name and symbol for display purposes
            symbol=tokenSymbol;
        }

        //create an internal function and can only be called by this smartContract
        function _transfer(address _from, address _to, uint _value) internal
        {
            //prevent transfer to 0x0 address
            //check that the balance of the sender has enough
            //add thesame to the recepient
            //insert assert to use static analysis to find bugs in your code,they should never fail
            require(_to!=address(0x0));
            //subtract from the sender
            require(balanceOf[_from]>= _value);
            //add thesame to the receipent
            require(balanceOf[_to] + _value>= balanceOf[_to]);
            uint previousBalances = balanceOf[_from] + balanceOf[_to];
            balanceOf[_from] -= _value;
            balanceOf[_from] += _value;
            emit Transfer(_from , _to, _value);
            //assert are used to find static analysis in your code,they should never fail
            assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
        }

        //create to transfer function
        function transfer(address _to, uint256 _value) 
        public returns(bool success)
        {
        _transfer(msg.sender, _to, _value);
        return true;
        }

        //create a from transfer function to transfer tokens from other address

        function transferFrom(address _from, address _to, uint256 _value) 
        public returns(bool success)
        {
            require(_value <= allowance[_from][msg.sender]);
            allowance[_from][msg.sender] -= _value;
            _transfer(_from, _to, _value);
            return true;
        }

        //create allowances for other address
        //allows spender not spend a certain allowance on your behalf

        function approveAndCall(address _spender, uint256 _value, bytes memory _extraData)
        public returns(bool success)
        {
            tokenRecipient spender = tokenRecipient(_spender);
            if (approve(_spender, _value)) {
                 spender.receiveApproval(msg.sender, _value, address(this), _extraData);
                 return true;
                }
        }
        function burn(uint256 _value)
        public returns (bool success)
        {
            require(balanceOf[msg.sender]>= _value);
                balanceOf[msg.sender] -= _value; //subtract from the sender
                totalSupply -= _value; //update the total supply of tokens
            emit Burn(msg.sender, _value);
                return true;
        }
        // function that destroys token from other(users/subscribers) accounts
        function burnFrom(address _from, uint256 _value) 
        public returns(bool success)
        {
            require(balanceOf[_from] >= _value);
            require(_value <= allowance[_from][msg.sender]);
            balanceOf[_from] -= _value;
            allowance[_from][msg.sender] -= _value;
            totalSupply -= _value;
            emit Burn(_from, _value);
        return true;
        }

    }

Upvotes: 6

Views: 25314

Answers (1)

abcoathup
abcoathup

Reputation: 456

The code is failing to compile due to the error:

browser/Token.sol:73:17: DeclarationError: Undeclared identifier.
            if (approve(_spender, _value)) {
                ^-----^

Your code doesn't declare an approve function, hence the error.
If you didn't write the code yourself, I suggest you check the original source of the code for the approve function.

        function approveAndCall(address _spender, uint256 _value, bytes memory _extraData)
        public returns(bool success)
        {
            tokenRecipient spender = tokenRecipient(_spender);
            if (approve(_spender, _value)) {
                 spender.receiveApproval(msg.sender, _value, address(this), _extraData);
                 return true;
                }
        }

To learn about tokens I suggest you read the OpenZeppelin documentation on tokens: https://docs.openzeppelin.org/v2.3.0/tokens

You could deploy to a testnet a simple token that uses OpenZeppelin with Remix

pragma solidity ^0.5.0;

import "http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";

/**
 * @title SimpleToken
 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` functions.
 */
contract SimpleToken is ERC20, ERC20Detailed {

    /**
     * @dev Constructor that gives msg.sender all of existing tokens.
     */
    constructor () public ERC20Detailed("SimpleToken", "SIM", 18) {
        _mint(msg.sender, 10000 * (10 ** uint256(decimals())));
    }
}

You can also ask questions at:

Upvotes: 4

Related Questions