I'm trying to write a script that mimics a contract value read flow that works on Etherscan

I have a bytes32 value 0x.......... of character length 48. On etherscan I can just paste this value into the box and read a return value from a function.

However, when passing it into an ethers contract instance like contract.function(bytes32Value), I'm getting this error in my script: incorrect data length

Not sure whether etherscan is doing something behind the scenes to clean up the data type, but not clear to me how to run this

1 Answer 1


A bytes32 hex string should have 2+64=66 char length. It is possible that some tools can pad zero bytes around a value if it is smaller than the required length, for easy use.

// pads 00s from left until the hex string is bytes32
const padded = ethers.utils.hexZeroPad(shortHexString, 32)

Ethers library always wants the dev to be explicit with what they're doing, i.e. doesn't guess stuff if there can be multiple possibilities. Imagine someone unintentionally passed an address to a field that expects bytes32 (maybe incorrect ordering of params to a function that takes address and bytes32), it might cost them some time debugging where it went wrong.

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.