-
In Solidity, doing this is as simple as: function convert(uint256 n) returns (bytes32) {
return bytes32(n);
} How could we replicate this with ethers.js? I found formatBytes32String in the docs but that is not quite the same thing as as I'm asking about here. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
You can use hexZeroPad. const amount = ethers.BigNumber.from(1);
ethers.utils.hexZeroPad(amount.toHexString(), 32) // 0x0000000000000000000000000000000000000000000000000000000000000001 And yeah parseBytes32String, formatBytes32String are different, use case for them is to store strings in smart contracts as |
Beta Was this translation helpful? Give feedback.
-
hi @PaulRBerg and @zemse found this discussion while searching my question.
|
Beta Was this translation helpful? Give feedback.
You can use hexZeroPad.
And yeah parseBytes32String, formatBytes32String are different, use case for them is to store strings in smart contracts as
bytes32
instead of the dynamic length typestring
. But that puts a limit of 32 utf8 characters, which can be reasonable for things likename
andsymbol
(interestingly MKR uses bytes32 for name and symbol, as it was deployed before ERC20 was finalized)