8

I have a function that takes a bytes in solidity, and I want to pass it an empty bytes string without doing "0x000...", can I do this in ethersjs?

2 Answers 2

8

ethers.js has a Constants object that contains common values. One of those is ethers.constants.HashZero, which represents an empty bytes32 string.

There are more constants that you could read about here: https://docs.ethers.io/v5/api/utils/constants

For bytes, you would place an empty array ([]) as input in ethers.js. This is due to bytes being an array-based value.

4
  • Thanks, but what if I wanted to format a bytes object? Commented Feb 23, 2022 at 15:36
  • 1
    @PatrickCollins I've updated my answer above to include bytes. Commented Feb 23, 2022 at 15:46
  • 2
    Just to mention, the hex value '0x' should also work for empty bytes. Commented Feb 23, 2022 at 16:54
  • 1
    @SohamZemse That's correct. A 0x hex value would also work. Commented Feb 23, 2022 at 17:17
6
let value = ethers.utils.formatBytes32String("")

This will give you 0x00....00

1
  • 1
    In Ethers.js V6, formatBytes32String was replaced by encodeBytes32String.
    – Peter
    Commented Feb 11, 2023 at 4:24

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.