ERC721Wrapper.sol 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../ERC721.sol";
  4. import "../utils/ERC721Holder.sol";
  5. /**
  6. * @dev Extension of the ERC721 token contract to support token wrapping.
  7. *
  8. * Users can deposit and withdraw an "underlying token" and receive a "wrapped token" with a matching tokenId. This is useful
  9. * in conjunction with other modules. For example, combining this wrapping mechanism with {ERC721Votes} will allow the
  10. * wrapping of an existing "basic" ERC721 into a governance token.
  11. *
  12. * _Available since v4.9.0_
  13. */
  14. abstract contract ERC721Wrapper is ERC721, ERC721Holder {
  15. IERC721 private immutable _underlying;
  16. // Kept as bytes12 so it can be packed with an address
  17. // Equal to 0xb125e89df18e2ceac5fd2fa8
  18. bytes12 public constant WRAPPER_ACCEPT_MAGIC = bytes12(keccak256("WRAPPER_ACCEPT_MAGIC"));
  19. constructor(IERC721 underlyingToken) {
  20. _underlying = underlyingToken;
  21. }
  22. /**
  23. * @dev Allow a user to deposit underlying tokens and mint the corresponding tokenIds.
  24. */
  25. function depositFor(address account, uint256[] memory tokenIds) public virtual returns (bool) {
  26. bytes memory data = abi.encodePacked(WRAPPER_ACCEPT_MAGIC, account);
  27. uint256 length = tokenIds.length;
  28. for (uint256 i = 0; i < length; ++i) {
  29. underlying().safeTransferFrom(_msgSender(), address(this), tokenIds[i], data);
  30. }
  31. return true;
  32. }
  33. /**
  34. * @dev Allow a user to burn wrapped tokens and withdraw the corresponding tokenIds of the underlying tokens.
  35. */
  36. function withdrawTo(address account, uint256[] memory tokenIds) public virtual returns (bool) {
  37. uint256 length = tokenIds.length;
  38. for (uint256 i = 0; i < length; ++i) {
  39. uint256 tokenId = tokenIds[i];
  40. require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721Wrapper: caller is not token owner or approved");
  41. _burn(tokenId);
  42. // Checks were already performed at this point, and there's no way to retake ownership or approval from
  43. // the wrapped tokenId after this point, so it's safe to remove the reentrancy check for the next line.
  44. // slither-disable-next-line reentrancy-no-eth
  45. underlying().safeTransferFrom(address(this), account, tokenId);
  46. }
  47. return true;
  48. }
  49. /**
  50. * @dev Overrides {IERC721Receiver-onERC721Received} to allow minting on direct ERC721 transfers to
  51. * this contract.
  52. *
  53. * In case there's data attached, it validates that the sender is aware of this contract's existence and behavior
  54. * by checking a magic value (`WRAPPER_ACCEPT_MAGIC`) in the first 12 bytes. If it also matches, the rest 20
  55. * bytes are used as an address to send the tokens to.
  56. *
  57. * WARNING: Doesn't work with unsafe transfers (eg. {IERC721-transferFrom}). Use {ERC721Wrapper-_recover}
  58. * for recovering in that scenario.
  59. */
  60. function onERC721Received(
  61. address,
  62. address from,
  63. uint256 tokenId,
  64. bytes memory data
  65. ) public override returns (bytes4) {
  66. require(address(underlying()) == _msgSender(), "ERC721Wrapper: caller is not underlying");
  67. if (data.length > 0) {
  68. require(data.length == 32 && WRAPPER_ACCEPT_MAGIC == bytes12(data), "ERC721Wrapper: Invalid data format");
  69. from = address(bytes20(bytes32(data) << 96));
  70. }
  71. _safeMint(from, tokenId);
  72. return IERC721Receiver.onERC721Received.selector;
  73. }
  74. /**
  75. * @dev Mint a wrapped token to cover any underlyingToken that would have been transferred by mistake. Internal
  76. * function that can be exposed with access control if desired.
  77. */
  78. function _recover(address account, uint256 tokenId) internal virtual returns (uint256) {
  79. require(underlying().ownerOf(tokenId) == address(this), "ERC721Wrapper: wrapper is not token owner");
  80. _safeMint(account, tokenId);
  81. return tokenId;
  82. }
  83. /**
  84. * @dev Returns the underlying token.
  85. */
  86. function underlying() public view virtual returns (IERC721) {
  87. return _underlying;
  88. }
  89. }