Bytes32 in createDeterministic

I use OpenZeppelin Clones library, method cloneDeterministic, which accepts bytes32 salt argument. There are some questions, I am uncertain.

  1. I use unsigned integer as salt, which is number in js. What is a proper way to convert js number to solidity bytes32?
const salt = 99;
const saltByte32 = web3.eth.abi.encodeParameter('bytes32'', salt); // returns error

I can’t find any documentation where it is clearly stated which js types could be converted to corresponding solidity types, however the error is clear about the fact, that js number is impossible to convert to bytes32

  1. I tried a workaround and converted my js number to solidity uint256:
const salt = 99;
const saltUint256 = web3.eth.abi.encodeParameter('uint256', salt);

Now contract, which requires bytes32 argument works if I pass saltUint256. However that’s kind of unexplicit and I am not sure, whether that is a proper way to convert things. Does solidity treat uint256 numbers as bytes32 and it is safe to to so?

uint256 and bytes32 are equivalent except for the operations they have available. (uint256 has numeric operations such as + and so on.)

When you call a function you can pass in a uint256 instead of bytes32 safely.

You should be careful when using JavaScript numbers because since they are floating point they do not cover the entire range of uint256 numbers.

Web3.js probably has a way to convert hex strings to bytes32, I’d recommend looking for that in the documentation.