In my contract code I have implemented checks to stop 0 amount transactions inside Transfer method:

function transfer(address _to, uint256 _value) isRunning validAddress public returns (bool success) {
    require(balanceOf[msg.sender] >= _value,"Insufficient balance.");
    require(balanceOf[_to] + _value >= balanceOf[_to],"Pass a correct value."); 

But its not working as expected, I tried to sent 0 ethers to a test address and its sent without any contract error. Any help would be appreciated.

  • Are you talking about transferring Ether or tokens? Your code looks like token transfer function but you are testing it by sending Ether? Apr 23 '18 at 7:39
  • Sorry for the confusion I want to stop 0 ether transactions and 0 token transfer as well. So nobody can send 0 ether to contract, and can't send 0 tokens to other users. Apr 23 '18 at 7:49

You don't really need to stop 0 Ether transfers. I'm not sure if clients would typically even allow sending 0 Ethers, but even if they do, it doesn't hurt anyone if your contract receives 0 Ether. You just get 0 Ethers in the contract, they pay for the transaction and that's it, nothing else happens.

For refusing 0 token transfers, you can use something like what @Eli Drion wrote:

require(_value > 0, "Enter valid token amount");

But basically you're already doing this with your line (if you remove the equal sign):

require(balanceOf[_to] + _value >= balanceOf[_to],"Pass a correct value."); 
  • Are messages supported on requires? I thought that was for a later implementation Apr 23 '18 at 8:53
  • They just added them. And as the OP used them, I assume he uses a recent version :) github.com/ethereum/solidity/releases Apr 23 '18 at 8:55
  • Sweet! I'm running my contracts on Ganache, I wonder what I have to update to include it, would changing the Pragma version be enough? Do I have to way for the Ganache team to implement it? And where would the error message show up exactly? Apr 23 '18 at 8:59

How did you implement it? I don't see it in your code.

Simply add

require(_value > 0);

you mention in your comment that :

I want to stop 0 ether transactions and 0 token transfer as wel

1.so as the other friends have answered you will add to your transfer function the following verification

require(_value > 0);
  1. in order to stop 0 ether transactions :

in you payable function (deposit) add

require (msg.value > 0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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