3

my question is if "THROW" is deprecated then what i have to put instead throw in the following solidity code ?

// This function creates Tokens  
     function createTokens() payable {
        if(isMinting == true){
            require(msg.value > 0);
            uint256  tokens = msg.value.div(100000000000000).mul(RATE);
            balances[msg.sender] = balances[msg.sender].add(tokens);
            _totalSupply = _totalSupply.add(tokens);
            owner.transfer(msg.value);
        }
        else{
            throw;
        }
    }

so the code should look like this:

// This function creates Tokens  
 function createTokens() payable {
 require(isMinting == true && msg.value > 0);
 uint256  tokens = msg.value.div(100000000000000).mul(RATE);
 balances[msg.sender] = balances[msg.sender].add(tokens);
 _totalSupply = _totalSupply.add(tokens);
 owner.transfer(msg.value);
    }
    else{
        throw;
    }
}

Or i have to delete the

else{ throw;

last update:

function createTokens() payable public {
    if(isMinting == true){
       require(isMinting, "Not allowed");
       require(msg.value > 0, "No funds");
        uint256  tokens = msg.value.div(100000000000000).mul(RATE);
        balances[msg.sender] = balances[msg.sender].add(tokens);
        _totalSupply = _totalSupply.add(tokens);
        owner.transfer(msg.value);
    }
    else{
        revert();
    }

thanks AK

2 Answers 2

4

If you are looking for an alternative of throw, you can use revert in the similar way. The revert function can be used to flag an error and revert the current call. As stated in solidity docs :

Note

There used to be a keyword called throw with the same semantics as revert() which was deprecated in version 0.4.13 and removed in version 0.5.0.

From code point of view, you can use the require as below:

 function createTokens() payable {
        require(isMinting == true && msg.value > 0);
        uint256  tokens = msg.value.div(100000000000000).mul(RATE);
        balances[msg.sender] = balances[msg.sender].add(tokens);
        _totalSupply = _totalSupply.add(tokens);
        owner.transfer(msg.value);
  }

So if isMinting is not equal to true, it will throw straight from there without making any changes in the state.

For more : https://solidity.readthedocs.io/en/v0.5.0/control-structures.html#error-handling-assert-require-revert-and-exceptions

9
  • Could you give me an example in the code please? I am a beginner and I want to go the right way. and thanks A.K.
    – upracha
    Commented Nov 21, 2018 at 5:11
  • Hope edited answer helps.
    – Aniket
    Commented Nov 21, 2018 at 5:30
  • delete else section as this is in functionality of require that it will throw if any one of the passed condition fails, use same code as i added in the answer.
    – Aniket
    Commented Nov 21, 2018 at 5:53
  • Look if it's ok please and tell me if it's like that in my question, below last update
    – upracha
    Commented Nov 21, 2018 at 6:09
  • Answer updated. You can also use online solidity IDE remix : remix.ethereum.org/…
    – Aniket
    Commented Nov 21, 2018 at 6:41
0

For your case and most cases you should use require(isMinting, "Unallowed");. require(false) condition reverts the current call, i.e. storage changes are undone and msg.value is not transferred to the contract.

throw behavior is equivalent to assert(false, "Reason"), they will cause transaction fail and also waste all remaining gas, this reverts all the call stack.

Learn more about failing transactions using this example https://gist.github.com/3esmit/8954425d2ed0322505245803d6c045e4

function createTokens() payable { require(isMinting, "Not allowed"); require(msg.value > 0, "No funds"); uint256 tokens = msg.value.div(100000000000000).mul(RATE); balances[msg.sender] = balances[msg.sender].add(tokens); _totalSupply = _totalSupply.add(tokens); owner.transfer(msg.value); }

4
  • hello can you give me example with the code in my question please ?
    – upracha
    Commented Nov 21, 2018 at 7:18
  • edited answer with specific suggestion for your code. Commented Nov 21, 2018 at 7:25
  • i edit the question look below last updated, in remix this code dont show warnings, so please check and tell me if is good and you make some mistake in your answer, thanks
    – upracha
    Commented Nov 21, 2018 at 7:37
  • I cannot do the work for you. See if it compiles, if yes, test it. Consider installing embark framework and make test scripts. Commented Nov 21, 2018 at 9:25

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.