ERC721Wrapper.sol 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (token/ERC721/extensions/ERC721Wrapper.sol)
  3. pragma solidity ^0.8.24;
  4. import {IERC721, ERC721} from "../ERC721.sol";
  5. import {IERC721Receiver} from "../IERC721Receiver.sol";
  6. /**
  7. * @dev Extension of the ERC-721 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
  10. * useful in conjunction with other modules. For example, combining this wrapping mechanism with {ERC721Votes} will allow
  11. * the wrapping of an existing "basic" ERC-721 into a governance token.
  12. */
  13. abstract contract ERC721Wrapper is ERC721, IERC721Receiver {
  14. IERC721 private immutable _underlying;
  15. /**
  16. * @dev The received ERC-721 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. // Setting an "auth" arguments enables the `_isAuthorized` check which verifies that the token exists
  45. // (from != 0). Therefore, it is not needed to verify that the return value is not 0 here.
  46. _update(address(0), tokenId, _msgSender());
  47. // Checks were already performed at this point, and there's no way to retake ownership or approval from
  48. // the wrapped tokenId after this point, so it's safe to remove the reentrancy check for the next line.
  49. // slither-disable-next-line reentrancy-no-eth
  50. underlying().safeTransferFrom(address(this), account, tokenId);
  51. }
  52. return true;
  53. }
  54. /**
  55. * @dev Overrides {IERC721Receiver-onERC721Received} to allow minting on direct ERC-721 transfers to
  56. * this contract.
  57. *
  58. * In case there's data attached, it validates that the operator is this contract, so only trusted data
  59. * is accepted from {depositFor}.
  60. *
  61. * WARNING: Doesn't work with unsafe transfers (eg. {IERC721-transferFrom}). Use {ERC721Wrapper-_recover}
  62. * for recovering in that scenario.
  63. */
  64. function onERC721Received(address, address from, uint256 tokenId, bytes memory) public virtual returns (bytes4) {
  65. if (address(underlying()) != _msgSender()) {
  66. revert ERC721UnsupportedToken(_msgSender());
  67. }
  68. _safeMint(from, tokenId);
  69. return IERC721Receiver.onERC721Received.selector;
  70. }
  71. /**
  72. * @dev Mint a wrapped token to cover any underlyingToken that would have been transferred by mistake. Internal
  73. * function that can be exposed with access control if desired.
  74. */
  75. function _recover(address account, uint256 tokenId) internal virtual returns (uint256) {
  76. address owner = underlying().ownerOf(tokenId);
  77. if (owner != address(this)) {
  78. revert ERC721IncorrectOwner(address(this), tokenId, owner);
  79. }
  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. }