2

I am trying to wrap my head around magic values in solidity code, but cant seem to find any resources on this.

Here is an example of a contract I am looking at:

/// @dev Magic value to be returned upon successful reception of an NFT
bytes4 constant ERC721_RECEIVED_NEW = 0x150b7a02;
bytes4 constant ERC721_RECEIVED_OLD = 0xf0b9e5ba;
bytes32 constant ERC998_MAGIC_VALUE = 0xcd740db5;

The full contract can be found here:https://etherscan.io/address/0xaaf401585b72c678afc09036510d3ef759bdaf7e#code Would appreciate it if some could provide an explanation or point me to resources .

2
  • Where did you get the term "magic value"? What are you referring to? Aug 10 '18 at 17:52
  • updating my question Aug 10 '18 at 17:54
3

These come from ERC165, which gives contracts a way to query each other about what interfaces they support. The return value is the XOR of the function selectors of the supported functions. In this case, here are the values:

bytes4 constant ERC721_RECEIVED_NEW = 0x150b7a02;
bytes4 constant ERC721_RECEIVED_OLD = 0xf0b9e5ba;
bytes32 constant ERC998_MAGIC_VALUE = 0xcd740db5;

and here is how they're derived:

web3.sha3('onERC721Received(address,address,uint256,bytes)').substring(0, 10) === "0x150b7a02"
web3.sha3('onERC721Received(address,uint256,bytes)').substring(0, 10) === "0xf0b9e5ba"

The last one is a little tougher. From https://github.com/ethereum/EIPs/blob/master/EIPS/eip-998.md:

0xcd740db5 is equal to:

this.rootOwnerOf.selector ^ this.rootOwnerOfChild.selector ^ 
this.tokenOwnerOf.selector ^ this.ownerOfChild.selector;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.