Solidity

ERC721 _checkOnERC721Received 是做什麼的?

  • January 12, 2022
function _checkOnERC721Received(
   address from,
   address to,
   uint256 tokenId,
   bytes memory _data
) private returns (bool) {
   if (to.isContract()) {
       try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) {
           return retval == IERC721Receiver.onERC721Received.selector;
       } catch (bytes memory reason) {
           if (reason.length == 0) {
               revert("ERC721: transfer to non ERC721Receiver implementer");
           } else {
               assembly {
                   revert(add(32, reason), mload(reason))
               }
           }
       }
   } else {
       return true;
   }
}

If 通常在令牌被轉移後呼叫(在同一 tx 中)。

如果令牌的接收者是合約,它會檢查合約是否實現了 onERC721Received 介面。

如果否,它會還原交易。

如果是,接收者合約有一個onERC721Received方法。ERC721 呼叫這個方法,現在執行到接收者合約來做他想做的任何事情。例如 - 質押收到的代幣。

$$ or more dangerously - reenter the ERC721 contarct. $$ 接收者令牌onERC721Received完成後,在 ERC721 合約中恢復執行。

引用自:https://ethereum.stackexchange.com/questions/118682