require not working in view/pure functions
See original GitHub issueMany functions in openzepplin contracts are using require
statement in view
function but it doesn’t work as expected.
📝 Details
require
in view
function does not actually perform any role for now. It is being used in many openzepplin contract. For example:
https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L62
https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L74
https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L106
It doesn’t stop execution even if require condition is false and return the output. In case address
is return data type, it returns zero address and in case uint256
is return type, it returns 3963877391197344453575983046348115674221700746820753546331534351508065746944
whose hex value is 8C379A000000000000000000000000000000000000000000000000000000000
which is the function selector for function Error(string)
.
Also this works fine with remix-vm and truffle test vm but shows above behaviour with each Ethereum testnet.
I think we need to add a note to inform the users about it in function developer comments or do what is discussed.
Related references: https://github.com/ethereum/web3.js/issues/1903#issuecomment-467191865 https://github.com/ethereum/go-ethereum/issues/19027#issuecomment-513819354 https://github.com/ethereum/solidity/issues/3971#issuecomment-410839984 https://github.com/ethereum/solidity/issues/4840
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Thanks for bringing this up @Aniket-Engg! It’s an interesting subtlety. Unfortunately since this affects basically everything, I’m not sure where we would place the notice in our documentation. I guess we can consider that this has been tackled by @abcoathup’s thorough forum post and close it.
Hi @Aniket-Engg
Thanks for raising this. I have created a topic in the forum and made it a wiki post (so that anyone can edit it): https://forum.openzeppelin.com/t/require-in-view-pure-functions-dont-revert-on-public-networks/1211