2
     // Attempt transfer tokens, when you have none
     invalidAmount = tokens(10) // recipient has no tokens ??
     await token.transfer(deployer, invalidAmount, { from: receiver }).should.be.rejectedWith(EVM_REVERT)

Fellow Developers , I am following a tutorial to make a standard ERC-20 token, wherein there is a test to check if user is sending zero tokens. but still in the code presented by tutor the test defines invalidAmount as 10 token.

Can someone please let me know why is it so that we are passing 10 tokens and not 0 . Is it some kind of a convention?

Thank you in advance.

Shubham
  • 21
  • 2

1 Answers1

0

If the sender doesn't have 10 (or 20, or any number of) tokens that they're trying to send, the transaction should revert. Which is the case that is tested in this snippet:

should.be.rejectedWith(EVM_REVERT)

So it's a code that checks if the transaction really reverts when it should.


Depending on how the contract is implemented, it might be possible to successfully transfer 0 tokens, and the transaction might not revert.

For example:

function transfer(address _to, uint256 _amount) external returns (bool) {
    // doesn't revert, because their 0 balance is "greater or equal" to the 0 `amount`
    require(balances[msg.sender] >= _amount);

    balances[msg.sender] -= _amount; // subtract 0 from 0
    balances[_to] += _amount; // add 0

    emit Transfer(msg.sender, _to, _amount);
    return true;
}
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100