ОбъявлениеError: необъявленный идентификатор - PullRequest
0 голосов
/ 08 июля 2019

Я пытаюсь создать токен, используя программирование на солидность, но продолжаю получать эту необъявленную ошибку идентификатора при компиляции в IDE браузера remix. Я новичок в солидности и как я могу решить эту проблему?

Я прикрепил свой код здесь:

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;
        }

    }

1 Ответ

0 голосов
/ 09 июля 2019

Код не компилируется из-за ошибки:

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

Ваш код не объявляет функцию approve, следовательно, ошибка.
Если вы сами не написали код, я предлагаю вам проверить исходный код кода для функции approve.

        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;
                }
        }

Чтобы узнать о токенах, я предлагаю вам прочитать документацию OpenZeppelin по токенам: https://docs.openzeppelin.org/v2.3.0/tokens

Вы можете развернуть в testnet простой токен, который использует OpenZeppelin с 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())));
    }
}

Вы также можете задать вопросы по адресу:

...