ERC721Wrapper.sol 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. abstract contract ERC721Wrapper is ERC721, IERC721Receiver {
  14. IERC721 private immutable _underlying;
  15. /**
  16. * @dev The received ERC721 token couldn't be wrapped.
  17. */
  18. error ERC721UnsupportedToken(address token);
  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. uint256 length = tokenIds.length;
  27. for (uint256 i = 0; i < length; ++i) {
  28. uint256 tokenId = tokenIds[i];
  29. // This is an "unsafe" transfer that doesn't call any hook on the receiver. With underlying() being trusted
  30. // (by design of this contract) and no other contracts expected to be called from there, we are safe.
  31. // slither-disable-next-line reentrancy-no-eth
  32. underlying().transferFrom(_msgSender(), address(this), tokenId);
  33. _safeMint(account, tokenId);
  34. }
  35. return true;
  36. }
  37. /**
  38. * @dev Allow a user to burn wrapped tokens and withdraw the corresponding tokenIds of the underlying tokens.
  39. */
  40. function withdrawTo(address account, uint256[] memory tokenIds) public virtual returns (bool) {
  41. uint256 length = tokenIds.length;
  42. for (uint256 i = 0; i < length; ++i) {
  43. uint256 tokenId = tokenIds[i];
  44. if (!_isApprovedOrOwner(_msgSender(), tokenId)) {
  45. revert ERC721InsufficientApproval(_msgSender(), tokenId);
  46. }
  47. _burn(tokenId);
  48. // Checks were already performed at this point, and there's no way to retake ownership or approval from
  49. // the wrapped tokenId after this point, so it's safe to remove the reentrancy check for the next line.
  50. // slither-disable-next-line reentrancy-no-eth
  51. underlying().safeTransferFrom(address(this), account, tokenId);
  52. }
  53. return true;
  54. }
  55. /**
  56. * @dev Overrides {IERC721Receiver-onERC721Received} to allow minting on direct ERC721 transfers to
  57. * this contract.
  58. *
  59. * In case there's data attached, it validates that the operator is this contract, so only trusted data
  60. * is accepted from {depositFor}.
  61. *
  62. * WARNING: Doesn't work with unsafe transfers (eg. {IERC721-transferFrom}). Use {ERC721Wrapper-_recover}
  63. * for recovering in that scenario.
  64. */
  65. function onERC721Received(address, address from, uint256 tokenId, bytes memory) public virtual returns (bytes4) {
  66. if (address(underlying()) != _msgSender()) {
  67. revert ERC721UnsupportedToken(_msgSender());
  68. }
  69. _safeMint(from, tokenId);
  70. return IERC721Receiver.onERC721Received.selector;
  71. }
  72. /**
  73. * @dev Mint a wrapped token to cover any underlyingToken that would have been transferred by mistake. Internal
  74. * function that can be exposed with access control if desired.
  75. */
  76. function _recover(address account, uint256 tokenId) internal virtual returns (uint256) {
  77. address owner = underlying().ownerOf(tokenId);
  78. if (owner != address(this)) {
  79. revert ERC721IncorrectOwner(address(this), tokenId, owner);
  80. }
  81. _safeMint(account, tokenId);
  82. return tokenId;
  83. }
  84. /**
  85. * @dev Returns the underlying token.
  86. */
  87. function underlying() public view virtual returns (IERC721) {
  88. return _underlying;
  89. }
  90. }