1

I have created an ERC-20 token with basic functionalities and it works fine and as expected. Now I want to add require() for error handling for frozen accounts.

require(!frozenAccount[_from]);
require(!frozenAccount[_to]);

I am using above in my internal function but it gives me an error as below

error undeclared identifier

I am new to solidity and programming. Plz help me with this.

Below is my complete code for internal function

 /**
 * Internal transfer, only can be called by this contract
 */
function _transfer(address _from, address _to, uint _value) internal {
    // Prevent transfer to 0x0 address. Use burn() instead
    require(_to != 0x0);
    // Check if the sender has enough
    require(balanceOf[_from] >= _value);
    // Check for overflows
    require(balanceOf[_to] + _value >= balanceOf[_to]);

    require(!frozenAccount[_from]);
    require(!frozenAccount[_to]);

     // Save this for an assertion in the future
    uint previousBalances = balanceOf[_from] + balanceOf[_to];
    // Subtract from the sender
    balanceOf[_from] -= _value;
    // Add the same to the recipient
    balanceOf[_to] += _value;
    emit Transfer(_from, _to, _value);
    // Asserts are used to use static analysis to find bugs in your code. They should never fail
    assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}

//Declared Variables at the contract beginning

contract TokenERC20 {
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals = 3;
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;

// Constructor function

constructor (
    uint256 initialSupply,
    string tokenName,
    string tokenSymbol
) public {
    totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount
    balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens
    name = tokenName;                                   // Set the name for display purposes
    symbol = tokenSymbol;                               // Set the symbol for display purposes
}

// declared frozen account

mapping (address => uint256) public frozenAccount;

Is this the right way ?

5
  • Please include your full code or at least the parts where you initialize frozenAccount variable Aug 27, 2018 at 12:58
  • I am trying to initialize frozenAccount variable but not been able to do it as I am new. Kindly help me on how to initialize it ? Aug 27, 2018 at 13:04
  • Please add the start of the code where you declare the variable, the constructor, and any other functions that set the value for frozenAccount.
    – WBT
    Aug 27, 2018 at 13:05
  • I have added my starting code and . constructor function Aug 27, 2018 at 13:10
  • Still no trace of frozenAccount declaration in your code!!! Aug 27, 2018 at 13:11

1 Answer 1

1

It seems you are not initializing your frozenAccount variable anywhere. It is not a built-in variable in the system so you have to initialize it somewhere.

I assume you saw the variable being used in some tutorial and copypasted it to your code. Your code will not work unless you add code to initialize the variable and some logic how to add values to that mapping. So something like: mapping(address => bool) public frozenAccount; And after that you still need logic for it. But I have no idea what it's supposed to do so can't help you with that.

So your question is not about "require efficiency" but about not declaring variables that you use in your code.

1
  • Thanks. I have declared the variable this way and error is gone. I will add the relevant logic to it now. Aug 27, 2018 at 13:20

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.