5

This is going to be short question and may be silly, but I'm literally stuck with a problem, where I need to pass address(0) to function in Truffle Test in JS and don't really know how to do it. In solidity it's simply address(0), but in Truffle JS?

0

3 Answers 3

10

You can simply use 0, or 0x0000000000000000000000000000000000000000.

address(0) in Solidity is equivalent to 0x0000000000000000000000000000000000000000.

2
  • 3
    I get invalid address error, with Truffle 5, Web3 1, Solidity .5
    – ferit
    Jun 18, 2019 at 20:11
  • 4
    To be clear, you have to use it as a String: zero_address = "0x0000000000000000000000000000000000000000"; this works for me.
    – almel
    Jun 10, 2020 at 1:09
6

You must put single quotes around it. '0x0000000000000000000000000000000000000000' worked for me Been hunting down that bug for some time ...

1
  • this did not work for me :|
    – almel
    Jun 9, 2020 at 23:20
2

Don't know about web3.js, but in ethers.js ethers.constants.AddressZero will do the work.

Example: Suppose you have a transfer function in your contract like this:

function transfer(address to, uint256 amount) public {
    require(to != address(0), "Transfer to the zero address");
    ////
    ////
}

you can test this by:

await expect(
  contract.transfer(ethers.constants.AddressZero, amount)
).to.be.revertedWith("Transfer to the zero address");

You can check out this link for reference: https://docs.ethers.io/v5/api/utils/constants/#constants-AddressZero

2
  • Hello. Providing a little sample and a link to the function documentation would make this answer way better ;) Jun 18, 2022 at 4:07
  • As of ethers v6 it is simply ethers.ZeroAddress
    – Madbreaks
    Nov 3, 2023 at 20:51

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.