openzeppelin-contracts icon indicating copy to clipboard operation
openzeppelin-contracts copied to clipboard

Support for Custom Errors in ERC721 and 1155 onReceived hooks try catch

Open dievardump opened this issue 2 years ago • 4 comments

🧐 Motivation

Right now ERC1155 and 721 checks on safeTransfers do a try {} catch {} with only a catch on string and then a global catch that returns the default ERCXXXX: transfer to non-ERCXXXXReceiver implementer

This leads to always getting the default revert string when reverting in an onERCXXXReceived hook with a Custom Error, which does not allow the caller to get accurate revert reasons.

It would be very nice for testing purpose and also to show the right reason of reverts on the front-end, to bubble the revert code when it's a custom error

📝 Details

It is possible to easily bubble revert codes using this simple code before or after the catch on string:

} catch (bytes memory reason) {
            assembly {
                revert(add(32, reason), mload(reason))
            }
        }

dievardump avatar Oct 21 '22 15:10 dievardump

That is indeed a very good point.

I hoped solidy would support that without assemble :/

Can you make a PR ?

Amxx avatar Oct 24 '22 12:10 Amxx

Sure, I will work on this during the week and make one.

dievardump avatar Oct 24 '22 12:10 dievardump

I added check that confirm that ERC721 already support Custom error emitted by receive hook.

I submitted a PR that aligns ERC1155's implementation with ERC721's

Amxx avatar Oct 25 '22 17:10 Amxx

hey i am new to open source can you please suggest me, how to contribute

Gurpreet-2602 avatar Apr 18 '23 20:04 Gurpreet-2602