Test if msg.sender is equal to 0x0

soliditytesting

I am trying to write a test for one of my smart contract functions but I'm finding it difficult to change the value of msg.sender to 0x0.

I know that msg.sender's value is gonna be the address that has called or initiated the function or created the transaction but for this test I need to set it to 0x0.

Here is the function inside my contract:

  function claimToken(address nativeTokenAddress, uint256 amount) public {
      require( amount > 0, 'Trying to claim 0 tokens.');
      require(msg.sender != address(0x0)); <---- I need to test this

    //some extra function is here
     emit Claimed(msg.sender, amount, wrappedTokenAddress);
   }

Here is the test I've written:

  it("Should revert claimToken if msg.sender is 0x0.", async function() {
    await bridgeBase.claimToken(rinkebyToken.address, 50).call({from:'0x0000000000000000000000000000000000000000'});
  })

I saw in one other question that I can change the address of which the function is called with .call() but that didn't work for me as well.

How can I test this require section of the claimToken function?

Best Answer

You can compare to address(0) but msg.sender cannot possibly be that unless someone magically discovers a private key for it. It is always the inner-most sender, either the txn signer or the contract that called the function and it cannot be impersonated in production.

You will sometimes see functions compare an argument that was passed in, e.g.

function doSomething(address user) ...
  require(user != address(0));

Zero would usually indicate a user error and the check would be to prevent trouble. That is more of an arbitrary input than something assured by the protocol, so worth checking.

Hope it helps.

Related Topic