ERC721Wrapper.sol 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/extensions/ERC721Wrapper.sol)
  3. pragma solidity ^0.8.19;
  4. import {IERC721, ERC721} from "../ERC721.sol";
  5. import {IERC721Receiver} from "../IERC721Receiver.sol";
  6. /**
  7. * @dev Extension of the ERC721 token contract to support token wrapping.
  8. *
  9. * Users can deposit and withdraw an "underlying token" and receive a "wrapped token" with a matching tokenId. This is useful
  10. * in conjunction with other modules. For example, combining this wrapping mechanism with {ERC721Votes} will allow the
  11. * wrapping of an existing "basic" ERC721 into a governance token.
  12. *
  13. * _Available since v4.9.0_
  14. */
  15. abstract contract ERC721Wrapper is ERC721, IERC721Receiver {
  16. IERC721 private immutable _underlying;
  17. /**
  18. * @dev The received ERC721 token couldn't be wrapped.
  19. */
  20. error ERC721UnsupportedToken(address token);
  21. constructor(IERC721 underlyingToken) {
  22. _underlying = underlyingToken;
  23. }
  24. /**
  25. * @dev Allow a user to deposit underlying tokens and mint the corresponding tokenIds.
  26. */
  27. function depositFor(address account, uint256[] memory tokenIds) public virtual returns (bool) {
  28. uint256 length = tokenIds.length;
  29. for (uint256 i = 0; i < length; ++i) {
  30. uint256 tokenId = tokenIds[i];
  31. // This is an "unsafe" transfer that doesn't call any hook on the receiver. With underlying() being trusted
  32. // (by design of this contract) and no other contracts expected to be called from there, we are safe.
  33. // slither-disable-next-line reentrancy-no-eth
  34. underlying().transferFrom(_msgSender(), address(this), tokenId);
  35. _safeMint(account, tokenId);
  36. }
  37. return true;
  38. }
  39. /**
  40. * @dev Allow a user to burn wrapped tokens and withdraw the corresponding tokenIds of the underlying tokens.
  41. */
  42. function withdrawTo(address account, uint256[] memory tokenIds) public virtual returns (bool) {
  43. uint256 length = tokenIds.length;
  44. for (uint256 i = 0; i < length; ++i) {
  45. uint256 tokenId = tokenIds[i];
  46. if (!_isApprovedOrOwner(_msgSender(), tokenId)) {
  47. revert ERC721InsufficientApproval(_msgSender(), tokenId);
  48. }
  49. _burn(tokenId);
  50. // Checks were already performed at this point, and there's no way to retake ownership or approval from
  51. // the wrapped tokenId after this point, so it's safe to remove the reentrancy check for the next line.
  52. // slither-disable-next-line reentrancy-no-eth
  53. underlying().safeTransferFrom(address(this), account, tokenId);
  54. }
  55. return true;
  56. }
  57. /**
  58. * @dev Overrides {IERC721Receiver-onERC721Received} to allow minting on direct ERC721 transfers to
  59. * this contract.
  60. *
  61. * In case there's data attached, it validates that the operator is this contract, so only trusted data
  62. * is accepted from {depositFor}.
  63. *
  64. * WARNING: Doesn't work with unsafe transfers (eg. {IERC721-transferFrom}). Use {ERC721Wrapper-_recover}
  65. * for recovering in that scenario.
  66. */
  67. function onERC721Received(address, address from, uint256 tokenId, bytes memory) public virtual returns (bytes4) {
  68. if (address(underlying()) != _msgSender()) {
  69. revert ERC721UnsupportedToken(_msgSender());
  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. address owner = underlying().ownerOf(tokenId);
  80. if (owner != address(this)) {
  81. revert ERC721IncorrectOwner(address(this), tokenId, owner);
  82. }
  83. _safeMint(account, tokenId);
  84. return tokenId;
  85. }
  86. /**
  87. * @dev Returns the underlying token.
  88. */
  89. function underlying() public view virtual returns (IERC721) {
  90. return _underlying;
  91. }
  92. }