4

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?

6
  • Im curious to see an aswer to this question...I thought msg.sender was an in built function pointing to your address -and that wasnt something that could be manipulated.. Apr 12, 2022 at 20:31
  • @DanielWeigel I mean but then why do we have checks such as require(msg.sender != address(0x0) ? Also an upvote would help the question get noticed lol Apr 12, 2022 at 21:04
  • The test you have written is not working ?
    – Emrah
    Apr 12, 2022 at 22:08
  • No, it's not @blockByblock Apr 12, 2022 at 22:10
  • I can give an example where you would use check for 0x address. Unintialized address variable is set to 0x00... You can add require to your function to make sure the state variable or a field of struct is set to address and initialized.
    – Emrah
    Apr 12, 2022 at 22:16

1 Answer 1

3

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.

2
  • So essentially, not that my test is wrong but it's the function logic itself. I should be comparing msg.sender to nativeTokenAddress ? Apr 13, 2022 at 8:08
  • 1
    You don't need to compare at all. msg.sender cannot be invalid. If nativeTokenAddress is null, the transaction will fail in any case. You can raise a more informative message if you check nativeTokenAddress != address(0) Apr 13, 2022 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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